Skip to content

Commit

Permalink
merge: pull request #101 from G-Epitech/73-server-add-world-lifecycle
Browse files Browse the repository at this point in the history
feat(server): add world lifecycle
  • Loading branch information
flavien-chenu authored Jun 22, 2024
2 parents 27197ea + 6081903 commit 4d4ae5b
Show file tree
Hide file tree
Showing 45 changed files with 753 additions and 139 deletions.
2 changes: 1 addition & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ re:
@$(MAKE) fclean
@$(MAKE) all

tests_run: fclean
tests_run: fclean
@$(MAKE) -C tests/libs
@cmake -S . -B $(BUILD_PATH) -DCOVERAGE=ON
@cmake --build $(BUILD_PATH) --target $(TESTS_NAME)
Expand Down
51 changes: 51 additions & 0 deletions server/includes/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,54 @@ void app_handle_world_routine_resources_generation(world_t *world);
* @return true if a team has won, false otherwise
*/
bool app_handle_world_routine_team_victory(world_t *world, server_t *server);

/**
* @brief Handle controllers requests
* @param app App instance
*/
void app_handle_controllers_requests(app_t *app);

/**
* @brief Promote an unknown controller to graphic controller
* @param app App instance
* @param controller Controller to promote
*/
void app_try_promote_controller_to_graphic(app_t *app,
controller_t *controller);

/**
* @brief Promote an unknown controller to player controller
* @param app App instance
* @param controller Controller to promote
* @param request Request to promote
* @param team_name_len Length of the team name
*/
void app_try_promote_controller_to_player(app_t *app,
controller_t *controller, request_t *request, size_t team_name_len);

/**
* @brief Handle request of an unknown controller
* @param app App instance
* @param controller Controller to handle
* @param request Request to handle
*/
void app_handle_unknown_request(app_t *app, controller_t *controller,
request_t *request);

/**
* @brief Handle request of a graphic controller
* @param app App instance
* @param controller Controller to handle
* @param request Request to handle
*/
void app_handle_graphic_request(app_t *app, controller_t *controller,
request_t *request);

/**
* @brief Handle request of a player controller
* @param app App instance
* @param controller Controller to handle
* @param request Request to handle
*/
void app_handle_player_request(app_t *app, controller_t *controller,
request_t *request);
46 changes: 45 additions & 1 deletion server/includes/types/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
#include "types/request.h"
#include "types/trantor/chrono.h"
#include "types/trantor/team.h"
#include "types/trantor/map.h"
#include "types/vector2.h"
#include "buffer.h"

// Forward declaration of server
typedef struct server_s server_t;

// Max number of requests a player can have
#define CTRL_PLAYER_MAX_REQ 10

// Max number of requests a generic controller can have
#define CTRL_GENERIC_MAX_REQ 3

// Average size of emission line for graphic controller
#define CTRL_GRAPHIC_AVERAGE_EMISSION_SIZE 40

// Size of emission buffer
#define CTRL_EMIT_BUFF_SIZE 4096

Expand Down Expand Up @@ -62,6 +72,8 @@ typedef struct generic_controller_s {
controller_type_t type;
// @brief Controller state
controller_state_t state;
// @brief Parent server
server_t *server;
} generic_controller_t;

// @brief Represent a graphic controller
Expand All @@ -79,6 +91,8 @@ typedef struct player_controller_s {
controller_type_t type;
// @brief Controller state
controller_state_t state;
// @brief Parent server
server_t *server;
// @brief Link to player instance
player_t *player;
// @brief Player cooldown (time units locked for)
Expand Down Expand Up @@ -125,8 +139,16 @@ void controller_free_as_node_data(node_data_t data);
/**
* @brief Emit as much emissions as possible of the controller
* @param controller Controller to emit
* @return true if the controller emitted, false otherwise
*/
void controller_emit(controller_t *controller);
bool controller_emit(controller_t *controller);

/**
* @brief Try to emit current buffer of a controller
* @param controller Controller to emit
* @return true if the controller emitted, false otherwise
*/
bool controller_try_emit(controller_t *controller);

/**
* @brief Add an emission to the controller from a format
Expand Down Expand Up @@ -214,3 +236,25 @@ void controller_handle_buffer(controller_t *controller,
*/
bool controller_player_from_generic(controller_t *controller,
player_t *player);

/**
* @brief Initialize a graphic controller from a generic controller
* @param controller Controller to initialize
* @param map Map to use to dynamically allocate buffer
* @return true if the controller was initialized, false otherwise
*/
bool controller_graphic_from_generic(controller_t *controller, map_t *map);

/**
* @brief Check if a controller can emit data
* @param controller Controller to check
* @return Emission possibility
*/
bool controller_can_receive(controller_t *controller);

/**
* @brief Check if a controller has content to read
* @param controller Controller to check
* @return Read possibility
*/
bool controller_has_content_to_read(controller_t *controller);
7 changes: 7 additions & 0 deletions server/includes/types/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ void request_free_as_node_data(node_data_t data);
* @param size Size of data (must be less or equal to REQUEST_BUFF_SIZE)
*/
bool request_append(request_t *request, const char *data, size_t size);

/**
* @brief Get the size of the first token in the request buffer
* @param request Request to get the first token size from
* @return Size of the first token
*/
size_t request_get_first_token_size(request_t *request);
20 changes: 9 additions & 11 deletions server/includes/types/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,22 @@ void server_remove_disconnected_controllers(server_t *server);
*/
controller_t *server_get_controller_by_socket(server_t *server, int socket);

/**
* @brief Check if a controller has content to read
* @param server Server to check
* @param controller Controller to check
* @return true If controller has content to read
* @return false If controller has no content to read
*/
bool server_controller_has_content_to_read(server_t *server,
controller_t *controller);

/**
* @brief Poll server controllers
* @param server Server to poll
* @param timeout Timeout to wait for
* @return Number of controllers that have content to read
* or -1 if an error occurred
*/
int server_poll(server_t *server, timeval_t *timeout);
int server_poll_all_controllers(server_t *server, timeval_t *timeout);

/**
* @brief Poll server controller
* @param server Server to poll controller from
* @param controller Controller to poll
* @return Polling success
*/
bool server_poll_controller(server_t *server, controller_t *controller);

/**
* @brief Close all server connections
Expand Down
7 changes: 7 additions & 0 deletions server/includes/types/trantor/team.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ void team_free(team_t *team);
* @param data The team to free
*/
void team_free_as_node_data(node_data_t data);

/**
* @brief Get a random egg from the team
* @param team The team to get the egg from
* @return Random egg from the team or NULL if no egg is available
*/
egg_t *team_get_random_egg(team_t *team);
7 changes: 4 additions & 3 deletions server/includes/types/trantor/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ void world_unregister_player(world_t *world, player_t *player);
* @param player Player to remove
* @param zombie Specify if the player is a zombie and should not be
* handled as a dead player. It will be freed.
* @return Added egg if needed or NULL if fail or not needed
*/
void world_kill_player(world_t *world, player_t *player, bool zombie);
egg_t *world_kill_player(world_t *world, player_t *player, bool zombie);

/**
* @brief Create an egg in the world for a team
Expand All @@ -114,9 +115,9 @@ egg_t *world_add_egg(world_t *world, team_t *team, long laid_by);
* @brief Ensure a team has a minimum number of slots
* @param world World to ensure the slots in
* @param team Team to ensure the slots in
* @return Number of slots that have been added
* @return Added egg or NULL if failed or not needed
*/
size_t world_ensure_team_slots(world_t *world, team_t *team);
egg_t *world_add_egg_if_needed(world_t *world, team_t *team);

/**
* @brief Kill an egg in the world
Expand Down
9 changes: 9 additions & 0 deletions server/includes/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@

#pragma once

#include <stdbool.h>
#include <stdarg.h>

// @brief Macro to get the max value between two values
#define MAX(a, b) ((a) > (b) ? (a) : (b))

// @brief Macro to get the min value between two values
#define MIN(a, b) ((a) < (b) ? (a) : (b))

// @brief Macro to get the size of a string without
// the null-terminating character
#define STR_STRICT_SIZEOF(s) (sizeof(s) - 1)

// @brief Time value
Expand Down
1 change: 1 addition & 0 deletions server/src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ target_sources(zappy_server_core PRIVATE
)
add_subdirectory(steps)
add_subdirectory(world)
add_subdirectory(requests)
1 change: 1 addition & 0 deletions server/src/app/pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
static const app_step_t app_pipeline[] = {
&app_handle_server_connections,
& app_handle_world_lifecycle,
&app_handle_controllers_requests,
&app_handle_world_routines,
};

Expand Down
7 changes: 7 additions & 0 deletions server/src/app/requests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target_sources(zappy_server_core PRIVATE
unknown/handler.c
unknown/promote_graphic.c
unknown/promote_player.c
player/handler.c
graphic/handler.c
)
15 changes: 15 additions & 0 deletions server/src/app/requests/graphic/handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2024
** zappy_server
** File description:
** handler.c
*/

#include <string.h>
#include "app.h"

void app_handle_graphic_request(app_t *app, controller_t *controller,
request_t *request)
{
request->status = REQ_FINISHED;
}
15 changes: 15 additions & 0 deletions server/src/app/requests/player/handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
** EPITECH PROJECT, 2024
** zappy_server
** File description:
** handler.c
*/

#include <string.h>
#include "app.h"

void app_handle_player_request(app_t *app, controller_t *controller,
request_t *request)
{
request->status = REQ_FINISHED;
}
26 changes: 26 additions & 0 deletions server/src/app/requests/unknown/handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
** EPITECH PROJECT, 2024
** zappy_server
** File description:
** handler.c
*/

#include <string.h>
#include <stdio.h>
#include "app.h"

void app_handle_unknown_request(app_t *app, controller_t *controller,
request_t *request)
{
size_t token_size = request_get_first_token_size(request);

if (token_size == STR_STRICT_SIZEOF("GRAPHIC") &&
memcmp(request->buffer, "GRAPHIC", token_size) == 0
) {
app_try_promote_controller_to_graphic(app, controller);
} else {
app_try_promote_controller_to_player(app, controller, request,
token_size);
}
request->status = REQ_FINISHED;
}
Loading

0 comments on commit 4d4ae5b

Please sign in to comment.