| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
** EPITECH PROJECT, 2024 |
| 3 |
|
|
** zappy |
| 4 |
|
|
** File description: |
| 5 |
|
|
** handler.c |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include "server.h" |
| 9 |
|
|
|
| 10 |
|
|
const commands_t commands_gui[NB_GUI_COMMANDS] = { |
| 11 |
|
|
{"msz", msz}, |
| 12 |
|
|
{"bct", bct}, |
| 13 |
|
|
{"mct", mct}, |
| 14 |
|
|
{"tna", tna}, |
| 15 |
|
|
{"ppo", ppo}, |
| 16 |
|
|
{"plv", plv}, |
| 17 |
|
|
{"pin", pin}, |
| 18 |
|
|
{"sgt", sgt}, |
| 19 |
|
|
{"sst", sst} |
| 20 |
|
|
}; |
| 21 |
|
|
|
| 22 |
|
|
const commands_t commands_ai[NB_AI_COMMANDS] = { |
| 23 |
|
|
{"Forward", forward}, |
| 24 |
|
|
{"Right", right}, |
| 25 |
|
|
{"Left", left}, |
| 26 |
|
|
{"Look", look}, |
| 27 |
|
|
{"Inventory", inventory}, |
| 28 |
|
|
{"Broadcast", broadcast}, |
| 29 |
|
|
{"Connect_nbr", connect_nbr}, |
| 30 |
|
|
{"Fork", fork_z}, |
| 31 |
|
|
{"Eject", eject}, |
| 32 |
|
|
{"Take", take}, |
| 33 |
|
|
{"Set", set}, |
| 34 |
|
|
{"Incantation", incantation}, |
| 35 |
|
|
}; |
| 36 |
|
|
|
| 37 |
|
2 |
static bool execute_command(client_t *client, server_t *server) |
| 38 |
|
|
{ |
| 39 |
|
|
size_t nb_commands; |
| 40 |
|
|
char *name; |
| 41 |
|
|
|
| 42 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (client->is_connected == false) |
| 43 |
|
|
return false; |
| 44 |
|
✗ |
nb_commands = client->is_graphic ? NB_GUI_COMMANDS : NB_AI_COMMANDS; |
| 45 |
|
✗ |
for (size_t i = 0; i < nb_commands; i++) { |
| 46 |
|
✗ |
name = client->is_graphic ? commands_gui[i].name : commands_ai[i].name; |
| 47 |
|
✗ |
if (strcmp(client->commands[0], name) != 0) |
| 48 |
|
|
continue; |
| 49 |
|
✗ |
if (client->is_graphic) |
| 50 |
|
✗ |
commands_gui[i].command(client, server); |
| 51 |
|
|
else |
| 52 |
|
✗ |
commands_ai[i].command(client, server); |
| 53 |
|
✗ |
free_array((void **)client->commands); |
| 54 |
|
✗ |
return true; |
| 55 |
|
|
} |
| 56 |
|
✗ |
if (client->is_graphic) |
| 57 |
|
✗ |
message_to_graphicals(server, "sbp\n"); |
| 58 |
|
|
return false; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
4 |
void handle_client_message(client_t *client, server_t *server) |
| 62 |
|
|
{ |
| 63 |
|
4 |
client->commands = str_to_array_separator(client->message, " \n\t"); |
| 64 |
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
4 |
if (client->commands == NULL || client->commands[0] == NULL) |
| 65 |
|
|
return; |
| 66 |
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 |
if (execute_command(client, server) == true) |
| 67 |
|
|
return; |
| 68 |
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 |
if (connector(client, server) == false) { |
| 69 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
if (client->is_graphic == false) |
| 70 |
|
2 |
dprintf(client->fd, "ko\n"); |
| 71 |
|
2 |
free_array((void **)client->commands); |
| 72 |
|
2 |
return; |
| 73 |
|
|
} |
| 74 |
|
|
} |
| 75 |
|
|
|