Coverage report for server


src/
File: src/commands/ai/set.c
Date: 2024-06-25 10:57:05
Lines:
34/37
91.9%
Functions:
3/3
100.0%
Branches:
20/30
66.7%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** set.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 1 static void remove_element_from_inventory(client_t *c, object_t object)
24 {
25
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (size_t i = 0; i < 7; i++) {
26
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (object_handlers[i].type == object) {
27 1 c->inventory.food = object == FOOD
28
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ? c->inventory.food - 1 : c->inventory.food;
29 1 c->inventory.linemate = object == LINEMATE
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.linemate - 1 : c->inventory.linemate;
31 1 c->inventory.deraumere = object == DERAUMERE
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.deraumere - 1 : c->inventory.deraumere;
33 1 c->inventory.sibur = object == SIBUR
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.sibur - 1 : c->inventory.sibur;
35 1 c->inventory.mendiane = object == MENDIANE
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.mendiane - 1 : c->inventory.mendiane;
37 1 c->inventory.phiras = object == PHIRAS
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.phiras - 1 : c->inventory.phiras;
39 1 c->inventory.thystame = object == THYSTAME
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ? c->inventory.thystame - 1 : c->inventory.thystame;
41 }
42 }
43 1 }
44
45 2 static object_t find_object_type(const char *name)
46 {
47
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!name)
48 return -1;
49
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (size_t i = 0; i < 7; i++) {
50
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (strcmp(object_handlers[i].name, name) == 0) {
51 1 return object_handlers[i].type;
52 }
53 }
54 return -1;
55 }
56
57 2 void set(client_t *c, server_t *s)
58 {
59 2 object_t o = find_object_type(c->commands[1]);
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (c->tclient[NB_REQUESTS_HANDLEABLE - 1].available_request) {
62 handle_response(&c->payload, "ko\n");
63 client_time_handler(c, SET);
64 return;
65 }
66
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 if ((int)o != -1 && c->inventory.food) {
67 1 remove_element_from_inventory(c, FOOD);
68 1 add_element_to_map(s, c->x, c->y, o);
69 1 handle_response(&c->payload, "ok\n");
70 1 message_to_graphicals(s, "pdr %d %d\n", c->id, o);
71 1 client_time_handler(c, SET);
72 1 return;
73 }
74 1 dprintf(c->fd, "ko\n");
75 }
76