ソースを参照

Ajout recherche nom game

Arthur Brandao 6 年 前
コミット
7e22dd72ff
1 ファイル変更32 行追加0 行削除
  1. 32 0
      Serveur/handler.c

+ 32 - 0
Serveur/handler.c

@@ -50,6 +50,25 @@ int search_client_game(int cliId){
     return index;
 }
 
+/**
+ * Cherche une partie par son nom
+ * @param char* Le nom de la partie
+ * @return int L'index dans le tableau
+ */
+int search_game(char* name){
+    int index = ERR;
+    for(int i = 0; i < MAXGAME; i++){
+        //Regarde si la game est active et le nom
+        if(game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0){
+            index = i;
+            break;
+        }
+    }
+    return index;
+}
+
+
+
 /* --- Fonctions publiques --- */
 void ini_handler(){
     //Get
@@ -116,6 +135,19 @@ int handler_game_create(int cliId, JsonParser* json){
     //Recup valeur
     map = get_string(json, "map");
     name = get_string(json, "name");
+    //Verif nom non existant
+    if(search_game(name) != ERR){
+        reponse = malloc(sizeof(JsonEncoder));
+        ini_encoder(reponse);
+        add_string(reponse, "status", "501");
+        add_string(reponse, "message", "Cannot create game");
+        if(!send_client(cliId, reponse)){
+            adderror("Impossible de répondre au client");
+        }
+        clean_json_encoder(reponse);
+        free(reponse);
+        return FAIL;
+    }
     //Creation
     index = create_game(name, map);
     if(index == ERR){