game.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  56. * Associe une map à une game
  57. * @param Game* La game à associé à la map
  58. * @param char* Le nom de la map
  59. */
  60. void get_map(Game*, char*);