From a2a020080676e80ebd25b345b0800af111ba1167 Mon Sep 17 00:00:00 2001 From: romain Date: Fri, 16 Jun 2023 16:51:19 +0200 Subject: [PATCH 01/11] ZappyServer: Add graphics events cmd --- src/SERVER/Makefile | 15 +++ src/SERVER/include/broadcast_events.h | 124 ++++++++++++++++++ src/SERVER/include/trantorien.h | 5 + src/SERVER/include/zappy.h | 8 ++ src/SERVER/src/broadcast_graphic.c | 2 +- src/SERVER/src/loop/internal.h | 6 +- src/SERVER/src/loop/loop.c | 18 ++- .../state_connected/ai_cmd/cmd_invocation.c | 2 + .../state_connected/graphic_cmd/cmd_bct.c | 3 +- .../state_connected/graphic_cmd/cmd_ebo.c | 27 ++++ .../state_connected/graphic_cmd/cmd_edi.c | 30 +++++ .../state_connected/graphic_cmd/cmd_enw.c | 27 ++++ .../state_connected/graphic_cmd/cmd_pbc.c | 32 +++++ .../state_connected/graphic_cmd/cmd_pdi.c | 27 ++++ .../state_connected/graphic_cmd/cmd_pdr.c | 27 ++++ .../state_connected/graphic_cmd/cmd_pex.c | 26 ++++ .../state_connected/graphic_cmd/cmd_pfk.c | 30 +++++ .../state_connected/graphic_cmd/cmd_pgt.c | 27 ++++ .../state_connected/graphic_cmd/cmd_pnw.c | 33 +++++ .../state_connected/graphic_cmd/cmd_sbp.c | 22 ++++ .../state_connected/graphic_cmd/cmd_seg.c | 25 ++++ .../state_connected/graphic_cmd/cmd_smg.c | 25 ++++ .../state_connected/graphic_cmd/cmd_suc.c | 22 ++++ .../graphic_cmd/update_graphic_cmd.c | 8 +- .../loop/update_client_waiting_slot_opened.c | 9 ++ src/SERVER/src/loop/update_food.c | 17 ++- src/SERVER/src/main.c | 27 ++++ .../commands/broadcast/command_broadcast.c | 2 + .../src/trantorien/commands/command_eject.c | 32 ++--- .../src/trantorien/commands/command_fork.c | 4 + .../commands/command_set_object_down.c | 2 + .../trantorien/commands/command_take_object.c | 11 +- src/SERVER/src/trantorien/internal.h | 2 - src/SERVER/src/zappy_check_end.c | 33 +++++ 34 files changed, 668 insertions(+), 42 deletions(-) create mode 100644 src/SERVER/include/broadcast_events.h create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_ebo.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pbc.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdr.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pex.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pfk.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pgt.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_sbp.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_seg.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_smg.c create mode 100644 src/SERVER/src/loop/state_connected/graphic_cmd/cmd_suc.c create mode 100644 src/SERVER/src/zappy_check_end.c diff --git a/src/SERVER/Makefile b/src/SERVER/Makefile index fb65295d..e2727aec 100644 --- a/src/SERVER/Makefile +++ b/src/SERVER/Makefile @@ -93,6 +93,20 @@ SRC_LOOP_GRCMD = \ cmd_pin.c \ cmd_sgt.c \ cmd_sst.c \ + cmd_pnw.c \ + cmd_pex.c \ + cmd_pbc.c \ + cmd_pfk.c \ + cmd_pdr.c \ + cmd_pgt.c \ + cmd_pdi.c \ + cmd_enw.c \ + cmd_ebo.c \ + cmd_edi.c \ + cmd_seg.c \ + cmd_smg.c \ + cmd_suc.c \ + cmd_sbp.c \ update_graphic_cmd.c SRC_LOOP_GRCMD := $(addprefix graphic_cmd/, $(SRC_LOOP_GRCMD)) @@ -186,6 +200,7 @@ SRC := \ get_id.c \ main.c \ on_new_conn.c \ + zappy_check_end.c \ zappy_create_initial_egg.c \ zappy_destroy.c \ zappy_find_trantorien_by_id.c \ diff --git a/src/SERVER/include/broadcast_events.h b/src/SERVER/include/broadcast_events.h new file mode 100644 index 00000000..f3bd2ae4 --- /dev/null +++ b/src/SERVER/include/broadcast_events.h @@ -0,0 +1,124 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** broadcast_events +*/ + +#ifndef BROADCAST_EVENTS_H_ + #define BROADCAST_EVENTS_H_ + +/** +** @brief broadcast event pnw (new player) +** @param zappy +** @param cl +** @return bool +**/ +bool cmd_pnw(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event pex (player expulse) +** @param ntw +** @param cl +** @return bool +**/ +bool cmd_pex(ntw_t *ntw, ntw_client_t *cl, trantorien_t *tr); + +/** +** @brief broadcast event pbc (player broadcast) +** @param ntw +** @param cl +** @param action +** @return bool +**/ +bool cmd_pbc(ntw_t *ntw, ntw_client_t *cl, action_t *action); + +/** +** @brief broadcast event pfk (player fork) +** @param ntw +** @param cl +** @return bool +**/ +bool cmd_pfk(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event pdr (player drop ressource) +** @param ntw +** @param cl +** @param action +** @return bool +**/ +bool cmd_pdr(ntw_t *ntw, ntw_client_t *cl, action_t *action); + +/** + * @brief broadcast event pgt (player take ressource) + * @param ntw + * @param cl + * @param action + * @return + */ +bool cmd_pgt(ntw_t *ntw, ntw_client_t *cl, action_t *action); + +/** +** @brief broadcast event pdi (player died) +** @param ntw +** @param cl +** @return +**/ +bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event enw (new egg) +** @param ntw +** @param cl +** @return bool +**/ +bool cmd_enw(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event eht (egg hatched) +** @param ntw +** @param cl +** @return +**/ +bool cmd_ebo(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event edi (egg died) +** @param ntw +** @param cl +** @return +**/ +bool cmd_edi(ntw_t *ntw, ntw_client_t *cl); + +/** +** @brief broadcast event smg (server message) +** @param ntw +** @param smg +** @return +**/ +bool cmd_smg(ntw_t *ntw, const char *smg); + +/** +** @brief broadcast event seg (end game) +** @param ntw +** @param cl +** @return +**/ +bool cmd_seg(ntw_t *ntw, client_t *cl); + +/** +** @brief broadcast event suc (success) +** @param cl +** @return +**/ +bool cmd_suc(ntw_client_t *cl); + +/** +** @brief broadcast event sbp (error) +** @param cl +** @return +**/ +bool cmd_sbp(ntw_client_t *cl); + +#endif // BROADCAST_EVENTS_H_ diff --git a/src/SERVER/include/trantorien.h b/src/SERVER/include/trantorien.h index ce6e3f6c..78a5c49d 100644 --- a/src/SERVER/include/trantorien.h +++ b/src/SERVER/include/trantorien.h @@ -85,6 +85,11 @@ typedef struct zappy_s zappy_t; void trantorien_reduce_freq(trantorien_t *trantorien, zappy_t *zappy, ntw_client_t *cl); +/** +** @brief Destroy an action +** @param action +**/ +void action_destroy(action_t *action); /** ** @brief Get the min case for the look around wiht substration diff --git a/src/SERVER/include/zappy.h b/src/SERVER/include/zappy.h index 353b9b4c..977337a0 100644 --- a/src/SERVER/include/zappy.h +++ b/src/SERVER/include/zappy.h @@ -26,6 +26,7 @@ struct zappy_s { size_t cur_tick; size_t before_add_resources; list_t *trantoriens_available; + bool is_end; }; typedef struct zappy_s zappy_t; @@ -95,4 +96,11 @@ bool broadcast_graphic(ntw_t *ntw, const char *msg); **/ list_t *zappy_create_initial_egg(args_t *args); +/** +** @brief Check if the game is ended +** @param zappy +** @return true if the game is ended +**/ +bool check_end(zappy_t *zappy, bool is_end); + #endif diff --git a/src/SERVER/src/broadcast_graphic.c b/src/SERVER/src/broadcast_graphic.c index ee31f6cc..aeb19dcb 100644 --- a/src/SERVER/src/broadcast_graphic.c +++ b/src/SERVER/src/broadcast_graphic.c @@ -7,8 +7,8 @@ #include #include "circular_buffer.h" -#include "client.h" #include "ntw.h" +#include "client.h" bool broadcast_graphic(ntw_t *ntw, const char *msg) { diff --git a/src/SERVER/src/loop/internal.h b/src/SERVER/src/loop/internal.h index cb026234..facc36f4 100644 --- a/src/SERVER/src/loop/internal.h +++ b/src/SERVER/src/loop/internal.h @@ -27,11 +27,13 @@ void send_id(client_t *cc, ntw_client_t *cl); /** ** @brief update food for every trantoriens, associated or not with clients +** @param ntw ** @param trantorien ** @param cl -** @param new_freq +** @param is_and_egg **/ -void update_food(trantorien_t *trantorien, ntw_client_t *cl, bool new_freq); +void update_food(ntw_t *ntw, trantorien_t *trantorien, + ntw_client_t *cl, bool is_and_egg); // Update in states diff --git a/src/SERVER/src/loop/loop.c b/src/SERVER/src/loop/loop.c index fefe6690..b5df95b7 100644 --- a/src/SERVER/src/loop/loop.c +++ b/src/SERVER/src/loop/loop.c @@ -70,8 +70,8 @@ static void check_ressources(zappy_t *zappy, bool new_freq) } } -static void update_trantoriens_available_food(list_t *trantoriens_available, - bool new_freq) +static void update_trantoriens_available_food(ntw_t *ntw, + list_t *trantoriens_available, bool new_freq) { trantorien_t *trantorien = NULL; @@ -83,7 +83,9 @@ static void update_trantoriens_available_food(list_t *trantoriens_available, if (trantorien == NULL) { continue; } - update_food(trantorien, NULL, new_freq); + if (new_freq == true) { + update_food(ntw, trantorien, NULL, true); + } } } @@ -100,11 +102,13 @@ bool loop(zappy_t *zappy, bool new_freq) } status = update_client(zappy, cl, new_freq) & status; update_clients_connections(zappy->ntw); - if (L_DATAT(client_t *, client)->type == AI) { - update_food(L_DATAT(client_t *, client)->cl.ai.trantorien, - cl, new_freq); + if (L_DATAT(client_t *, client)->type == AI && new_freq == true) { + update_food(zappy->ntw, + L_DATAT(client_t *, client)->cl.ai.trantorien, cl, false); } } - update_trantoriens_available_food(zappy->trantoriens_available, new_freq); + update_trantoriens_available_food(zappy->ntw, + zappy->trantoriens_available, new_freq); + check_end(zappy, status); return !status; } diff --git a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c index b3dd5fe2..ca22c1c6 100644 --- a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c +++ b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c @@ -10,6 +10,7 @@ #include "zappy.h" #include "internal.h" #include "trantorien.h" +#include "broadcast_events.h" bool cmd_incantation(__attribute__((unused)) zappy_t *zappy, ntw_client_t *cl, __attribute__((unused)) char **cmd_split) @@ -21,5 +22,6 @@ bool cmd_incantation(__attribute__((unused)) zappy_t *zappy, ntw_client_t *cl, zappy->map, zappy->ntw) == false) { return false; } + cmd_pfk(zappy->ntw, cl); return true; } diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_bct.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_bct.c index fbcadd31..928008eb 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_bct.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_bct.c @@ -9,11 +9,10 @@ #include #include #include -#include "ntw.h" #include "circular_buffer.h" +#include "ntw.h" #include "map.h" #include "internal.h" -#include "zappy.h" static bool send_pos(ntw_client_t *cl, int x, int y) { diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_ebo.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_ebo.c new file mode 100644 index 00000000..e15a3649 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_ebo.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_ebo +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_ebo(ntw_t *ntw, ntw_client_t *cl) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL) { + return false; + } + client = L_DATA(cl); + snprintf(buff, 511, "ebo %d\n", client->id); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c new file mode 100644 index 00000000..6b0fea63 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_edi +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_edi(ntw_t *ntw, ntw_client_t *cl) +{ + client_t *client = NULL; + char buff[512] = {0}; + + if (ntw == NULL || cl == NULL) { + return false; + } + client = L_DATA(cl); + if (client == NULL) { + return false; + } + snprintf(buff, 511, "edi %d\n", client->id); + circular_buffer_write(cl->write_to_outside, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c new file mode 100644 index 00000000..174a42d5 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_enw +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_enw(ntw_t *ntw, ntw_client_t *cl) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL) { + return false; + } + client = L_DATA(cl); + snprintf(buff, 511, "enw %d\n", client->id); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pbc.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pbc.c new file mode 100644 index 00000000..cb25966f --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pbc.c @@ -0,0 +1,32 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_broadcast +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_pbc(ntw_t *ntw, ntw_client_t *cl, action_t *action) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL || action == NULL || + action->code != BROADCAST || action->param.broadcast_msg == NULL) { + return false; + } + client = L_DATA(cl); + if (client == NULL) { + return false; + } + snprintf(buff, 511, "pbc %d %s\n", + client->id, action->param.broadcast_msg); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c new file mode 100644 index 00000000..ab50939b --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pdi +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL) { + return false; + } + client = L_DATA(cl); + snprintf(buff, 511, "pdi %d\n", client->id); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdr.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdr.c new file mode 100644 index 00000000..ca7518e7 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdr.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pdr +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_pdr(ntw_t *ntw, ntw_client_t *cl, action_t *action) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL || action == NULL) { + return false; + } + client = L_DATA(cl); + snprintf(buff, 511, "pdr %d %i\n", client->id, action->param.object); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pex.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pex.c new file mode 100644 index 00000000..831b3916 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pex.c @@ -0,0 +1,26 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pex +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "client.h" +#include "trantorien.h" +#include "zappy.h" + +bool cmd_pex(ntw_t *ntw, trantorien_t *tr) +{ + char buff[512] = {0}; + + if (ntw == NULL || tr == NULL) { + return false; + } + snprintf(buff, 511, "pex %d\n", tr->id); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pfk.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pfk.c new file mode 100644 index 00000000..b134ac96 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pfk.c @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pfk +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_pfk(ntw_t *ntw, ntw_client_t *cl) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL) { + return false; + } + client = L_DATA(cl); + if (client == NULL) { + return false; + } + snprintf(buff, 511, "pfk %d\n", client->id); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pgt.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pgt.c new file mode 100644 index 00000000..a5f60209 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pgt.c @@ -0,0 +1,27 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pgt +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_pgt(ntw_t *ntw, ntw_client_t *cl, action_t *action) +{ + char buff[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL || action == NULL) { + return false; + } + client = L_DATA(cl); + snprintf(buff, 511, "pgt %d %i\n", client->id, action->param.object); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c new file mode 100644 index 00000000..0369e8b1 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_pnw +*/ + +#include +#include +#include "client.h" +#include "internal.h" +#include "trantorien.h" + +bool cmd_pnw(ntw_t *ntw, ntw_client_t *cl) +{ + char buffer[512] = {0}; + client_t *client = NULL; + + if (ntw == NULL || cl == NULL || cl->data == NULL) { + return false; + } + client = cl->data; + if (client == NULL || client->type != AI + || client->cl.ai.trantorien == NULL) { + return false; + } + sprintf(buffer, "pnw %d %d %d %d %d %s\n", client->id, + client->cl.ai.trantorien->x, client->cl.ai.trantorien->y, + client->cl.ai.trantorien->direction, client->cl.ai.trantorien->level, + client->cl.ai.trantorien->team_name); + broadcast_graphic(ntw, buffer); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_sbp.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_sbp.c new file mode 100644 index 00000000..f5beca33 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_sbp.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_sbp +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_sbp(ntw_client_t *cl) +{ + if (cl == NULL) { + return false; + } + circular_buffer_write(cl->write_to_outside, "sbp\n"); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_seg.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_seg.c new file mode 100644 index 00000000..32a9145e --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_seg.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_seg +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_seg(ntw_t *ntw, client_t *cl) +{ + char buff[512] = {0}; + + if (ntw == NULL || cl == NULL) { + return false; + } + snprintf(buff, 511, "seg %s\n", cl->cl.ai.trantorien->team_name); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_smg.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_smg.c new file mode 100644 index 00000000..6ca20ca8 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_smg.c @@ -0,0 +1,25 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_smg +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_smg(ntw_t *ntw, const char *smg) +{ + char buff[512] = {0}; + + if (ntw == NULL || smg == NULL) { + return false; + } + snprintf(buff, 511, "smg %s\n", smg); + broadcast_graphic(ntw, buff); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_suc.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_suc.c new file mode 100644 index 00000000..513e7610 --- /dev/null +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_suc.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** cmd_suc +*/ + +#include +#include +#include "ntw.h" +#include "map.h" +#include "internal.h" +#include "client.h" + +bool cmd_suc(ntw_client_t *cl) +{ + if (cl == NULL) { + return false; + } + circular_buffer_write(cl->write_to_outside, "suc\n"); + return true; +} diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c b/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c index e8822bc2..061c40d5 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c @@ -12,6 +12,7 @@ #include "internal.h" #include "tlcstrings.h" #include "command_reponses.h" +#include "broadcast_events.h" static const char cmds_graphic[NB_CMD_AVAILIBLE][6] = { "msz\n", @@ -43,16 +44,21 @@ static bool static bool update_cmd(zappy_t *zappy, ntw_client_t *cl, char **cmd_split) { + bool is_entered = false; bool status = false; for (int i = 0; cmds_graphic[i][0] != '\0'; i++) { if (strcmp(cmd_split[0], cmds_graphic[i]) == 0) { status = graphic_funcs[i](zappy, cl, cmd_split); + is_entered = true; break; } } if (status == false) { - circular_buffer_write(cl->write_to_outside, KO_RESPONSE); + if (is_entered == false) + cmd_sbp(cl); + else + cmd_suc(cl); status = true; } return status; diff --git a/src/SERVER/src/loop/update_client_waiting_slot_opened.c b/src/SERVER/src/loop/update_client_waiting_slot_opened.c index 9aa05a11..ad4b9d9a 100644 --- a/src/SERVER/src/loop/update_client_waiting_slot_opened.c +++ b/src/SERVER/src/loop/update_client_waiting_slot_opened.c @@ -6,9 +6,17 @@ */ #include +#include #include "client.h" #include "internal.h" #include "trantorien.h" +#include "broadcast_events.h" + +static void send_new_connection(ntw_t *ntw, ntw_client_t *cl) +{ + cmd_ebo(ntw, cl); + cmd_pnw(ntw, cl); +} bool update_client_waiting_slot_opened(zappy_t *zappy, ntw_client_t *cl) { @@ -22,6 +30,7 @@ bool update_client_waiting_slot_opened(zappy_t *zappy, ntw_client_t *cl) zappy->trantoriens_available) == false) { return true; } + send_new_connection(zappy->ntw, cl); client->state = CONNECTED; client->type = AI; send_id(client, cl); diff --git a/src/SERVER/src/loop/update_food.c b/src/SERVER/src/loop/update_food.c index 4cb165bf..a57d3fa6 100644 --- a/src/SERVER/src/loop/update_food.c +++ b/src/SERVER/src/loop/update_food.c @@ -7,13 +7,23 @@ #include "circular_buffer.h" #include "client.h" +#include "broadcast_events.h" -void update_food(trantorien_t *trantorien, ntw_client_t *cl, bool new_freq) +static void send_event_death(ntw_t *ntw, ntw_client_t *cl, bool is_an_egg) { - if (!new_freq || trantorien == NULL || trantorien->alive == false) { + if (is_an_egg) { + cmd_edi(ntw, cl); + } + cmd_pdi(ntw, cl); +} + +void update_food(ntw_t *ntw, trantorien_t *trantorien, +ntw_client_t *cl, bool is_an_egg) +{ + if (trantorien == NULL || trantorien->alive == false) { return; } - trantorien->food_stack_freq += new_freq; + trantorien->food_stack_freq += 1; if (trantorien->food_stack_freq < MAX_FOOD_FREQ) { return; } @@ -24,6 +34,7 @@ void update_food(trantorien_t *trantorien, ntw_client_t *cl, bool new_freq) trantorien->alive = false; if (cl) { circular_buffer_write(cl->write_to_outside, "dead\n"); + send_event_death(ntw, cl, is_an_egg); } } } diff --git a/src/SERVER/src/main.c b/src/SERVER/src/main.c index 6c92b955..c4acc64f 100644 --- a/src/SERVER/src/main.c +++ b/src/SERVER/src/main.c @@ -10,6 +10,32 @@ #include "args.h" #include "ntw.h" #include "zappy.h" +#include "client.h" +#include "broadcast_events.h" + +static void broadcast_end(zappy_t *zappy) +{ + client_t *alive_client = NULL; + client_t *client = NULL; + + for (L_EACH(data, zappy->ntw->clients)) { + client = L_DATA(L_DATAT(ntw_client_t *, data)); + if (client == NULL || client->type != AI + || client->cl.ai.trantorien == NULL) { + continue; + } + if (client->cl.ai.trantorien->level == 8) { + cmd_seg(zappy->ntw, client); + return; + } + if (client->cl.ai.trantorien->alive == true) { + alive_client = client; + } + } + if (alive_client != NULL) { + cmd_seg(zappy->ntw, alive_client); + } +} static int server_update(time_t *s_timeout, suseconds_t *m_timeout, zappy_t *zappy) @@ -48,6 +74,7 @@ static int server_start(args_t *args) while (is_end == false) { is_end = server_update(&s_timeout, &m_timeout, zappy); } + broadcast_end(zappy); zappy_destroy(zappy); return 0; } diff --git a/src/SERVER/src/trantorien/commands/broadcast/command_broadcast.c b/src/SERVER/src/trantorien/commands/broadcast/command_broadcast.c index 6e4feb79..76ef1215 100644 --- a/src/SERVER/src/trantorien/commands/broadcast/command_broadcast.c +++ b/src/SERVER/src/trantorien/commands/broadcast/command_broadcast.c @@ -20,6 +20,7 @@ #include "map.h" #include "client.h" #include "internal.h" +#include "broadcast_events.h" /* -0.5 0.5 | -1 | +0 | +1 | @@ -127,6 +128,7 @@ int command_broadcast(trantorien_t *trnt, zappy_t *zappy, return EXIT_FAILURE; } spread_broadcast(trnt, zappy, action->param.broadcast_msg); + cmd_pbc(zappy->ntw, cl, action); circular_buffer_write(cl->write_to_outside, OK_RESPONSE); return EXIT_SUCCESS; } diff --git a/src/SERVER/src/trantorien/commands/command_eject.c b/src/SERVER/src/trantorien/commands/command_eject.c index f4b8af72..ce1377f2 100644 --- a/src/SERVER/src/trantorien/commands/command_eject.c +++ b/src/SERVER/src/trantorien/commands/command_eject.c @@ -10,8 +10,8 @@ #include "ntw.h" #include "zappy.h" #include "client.h" -#include "../internal.h" #include "command_reponses.h" +#include "broadcast_events.h" static void remove_eggs_on_tile(trantorien_t *tr_src, list_t *trantoriens_available) @@ -34,19 +34,6 @@ static void remove_eggs_on_tile(trantorien_t *tr_src, list_delete(to_remove); } -static void send_eject_broadcast(ntw_client_t *cl, trantorien_t *tr) -{ - if (cl == NULL || tr == NULL) { - return; - } - if (tr->actions[0] != NULL) { - action_destroy(tr->actions[0]); - tr->actions[0] = NULL; - circular_buffer_write(cl->write_to_outside, KO_EJECT_RESPONSE); - } - circular_buffer_write(cl->write_to_outside, EJECT_RESPONSE); -} - static void apply_eject_tr_pos(trantorien_t *trantorien, enum direction_e dir, map_t *map) { @@ -72,6 +59,21 @@ static void apply_eject_tr_pos(trantorien_t *trantorien, } } +static void send_eject_broadcast(ntw_t *ntw, ntw_client_t *cl, + trantorien_t *tr) +{ + if (cl == NULL || tr == NULL) { + return; + } + if (tr->actions[0] != NULL) { + action_destroy(tr->actions[0]); + tr->actions[0] = NULL; + circular_buffer_write(cl->write_to_outside, KO_EJECT_RESPONSE); + } + cmd_pex(ntw, cl, tr); + circular_buffer_write(cl->write_to_outside, EJECT_RESPONSE); +} + int command_eject(trantorien_t *trantorien_src, zappy_t *zappy, ntw_client_t *cl_src, action_t *action) { @@ -90,7 +92,7 @@ ntw_client_t *cl_src, action_t *action) if (!(IS_SAME_TR_POS(trantorien_src, tr)) || tr == trantorien_src) continue; apply_eject_tr_pos(tr, trantorien_src->direction, zappy->map); - send_eject_broadcast(L_DATA(client), tr); + send_eject_broadcast(zappy->ntw, L_DATA(client), tr); } circular_buffer_write(cl_src->write_to_outside, OK_RESPONSE); remove_eggs_on_tile(trantorien_src, zappy->trantoriens_available); diff --git a/src/SERVER/src/trantorien/commands/command_fork.c b/src/SERVER/src/trantorien/commands/command_fork.c index dd1861c3..71b575aa 100644 --- a/src/SERVER/src/trantorien/commands/command_fork.c +++ b/src/SERVER/src/trantorien/commands/command_fork.c @@ -6,10 +6,13 @@ */ #include +#include #include #include "ntw.h" +#include "client.h" #include "zappy.h" #include "command_reponses.h" +#include "broadcast_events.h" int command_fork(trantorien_t *trantorien, zappy_t *zappy, ntw_client_t *cl, action_t *action) @@ -31,5 +34,6 @@ ntw_client_t *cl, action_t *action) return EXIT_FAILURE; } circular_buffer_write(cl->write_to_outside, OK_RESPONSE); + cmd_enw(zappy->ntw, cl); return EXIT_FAILURE; } diff --git a/src/SERVER/src/trantorien/commands/command_set_object_down.c b/src/SERVER/src/trantorien/commands/command_set_object_down.c index ac590872..4e9567a1 100644 --- a/src/SERVER/src/trantorien/commands/command_set_object_down.c +++ b/src/SERVER/src/trantorien/commands/command_set_object_down.c @@ -11,6 +11,7 @@ #include "ntw.h" #include "zappy.h" #include "command_reponses.h" +#include "broadcast_events.h" static void send_broadcast(enum ressource_e obj, ntw_t *ntw, int player_id) { @@ -35,6 +36,7 @@ int command_set_object_down(trantorien_t *trantorien, zappy_t *zappy, zappy->map->tiles[i].ressources[action->param.object] += 1; circular_buffer_write(cl->write_to_outside, OK_RESPONSE); send_broadcast(action->param.object, zappy->ntw, trantorien->id); + cmd_pdr(zappy->ntw, cl, action); } else { circular_buffer_write(cl->write_to_outside, KO_RESPONSE); } diff --git a/src/SERVER/src/trantorien/commands/command_take_object.c b/src/SERVER/src/trantorien/commands/command_take_object.c index 843b1bdf..f9a5fb6a 100644 --- a/src/SERVER/src/trantorien/commands/command_take_object.c +++ b/src/SERVER/src/trantorien/commands/command_take_object.c @@ -11,14 +11,7 @@ #include "ntw.h" #include "zappy.h" #include "command_reponses.h" - -static void send_broadcast(enum ressource_e obj, ntw_t *ntw, int player_id) -{ - char buff[512] = {0}; - - snprintf(buff, 511, "pgt %d %d\n", player_id, obj); - broadcast_graphic(ntw, buff); -} +#include "broadcast_events.h" int command_take_object(trantorien_t *trantorien, zappy_t *zappy, ntw_client_t *cl, action_t *action) @@ -34,7 +27,7 @@ int command_take_object(trantorien_t *trantorien, zappy_t *zappy, zappy->map->tiles[i].ressources[action->param.object] -= 1; trantorien->ressources[action->param.object] += 1; circular_buffer_write(cl->write_to_outside, OK_RESPONSE); - send_broadcast(action->param.object, zappy->ntw, trantorien->id); + cmd_pgt(zappy->ntw, cl, action); } else { circular_buffer_write(cl->write_to_outside, KO_RESPONSE); } diff --git a/src/SERVER/src/trantorien/internal.h b/src/SERVER/src/trantorien/internal.h index 84135f0f..f2089cc7 100644 --- a/src/SERVER/src/trantorien/internal.h +++ b/src/SERVER/src/trantorien/internal.h @@ -14,8 +14,6 @@ action_t *action_init(enum cmd_code_e, enum ressource_e object, char *broadcast_msg); -void action_destroy(action_t *action); - bool trantorien_is_cmd_availible(trantorien_t *trantorien); bool trantorien_place_cmd(trantorien_t *trantorien, action_t *action); diff --git a/src/SERVER/src/zappy_check_end.c b/src/SERVER/src/zappy_check_end.c new file mode 100644 index 00000000..278b1e52 --- /dev/null +++ b/src/SERVER/src/zappy_check_end.c @@ -0,0 +1,33 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** zappy_check_end +*/ + +#include +#include "client.h" +#include "zappy.h" + +bool check_end(zappy_t *zappy, bool is_end) +{ + int nb_team_alive = 0; + client_t *cl = NULL; + + if (is_end || zappy->is_end) { + return true; + } + for (L_EACH(data, zappy->ntw->clients)) { + cl = L_DATA(L_DATAT(ntw_client_t *, data)); + if (cl == NULL || cl->type != AI || cl->cl.ai.trantorien == NULL) { + continue; + } + if (cl->cl.ai.trantorien->alive == true) { + nb_team_alive++; + } + } + if (nb_team_alive <= 1 && zappy->trantoriens_available->len == 0) { + return true; + } + return false; +} From 54be2893bfce62399014735d5d2562fab4425843 Mon Sep 17 00:00:00 2001 From: romain Date: Fri, 16 Jun 2023 17:16:13 +0200 Subject: [PATCH 02/11] ZappyServer: Update cmd pnw --- src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c | 2 +- src/SERVER/src/loop/update_client_waiting_slot_opened.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c index 0369e8b1..b1e7bf5d 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pnw.c @@ -24,7 +24,7 @@ bool cmd_pnw(ntw_t *ntw, ntw_client_t *cl) || client->cl.ai.trantorien == NULL) { return false; } - sprintf(buffer, "pnw %d %d %d %d %d %s\n", client->id, + snprintf(buffer, 511, "pnw %d %d %d %d %d %s\n", client->id, client->cl.ai.trantorien->x, client->cl.ai.trantorien->y, client->cl.ai.trantorien->direction, client->cl.ai.trantorien->level, client->cl.ai.trantorien->team_name); diff --git a/src/SERVER/src/loop/update_client_waiting_slot_opened.c b/src/SERVER/src/loop/update_client_waiting_slot_opened.c index ad4b9d9a..6c80ea3c 100644 --- a/src/SERVER/src/loop/update_client_waiting_slot_opened.c +++ b/src/SERVER/src/loop/update_client_waiting_slot_opened.c @@ -30,11 +30,11 @@ bool update_client_waiting_slot_opened(zappy_t *zappy, ntw_client_t *cl) zappy->trantoriens_available) == false) { return true; } - send_new_connection(zappy->ntw, cl); client->state = CONNECTED; client->type = AI; send_id(client, cl); send_size(zappy->args, cl); client->cl.ai.trantorien->id = client->id; + send_new_connection(zappy->ntw, cl); return true; } From 433d142a190875e168a7658d742f2c6c2fd97b87 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Sat, 17 Jun 2023 13:24:18 +0200 Subject: [PATCH 03/11] ZappyServer: fix error norm tests file --- .gitignore | 2 ++ Makefile | 4 +++- src/AI/Makefile | 14 +++++++++----- src/SERVER/Makefile | 2 +- {src/SERVER/tests => tests/SERVER}/args_t.c | 0 .../tests => tests/SERVER}/libcircular/create.c | 0 .../tests => tests/SERVER}/libcircular/destroy.c | 0 .../tests => tests/SERVER}/libcircular/empty.c | 0 .../tests => tests/SERVER}/libcircular/read.c | 0 .../tests => tests/SERVER}/libcircular/write.c | 0 .../SERVER}/loop/ai_cmd/cmd_broadcast.c | 0 .../SERVER}/loop/ai_cmd/cmd_connect_nbr.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_eject.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_fork.c | 0 .../SERVER}/loop/ai_cmd/cmd_incantation.c | 0 .../SERVER}/loop/ai_cmd/cmd_inventory.c | 0 .../SERVER}/loop/ai_cmd/cmd_look_around.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_move.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_set.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_take.c | 0 .../tests => tests/SERVER}/loop/ai_cmd/cmd_turn.c | 0 {src/SERVER/tests => tests/SERVER}/zappy_t.c | 0 22 files changed, 15 insertions(+), 7 deletions(-) rename {src/SERVER/tests => tests/SERVER}/args_t.c (100%) rename {src/SERVER/tests => tests/SERVER}/libcircular/create.c (100%) rename {src/SERVER/tests => tests/SERVER}/libcircular/destroy.c (100%) rename {src/SERVER/tests => tests/SERVER}/libcircular/empty.c (100%) rename {src/SERVER/tests => tests/SERVER}/libcircular/read.c (100%) rename {src/SERVER/tests => tests/SERVER}/libcircular/write.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_broadcast.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_connect_nbr.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_eject.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_fork.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_incantation.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_inventory.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_look_around.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_move.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_set.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_take.c (100%) rename {src/SERVER/tests => tests/SERVER}/loop/ai_cmd/cmd_turn.c (100%) rename {src/SERVER/tests => tests/SERVER}/zappy_t.c (100%) diff --git a/.gitignore b/.gitignore index 26d67b78..59bafa42 100644 --- a/.gitignore +++ b/.gitignore @@ -1006,3 +1006,5 @@ vgcore.* *.clang-format *.vscode/ *.idea +*.gcno +*.gcda diff --git a/Makefile b/Makefile index 6220d472..0fee76dd 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,8 @@ $(TARGET_server)_clean: $(MAKE) -C $(TARGET_server_DIR) clean $(TARGET_gui)_clean: - if [ -d $(TARGET_gui_DIR)/build ]; then $(RM) -r $(TARGET_gui_DIR)/build; fi + if [ -d $(TARGET_gui_DIR)/build ]; then $(RM) -r $(TARGET_gui_DIR)/build;\ + fi $(TARGET_ai)_clean: $(MAKE) -C $(TARGET_ai_DIR) clean @@ -55,6 +56,7 @@ $(TARGET_server)_fclean: $(TARGET_server)_clean $(MAKE) -C $(TARGET_server_DIR) fclean $(TARGET_gui)_fclean: $(TARGET_gui)_clean + $(RM) $(TARGET_gui_DIR)/$(TARGET_gui) $(TARGET_ai)_fclean: $(TARGET_ai)_clean $(MAKE) -C $(TARGET_ai_DIR) fclean diff --git a/src/AI/Makefile b/src/AI/Makefile index 620d5ed5..33e7df55 100644 --- a/src/AI/Makefile +++ b/src/AI/Makefile @@ -43,8 +43,9 @@ $(LOCAL_DEPS)/simple-force: $(LOCAL_DEPS)/requirements.txt piprepo build ./local_deps $(TARGET): - ([ ! -d $(LOCAL_DEPS)/simple ] && $(MAKE) $(LOCAL_DEPS)/simple-force) || \ - true + if [ ! -d $(LOCAL_DEPS)/simple ]; \ + then $(MAKE) $(LOCAL_DEPS)/simple-force; \ + fi pip install --break-system-packages --user -i ./local_deps/simple . $(CP) "$(TARGET).py" "$(TARGET)" $(TOEXEC) "$(TARGET)" @@ -55,13 +56,16 @@ clean: fclean: clean pip uninstall --break-system-packages -y $(PIP_TARGET) - $(RM) $(LOCAL_DEPS)/requirements.txt $(RM) $(TARGET) fclean-simple: fclean + $(RM) $(LOCAL_DEPS)/requirements.txt $(RM) -r $(LOCAL_DEPS)/simple $(RM) $(LOCAL_DEPS)/*.whl -re: fclean all +re: fclean +re: all -re-simple: fclean-simple $(LOCAL_DEPS)/simple all +re-simple: fclean-simple +re-simple: $(LOCAL_DEPS)/simple +re-simple: all diff --git a/src/SERVER/Makefile b/src/SERVER/Makefile index e2727aec..44f583d3 100644 --- a/src/SERVER/Makefile +++ b/src/SERVER/Makefile @@ -216,7 +216,7 @@ CC = gcc TTARGET := bin_test -T_DIR := tests +T_DIR := ../../tests/SERVER T_SRC := $(shell find $(T_DIR)/ -name '*.c' -type f) \ $(filter-out src/main.c, $(SRC)) diff --git a/src/SERVER/tests/args_t.c b/tests/SERVER/args_t.c similarity index 100% rename from src/SERVER/tests/args_t.c rename to tests/SERVER/args_t.c diff --git a/src/SERVER/tests/libcircular/create.c b/tests/SERVER/libcircular/create.c similarity index 100% rename from src/SERVER/tests/libcircular/create.c rename to tests/SERVER/libcircular/create.c diff --git a/src/SERVER/tests/libcircular/destroy.c b/tests/SERVER/libcircular/destroy.c similarity index 100% rename from src/SERVER/tests/libcircular/destroy.c rename to tests/SERVER/libcircular/destroy.c diff --git a/src/SERVER/tests/libcircular/empty.c b/tests/SERVER/libcircular/empty.c similarity index 100% rename from src/SERVER/tests/libcircular/empty.c rename to tests/SERVER/libcircular/empty.c diff --git a/src/SERVER/tests/libcircular/read.c b/tests/SERVER/libcircular/read.c similarity index 100% rename from src/SERVER/tests/libcircular/read.c rename to tests/SERVER/libcircular/read.c diff --git a/src/SERVER/tests/libcircular/write.c b/tests/SERVER/libcircular/write.c similarity index 100% rename from src/SERVER/tests/libcircular/write.c rename to tests/SERVER/libcircular/write.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_broadcast.c b/tests/SERVER/loop/ai_cmd/cmd_broadcast.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_broadcast.c rename to tests/SERVER/loop/ai_cmd/cmd_broadcast.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_connect_nbr.c b/tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_connect_nbr.c rename to tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_eject.c b/tests/SERVER/loop/ai_cmd/cmd_eject.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_eject.c rename to tests/SERVER/loop/ai_cmd/cmd_eject.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_fork.c b/tests/SERVER/loop/ai_cmd/cmd_fork.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_fork.c rename to tests/SERVER/loop/ai_cmd/cmd_fork.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_incantation.c b/tests/SERVER/loop/ai_cmd/cmd_incantation.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_incantation.c rename to tests/SERVER/loop/ai_cmd/cmd_incantation.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_inventory.c b/tests/SERVER/loop/ai_cmd/cmd_inventory.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_inventory.c rename to tests/SERVER/loop/ai_cmd/cmd_inventory.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_look_around.c b/tests/SERVER/loop/ai_cmd/cmd_look_around.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_look_around.c rename to tests/SERVER/loop/ai_cmd/cmd_look_around.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_move.c b/tests/SERVER/loop/ai_cmd/cmd_move.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_move.c rename to tests/SERVER/loop/ai_cmd/cmd_move.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_set.c b/tests/SERVER/loop/ai_cmd/cmd_set.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_set.c rename to tests/SERVER/loop/ai_cmd/cmd_set.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_take.c b/tests/SERVER/loop/ai_cmd/cmd_take.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_take.c rename to tests/SERVER/loop/ai_cmd/cmd_take.c diff --git a/src/SERVER/tests/loop/ai_cmd/cmd_turn.c b/tests/SERVER/loop/ai_cmd/cmd_turn.c similarity index 100% rename from src/SERVER/tests/loop/ai_cmd/cmd_turn.c rename to tests/SERVER/loop/ai_cmd/cmd_turn.c diff --git a/src/SERVER/tests/zappy_t.c b/tests/SERVER/zappy_t.c similarity index 100% rename from src/SERVER/tests/zappy_t.c rename to tests/SERVER/zappy_t.c From 705adfe96c86bacb23738db9a96329a26a1c4d94 Mon Sep 17 00:00:00 2001 From: romain Date: Sat, 17 Jun 2023 16:15:30 +0200 Subject: [PATCH 04/11] ZappyServer: Fix cmd_invocation ivent and fix 'args_t args' in tests with 'static args_t args' --- src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c index ca22c1c6..d614dbe1 100644 --- a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c +++ b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_invocation.c @@ -22,6 +22,5 @@ bool cmd_incantation(__attribute__((unused)) zappy_t *zappy, ntw_client_t *cl, zappy->map, zappy->ntw) == false) { return false; } - cmd_pfk(zappy->ntw, cl); return true; } From 8c1a098e0df53f926aeb6dae59c83bb31820092d Mon Sep 17 00:00:00 2001 From: romain Date: Sat, 17 Jun 2023 17:43:47 +0200 Subject: [PATCH 05/11] ZappyServer: Add tests for event pwn and ebo and pbc and pex Fix tests struct arg --- tests/SERVER/loop/events/event_incantation.c | 6 ++++++ tests/SERVER/loop/events/event_pbc.c | 6 ++++++ tests/SERVER/loop/events/event_pex.c | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 tests/SERVER/loop/events/event_incantation.c create mode 100644 tests/SERVER/loop/events/event_pbc.c create mode 100644 tests/SERVER/loop/events/event_pex.c diff --git a/tests/SERVER/loop/events/event_incantation.c b/tests/SERVER/loop/events/event_incantation.c new file mode 100644 index 00000000..512455c8 --- /dev/null +++ b/tests/SERVER/loop/events/event_incantation.c @@ -0,0 +1,6 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_incantation +*/ diff --git a/tests/SERVER/loop/events/event_pbc.c b/tests/SERVER/loop/events/event_pbc.c new file mode 100644 index 00000000..d2a2e424 --- /dev/null +++ b/tests/SERVER/loop/events/event_pbc.c @@ -0,0 +1,6 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_pbc +*/ diff --git a/tests/SERVER/loop/events/event_pex.c b/tests/SERVER/loop/events/event_pex.c new file mode 100644 index 00000000..c6e12922 --- /dev/null +++ b/tests/SERVER/loop/events/event_pex.c @@ -0,0 +1,6 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_pex +*/ From 19dd52d415d050a045987f37e1ae557404be8ee8 Mon Sep 17 00:00:00 2001 From: romain Date: Sat, 17 Jun 2023 17:44:05 +0200 Subject: [PATCH 06/11] ZappyServer: Add tests for event pwn and ebo and pbc and pex Fix tests struct arg --- tests/SERVER/loop/ai_cmd/cmd_broadcast.c | 8 +- tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_eject.c | 18 +-- tests/SERVER/loop/ai_cmd/cmd_fork.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_incantation.c | 8 +- tests/SERVER/loop/ai_cmd/cmd_inventory.c | 18 +-- tests/SERVER/loop/ai_cmd/cmd_look_around.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_move.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_set.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_take.c | 10 +- tests/SERVER/loop/ai_cmd/cmd_turn.c | 10 +- tests/SERVER/loop/events/event_incantation.c | 134 +++++++++++++++++++ tests/SERVER/loop/events/event_pbc.c | 99 ++++++++++++++ tests/SERVER/loop/events/event_pex.c | 96 +++++++++++++ tests/SERVER/loop/events/event_pwn_and_ebo.c | 109 +++++++++++++++ 15 files changed, 510 insertions(+), 50 deletions(-) create mode 100644 tests/SERVER/loop/events/event_pwn_and_ebo.c diff --git a/tests/SERVER/loop/ai_cmd/cmd_broadcast.c b/tests/SERVER/loop/ai_cmd/cmd_broadcast.c index c113707e..4c57dcf7 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_broadcast.c +++ b/tests/SERVER/loop/ai_cmd/cmd_broadcast.c @@ -16,15 +16,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), + .teams_name = NULL, .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c b/tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c index da1bfaec..7701b152 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c +++ b/tests/SERVER/loop/ai_cmd/cmd_connect_nbr.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_eject.c b/tests/SERVER/loop/ai_cmd/cmd_eject.c index c372cd7a..400e4845 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_eject.c +++ b/tests/SERVER/loop/ai_cmd/cmd_eject.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { - .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, - .height = 10, - .width = 10, - .is_ok = true, - .port = port, + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_fork.c b/tests/SERVER/loop/ai_cmd/cmd_fork.c index 6e117776..860c9a78 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_fork.c +++ b/tests/SERVER/loop/ai_cmd/cmd_fork.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_incantation.c b/tests/SERVER/loop/ai_cmd/cmd_incantation.c index 864f8604..5018396b 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_incantation.c +++ b/tests/SERVER/loop/ai_cmd/cmd_incantation.c @@ -15,15 +15,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), + .teams_name = NULL, .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_inventory.c b/tests/SERVER/loop/ai_cmd/cmd_inventory.c index e6b13cd8..c486a1e6 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_inventory.c +++ b/tests/SERVER/loop/ai_cmd/cmd_inventory.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { - .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, - .height = 10, - .width = 10, - .is_ok = true, - .port = port, + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_look_around.c b/tests/SERVER/loop/ai_cmd/cmd_look_around.c index 52b1d1e9..961c819f 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_look_around.c +++ b/tests/SERVER/loop/ai_cmd/cmd_look_around.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_move.c b/tests/SERVER/loop/ai_cmd/cmd_move.c index 4c49b7b1..6a516fa1 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_move.c +++ b/tests/SERVER/loop/ai_cmd/cmd_move.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_set.c b/tests/SERVER/loop/ai_cmd/cmd_set.c index 5b712cbf..19006a02 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_set.c +++ b/tests/SERVER/loop/ai_cmd/cmd_set.c @@ -15,15 +15,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_take.c b/tests/SERVER/loop/ai_cmd/cmd_take.c index 51055708..915e67d2 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_take.c +++ b/tests/SERVER/loop/ai_cmd/cmd_take.c @@ -15,15 +15,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/ai_cmd/cmd_turn.c b/tests/SERVER/loop/ai_cmd/cmd_turn.c index 5b909b17..40ea5c22 100644 --- a/tests/SERVER/loop/ai_cmd/cmd_turn.c +++ b/tests/SERVER/loop/ai_cmd/cmd_turn.c @@ -18,15 +18,17 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port) { - args_t args = { + static args_t args = { .clients_per_teams = 1, - .teams_name = list_create(), - .freq = 1, + .teams_name = NULL, + .freq = 1000, .height = 10, .width = 10, .is_ok = true, - .port = port, + .port = 0, }; + args.port = port; + args.teams_name = list_create(); *zappy = zappy_init(&args); ntw_client_t *client; diff --git a/tests/SERVER/loop/events/event_incantation.c b/tests/SERVER/loop/events/event_incantation.c index 512455c8..9108872d 100644 --- a/tests/SERVER/loop/events/event_incantation.c +++ b/tests/SERVER/loop/events/event_incantation.c @@ -4,3 +4,137 @@ ** File description: ** event_incantation */ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_pic_and_pie, basic_pic_and_pie) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 2, 9403, &graph); + ntw_client_t *client_r = L_DATA(zappy->ntw->clients->start); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start->next); + cr_assert_not_null(client_r); + cr_assert_not_null(client_e); + client_t *c_r = L_DATA(client_r); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_r); + cr_assert_not_null(c_e); + zappy->map->tiles[0].ressources[LINEMATE] = 1; + c_e->cl.ai.trantorien->ressources[FOOD] = 20; + c_r->cl.ai.trantorien->ressources[FOOD] = 20; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + c_r->cl.ai.trantorien->x = 1; + c_r->cl.ai.trantorien->y = 2; + circular_buffer_write(client_e->read_from_outside, "Incantation\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} + +Test(loop_event_pic_and_pie, complex_pic_and_pie) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 2, 9404, &graph); + ntw_client_t *client_r = L_DATA(zappy->ntw->clients->start); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start->next); + cr_assert_not_null(client_r); + cr_assert_not_null(client_e); + client_t *c_r = L_DATA(client_r); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_r); + cr_assert_not_null(c_e); + zappy->map->tiles[0].ressources[LINEMATE] = 1; + zappy->map->tiles[0].ressources[DERAUMERE] = 1; + zappy->map->tiles[0].ressources[SIBUR] = 1; + c_e->cl.ai.trantorien->ressources[FOOD] = 20; + c_r->cl.ai.trantorien->ressources[FOOD] = 20; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + c_e->cl.ai.trantorien->level = 2; + c_r->cl.ai.trantorien->x = 0; + c_r->cl.ai.trantorien->y = 0; + c_r->cl.ai.trantorien->level = 2; + circular_buffer_write(client_e->read_from_outside, "Incantation\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "pic %d %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_r->cl.ai.trantorien->id, c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} diff --git a/tests/SERVER/loop/events/event_pbc.c b/tests/SERVER/loop/events/event_pbc.c index d2a2e424..abe8dd0c 100644 --- a/tests/SERVER/loop/events/event_pbc.c +++ b/tests/SERVER/loop/events/event_pbc.c @@ -4,3 +4,102 @@ ** File description: ** event_pbc */ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_pbc, basic_pbc) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + char msg[] = "caca"; + char full_msg[512] = {0}; + + set_up_tests(&zappy, 2, 9402, &graph); + ntw_client_t *client_r = L_DATA(zappy->ntw->clients->start); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start->next); + cr_assert_not_null(client_r); + cr_assert_not_null(client_e); + client_t *c_r = L_DATA(client_r); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_r); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + c_r->cl.ai.trantorien->x = 0; + c_r->cl.ai.trantorien->y = 0; + snprintf(full_msg, 511, "Broadcast %s\n", msg); + circular_buffer_write(client_e->read_from_outside, full_msg); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "pbc %d %s\n", c_e->id, msg); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} diff --git a/tests/SERVER/loop/events/event_pex.c b/tests/SERVER/loop/events/event_pex.c index c6e12922..f97dd542 100644 --- a/tests/SERVER/loop/events/event_pex.c +++ b/tests/SERVER/loop/events/event_pex.c @@ -4,3 +4,99 @@ ** File description: ** event_pex */ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_pex, basic_pex) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 2, 9402, &graph); + ntw_client_t *client_r = L_DATA(zappy->ntw->clients->start); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start->next); + cr_assert_not_null(client_r); + cr_assert_not_null(client_e); + client_t *c_r = L_DATA(client_r); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_r); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + c_r->cl.ai.trantorien->x = 0; + c_r->cl.ai.trantorien->y = 0; + circular_buffer_write(client_e->read_from_outside, "Eject\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "pex %d\n", c_r->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} \ No newline at end of file diff --git a/tests/SERVER/loop/events/event_pwn_and_ebo.c b/tests/SERVER/loop/events/event_pwn_and_ebo.c new file mode 100644 index 00000000..6afb0865 --- /dev/null +++ b/tests/SERVER/loop/events/event_pwn_and_ebo.c @@ -0,0 +1,109 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** pwn_and_ebo +*/ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = WAITING_TEAM_NAME; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_at_login, pwn_and_ebo) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 1, 9401, &graph); + ntw_client_t *client = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client); + client_t *c = L_DATA(client); + cr_assert_not_null(c); + c->cl.ai.trantorien->x = 0; + c->cl.ai.trantorien->y = 0; + circular_buffer_write(client->read_from_outside, "Team1\n"); + while (circular_buffer_is_read_ready(client->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + snprintf(res, 511, "%d\n", c->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(client->write_to_outside), res); + while (circular_buffer_is_read_ready(client->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "%d %d\n", zappy->map->width, zappy->map->height); + cr_assert_str_eq(circular_buffer_read(client->write_to_outside), res); + memset(res, 0, 512); + snprintf(res, 511, "ebo %d\n", c->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + memset(res, 0, 512); + snprintf(res, 511, "pnw %d %d %d %d %d %s\n", c->cl.ai.trantorien->id, + c->cl.ai.trantorien->x, c->cl.ai.trantorien->y, c->cl.ai.trantorien->direction, + c->cl.ai.trantorien->level, c->cl.ai.trantorien->team_name); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} From 240bf7adcfba24c46ff559fb73b92210b4ba3608 Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 19 Jun 2023 10:03:01 +0200 Subject: [PATCH 07/11] ZappyServer: Add tests pfk and enk (fork) and fix fork cmd event --- src/SERVER/include/broadcast_events.h | 5 +- .../loop/state_connected/ai_cmd/cmd_fork.c | 4 +- .../state_connected/graphic_cmd/cmd_enw.c | 10 +- .../src/trantorien/commands/command_fork.c | 3 +- src/SERVER/src/trantorien/trantorien_init.c | 1 + tests/SERVER/loop/events/event_pfk_enw.c | 130 ++++++++++++++++++ 6 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 tests/SERVER/loop/events/event_pfk_enw.c diff --git a/src/SERVER/include/broadcast_events.h b/src/SERVER/include/broadcast_events.h index f3bd2ae4..d2d3f906 100644 --- a/src/SERVER/include/broadcast_events.h +++ b/src/SERVER/include/broadcast_events.h @@ -70,10 +70,11 @@ bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl); /** ** @brief broadcast event enw (new egg) ** @param ntw -** @param cl +** @param tr (trantorien) +** @param parent (parent trantorien) ** @return bool **/ -bool cmd_enw(ntw_t *ntw, ntw_client_t *cl); +bool cmd_enw(ntw_t *ntw, trantorien_t *tr, trantorien_t *parent); /** ** @brief broadcast event eht (egg hatched) diff --git a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_fork.c b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_fork.c index 1cd766c7..78d72d34 100644 --- a/src/SERVER/src/loop/state_connected/ai_cmd/cmd_fork.c +++ b/src/SERVER/src/loop/state_connected/ai_cmd/cmd_fork.c @@ -10,8 +10,9 @@ #include "zappy.h" #include "internal.h" #include "trantorien.h" +#include "broadcast_events.h" -bool cmd_fork(__attribute__((unused)) zappy_t *zappy, ntw_client_t *cl, +bool cmd_fork(zappy_t *zappy, ntw_client_t *cl, __attribute__((unused)) char **cmd_split) { client_t *cc = cl->data; @@ -19,5 +20,6 @@ bool cmd_fork(__attribute__((unused)) zappy_t *zappy, ntw_client_t *cl, if (trantorien_add_act(cc->cl.ai.trantorien, FORK) == false) { return false; } + cmd_pfk(zappy->ntw, cl); return true; } diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c index 174a42d5..76cd751b 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_enw.c @@ -8,20 +8,18 @@ #include #include #include "ntw.h" -#include "map.h" #include "internal.h" #include "client.h" -bool cmd_enw(ntw_t *ntw, ntw_client_t *cl) +bool cmd_enw(ntw_t *ntw, trantorien_t *tr, trantorien_t *parent) { char buff[512] = {0}; - client_t *client = NULL; - if (ntw == NULL || cl == NULL) { + if (ntw == NULL || tr == NULL || parent == NULL) { return false; } - client = L_DATA(cl); - snprintf(buff, 511, "enw %d\n", client->id); + snprintf(buff, 511, "enw %d %d %d %d\n", + tr->id, parent->id, tr->x, tr->y); broadcast_graphic(ntw, buff); return true; } diff --git a/src/SERVER/src/trantorien/commands/command_fork.c b/src/SERVER/src/trantorien/commands/command_fork.c index 71b575aa..f9e0c415 100644 --- a/src/SERVER/src/trantorien/commands/command_fork.c +++ b/src/SERVER/src/trantorien/commands/command_fork.c @@ -27,6 +27,7 @@ ntw_client_t *cl, action_t *action) zappy->map->width, zappy->map->height); new_trantorien->x = trantorien->x; new_trantorien->y = trantorien->y; + new_trantorien->id = get_id(); if (!new_trantorien || list_append(zappy->trantoriens_available, new_trantorien, (void (*)(void *)) &trantorien_destroy, NULL) == NULL) { @@ -34,6 +35,6 @@ ntw_client_t *cl, action_t *action) return EXIT_FAILURE; } circular_buffer_write(cl->write_to_outside, OK_RESPONSE); - cmd_enw(zappy->ntw, cl); + cmd_enw(zappy->ntw, new_trantorien, trantorien); return EXIT_FAILURE; } diff --git a/src/SERVER/src/trantorien/trantorien_init.c b/src/SERVER/src/trantorien/trantorien_init.c index 1de9255c..f2d0f3aa 100644 --- a/src/SERVER/src/trantorien/trantorien_init.c +++ b/src/SERVER/src/trantorien/trantorien_init.c @@ -10,6 +10,7 @@ #include #include "tlcstrings.h" #include "trantorien.h" +#include "zappy.h" trantorien_t *trantorien_init(const char *team_name, int width, int height) { diff --git a/tests/SERVER/loop/events/event_pfk_enw.c b/tests/SERVER/loop/events/event_pfk_enw.c new file mode 100644 index 00000000..2961d5aa --- /dev/null +++ b/tests/SERVER/loop/events/event_pfk_enw.c @@ -0,0 +1,130 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_pfk_enw +*/ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_pfk, basic_pfk) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 1, 9410, &graph); + cr_assert_not_null(zappy); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + circular_buffer_write(client_e->read_from_outside, "Fork\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} + +Test(loop_event_pfk, basic_pfk_and_enw) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 1, 9411, &graph); + cr_assert_not_null(zappy); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + zappy->map->tiles[0].ressources[LINEMATE] = 1; + zappy->map->tiles[0].ressources[DERAUMERE] = 1; + zappy->map->tiles[0].ressources[SIBUR] = 1; + c_e->cl.ai.trantorien->ressources[FOOD] = 20; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + c_e->cl.ai.trantorien->level = 2; + printf("len before = %d\n", zappy->trantoriens_available->len); + circular_buffer_write(client_e->read_from_outside, "Fork\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + memset(res, 0, 512); + snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + printf("len: %d\n", zappy->trantoriens_available->len); + trantorien_t *trantorien = L_DATA(zappy->trantoriens_available->end); + cr_assert_not_null(trantorien); + memset(res, 0, 512); + snprintf(res, 511, "enw %d %d %d %d\n", trantorien->id, c_e->cl.ai.trantorien->id, trantorien->x, trantorien->y); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + puts("ok"); +} From d3aba270aa3447d22c89e5ef7d5e67086923b4e0 Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 19 Jun 2023 18:28:11 +0200 Subject: [PATCH 08/11] ZappyServer: Add tests pex pbc edi pfk enw --- src/SERVER/src/loop/update_food.c | 6 +- .../trantorien/commands/command_incantation.c | 2 +- tests/SERVER/loop/events/event_edi.c | 100 ++++++++++++++++++ tests/SERVER/loop/events/event_pbc.c | 1 + tests/SERVER/loop/events/event_pex.c | 14 +-- tests/SERVER/loop/events/event_pfk_enw.c | 5 - 6 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 tests/SERVER/loop/events/event_edi.c diff --git a/src/SERVER/src/loop/update_food.c b/src/SERVER/src/loop/update_food.c index a57d3fa6..68e9dc13 100644 --- a/src/SERVER/src/loop/update_food.c +++ b/src/SERVER/src/loop/update_food.c @@ -13,20 +13,24 @@ static void send_event_death(ntw_t *ntw, ntw_client_t *cl, bool is_an_egg) { if (is_an_egg) { cmd_edi(ntw, cl); + } else { + cmd_pdi(ntw, cl); } - cmd_pdi(ntw, cl); } void update_food(ntw_t *ntw, trantorien_t *trantorien, ntw_client_t *cl, bool is_an_egg) { + puts("update food");j if (trantorien == NULL || trantorien->alive == false) { return; } trantorien->food_stack_freq += 1; + printf("food stack freq: %d\n", trantorien->food_stack_freq); if (trantorien->food_stack_freq < MAX_FOOD_FREQ) { return; } + printf("ca update la food\n"); trantorien->food_stack_freq = 0; if (trantorien->ressources[FOOD] > 0) { trantorien->ressources[FOOD]--; diff --git a/src/SERVER/src/trantorien/commands/command_incantation.c b/src/SERVER/src/trantorien/commands/command_incantation.c index 2bfd47bb..fa89d355 100644 --- a/src/SERVER/src/trantorien/commands/command_incantation.c +++ b/src/SERVER/src/trantorien/commands/command_incantation.c @@ -59,7 +59,7 @@ bool check_incantation_availability(trantorien_t *trantorien, map_t *map, } map_index_x_y_to_i(map, trantorien->x, trantorien->y, &map_index); for (int i = FOOD; i < PLAYER; i++) { - if (map[map_index].tiles->ressources[i + LINEMATE] < + if (map->tiles[map_index].ressources[i + LINEMATE] < level_ressources[trantorien->level - 1][i]) { return false; } diff --git a/tests/SERVER/loop/events/event_edi.c b/tests/SERVER/loop/events/event_edi.c new file mode 100644 index 00000000..f13fe7e7 --- /dev/null +++ b/tests/SERVER/loop/events/event_edi.c @@ -0,0 +1,100 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_edi +*/ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_edi, basic_edii) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 1, 9500, &graph); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->ressources[FOOD] = 1; + zappy->map->tiles[0].ressources[LINEMATE] = 1; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + circular_buffer_write(client_e->read_from_outside, "Incantation\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + memset(res, 0, 512); + snprintf(res, 511, "edi %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} diff --git a/tests/SERVER/loop/events/event_pbc.c b/tests/SERVER/loop/events/event_pbc.c index abe8dd0c..ff169e55 100644 --- a/tests/SERVER/loop/events/event_pbc.c +++ b/tests/SERVER/loop/events/event_pbc.c @@ -102,4 +102,5 @@ Test(loop_event_pbc, basic_pbc) memset(res, 0, 512); snprintf(res, 511, "pbc %d %s\n", c_e->id, msg); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + zappy_destroy(zappy); } diff --git a/tests/SERVER/loop/events/event_pex.c b/tests/SERVER/loop/events/event_pex.c index f97dd542..06b0aa0c 100644 --- a/tests/SERVER/loop/events/event_pex.c +++ b/tests/SERVER/loop/events/event_pex.c @@ -23,13 +23,13 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { static args_t args = { - .clients_per_teams = 1, - .teams_name = NULL, - .freq = 1000, - .height = 10, - .width = 10, - .is_ok = true, - .port = 0, + .clients_per_teams = 1, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, }; args.port = port; args.teams_name = list_create(); diff --git a/tests/SERVER/loop/events/event_pfk_enw.c b/tests/SERVER/loop/events/event_pfk_enw.c index 2961d5aa..63d99c8b 100644 --- a/tests/SERVER/loop/events/event_pfk_enw.c +++ b/tests/SERVER/loop/events/event_pfk_enw.c @@ -80,7 +80,6 @@ Test(loop_event_pfk, basic_pfk) char res[512] = {0}; set_up_tests(&zappy, 1, 9410, &graph); - cr_assert_not_null(zappy); ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); cr_assert_not_null(client_e); client_t *c_e = L_DATA(client_e); @@ -100,7 +99,6 @@ Test(loop_event_pfk, basic_pfk_and_enw) char res[512] = {0}; set_up_tests(&zappy, 1, 9411, &graph); - cr_assert_not_null(zappy); ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); cr_assert_not_null(client_e); client_t *c_e = L_DATA(client_e); @@ -112,7 +110,6 @@ Test(loop_event_pfk, basic_pfk_and_enw) c_e->cl.ai.trantorien->x = 0; c_e->cl.ai.trantorien->y = 0; c_e->cl.ai.trantorien->level = 2; - printf("len before = %d\n", zappy->trantoriens_available->len); circular_buffer_write(client_e->read_from_outside, "Fork\n"); while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); @@ -120,11 +117,9 @@ Test(loop_event_pfk, basic_pfk_and_enw) memset(res, 0, 512); snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); - printf("len: %d\n", zappy->trantoriens_available->len); trantorien_t *trantorien = L_DATA(zappy->trantoriens_available->end); cr_assert_not_null(trantorien); memset(res, 0, 512); snprintf(res, 511, "enw %d %d %d %d\n", trantorien->id, c_e->cl.ai.trantorien->id, trantorien->x, trantorien->y); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); - puts("ok"); } From 3c1233131e73e551af25bbec816abf54cca02457 Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 19 Jun 2023 19:23:01 +0200 Subject: [PATCH 09/11] ZappyServer: Rename /event_edi.c -> event_edi_pdi.c --- .../events/{event_edi.c => event_edi_pdi.c} | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) rename tests/SERVER/loop/events/{event_edi.c => event_edi_pdi.c} (50%) diff --git a/tests/SERVER/loop/events/event_edi.c b/tests/SERVER/loop/events/event_edi_pdi.c similarity index 50% rename from tests/SERVER/loop/events/event_edi.c rename to tests/SERVER/loop/events/event_edi_pdi.c index f13fe7e7..032b5d77 100644 --- a/tests/SERVER/loop/events/event_edi.c +++ b/tests/SERVER/loop/events/event_edi_pdi.c @@ -23,7 +23,7 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { static args_t args = { - .clients_per_teams = 1, + .clients_per_teams = 0, .teams_name = NULL, .freq = 1000, .height = 10, @@ -73,7 +73,7 @@ ntw_client_t **graphic) } } -Test(loop_event_edi, basic_edii) +Test(loop_event_pdi, basic_pdi) { zappy_t *zappy = NULL; ntw_client_t *graph = NULL; @@ -92,9 +92,63 @@ Test(loop_event_edi, basic_edii) while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } + printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + memset(res, 0, 512); + snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); +} + +Test(loop_event_edi_pdi, basic_edi) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + char res[512] = {0}; + + set_up_tests(&zappy, 1, 9500, &graph); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->ressources[FOOD] = 1; + zappy->map->tiles[0].ressources[LINEMATE] = 1; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + circular_buffer_write(client_e->read_from_outside, "Fork\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + printf("result fork = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + printf("len trantoriens available = %d\n", zappy->trantoriens_available->len); + trantorien_t *trantorien = L_DATA(zappy->trantoriens_available->end); + cr_assert_not_null(trantorien); + memset(res, 0, 512); + snprintf(res, 511, "enw %d %d %d %d\n", trantorien->id, c_e->cl.ai.trantorien->id, trantorien->x, trantorien->y); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + trantorien->ressources[FOOD] = 1; + circular_buffer_write(client_e->read_from_outside, "Incantation\n"); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + memset(res, 0, 512); + snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); + while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + memset(res, 0, 512); + snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id); + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); memset(res, 0, 512); - snprintf(res, 511, "edi %d\n", c_e->cl.ai.trantorien->id); + snprintf(res, 511, "edi %d\n", trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); } From aa75bb83dd0e03ba653555f717f87c23b9d99a85 Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 19 Jun 2023 19:57:05 +0200 Subject: [PATCH 10/11] ZappyServer: Add and fix tests for cmd_edi --- src/SERVER/Makefile | 2 +- src/SERVER/include/broadcast_events.h | 8 ++--- src/SERVER/src/loop/loop.c | 8 ++--- .../state_connected/graphic_cmd/cmd_edi.c | 13 +++----- .../state_connected/graphic_cmd/cmd_pdi.c | 8 ++--- src/SERVER/src/loop/update_food.c | 11 +++---- tests/SERVER/loop/events/event_edi_pdi.c | 31 +++++++++---------- 7 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/SERVER/Makefile b/src/SERVER/Makefile index 44f583d3..305420ed 100644 --- a/src/SERVER/Makefile +++ b/src/SERVER/Makefile @@ -283,7 +283,7 @@ tests_run: fclean $(T_OBJ) tests_run: $(LIBS_DIR)/tinylibc/libtinylibc.a tests_run: $(LIBS_DIR)/circularbuffer/libcircularbuffer.a $(CC) $(T_OBJ) -o $(TTARGET) $(LDFLAGS) $(CFLAGS) - ./$(TTARGET) || exit 1 + ./$(TTARGET) -j1 || exit 1 gcovr --exclude-directories tests || true gcovr --exclude-directories tests --branch || true true diff --git a/src/SERVER/include/broadcast_events.h b/src/SERVER/include/broadcast_events.h index d2d3f906..a33c24d2 100644 --- a/src/SERVER/include/broadcast_events.h +++ b/src/SERVER/include/broadcast_events.h @@ -62,10 +62,10 @@ bool cmd_pgt(ntw_t *ntw, ntw_client_t *cl, action_t *action); /** ** @brief broadcast event pdi (player died) ** @param ntw -** @param cl +** @param id ** @return **/ -bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl); +bool cmd_pdi(ntw_t *ntw, int id); /** ** @brief broadcast event enw (new egg) @@ -87,10 +87,10 @@ bool cmd_ebo(ntw_t *ntw, ntw_client_t *cl); /** ** @brief broadcast event edi (egg died) ** @param ntw -** @param cl +** @param id ** @return **/ -bool cmd_edi(ntw_t *ntw, ntw_client_t *cl); +bool cmd_edi(ntw_t *ntw, int id); /** ** @brief broadcast event smg (server message) diff --git a/src/SERVER/src/loop/loop.c b/src/SERVER/src/loop/loop.c index b5df95b7..a15156ae 100644 --- a/src/SERVER/src/loop/loop.c +++ b/src/SERVER/src/loop/loop.c @@ -83,9 +83,7 @@ static void update_trantoriens_available_food(ntw_t *ntw, if (trantorien == NULL) { continue; } - if (new_freq == true) { - update_food(ntw, trantorien, NULL, true); - } + update_food(ntw, trantorien, NULL, true); } } @@ -102,9 +100,9 @@ bool loop(zappy_t *zappy, bool new_freq) } status = update_client(zappy, cl, new_freq) & status; update_clients_connections(zappy->ntw); - if (L_DATAT(client_t *, client)->type == AI && new_freq == true) { + if (L_DATAT(client_t *, cl)->type == AI && new_freq == true) { update_food(zappy->ntw, - L_DATAT(client_t *, client)->cl.ai.trantorien, cl, false); + L_DATAT(client_t *, cl)->cl.ai.trantorien, cl, false); } } update_trantoriens_available_food(zappy->ntw, diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c index 6b0fea63..613cd7dc 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_edi.c @@ -12,19 +12,14 @@ #include "internal.h" #include "client.h" -bool cmd_edi(ntw_t *ntw, ntw_client_t *cl) +bool cmd_edi(ntw_t *ntw, int id) { - client_t *client = NULL; char buff[512] = {0}; - if (ntw == NULL || cl == NULL) { + if (ntw == NULL) { return false; } - client = L_DATA(cl); - if (client == NULL) { - return false; - } - snprintf(buff, 511, "edi %d\n", client->id); - circular_buffer_write(cl->write_to_outside, buff); + snprintf(buff, 511, "edi %d\n", id); + broadcast_graphic(ntw, buff); return true; } diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c index ab50939b..a44f75c1 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/cmd_pdi.c @@ -12,16 +12,14 @@ #include "internal.h" #include "client.h" -bool cmd_pdi(ntw_t *ntw, ntw_client_t *cl) +bool cmd_pdi(ntw_t *ntw, int id) { char buff[512] = {0}; - client_t *client = NULL; - if (ntw == NULL || cl == NULL) { + if (ntw == NULL) { return false; } - client = L_DATA(cl); - snprintf(buff, 511, "pdi %d\n", client->id); + snprintf(buff, 511, "pdi %d\n", id); broadcast_graphic(ntw, buff); return true; } diff --git a/src/SERVER/src/loop/update_food.c b/src/SERVER/src/loop/update_food.c index 68e9dc13..8ee793a6 100644 --- a/src/SERVER/src/loop/update_food.c +++ b/src/SERVER/src/loop/update_food.c @@ -9,28 +9,25 @@ #include "client.h" #include "broadcast_events.h" -static void send_event_death(ntw_t *ntw, ntw_client_t *cl, bool is_an_egg) +static void send_event_death(ntw_t *ntw, int id, bool is_an_egg) { if (is_an_egg) { - cmd_edi(ntw, cl); + cmd_edi(ntw, id); } else { - cmd_pdi(ntw, cl); + cmd_pdi(ntw, id); } } void update_food(ntw_t *ntw, trantorien_t *trantorien, ntw_client_t *cl, bool is_an_egg) { - puts("update food");j if (trantorien == NULL || trantorien->alive == false) { return; } trantorien->food_stack_freq += 1; - printf("food stack freq: %d\n", trantorien->food_stack_freq); if (trantorien->food_stack_freq < MAX_FOOD_FREQ) { return; } - printf("ca update la food\n"); trantorien->food_stack_freq = 0; if (trantorien->ressources[FOOD] > 0) { trantorien->ressources[FOOD]--; @@ -38,7 +35,7 @@ ntw_client_t *cl, bool is_an_egg) trantorien->alive = false; if (cl) { circular_buffer_write(cl->write_to_outside, "dead\n"); - send_event_death(ntw, cl, is_an_egg); } + send_event_death(ntw, trantorien->id, is_an_egg); } } diff --git a/tests/SERVER/loop/events/event_edi_pdi.c b/tests/SERVER/loop/events/event_edi_pdi.c index 032b5d77..9a38552a 100644 --- a/tests/SERVER/loop/events/event_edi_pdi.c +++ b/tests/SERVER/loop/events/event_edi_pdi.c @@ -23,13 +23,13 @@ static void set_up_tests(zappy_t **zappy, int nb_client, int port, ntw_client_t **graphic) { static args_t args = { - .clients_per_teams = 0, - .teams_name = NULL, - .freq = 1000, - .height = 10, - .width = 10, - .is_ok = true, - .port = 0, + .clients_per_teams = 0, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, }; args.port = port; args.teams_name = list_create(); @@ -92,13 +92,13 @@ Test(loop_event_pdi, basic_pdi) while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } - printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Elevation underway\n"); snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } - printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "dead\n"); memset(res, 0, 512); snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); @@ -115,7 +115,7 @@ Test(loop_event_edi_pdi, basic_edi) cr_assert_not_null(client_e); client_t *c_e = L_DATA(client_e); cr_assert_not_null(c_e); - c_e->cl.ai.trantorien->ressources[FOOD] = 1; + c_e->cl.ai.trantorien->ressources[FOOD] = 10000; zappy->map->tiles[0].ressources[LINEMATE] = 1; c_e->cl.ai.trantorien->x = 0; c_e->cl.ai.trantorien->y = 0; @@ -123,10 +123,9 @@ Test(loop_event_edi_pdi, basic_edi) while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } - printf("result fork = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "ok\n"); snprintf(res, 511, "pfk %d\n", c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); - printf("len trantoriens available = %d\n", zappy->trantoriens_available->len); trantorien_t *trantorien = L_DATA(zappy->trantoriens_available->end); cr_assert_not_null(trantorien); memset(res, 0, 512); @@ -137,18 +136,18 @@ Test(loop_event_edi_pdi, basic_edi) while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } - printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Elevation underway\n"); memset(res, 0, 512); snprintf(res, 511, "pic %d %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level, c_e->cl.ai.trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); while (circular_buffer_is_read_ready(client_e->write_to_outside) == false) { cr_assert_eq(loop(zappy, true), false); } - printf("result incantation = '%s'\n", circular_buffer_read(client_e->write_to_outside)); + cr_assert_str_eq(circular_buffer_read(client_e->write_to_outside), "Current level: 2\n"); memset(res, 0, 512); - snprintf(res, 511, "pdi %d\n", c_e->cl.ai.trantorien->id); + snprintf(res, 511, "edi %d\n", trantorien->id); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); memset(res, 0, 512); - snprintf(res, 511, "edi %d\n", trantorien->id); + snprintf(res, 511, "pie %d %d %d\n", c_e->cl.ai.trantorien->x, c_e->cl.ai.trantorien->y, c_e->cl.ai.trantorien->level); cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), res); } From b2502fcc24b6fa11a53789f5c6df40d3c56cdd3c Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 19 Jun 2023 20:12:24 +0200 Subject: [PATCH 11/11] ZappyServer: Add tests for suc and sbp --- .../graphic_cmd/update_graphic_cmd.c | 4 +- tests/SERVER/loop/events/event_sbp.c | 95 +++++++++++++++++++ tests/SERVER/loop/events/event_suc.c | 95 +++++++++++++++++++ 3 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 tests/SERVER/loop/events/event_sbp.c create mode 100644 tests/SERVER/loop/events/event_suc.c diff --git a/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c b/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c index 061c40d5..fed52a12 100644 --- a/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c +++ b/src/SERVER/src/loop/state_connected/graphic_cmd/update_graphic_cmd.c @@ -56,9 +56,9 @@ static bool update_cmd(zappy_t *zappy, ntw_client_t *cl, char **cmd_split) } if (status == false) { if (is_entered == false) - cmd_sbp(cl); - else cmd_suc(cl); + else + cmd_sbp(cl); status = true; } return status; diff --git a/tests/SERVER/loop/events/event_sbp.c b/tests/SERVER/loop/events/event_sbp.c new file mode 100644 index 00000000..689486d9 --- /dev/null +++ b/tests/SERVER/loop/events/event_sbp.c @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_sbp +*/ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 0, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_sbp, basic_sbp) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + + set_up_tests(&zappy, 1, 9700, &graph); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->ressources[FOOD] = 1; + zappy->map->tiles[0].ressources[LINEMATE] = 1; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + circular_buffer_write(graph->read_from_outside, "sst -1233 kljh\n"); + while (circular_buffer_is_read_ready(graph->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), "sbp\n"); +} diff --git a/tests/SERVER/loop/events/event_suc.c b/tests/SERVER/loop/events/event_suc.c new file mode 100644 index 00000000..38e6112c --- /dev/null +++ b/tests/SERVER/loop/events/event_suc.c @@ -0,0 +1,95 @@ +/* +** EPITECH PROJECT, 2023 +** ZappyCulteur +** File description: +** event_suc +*/ + +#include +#include +#include + +#include "args.h" +#include "circular_buffer.h" +#include "map.h" +#include "ntw.h" +#include "client.h" +#include "tlcllists.h" +#include "trantorien.h" +#include "zappy.h" +#include "../../../src/network/ntw_internal.h" + +static void set_up_tests(zappy_t **zappy, int nb_client, int port, +ntw_client_t **graphic) +{ + static args_t args = { + .clients_per_teams = 0, + .teams_name = NULL, + .freq = 1000, + .height = 10, + .width = 10, + .is_ok = true, + .port = 0, + }; + args.port = port; + args.teams_name = list_create(); + list_append(args.teams_name, "Team1", NULL, NULL); + *zappy = zappy_init(&args); + ntw_client_t *client; + + cr_assert_not_null(*zappy); + cr_assert_not_null((*zappy)->map); + cr_assert_not_null((*zappy)->map->tiles); + for (int i = 0; i < nb_client; i++) { + client = ntw_client_init(1); + list_append((*zappy)->ntw->clients, client, NULL, NULL); + (*zappy)->ntw->on_new_conn(client); + } + for (L_EACH(x, (*zappy)->ntw->clients)) { + ntw_client_t *client = L_DATA(x); + client_t *c = L_DATA(client); + + cr_assert_not_null(c); + c->id = get_id(); + strcpy(c->name, "test"); + c->state = CONNECTED; + c->type = AI; + c->cl.ai.trantorien = trantorien_init("mdr", args.width, args.height); + c->cl.ai.trantorien->id = c->id; + c->height = args.height; + c->width = args.width; + cr_assert_not_null(c->cl.ai.trantorien); + } + if (graphic != NULL) { + *graphic = ntw_client_init(1); + list_append((*zappy)->ntw->clients, *graphic, NULL, NULL); + (*zappy)->ntw->on_new_conn(*graphic); + client_t *c = L_DATA(*graphic); + c->cl.graphic.tmp = 0; + c->id = get_id(); + strcpy(c->name, "GRAPHIC"); + c->type = GRAPHIC; + c->state = CONNECTED; + } +} + +Test(loop_event_suc, basic_suc) +{ + zappy_t *zappy = NULL; + ntw_client_t *graph = NULL; + + set_up_tests(&zappy, 1, 9600, &graph); + ntw_client_t *client_e = L_DATA(zappy->ntw->clients->start); + cr_assert_not_null(client_e); + client_t *c_e = L_DATA(client_e); + cr_assert_not_null(c_e); + c_e->cl.ai.trantorien->ressources[FOOD] = 1; + zappy->map->tiles[0].ressources[LINEMATE] = 1; + c_e->cl.ai.trantorien->x = 0; + c_e->cl.ai.trantorien->y = 0; + circular_buffer_write(graph->read_from_outside, "caca\n"); + while (circular_buffer_is_read_ready(graph->write_to_outside) == false) { + cr_assert_eq(loop(zappy, true), false); + } + cr_assert_str_eq(circular_buffer_read(graph->write_to_outside), "suc\n"); +}