game.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * File: game.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 28 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "error.h"
  10. #include "str.h"
  11. #include "file.h"
  12. #include "game.h"
  13. /* --- Extern --- */
  14. Game game[MAXGAME];
  15. int nbGame = 0;
  16. /* --- Fonctions privées --- */
  17. /**
  18. * Transforme le fichier map en un tableau 2D
  19. * @param char* Le contenu du fichier map
  20. * @param int Largeur de la map
  21. * @param int Hauteur de la map
  22. * @return char** La map parser
  23. */
  24. char** parse_map(char* mapContent, int width, int height){
  25. char** map = malloc(sizeof(char*) * width);
  26. //Creation des colonnes
  27. for(int i = 0; i < width; i++){
  28. map[i] = malloc(sizeof(char) * height);
  29. }
  30. //Remplissage
  31. for(int i = 0; i < height; i++){
  32. for(int j = 0; j < width; j++){
  33. map[j][i] = *mapContent;
  34. mapContent++;
  35. }
  36. mapContent++;
  37. }
  38. return map;
  39. }
  40. /* --- Fonctions publiques --- */
  41. void ini_games(){
  42. for(int i = 0; i < MAXGAME; i++){
  43. game[i].active = false;
  44. }
  45. }
  46. JsonArray* list_map(){
  47. char** result;
  48. int nbResult;
  49. JsonArray* ja;
  50. //Initialisation json
  51. ja = malloc(sizeof(JsonArray));
  52. ini_array_encoder(ja);
  53. //Regarde les fichiers dans le dossier
  54. result = file_list(MAPDIR, &nbResult);
  55. for(int i = 0; i < nbResult; i++){
  56. add_array_string(ja, result[i]);
  57. free(result[i]);
  58. }
  59. free(result);
  60. return ja;
  61. }
  62. JsonArray* list_game(){
  63. JsonArray* ja;
  64. JsonEncoder* je;
  65. int compteur = 0, i = 0;
  66. //Si il n' y a aucune game
  67. if(nbGame == 0){
  68. return NULL;
  69. }
  70. //Initialisation json
  71. ja = malloc(sizeof(JsonArray));
  72. ini_array_encoder(ja);
  73. //Ajoute chaque game
  74. while(compteur < nbGame && i < MAXGAME){
  75. if(!game[i].active){
  76. i++;
  77. continue;
  78. }
  79. //Creation objet json
  80. je = malloc(sizeof(JsonEncoder));
  81. ini_encoder(je);
  82. add_string(je, "name", game[i].name);
  83. add_integer(je, "nbPlayer", game[i].nbPlayer);
  84. add_string(je, "map", game[i].mapName);
  85. //Ajout dans le tableau
  86. add_array_object(ja, je);
  87. //Suppr encoder objet
  88. clean_json_encoder(je);
  89. free(je);
  90. //Incremente
  91. i++;
  92. compteur++;
  93. }
  94. return ja;
  95. }
  96. int* map_size(char* map){
  97. int* res;
  98. res = malloc(sizeof(int) * 2);
  99. res[WIDTH] = 0;
  100. res[HEIGHT] = 1;
  101. //Parcours la 1er ligne pour compter le nombre de caractère
  102. while(*map != '\n' && *map != '\0'){
  103. res[WIDTH]++;
  104. map++;
  105. }
  106. if(*map == '\0'){
  107. return res;
  108. }
  109. //Compte les lignes
  110. map++;
  111. while(*map != '\0'){
  112. if(*map == '\n'){
  113. res[HEIGHT]++;
  114. }
  115. map++;
  116. }
  117. return res;
  118. }
  119. int create_game(char* name, char* map){
  120. int length, index = 0;
  121. char* path;
  122. int* size;
  123. //Regarde si il reste une game de disponnible
  124. if(nbGame == MAXGAME){
  125. return ERR;
  126. }
  127. //Cherche une game libre
  128. while(index < MAXGAME){
  129. if(game[index].active == false){
  130. break;
  131. }
  132. index++;
  133. }
  134. //Recup la map
  135. length = strlen(MAPDIR) + strlen(map);
  136. path = new_string(length);
  137. sprintf(path, "%s%s", MAPDIR, map);
  138. game[index].mapContent = file_get_content(path);
  139. //Calcul taille de la map
  140. size = map_size(game[index].mapContent);
  141. //Set Up
  142. length = strlen(name) + 1;
  143. game[index].active = true;
  144. game[index].name = string_copy(name);
  145. game[index].nbPlayer = 0;
  146. game[index].mapName = string_copy(map);
  147. game[index].width = size[WIDTH];
  148. game[index].height = size[HEIGHT];
  149. game[index].map = parse_map(game[index].mapContent, size[WIDTH], size[HEIGHT]);
  150. for(int i = 0; i < MAXPLAYER; i++){
  151. game[index].player[i] = malloc(sizeof(Player));
  152. game[index].player[i]->ini = false;
  153. }
  154. //Retourne la position de la partie dans le tableau
  155. nbGame++;
  156. free(path);
  157. free(size);
  158. return index;
  159. }
  160. int add_player(Game* g, int cliId){
  161. int index = 0;
  162. Client* cli;
  163. //Verif que la game n'est pas deja pleine
  164. if(g->nbPlayer >= MAXPLAYER){
  165. return ERR;
  166. }
  167. //Cherche un joueur non initialisé
  168. while(index < MAXPLAYER){
  169. if(!g->player[index]->ini){
  170. break;
  171. }
  172. index++;
  173. }
  174. //Recup du client
  175. cli = get_client(cliId);
  176. if(cli == NULL){
  177. return ERR;
  178. }
  179. //Creation du joueur
  180. create_player(g->player[index], cli);
  181. g->nbPlayer++;
  182. return index;
  183. }
  184. JsonEncoder* describe_game(Game* g, int playerIndex){
  185. JsonEncoder* player;
  186. JsonEncoder* desc = malloc(sizeof(JsonEncoder));
  187. JsonEncoder* map = malloc(sizeof(JsonEncoder));
  188. char* content;
  189. //Initialisation
  190. ini_encoder(desc);
  191. ini_encoder(map);
  192. //Info map
  193. content = remove_char(g->mapContent, '\n'); //La map sans \n car interdit en JSON
  194. add_integer(map, "width", g->width);
  195. add_integer(map, "height", g->height);
  196. add_string(map, "content", content);
  197. free(content);
  198. //Ajout info
  199. add_integer(desc, "nbPlayers", g->nbPlayer);
  200. add_object(desc, "map", map);
  201. //Si besoins ajout un joueur
  202. if(playerIndex >= 0 && playerIndex < MAXPLAYER && g->player[playerIndex]->ini){
  203. player = describe_player(g->player[playerIndex]);
  204. add_object(desc, "player", player);
  205. clean_json_encoder(player);
  206. free(player);
  207. }
  208. //Nettoyage
  209. clean_json_encoder(map);
  210. free(map);
  211. return desc;
  212. }
  213. void stop_game(Game* g){
  214. //Indique comme inactive
  215. g->active = false;
  216. //Suppr les joueurs
  217. for(int i = 0; i < MAXPLAYER; i++){
  218. if(g->player[i]->ini){
  219. delete_player(g->player[i]);
  220. }
  221. free(g->player[i]);
  222. }
  223. //Libere la memoire
  224. for(int i = 0; i < g->width; i++){
  225. free(g->map[i]);
  226. }
  227. free(g->map);
  228. free(g->mapName);
  229. free(g->mapContent);
  230. }
  231. void clean_games(){
  232. for(int i = 0; i < MAXGAME; i++){
  233. if(game[i].active){
  234. stop_game(&game[i]);
  235. }
  236. }
  237. }