-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.c
110 lines (103 loc) · 3.52 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @authors LOPES Marco, ISELI Cyril and RINGOT Gaëtan
* Purpose: Management of signals
* Language: C
* Date : january 2017
*/
#include <stdlib.h>
#include <stdbool.h>
#include "controller.h"
#include "threads.h"
#include <signal.h>
#include <pthread.h>
/**
* check if the user have a jackpot, double win or lose
* update money, last win
* @param params of the controller
*/
void checkWin(paramsControllerSt *params) {
int counter[SIZE] = {0};
for (uint i = 0; i < NUMBER_SPINNERS; ++i) {
counter[params->spinners[i].pos]++;
}
for (uint j = 0; j < SIZE; ++j) {
if (counter[j] == NUMBER_SPINNERS) {
if (*params->display->money > 1) {
params->display->lastWin = *params->display->money / 2;
} else {
params->display->lastWin = 1;
}
*params->display->money -= params->display->lastWin;
params->display->win = JACKPOT;
return;
} else if (counter[j] == NUMBER_SPINNERS - 1) {
if ((*params->display->money - (NUMBER_SPINNERS - 1)) < *params->display->money) {
params->display->lastWin = NUMBER_SPINNERS - 1;
} else {
params->display->lastWin = *params->display->money;
}
*params->display->money -= params->display->lastWin;
params->display->win = DOUBLE_WIN;
return;
}
}
params->display->win = LOSE;
}
/**
* wait signals(SIGINT, SIGQUIT, SIGTSTP, SIGALRM) and handle it
* update game data, like money, last win and display state
* @param paramsController
* @return NULL
*/
void *controller(void *paramsController) {
paramsControllerSt *params = (paramsControllerSt *) paramsController;
sigset_t mask, maskold;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGTSTP);
sigaddset(&mask, SIGALRM);
pthread_sigmask(SIG_SETMASK, &mask, &maskold);
int sig;
int actualSpinner = 0;
do {
if (actualSpinner == NUMBER_SPINNERS) {
pthread_mutex_lock(params->mutex);
pthread_cond_wait(params->allSpinnersStopped, params->mutex);
pthread_mutex_unlock(params->mutex);
checkWin(params);
params->display->state = END;
alarm(0);
sleep(5);
actualSpinner = 0;
params->display->state = BEGIN;
}
sigwait(&mask, &sig);
//ctrl + c stop current spinner
if (params->display->state == GAME && (sig == SIGINT || sig == SIGALRM)) {
params->spinners[actualSpinner].run = false;
actualSpinner++;
alarm(3);
} else if (sig == SIGTSTP) { // ctrl + z insert money
for (int i = 0; i < NUMBER_SPINNERS; i++) {
params->spinners[i].run = true;
}
pthread_mutex_lock(params->mutex);
pthread_cond_broadcast(params->spinnersCond);
pthread_mutex_unlock(params->mutex);
(*params->display->money)++;
actualSpinner = 0;
params->display->state = GAME;
alarm(3);
}
} while (sig != SIGQUIT); //ctrl + \ quit game
params->display->state = QUIT;
pthread_mutex_lock(params->mutex);
pthread_cond_wait(params->condition, params->mutex);
pthread_mutex_unlock(params->mutex);
*params->quit = true;
pthread_mutex_lock(params->mutex);
pthread_cond_broadcast(params->spinnersCond);
pthread_mutex_unlock(params->mutex);
return NULL;
}