game.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "file.h"
  10. #include "game.h"
  11. /* --- Extern --- */
  12. Game game[MAXGAME];
  13. int nbGame = 0;
  14. /* --- Fonctions publiques --- */
  15. JsonArray* list_map(){
  16. char** result;
  17. int nbResult;
  18. JsonArray* ja;
  19. //Initialisation json
  20. ja = malloc(sizeof(JsonArray));
  21. ini_array_encoder(ja);
  22. //Regarde les fichiers dans le dossier
  23. result = file_list(MAPDIR, &nbResult);
  24. for(int i = 0; i < nbResult; i++){
  25. add_array_string(ja, result[i]);
  26. free(result[i]);
  27. }
  28. free(result);
  29. return ja;
  30. }
  31. JsonArray* list_game(){
  32. JsonArray* ja;
  33. //Si il n' y a aucune game
  34. if(nbGame == 0){
  35. return NULL;
  36. }
  37. //Initialisation json
  38. ja = malloc(sizeof(JsonArray));
  39. ini_array_encoder(ja);
  40. //Ajoute chaque game
  41. for(int i = 0; i < nbGame; i++){
  42. //Creation objet json
  43. JsonEncoder je;
  44. ini_encoder(&je);
  45. add_string(&je, "name", game[i].name);
  46. add_integer(&je, "nbPlayer", game[i].nbPlayer);
  47. add_string(&je, "map", game[i].mapName);
  48. //Ajout dans le tableau
  49. add_array_object(ja, &je);
  50. //Suppr encoder objet
  51. clean_json_encoder(&je);
  52. }
  53. return ja;
  54. }
  55. int* map_size(char* map){
  56. int* res;
  57. res = malloc(sizeof(int) * 2);
  58. res[WIDTH] = 0;
  59. res[HEIGHT] = 1;
  60. //Parcours la 1er ligne pour compter le nombre de caractère
  61. while(*map != '\n' && *map != '\0'){
  62. res[WIDTH]++;
  63. map++;
  64. }
  65. if(*map == '\0'){
  66. return res;
  67. }
  68. //Compte les lignes
  69. map++;
  70. while(*map != '\0'){
  71. if(*map == '\n'){
  72. res[HEIGHT]++;
  73. }
  74. map++;
  75. }
  76. return res;
  77. }
  78. /**
  79. * Associe une map à une game
  80. * @param Game* La game à associé à la map
  81. * @param char* Le nom de la map
  82. */
  83. void get_map(Game*, char*);