Coverage report for server


src/
File: src/commands/ai/connector.c
Date: 2024-06-25 10:57:05
Lines:
5/87
5.7%
Functions:
1/9
11.1%
Branches:
3/30
10.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** connector.c
6 */
7
8 #include "server.h"
9
10 static void end_assign_egg_to_client(
11 client_t *client,
12 server_t *server,
13 eggs_list_t *item
14 )
15 {
16 message_to_graphicals(server, "edi %d\n", client->egg_id);
17 handle_response(&client->payload, "ok\n");
18 secure_free((void **)&item->egg);
19 secure_free((void **)&item);
20 }
21
22 static void asign_egg_to_client(
23 client_t *client,
24 server_t *server,
25 size_t rand_idx
26 )
27 {
28 size_t counter = 0;
29 team_t *team = get_team_by_name(&server->teams, client->commands[0]);
30
31 for (eggs_list_t *item = TAILQ_FIRST(&team->eggs);
32 item;
33 item = TAILQ_NEXT(item, entries)) {
34 if (!(counter == rand_idx)) {
35 counter++;
36 continue;
37 }
38 client->is_connected = true;
39 client->x = item->egg->x;
40 client->y = item->egg->y;
41 asprintf(&client->team_name, "%s", team->name);
42 clock_gettime(CLOCK_REALTIME, &client->live_time);
43 client->egg_id = team->nb_eggs;
44 TAILQ_REMOVE(&team->eggs, item, entries);
45 end_assign_egg_to_client(client, server, item);
46 break;
47 }
48 }
49
50 static void print_eggs(client_t *c, server_t *s)
51 {
52 team_t *t;
53 egg_t *e;
54
55 for (team_list_t *item_t = TAILQ_FIRST(&s->teams);
56 item_t;
57 item_t = TAILQ_NEXT(item_t, entries)) {
58 t = item_t->team;
59 for (eggs_list_t *item_e = TAILQ_FIRST(&t->eggs);
60 item_e;
61 item_e = TAILQ_NEXT(item_e, entries)) {
62 e = item_e->egg;
63 dprintf(c->fd, "enw %d %d %d %d\n", e->id, -1, e->x, e->y);
64 }
65 }
66 }
67
68 static void print_teams(client_t *c, server_t *s)
69 {
70 team_list_t *item;
71 team_t *t;
72
73 TAILQ_FOREACH(item, &s->teams, entries) {
74 t = item->team;
75 dprintf(c->fd, "tna %s\n", t->name);
76 }
77 }
78
79 static info_map_t get_tile(server_t *server, size_t x, size_t y)
80 {
81 tile_t tile = server->map[x + y * server->proprieties.width];
82 info_map_t info = {0};
83
84 for (size_t i = 0; i < tile.num_objects; i++) {
85 info.food += tile.objects[i] == FOOD;
86 info.linemate += tile.objects[i] == LINEMATE;
87 info.deraumere += tile.objects[i] == DERAUMERE;
88 info.sibur += tile.objects[i] == SIBUR;
89 info.mendiane += tile.objects[i] == MENDIANE;
90 info.phiras += tile.objects[i] == PHIRAS;
91 info.thystame += tile.objects[i] == THYSTAME;
92 }
93 return info;
94 }
95
96 static void print_map_to_gui(client_t *c, server_t *s)
97 {
98 info_map_t info;
99
100 for (int y = 0; y < s->proprieties.height; y++) {
101 for (int x = 0; x < s->proprieties.width; x++) {
102 info = get_tile(s, x, y);
103 dprintf(c->fd, "bct %d %d %d %d %d %d %d %d %d\n",
104 x, y, info.food, info.linemate, info.deraumere, info.sibur,
105 info.mendiane, info.phiras, info.thystame);
106 }
107 }
108 }
109
110 static void send_players(client_t *c, server_t *s)
111 {
112 client_list_t *item;
113 client_t *tmp;
114
115 TAILQ_FOREACH(item, &s->clients, entries) {
116 tmp = item->client;
117 if (tmp->is_connected == true && tmp->is_graphic == false) {
118 dprintf(c->fd, "pnw %d %u %u %u %zu %s\n", tmp->id, tmp->x,
119 tmp->y, tmp->orientation, tmp->level, tmp->team_name);
120 dprintf(c->fd, "pin %d %u %u %u %u %u %u %u %u %u\n", tmp->id,
121 tmp->x, tmp->y, tmp->inventory.food,
122 tmp->inventory.linemate, tmp->inventory.deraumere,
123 tmp->inventory.sibur, tmp->inventory.mendiane,
124 tmp->inventory.phiras, tmp->inventory.thystame);
125 }
126 }
127 }
128
129 static void send_everything(client_t *c, server_t *server)
130 {
131 dprintf(c->fd, "msz %u %u\n", server->proprieties.width,
132 server->proprieties.height);
133 dprintf(c->fd, "sgt %u\n", server->proprieties.frequency);
134 print_teams(c, server);
135 print_map_to_gui(c, server);
136 send_players(c, server);
137 print_eggs(c, server);
138 }
139
140 2 bool connector(client_t *c, server_t *server)
141 {
142 size_t nb_slots;
143
144
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (c->is_connected == true)
145 return false;
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (strcmp(c->commands[0], "GRAPHIC") == 0) {
147 c->is_graphic = true;
148 c->is_connected = true;
149 send_everything(c, server);
150 return true;
151 }
152 2 nb_slots = team_nb_slots(&server->teams, c->commands[0]);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (nb_slots == 0)
154 return false;
155 asign_egg_to_client(c, server, rand() % nb_slots);
156 dprintf(c->fd, "%zu\n%u %u\n", (nb_slots - 1),
157 server->proprieties.width, server->proprieties.height);
158 message_to_graphicals(server, "pnw %d %u %u %u %u %s\n", c->id, c->x,
159 c->y, c->orientation, c->level, c->team_name);
160 return true;
161 }
162