/* * File: delay.c * Author: Arthur Brandao * * Created on 4 décembre 2018 */ #include #include #include #include #include "error.h" #include "delay.h" #include "bomberstudent_server.h" /* --- Extern --- */ extern Game game[MAXGAME]; extern int nbGame; extern pthread_mutex_t gameMutex[MAXGAME]; extern pthread_mutex_t playerMutex[MAXGAME * MAXPLAYER]; /* --- Fonctions privées --- */ /** * Thread d'attente de la fonction timer * @param void* La structure timer_data cast en void* * @return NULL */ void* delay_thread(void* data) { delay_t* d; //Recup données d = (delay_t*) data; //Detache le thread if (pthread_detach(pthread_self()) != 0) { adderror("Impossible de détacher le thread Delay"); return NULL; } //Attend le temps indiqué sleep(d->second); //Appel callback pthread_mutex_lock(&gameMutex[d->game]); if (game[d->game].active) { pthread_mutex_lock(&playerMutex[(d->game * MAXPLAYER) + d->player]); if (game[d->game].player[d->player]->ini) { if (d->callback(&game[d->game], d->player) != SUCCESS) { adderror("Erreur callback Delay"); } } pthread_mutex_unlock(&playerMutex[(d->game * MAXPLAYER) + d->player]); } pthread_mutex_unlock(&gameMutex[d->game]); return NULL; } /* --- Fonctions publiques --- */ void delay(int second, int game, int player, int(*callback)(Game*, int)) { pthread_t thread; delay_t* d = malloc(sizeof (timer_t)); d->second = second; d->game = game; d->player = player; d->callback = callback; //Creation thread if (pthread_create(&thread, NULL, delay_thread, d) != 0) { adderror("Impossible de créer le thread Delay"); } } int callback_major_end(Game* g, int playerIndex) { JsonEncoder notif; //Retire le bonus major g->player[playerIndex]->major = 0; //Creation json ini_encoder(¬if); describe_player(g->player[playerIndex], ¬if); //Envoi if (!notify_client(g->player[playerIndex]->cli, "POST", "player/major/end", ¬if)) { return FAIL; } return SUCCESS; }