delay.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * File: delay.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 4 décembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <pthread.h>
  11. #include "error.h"
  12. #include "delay.h"
  13. #include "bomberstudent_server.h"
  14. /* --- Extern --- */
  15. extern Game game[MAXGAME];
  16. extern int nbGame;
  17. extern pthread_mutex_t gameMutex[MAXGAME];
  18. extern pthread_mutex_t playerMutex[MAXGAME * MAXPLAYER];
  19. /* --- Fonctions privées --- */
  20. /**
  21. * Thread d'attente de la fonction timer
  22. * @param void* La structure timer_data cast en void*
  23. * @return NULL
  24. */
  25. void* delay_thread(void* data) {
  26. delay_t* d;
  27. //Recup données
  28. d = (delay_t*) data;
  29. //Detache le thread
  30. if (pthread_detach(pthread_self()) != 0) {
  31. adderror("Impossible de détacher le thread Delay");
  32. return NULL;
  33. }
  34. //Attend le temps indiqué
  35. sleep(d->second);
  36. //Appel callback
  37. pthread_mutex_lock(&gameMutex[d->game]);
  38. if (game[d->game].active) {
  39. pthread_mutex_lock(&playerMutex[(d->game * MAXPLAYER) + d->player]);
  40. if (game[d->game].player[d->player]->ini) {
  41. if (d->callback(&game[d->game], d->player, d->data) != SUCCESS) {
  42. adderror("Erreur callback Delay");
  43. }
  44. }
  45. pthread_mutex_unlock(&playerMutex[(d->game * MAXPLAYER) + d->player]);
  46. }
  47. pthread_mutex_unlock(&gameMutex[d->game]);
  48. //Free data
  49. if(d->data != NULL){
  50. free(data);
  51. }
  52. return NULL;
  53. }
  54. /* --- Fonctions publiques --- */
  55. void delay(int second, int game, int player, int(*callback)(Game*, int, void*)) {
  56. pthread_t thread;
  57. delay_t* d = malloc(sizeof (delay_t));
  58. d->second = second;
  59. d->game = game;
  60. d->player = player;
  61. d->data = NULL;
  62. d->callback = callback;
  63. //Creation thread
  64. if (pthread_create(&thread, NULL, delay_thread, d) != 0) {
  65. adderror("Impossible de créer le thread Delay");
  66. }
  67. }
  68. void delay_data(int second, int game, int player, void* data, int(*callback)(Game*, int, void*)) {
  69. pthread_t thread;
  70. delay_t* d = malloc(sizeof (delay_t));
  71. d->second = second;
  72. d->game = game;
  73. d->player = player;
  74. d->data = data;
  75. d->callback = callback;
  76. //Creation thread
  77. if (pthread_create(&thread, NULL, delay_thread, d) != 0) {
  78. adderror("Impossible de créer le thread Delay");
  79. }
  80. }
  81. int callback_major_end(Game* g, int playerIndex, void* data) {
  82. JsonEncoder notif;
  83. //Retire le bonus major
  84. g->player[playerIndex]->major = 0;
  85. //Creation json
  86. ini_encoder(&notif);
  87. describe_player(g->player[playerIndex], &notif);
  88. //Envoi
  89. if (!notify_client(g->player[playerIndex]->cli, "POST", "player/major/end", &notif)) {
  90. return FAIL;
  91. }
  92. return SUCCESS;
  93. }
  94. int callback_bomb_explode(Game* g, int playerIndex, void* data){
  95. JsonEncoder notif;
  96. int* pos = (int*) data;
  97. ini_encoder(&notif);
  98. //Retire mutex car mis dans la fonction
  99. pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  100. pthread_mutex_unlock(&gameMutex[g->index]);
  101. //Gestion des explosions
  102. if(bomb_explode(g, playerIndex, pos[0], pos[1], &notif)){
  103. //Notification joueurs
  104. if(!notify_player(g, "POST", "attack/explose", &notif, -1)){
  105. clean_json_encoder(&notif);
  106. return FAIL;
  107. }
  108. }
  109. //Remise des mutex
  110. pthread_mutex_lock(&gameMutex[g->index]);
  111. pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
  112. clean_json_encoder(&notif);
  113. return SUCCESS;
  114. }