Skip to content

Commit

Permalink
Merge pull request #250 from FppEpitech/upgrade/logic-server-gui-zapp…
Browse files Browse the repository at this point in the history
…y-server

Upgrade/logic server gui zappy server
  • Loading branch information
AxelF44 authored Jun 23, 2024
2 parents bd8840b + 19288cc commit c155b0c
Show file tree
Hide file tree
Showing 23 changed files with 186 additions and 47 deletions.
8 changes: 0 additions & 8 deletions gui/src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion gui/src/GUIUpdater/GUIUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ void Gui::GUIUpdater::updatePlayerRessourceDropping(const std::vector<std::strin
for (auto &player : team.getPlayers()) {
if (player.getId() == args[0]) {
player.setState(Gui::Player::PlayerState::DROP);
player.inventory.removeResource(args[1], 1);
_gameData.get()->getTile(player.getPosition().first, player.getPosition().second).inventory.addResource(args[1], 1);
return;
}
Expand Down Expand Up @@ -411,7 +412,7 @@ void Gui::GUIUpdater::updatePlayerRessourceCollecting(const std::vector<std::str
for (auto &player : team.getPlayers()) {
if (player.getId() == args[0]) {
player.setState(Gui::Player::PlayerState::COLLECT);
_network.get()->sendMessageServer("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;
}
Expand Down
Empty file removed logs/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 2 additions & 2 deletions server/include/ai/cmd/command_ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions server/include/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@

#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;
tile_t **map;
struct timeval start;
struct timeval start_food;
int freq;
int status_game;
} game_t;

/**
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions server/include/gui/communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
15 changes: 15 additions & 0 deletions server/include/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

typedef struct s_gui {
size_t fd;
list_t *list_command;
list_t *list_messages;
} gui_t;

Expand Down Expand Up @@ -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);
12 changes: 8 additions & 4 deletions server/include/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
10 changes: 0 additions & 10 deletions server/src/ai/ai_destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions server/src/ai/cmd/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions server/src/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions server/src/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions server/src/game/win.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
30 changes: 30 additions & 0 deletions server/src/gui/communication/mct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions server/src/gui/communication/ppo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions server/src/gui/communication/seg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions server/src/gui/communication/sst.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
#include <stdbool.h>
#include <string.h>

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;
Expand All @@ -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);
}
3 changes: 3 additions & 0 deletions server/src/gui/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit c155b0c

Please sign in to comment.