diff --git a/gui/src/Engine/Engine.cpp b/gui/src/Engine/Engine.cpp index 0d6e6eb5..7e5a8aa8 100644 --- a/gui/src/Engine/Engine.cpp +++ b/gui/src/Engine/Engine.cpp @@ -59,8 +59,6 @@ void Gui::Engine::listenServer() void Gui::Engine::sendMessageUpdate() { - updateMap(); - clock_t currentTick = clock(); if ((int)(_gameData->getServerTick()) == NO_TICK && (float)(currentTick - _gameData->getLastTick()) / CLOCKS_PER_SEC < 1) @@ -69,12 +67,6 @@ void Gui::Engine::sendMessageUpdate() return; _gameData->restartLastTick(); - _network.get()->sendMessageServer("sgt\n"); - for (auto &team : _gameData.get()->getTeams()) { - for (auto &player : team.getPlayers()) { - _network.get()->sendMessageServer("ppo " + std::to_string(player.getId()) + "\n"); - } - } if (_gameData.get()->getTimeUnitFromServer() == GameData::TimeUnitState::INCREASE) _network.get()->sendMessageServer("sst " + std::to_string(_gameData.get()->getServerTick() + 1) + "\n"); else if (_gameData.get()->getTimeUnitFromServer() == GameData::TimeUnitState::DECREASE) { diff --git a/gui/src/GUIUpdater/GUIUpdater.cpp b/gui/src/GUIUpdater/GUIUpdater.cpp index 54e0ac73..046c64f6 100644 --- a/gui/src/GUIUpdater/GUIUpdater.cpp +++ b/gui/src/GUIUpdater/GUIUpdater.cpp @@ -383,6 +383,7 @@ void Gui::GUIUpdater::updatePlayerRessourceDropping(const std::vectorgetTile(player.getPosition().first, player.getPosition().second).inventory.addResource(args[1], 1); return; } @@ -411,7 +412,7 @@ void Gui::GUIUpdater::updatePlayerRessourceCollecting(const std::vectorsendMessageServer("pin " + std::to_string(player.getId()) + "\n"); + player.inventory.addResource(args[1], 1); _gameData.get()->getTile(player.getPosition().first, player.getPosition().second).inventory.removeResource(args[1], 1); return; } diff --git a/logs/.gitkeep b/logs/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/server/Makefile b/server/Makefile index 16e0194f..76266283 100644 --- a/server/Makefile +++ b/server/Makefile @@ -45,6 +45,7 @@ SRC = src/list/list.c \ src/ai/cmd/incantation/level_up.c \ \ src/gui/gui.c \ + src/gui/handler.c \ \ src/server/run.c \ src/server/quit.c \ diff --git a/server/include/ai/cmd/command_ai.h b/server/include/ai/cmd/command_ai.h index a0b832fd..012f14ed 100644 --- a/server/include/ai/cmd/command_ai.h +++ b/server/include/ai/cmd/command_ai.h @@ -175,14 +175,14 @@ void level_up(app_t *app, ia_t *ai); * @param ai Ai who do the command. * @param line The command to add. */ -void add_command_to_list(ia_t *ai, char *line); +void add_command_to_ai_list(ia_t *ai, char *line); /** * @brief Treat command of all ai. * * @param app Application with necessary information. */ -void treat_command(app_t *app); +void treat_ai_command(app_t *app); /** * @brief Destroy the list of command. diff --git a/server/include/game/game.h b/server/include/game/game.h index 04b9dc07..2f85b911 100644 --- a/server/include/game/game.h +++ b/server/include/game/game.h @@ -9,6 +9,10 @@ #include "map/map.h" +#define END_GAME 1 +#define ERROR -1 +#define GAME_CONTINUE 0 + typedef struct s_game { size_t height; size_t width; @@ -16,6 +20,7 @@ typedef struct s_game { struct timeval start; struct timeval start_food; int freq; + int status_game; } game_t; /** @@ -39,10 +44,8 @@ void spawn_ressources(app_t *app); * @brief Check if a team have won. * * @param app Application with list of team. - * @return true if a team have won. - * @return false if there is no team's win. */ -bool check_win(app_t *app); +void check_win(app_t *app); /** * @brief Destroy the game struct. diff --git a/server/include/gui/communication.h b/server/include/gui/communication.h index 85928bd8..2f3c2f72 100644 --- a/server/include/gui/communication.h +++ b/server/include/gui/communication.h @@ -260,3 +260,18 @@ void edi_command(app_t *app, int egg_id); * @param egg_id The player id. */ void ebo_command(app_t *app, int egg_id); + +/** + * @brief Send ppo to all GUI. + * + * @param app Application with GUI list. + * @param ia IA who have move. + */ +void send_ppo(app_t *app, ia_t *ia); + +/** + * @brief Send mct to all GUI. + * + * @param app Application with GUI list. + */ +void send_mct(app_t *app); diff --git a/server/include/gui/gui.h b/server/include/gui/gui.h index 4e87f489..d9f42fed 100644 --- a/server/include/gui/gui.h +++ b/server/include/gui/gui.h @@ -15,6 +15,7 @@ typedef struct s_gui { size_t fd; + list_t *list_command; list_t *list_messages; } gui_t; @@ -50,3 +51,17 @@ gui_t *find_gui(app_t *app, size_t fd); * @param gui_list List of gui. */ void destroy_gui(list_t *gui_list); + +/** + * @brief Add a command to gui list. + * + * @param gui GUI where add the list. + * @param line Command to add. + */ +void add_command_to_gui_list(gui_t *gui, char *line); + +/** + * @brief Treat the command of all GUI. + * @param app Application with list of GUI. + */ +void treat_gui_command(app_t *app); diff --git a/server/include/server/server.h b/server/include/server/server.h index 5d15e71c..94b531fa 100644 --- a/server/include/server/server.h +++ b/server/include/server/server.h @@ -22,10 +22,6 @@ #define WELCOME_MESSAGE_LEN 8 #define SELECT_TIMEOUT_SECONDS 1 -#define END_GAME 1 -#define ERROR -1 -#define GAME_CONTINUE 0 - typedef struct s_app app_t; typedef struct s_server { @@ -185,3 +181,11 @@ void concatenate_strings(char **str1, char *str2); * @param message_list List of message to destroy. */ void destroy_message_list(list_t *message_list); + + +/** + * @brief Destroy list of command. + * + * @param message_list List of command to destroy. + */ +void destroy_command_list(list_t *command_list); diff --git a/server/src/ai/ai_destroy.c b/server/src/ai/ai_destroy.c index 864b8e7f..43ae8486 100644 --- a/server/src/ai/ai_destroy.c +++ b/server/src/ai/ai_destroy.c @@ -7,16 +7,6 @@ #include "server/client.h" -void destroy_command_list(list_t *command_list) -{ - list_node_t *temp = command_list->first; - - while (temp) { - free(temp->data.command); - temp = temp->next; - } -} - static void destroy_egg(list_t *egg_list) { list_node_t *temp = egg_list->first; diff --git a/server/src/ai/cmd/handler.c b/server/src/ai/cmd/handler.c index 2d4673f5..f6956a64 100644 --- a/server/src/ai/cmd/handler.c +++ b/server/src/ai/cmd/handler.c @@ -8,8 +8,9 @@ #include "app/app.h" #include "server/client.h" #include "ai/cmd/command_ai.h" +#include "gui/communication.h" -void add_command_to_list(ia_t *ai, char *line) +void add_command_to_ai_list(ia_t *ai, char *line) { node_data_t node; @@ -22,7 +23,7 @@ void command_ai_handler(app_t *app, ia_t *ai, char *line) char *bad_command = NULL; if (move_command(app, ai, line)) - return; + return send_ppo(app, ai); if (object_info_command(app, ai, line)) return; if (other_command(app, ai, line)) @@ -50,7 +51,7 @@ static void check_ai_stuck(ia_t *ai, app_t *app) } } -void treat_command(app_t *app) +void treat_ai_command(app_t *app) { list_node_t *temp_team = app->teams_list->first; team_t *team = NULL; diff --git a/server/src/app/app.c b/server/src/app/app.c index ce4e0093..7a2ae5c8 100644 --- a/server/src/app/app.c +++ b/server/src/app/app.c @@ -55,6 +55,16 @@ void destroy_message_list(list_t *message_list) } } +void destroy_command_list(list_t *command_list) +{ + list_node_t *temp = command_list->first; + + while (temp) { + free(temp->data.command); + temp = temp->next; + } +} + void destroy_app(app_t *app) { destroy_server(app->server); diff --git a/server/src/game/game.c b/server/src/game/game.c index f3d5579d..7dfa7d72 100644 --- a/server/src/game/game.c +++ b/server/src/game/game.c @@ -6,6 +6,7 @@ */ #include "app/app.h" +#include "gui/communication.h" static void add_ressources(size_t index_entity, tile_t **map, int x, int y) @@ -76,6 +77,7 @@ void spawn_ressources(app_t *app) if (time_elapsed(&app->game->start_food) >= (20.0 / app->game->freq)) { fill_ressources(app); gettimeofday(&app->game->start_food, NULL); + send_mct(app); } } @@ -88,6 +90,7 @@ game_t *create_game(int height, int width, int freq) new_game->height = height; new_game->width = width; new_game->freq = freq; + new_game->status_game = GAME_CONTINUE; new_game->map = create_map(width, height); gettimeofday(&new_game->start, NULL); gettimeofday(&new_game->start_food, NULL); diff --git a/server/src/game/win.c b/server/src/game/win.c index 1af34065..c23d191f 100644 --- a/server/src/game/win.c +++ b/server/src/game/win.c @@ -17,7 +17,7 @@ static size_t check_max_level(ia_t *ai) return 0; } -bool check_win(app_t *app) +void check_win(app_t *app) { list_node_t *temp_team = app->teams_list->first; team_t *team = NULL; @@ -33,10 +33,10 @@ bool check_win(app_t *app) } if (nb_player_max >= 6) { seg_command(app, team->name); - return true; + app->game->status_game = END_GAME; + return; } nb_player_max = 0; temp_team = temp_team->next; } - return false; } diff --git a/server/src/gui/communication/mct.c b/server/src/gui/communication/mct.c index bfd5bcc1..c143ab43 100644 --- a/server/src/gui/communication/mct.c +++ b/server/src/gui/communication/mct.c @@ -32,6 +32,36 @@ static void free_tile_map(char *tile, char *map) free(map); } +static void add_mct(app_t *app, char *map) +{ + list_node_t *tmp_gui = app->gui_list->first; + + while (tmp_gui) { + add_message(tmp_gui->data.gui->list_messages, map); + tmp_gui = tmp_gui->next; + } +} + +void send_mct(app_t *app) +{ + char *tile = NULL; + char *map = strdup(""); + char *final_response = NULL; + + if (!map) + return; + for (size_t y = 0; y < app->game->height; y++) { + for (size_t x = 0; x < app->game->width; x++) { + tile = get_string(app, x, y); + final_response = format_string("%s%s", map, tile); + free_tile_map(tile, map); + map = strdup(final_response); + free(final_response); + } + } + add_mct(app, map); +} + void mct_response(gui_t *gui, app_t *app, char *line) { char *tile = NULL; diff --git a/server/src/gui/communication/ppo.c b/server/src/gui/communication/ppo.c index d7e04c77..f324d93e 100644 --- a/server/src/gui/communication/ppo.c +++ b/server/src/gui/communication/ppo.c @@ -25,6 +25,19 @@ static char *get_response(ia_t *ia) return response; } +void send_ppo(app_t *app, ia_t *ia) +{ + char *response = NULL; + list_node_t *tmp_gui = app->gui_list->first; + + while (tmp_gui) { + response = format_string("ppo %d %d %d %d\n", + ia->fd, ia->position->x, ia->position->y, ia->direction); + add_message(tmp_gui->data.gui->list_messages, response); + tmp_gui = tmp_gui->next; + } +} + void ppo_response(gui_t *gui, app_t *app, char *line) { int player_id = 0; diff --git a/server/src/gui/communication/seg.c b/server/src/gui/communication/seg.c index 228453d8..16fd02e9 100644 --- a/server/src/gui/communication/seg.c +++ b/server/src/gui/communication/seg.c @@ -13,6 +13,7 @@ void seg_command(app_t *app, char *team) char *response = NULL; list_node_t *gui_node = app->gui_list->first; + printf("TEAM: [%s] win\n", team); while (gui_node) { response = format_string("seg %s\n", team); add_message(gui_node->data.gui->list_messages, response); diff --git a/server/src/gui/communication/sst.c b/server/src/gui/communication/sst.c index 4d3758c9..1d9a6f4d 100644 --- a/server/src/gui/communication/sst.c +++ b/server/src/gui/communication/sst.c @@ -16,6 +16,20 @@ #include #include +void send_sgt(app_t *app) +{ + char *response = NULL; + list_node_t *gui_node = NULL; + + if (!app->gui_list) + return; + while (gui_node) { + response = format_string("sgt %d\n", app->game->freq); + add_message(gui_node->data.gui->list_messages, response); + gui_node = gui_node->next; + } +} + void sst_response(gui_t *gui, app_t *app, char *line) { char *response = NULL; @@ -34,4 +48,5 @@ void sst_response(gui_t *gui, app_t *app, char *line) app->game->freq = freq; response = format_string("sst %d\n", freq); add_message(gui->list_messages, response); + send_sgt(app); } diff --git a/server/src/gui/gui.c b/server/src/gui/gui.c index 838ccb61..9c5048e2 100644 --- a/server/src/gui/gui.c +++ b/server/src/gui/gui.c @@ -15,6 +15,7 @@ gui_t *create_gui(int fd) if (new_gui == NULL) return NULL; new_gui->fd = fd; + new_gui->list_command = list_new(); new_gui->list_messages = list_new(); return new_gui; } @@ -87,6 +88,8 @@ void destroy_gui(list_t *gui_list) while (temp) { close(temp->data.gui->fd); destroy_message_list(temp->data.gui->list_messages); + destroy_command_list(temp->data.gui->list_command); + list_free(temp->data.gui->list_command); free(temp->data.gui->list_messages); free(temp->data.gui); temp = temp->next; diff --git a/server/src/gui/handler.c b/server/src/gui/handler.c new file mode 100644 index 00000000..c77f0c26 --- /dev/null +++ b/server/src/gui/handler.c @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2024 +** Zappy Server +** File description: +** ai +*/ + +#include "server/client.h" +#include "gui/communication.h" + +void add_command_to_gui_list(gui_t *gui, char *line) +{ + node_data_t node; + + node.command = line; + list_add_back(gui->list_command, node); +} + +static void launch_gui_cmd(gui_t *gui, app_t *app) +{ + list_node_t *tmp = gui->list_command->first; + char *command = NULL; + + while (tmp) { + command = tmp->data.command; + handle_command_gui(gui, app, command); + free(command); + list_remove_front(gui->list_command); + tmp = gui->list_command->first; + } +} + +void treat_gui_command(app_t *app) +{ + list_node_t *temp_gui = app->gui_list->first; + gui_t *gui = NULL; + + while (temp_gui) { + gui = temp_gui->data.gui; + launch_gui_cmd(gui, app); + temp_gui = temp_gui->next; + } +} diff --git a/server/src/server/read/read_data.c b/server/src/server/read/read_data.c index 65ed0d2f..cb2a6bbf 100644 --- a/server/src/server/read/read_data.c +++ b/server/src/server/read/read_data.c @@ -67,13 +67,13 @@ void handle_request(app_t *app, size_t fd, char *line) ia_t *ai = find_ia(app, fd); if (gui != NULL) { - handle_command_gui(gui, app, line); + add_command_to_gui_list(gui, strdup(line)); return; } if (ai != NULL) { if (ai->list_command->len >= 10) return; - add_command_to_list(ai, strdup(line)); + add_command_to_ai_list(ai, strdup(line)); return; } } diff --git a/server/src/server/run.c b/server/src/server/run.c index f66b8e10..db2dbb67 100644 --- a/server/src/server/run.c +++ b/server/src/server/run.c @@ -108,11 +108,13 @@ static int game_run(int result_select, app_t *app) } } spawn_ressources(app); - treat_command(app); + treat_ai_command(app); + treat_gui_command(app); treat_stuck(app); check_die(app); - if (check_win(app)) + if (app->game->status_game == END_GAME) return END_GAME; + check_win(app); return GAME_CONTINUE; } diff --git a/server/src/server/write/write_data.c b/server/src/server/write/write_data.c index 5fc8a690..6ff8e0de 100644 --- a/server/src/server/write/write_data.c +++ b/server/src/server/write/write_data.c @@ -52,15 +52,12 @@ bool write_message(app_t *app, list_t *list_messages, size_t fd) if (list_messages->len == 0) return true; tmp = list_messages->first; - while (tmp) { - bytes_writted = write(fd, tmp->data.message, - strlen(tmp->data.message)); - if (bytes_writted == -1) - return false; - free(tmp->data.message); - list_remove_front(list_messages); - tmp = list_messages->first; - } + bytes_writted = write(fd, tmp->data.message, + strlen(tmp->data.message)); + if (bytes_writted == -1) + return false; + free(tmp->data.message); + list_remove_front(list_messages); check_died_ai(app, fd); return true; }