Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** take.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "server.h" |
9 |
|
|
|
10 |
|
|
static const struct { |
11 |
|
|
char *name; |
12 |
|
|
object_t type; |
13 |
|
|
} object_handlers[] = { |
14 |
|
|
{"food", FOOD}, |
15 |
|
|
{"linemate", LINEMATE}, |
16 |
|
|
{"deraumere", DERAUMERE}, |
17 |
|
|
{"sibur", SIBUR}, |
18 |
|
|
{"mendiane", MENDIANE}, |
19 |
|
|
{"phiras", PHIRAS}, |
20 |
|
|
{"thystame", THYSTAME}, |
21 |
|
|
}; |
22 |
|
|
|
23 |
|
2 |
static void add_element_to_inventory(client_t *c, object_t object) |
24 |
|
|
{ |
25 |
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
|
16 |
for (size_t i = 0; i < 7; i++) { |
26 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 |
if (object_handlers[i].type == object) { |
27 |
|
2 |
c->inventory.food = object == FOOD |
28 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
? c->inventory.food + 1 : c->inventory.food; |
29 |
|
2 |
c->inventory.linemate = object == LINEMATE |
30 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.linemate + 1 : c->inventory.linemate; |
31 |
|
2 |
c->inventory.deraumere = object == DERAUMERE |
32 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.deraumere + 1 : c->inventory.deraumere; |
33 |
|
2 |
c->inventory.sibur = object == SIBUR |
34 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.sibur + 1 : c->inventory.sibur; |
35 |
|
2 |
c->inventory.mendiane = object == MENDIANE |
36 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.mendiane + 1 : c->inventory.mendiane; |
37 |
|
2 |
c->inventory.phiras = object == PHIRAS |
38 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.phiras + 1 : c->inventory.phiras; |
39 |
|
2 |
c->inventory.thystame = object == THYSTAME |
40 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
? c->inventory.thystame + 1 : c->inventory.thystame; |
41 |
|
|
} |
42 |
|
|
} |
43 |
|
2 |
} |
44 |
|
|
|
45 |
|
2 |
static bool does_object_exist_on_tile(tile_t *tile, object_t object) |
46 |
|
|
{ |
47 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
for (size_t i = 0; i < tile->num_objects; i++) { |
48 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (tile->objects[i] == object) |
49 |
|
|
return true; |
50 |
|
|
} |
51 |
|
|
return false; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
3 |
static object_t get_object_from_string(char *object_string) |
55 |
|
|
{ |
56 |
|
|
object_t object = -1; |
57 |
|
|
|
58 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
|
10 |
for (size_t i = 0; i < 7; i++) { |
59 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
|
9 |
if (strcmp(object_handlers[i].name, object_string) == 0) { |
60 |
|
2 |
object = object_handlers[i].type; |
61 |
|
2 |
break; |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
3 |
return object; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
3 |
void take(client_t *client, server_t *s) |
68 |
|
|
{ |
69 |
|
3 |
tile_t *tile = &s->map[client->y * s->proprieties.width + client->x]; |
70 |
|
|
object_t object; |
71 |
|
|
|
72 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 |
if (client->commands[1] == NULL) { |
73 |
|
✗ |
dprintf(client->fd, "ko\n"); |
74 |
|
✗ |
return; |
75 |
|
|
} |
76 |
|
3 |
object = get_object_from_string(client->commands[1]); |
77 |
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
3 |
if ((int)object == -1 || object == PLAYER || object == EMPTY) { |
78 |
|
1 |
dprintf(client->fd, "ko\n"); |
79 |
|
1 |
return; |
80 |
|
|
} |
81 |
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 |
if (does_object_exist_on_tile(tile, object)) { |
82 |
|
2 |
add_element_to_inventory(client, object); |
83 |
|
2 |
remove_element_from_map(s, client->x, client->y, object); |
84 |
|
2 |
handle_response(&client->payload, "ok\n"); |
85 |
|
2 |
message_to_graphicals(s, "pgt %d %d\n", client->id, object); |
86 |
|
2 |
client_time_handler(client, TAKE); |
87 |
|
|
} else |
88 |
|
✗ |
dprintf(client->fd, "ko\n"); |
89 |
|
|
} |
90 |
|
|
|