Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** death.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "server.h" |
9 |
|
|
|
10 |
|
✗ |
static void send_pin_for_food_update(client_t *client, server_t *s) |
11 |
|
|
{ |
12 |
|
✗ |
message_to_graphicals(s, "pin %d %d %d %d %d %d %d %d %d %d\n", |
13 |
|
✗ |
client->id, client->x, client->y, client->inventory.food, |
14 |
|
|
client->inventory.linemate, client->inventory.deraumere, |
15 |
|
|
client->inventory.sibur, client->inventory.mendiane, |
16 |
|
|
client->inventory.phiras, client->inventory.thystame); |
17 |
|
✗ |
} |
18 |
|
|
|
19 |
|
✗ |
static bool handle_client_death( |
20 |
|
|
client_t *client, |
21 |
|
|
server_t *s, |
22 |
|
|
time_t sec_sus, |
23 |
|
|
long nsec_sus |
24 |
|
|
) |
25 |
|
|
{ |
26 |
|
✗ |
double elapsed = sec_sus + (nsec_sus / (double)NANOSECONDS_IN_SECOND); |
27 |
|
✗ |
double interval = PLAYER_LIFE_LIMIT / (double)s->proprieties.frequency; |
28 |
|
|
|
29 |
|
✗ |
if (elapsed >= interval) { |
30 |
|
✗ |
client->inventory.food -= 1; |
31 |
|
✗ |
clock_gettime(CLOCK_REALTIME, &client->live_time); |
32 |
|
✗ |
send_pin_for_food_update(client, s); |
33 |
|
|
} |
34 |
|
✗ |
if (client->inventory.food == 0) { |
35 |
|
✗ |
dprintf(client->fd, "dead\n"); |
36 |
|
✗ |
close(client->fd); |
37 |
|
✗ |
FD_CLR(client->fd, &s->current_sockets); |
38 |
|
✗ |
message_to_graphicals(s, "pdi %d\n", client->id); |
39 |
|
✗ |
remove_client_by_fd(&s->clients, client->fd); |
40 |
|
✗ |
return true; |
41 |
|
|
} |
42 |
|
|
return false; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
✗ |
bool handle_client_life(server_t *s) |
46 |
|
|
{ |
47 |
|
✗ |
struct timespec current = s->current_time; |
48 |
|
|
struct timespec live_time; |
49 |
|
|
client_t *client; |
50 |
|
|
time_t sec_sus; |
51 |
|
|
long nsec_sus; |
52 |
|
|
|
53 |
|
✗ |
for (client_list_t *item = TAILQ_FIRST(&s->clients); item; |
54 |
|
✗ |
item = TAILQ_NEXT(item, entries)) { |
55 |
|
✗ |
client = item->client; |
56 |
|
✗ |
if (client->is_connected == false || client->is_graphic == true) |
57 |
|
✗ |
continue; |
58 |
|
✗ |
live_time = client->live_time; |
59 |
|
✗ |
sec_sus = (current.tv_sec - live_time.tv_sec); |
60 |
|
✗ |
nsec_sus = (current.tv_nsec - live_time.tv_nsec); |
61 |
|
✗ |
if (handle_client_death(client, s, sec_sus, nsec_sus) == true) |
62 |
|
|
return true; |
63 |
|
|
} |
64 |
|
|
return false; |
65 |
|
|
} |
66 |
|
|
|