Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** lists.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "server.h" |
9 |
|
|
|
10 |
|
|
const int orientations[4] = {NORTH, SOUTH, WEST, EAST}; |
11 |
|
|
|
12 |
|
7 |
static void init_inventory(inventory_t *inv) |
13 |
|
|
{ |
14 |
|
7 |
inv->thystame = 0; |
15 |
|
7 |
inv->phiras = 0; |
16 |
|
7 |
inv->mendiane = 0; |
17 |
|
7 |
inv->sibur = 0; |
18 |
|
7 |
inv->deraumere = 0; |
19 |
|
7 |
inv->linemate = 0; |
20 |
|
7 |
inv->food = 10; |
21 |
|
7 |
} |
22 |
|
|
|
23 |
|
7 |
static void init_bools(client_t *client) |
24 |
|
|
{ |
25 |
|
|
static int id = 0; |
26 |
|
|
|
27 |
|
7 |
client->id = id; |
28 |
|
7 |
client->is_incanting = false; |
29 |
|
7 |
client->is_connected = false; |
30 |
|
7 |
client->is_graphic = false; |
31 |
|
7 |
id++; |
32 |
|
7 |
} |
33 |
|
|
|
34 |
|
7 |
client_t *init_client(int client_fd) |
35 |
|
|
{ |
36 |
|
7 |
uuid_t binuuid; |
37 |
|
7 |
client_t *client = malloc(sizeof(client_t)); |
38 |
|
|
|
39 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
if (client == NULL) |
40 |
|
7 |
return NULL; |
41 |
|
7 |
uuid_generate_random(binuuid); |
42 |
|
7 |
uuid_unparse_lower(binuuid, client->uuid); |
43 |
|
7 |
client->commands = NULL; |
44 |
|
7 |
client->fd = client_fd; |
45 |
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 7 times.
|
77 |
for (unsigned char i = 0; i < NB_REQUESTS_HANDLEABLE; i++) { |
46 |
|
70 |
client->tclient[i].available_request = false; |
47 |
|
70 |
client->tclient[i].command = -1; |
48 |
|
|
} |
49 |
|
7 |
client->level = 1; |
50 |
|
7 |
init_bools(client); |
51 |
|
7 |
client->orientation = orientations[rand_p(4)]; |
52 |
|
7 |
client->payload = NULL; |
53 |
|
7 |
init_inventory(&client->inventory); |
54 |
|
7 |
client->egg_id = 0; |
55 |
|
7 |
return client; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
1 |
void destroy_clients(struct client_tailq *clients) |
59 |
|
|
{ |
60 |
|
|
client_list_t *item; |
61 |
|
|
|
62 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 |
TAILQ_FOREACH(item, clients, entries) { |
63 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
TAILQ_REMOVE(clients, item, entries); |
64 |
|
|
} |
65 |
|
1 |
} |
66 |
|
|
|
67 |
|
1 |
void remove_client_by_fd(struct client_tailq *clients, int fd) |
68 |
|
|
{ |
69 |
|
|
client_list_t *item; |
70 |
|
|
|
71 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
TAILQ_FOREACH(item, clients, entries) { |
72 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (item->client->fd == fd) { |
73 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
TAILQ_REMOVE(clients, item, entries); |
74 |
|
1 |
break; |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
1 |
} |
78 |
|
|
|
79 |
|
1 |
void print_clients_fds(struct client_tailq *clients) |
80 |
|
|
{ |
81 |
|
|
client_list_t *item; |
82 |
|
|
|
83 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 |
TAILQ_FOREACH(item, clients, entries) { |
84 |
|
2 |
printf("FD: %d\n", item->client->fd); |
85 |
|
|
} |
86 |
|
1 |
} |
87 |
|
|
|