game.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. int compteur = 0, i = 0;
  65. //Si il n' y a aucune game
  66. if(nbGame == 0){
  67. return NULL;
  68. }
  69. //Initialisation json
  70. ja = malloc(sizeof(JsonArray));
  71. ini_array_encoder(ja);
  72. //Ajoute chaque game
  73. while(compteur < nbGame && i < MAXGAME){
  74. if(!game[i].active){
  75. i++;
  76. continue;
  77. }
  78. //Creation objet json
  79. JsonEncoder je;
  80. ini_encoder(&je);
  81. add_string(&je, "name", game[i].name);
  82. add_integer(&je, "nbPlayer", game[i].nbPlayer);
  83. add_string(&je, "map", game[i].mapName);
  84. //Ajout dans le tableau
  85. add_array_object(ja, &je);
  86. //Suppr encoder objet
  87. clean_json_encoder(&je);
  88. //Incremente
  89. i++;
  90. compteur++;
  91. }
  92. return ja;
  93. }
  94. int* map_size(char* map){
  95. int* res;
  96. res = malloc(sizeof(int) * 2);
  97. res[WIDTH] = 0;
  98. res[HEIGHT] = 1;
  99. //Parcours la 1er ligne pour compter le nombre de caractère
  100. while(*map != '\n' && *map != '\0'){
  101. res[WIDTH]++;
  102. map++;
  103. }
  104. if(*map == '\0'){
  105. return res;
  106. }
  107. //Compte les lignes
  108. map++;
  109. while(*map != '\0'){
  110. if(*map == '\n'){
  111. res[HEIGHT]++;
  112. }
  113. map++;
  114. }
  115. return res;
  116. }
  117. int create_game(char* name, char* map){
  118. int length, index = 0;
  119. char* path;
  120. int* size;
  121. //Regarde si il reste une game de disponnible
  122. if(nbGame == MAXGAME){
  123. return ERR;
  124. }
  125. //Cherche une game libre
  126. while(index < MAXGAME){
  127. if(game[index].active == false){
  128. break;
  129. }
  130. index++;
  131. }
  132. //Recup la map
  133. length = strlen(MAPDIR) + strlen(map);
  134. path = new_string(length);
  135. sprintf(path, "%s%s", MAPDIR, map);
  136. game[index].mapContent = file_get_content(path);
  137. //Calcul taille de la map
  138. size = map_size(game[index].mapContent);
  139. //Set Up
  140. length = strlen(name) + 1;
  141. game[index].active = true;
  142. game[index].name = string_copy(name);
  143. game[index].nbPlayer = 0;
  144. game[index].mapName = string_copy(map);
  145. game[index].width = size[WIDTH];
  146. game[index].height = size[HEIGHT];
  147. game[index].map = parse_map(game[index].mapContent, size[WIDTH], size[HEIGHT]);
  148. for(int i = 0; i < MAXPLAYER; i++){
  149. game[index].player[i] = malloc(sizeof(Player));
  150. game[index].player[i]->ini = false;
  151. }
  152. //Retourne la position de la partie dans le tableau
  153. nbGame++;
  154. free(path);
  155. free(size);
  156. return index;
  157. }
  158. void stop_game(Game* g){
  159. //Indique comme inactive
  160. g->active = false;
  161. //Suppr les joueurs
  162. for(int i = 0; i < MAXPLAYER; i++){
  163. if(g->player[i]->ini){
  164. delete_player(g->player[i]);
  165. }
  166. free(g->player[i]);
  167. }
  168. //Libere la memoire
  169. for(int i = 0; g->width; i++){
  170. free(g->map[i]);
  171. }
  172. free(g->map);
  173. free(g->mapName);
  174. free(g->mapContent);
  175. }
  176. void clean_games(){
  177. for(int i = 0; i < MAXGAME; i++){
  178. if(game[i].active){
  179. stop_game(&game[i]);
  180. }
  181. }
  182. }