Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.
Merged

Dev #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ TIMESTAMP = NO
# normally produced when WARNINGS is set to YES.
# The default value is: NO.

EXTRACT_ALL = NO
EXTRACT_ALL = YES

# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
# be included in the documentation.
Expand Down Expand Up @@ -574,7 +574,7 @@ EXTRACT_LOCAL_CLASSES = YES
# included.
# The default value is: NO.

EXTRACT_LOCAL_METHODS = NO
EXTRACT_LOCAL_METHODS = YES

# If this flag is set to YES, the members of anonymous namespaces will be
# extracted and appear in the documentation as a namespace called
Expand Down Expand Up @@ -991,7 +991,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.

INPUT = Client/src/JetpackClient Client/src/Display Client/src/Player Client/src/Network Client/src/Network/socket Client/src/Network/Poll
INPUT = Client/src/JetpackClient Client/src/Display Client/src/Player Client/src/Network Client/src/Network/socket Client/src/Network/Poll Server/include



Expand Down
11 changes: 11 additions & 0 deletions Server/include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
#ifndef APP_H_
#define APP_H_

/**
* @brief Main server application entry point.
*
* Initializes and runs the game server on the specified port,
* loading the given map file, and optionally enabling debug mode.
*
* @param port Port number as C-string on which to listen.
* @param map Path to the map file to load.
* @param debug If non-null and enable debug logging.
* @return Zero on clean exit, non-zero on error.
*/
int run_app(const char *port, const char *map, const char *debug);

#endif
22 changes: 22 additions & 0 deletions Server/include/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@
#include "player.h"
#include "server.h"

/**
* @brief Safely free a dynamically allocated pointer and NULL it.
*
* @param ptr Double-pointer to the memory to free.
*/
void simple_free(void **ptr);

/**
* @brief Destroy and free a server instance.
*
* Invokes the server's destroy method, closes sockets,
* frees resources, and sets the pointer to NULL.
*
* @param server Double-pointer to the server_t instance.
*/
void free_server(server_t **server);


/**
* @brief Destroy and free a player instance.
*
* Cleans up player resources such as buffers and sockets,
* then frees the structure and sets the pointer to NULL.
*
* @param player Double-pointer to the player_t instance.
*/
void free_player(player_t **player);

#endif /* ifndef CLEANUP_H_*/
7 changes: 7 additions & 0 deletions Server/include/data_structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#ifndef DATA_STRUCTURE_H_
#define DATA_STRUCTURE_H_

/**
* @struct position_s
* @brief Represents a 2D vector of ints.
*
* Each component is an integer, used to store
* the position in the game.
*/
typedef struct position_s {
int xPos;
int yPos;
Expand Down
8 changes: 8 additions & 0 deletions Server/include/error_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

#include <setjmp.h>

/**
* @struct error_s
* @brief Error context for try/catch emulation.
*
* @details
* Contains the jump buffer for setjmp/longjmp and an error
* message describing the exception that was thrown.
*/
typedef struct error_s {
jmp_buf buf;
char *message;
Expand Down
35 changes: 35 additions & 0 deletions Server/include/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,50 @@
#include "server.h"
#include "player.h"

/**
* @struct command_s
* @brief Descriptor for a server command and its handler.
*
* @details
* Associates a command name with the function that implements
* its behavior, taking the server context and the target player
* as parameters.
*/
typedef struct command_s {
const char *name;
void (*function)(server_t *server, player_t *player);
} command_t;

extern const command_t commands[];

/**
* @brief Send current position and state to a player.
*
* Constructs a position update message and transmits it.
*
* @param server Server context.
* @param player Target player.
*/
void send_pos(server_t *server, player_t *player);

/**
* @brief Move the player's character upward.
*
* Applies velocity boost or upward movement per game physics.
*
* @param server Server context.
* @param player Target player.
*/
void move_up(server_t *server, player_t *player);

/**
* @brief Execute an instruction for one player.
*
* @param server Server context.
* @param player Target player.
* @param i Index of the player in the server players list.
* @return Number of instructions actually processed.
*/
int execute_instructions(server_t *server, player_t *player, size_t i);

#endif
8 changes: 8 additions & 0 deletions Server/include/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

typedef struct server_s server_t;

/**
* @brief Load the game map into server memory.
*
* Reads map file specified in server->map_file,
* populates server->map array, and validates dimensions.
*
* @param server Server instance to load map into.
*/
void load_map(server_t *server);

#endif
23 changes: 23 additions & 0 deletions Server/include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@
#include <stddef.h>
#include <stdint.h>

/**
* @struct vector2_s
* @brief Représente un vecteur 2D à virgule flottante.
*
* Chaque composante est un double, utilisé pour stocker
* la position dans le jeu.
*/
typedef struct vector2_s {
double x;
double y;
} vector2_t;

/**
* @struct player_s
* @brief Represents a connected player in the game server.
*
* @details
* Tracks the player's game state, position, and communication socket.
*/
typedef struct player_s {
size_t score;
int ended;
Expand All @@ -29,6 +43,15 @@ typedef struct player_s {
struct pollfd *socket;
} player_t;

/**
* @brief Allocate and initialize a new player.
*
* Creates a player_t, assigns the given socket descriptor,
* initializes default state, and returns the instance.
*
* @param socket Pointer to the pollfd for the new connection.
* @return Pointer to newly created player_t, or NULL on failure.
*/
player_t *create_player(struct pollfd *socket);

#endif /* ifndef PLAYER_H_ */
60 changes: 60 additions & 0 deletions Server/include/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,33 @@

#define NB_PLAYER_MAX 2

/**
* @enum GameState
* @brief Represents the different lifecycle states of the game server.
*
* @var GameState::WAITING_PLAYER
* Waiting for players to connect before initialization.
* @var GameState::INIT
* Performing initialization (loading map, setting up game).
* @var GameState::STARTED
* Game is actively running.
* @var GameState::ENDED
* Game has finished and is awaiting shutdown.
*/
enum GameState {
WAITING_PLAYER,
INIT,
STARTED,
ENDED
};

/**
* @struct server_s
* @brief Core server context for the Jetpack Joyride multiplayer game.
*
* Contains configuration flags, sockets, game state, connected players,
* and function pointers for all server operations.
*/
typedef struct server_s {
int is_debug;
int is_running;
Expand All @@ -48,9 +68,49 @@ typedef struct server_s {
void (*load_map)(struct server_s *server);
} server_t;

/**
* @brief Allocate and initialize a new server.
*
* Sets up listening socket on @p port, loads the map file,
* configures poll descriptors, and assigns virtual methods.
*
* @param port TCP port number to listen on.
* @param map Path to map file.
* @param debug Debug flag (non-zero to enable debug mode).
* @return Pointer to initialized server_t, or NULL on error.
*/
server_t *create_server(int port, const char *map, int debug);

/**
* @brief Run the server's main loop.
*
* Enters polling loop, accepts new connections,
* handles input, updates game state, and sends updates.
*
* @param this Pointer to server instance.
* @return Zero on clean shutdown, non-zero on error.
*/
int run_server(struct server_s *this);

/**
* @brief Send full map data to all connected players.
*
* Iterates over server->players and transmits the map contents.
*
* @param server Server instance.
*/
void send_map(server_t *server);

/**
* @brief Log debug information on socket.
*
* If @p is_send is true, writes @p data as send data; otherwise,
* write them as received data
*
* @param server Server context.
* @param data Debug message text.
* @param is_send Is the data received or send.
*/
void debug(server_t *server, const char *data, int is_send);

#endif /* ifndef SERVER_H_ */
7 changes: 7 additions & 0 deletions Server/src/clean_up.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "cleanup.h"
#include "map.h"
#include "player.h"
#include "server.h"
#include <netinet/in.h>
Expand Down Expand Up @@ -56,6 +57,12 @@ void free_player(player_t **player)
free((*player)->socket);
(*player)->socket = NULL;
}
for (size_t i = 0; i < MAP_HEIGHT; i += 1) {
if ((*player)->map[i]) {
free((*player)->map[i]);
(*player)->map[i] = NULL;
}
}
free(*player);
*player = NULL;
}
4 changes: 4 additions & 0 deletions Server/src/instructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ static int check_early_return(server_t *server, player_t *player)
return TRUE;
if (check_collision(server, player))
return TRUE;
if (server->nb_player == 1) {
send_first_win(server, player->score, 0);
return TRUE;
}
return FALSE;
}

Expand Down
5 changes: 3 additions & 2 deletions Server/src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ static void send_map_line(server_t *server, size_t i)
}
debug(server, server->map[i], TRUE);
server->players[j]->map[i] = strdup(server->map[i]);
if (send(server->players[j]->socket->fd, "\n",
1, 0) < 0) {
if (send(server->players[j]->socket->fd, "\n", 1, 0) < 0) {
fprintf(stderr, "Unable to send map\n");
server->destroy(&server);
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -167,6 +166,8 @@ void load_map(server_t *server)
buff = NULL;
}
}
if (buff != NULL)
free(buff);
check_map_height(server, nb_lines);
fclose(file);
}
Loading