From 9ae166e78fb751186de8986f042fb02d112d51f6 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Wed, 1 Nov 2023 20:06:23 +0100 Subject: disconnect and reconnect working --- build/zombies.exe | Bin 1991252 -> 1992349 bytes include/game.h | 7 ------- include/network.h | 11 +++++++++++ include/players.h | 4 ++++ main.c | 1 + src/game.c | 20 +++++++++++++++++--- src/players.c | 19 +++++++++++++++++++ 7 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 include/network.h diff --git a/build/zombies.exe b/build/zombies.exe index df4c6b3..e0be50f 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/include/game.h b/include/game.h index 03fbf11..869d64c 100644 --- a/include/game.h +++ b/include/game.h @@ -18,13 +18,6 @@ typedef enum t_game_state { GAMESTATE_PLAYING, } game_state; -typedef enum t_network_state { - CONNECTING, - WAITING_FOR_ID, - CONNECTED, - DISCONNECTED, -} network_state; - typedef struct t_game { game_state state; network_state network_state; diff --git a/include/network.h b/include/network.h new file mode 100644 index 0000000..2446342 --- /dev/null +++ b/include/network.h @@ -0,0 +1,11 @@ +#ifndef INCLUDE_NETWORK +#define INCLUDE_NETWORK + +typedef enum t_network_state { + CONNECTING, + WAITING_FOR_ID, + CONNECTED, + DISCONNECTED, +} network_state; + +#endif \ No newline at end of file diff --git a/include/players.h b/include/players.h index 99b230f..0c7433e 100644 --- a/include/players.h +++ b/include/players.h @@ -9,6 +9,7 @@ #include "math_helper.h" #include "guns.h" #include "sprite.h" +#include "../include/players.h" #define MAX_PLAYERS 10 @@ -49,6 +50,7 @@ typedef struct t_player { u64 ping; sprite sprite; vec3f velocity; + network_state connection_state; } player; #include "protocol.h" @@ -68,5 +70,7 @@ float get_player_size(platform_window* window); void move_user(platform_window* window, u32 id, protocol_move_type move, float delta); void update_players_server(); void spawn_player(u32 id, network_client client); +bool player_has_old_session(u32 id); +void rejoin_player(u32 id, network_client client); #endif \ No newline at end of file diff --git a/main.c b/main.c index 87ac283..9616392 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include "include/SDL2/SDL_mixer.h" +#include "include/network.h" #include "include/players.h" #include "include/objects.h" #include "include/map.h" diff --git a/src/game.c b/src/game.c index ffe0c0e..1e20cf9 100644 --- a/src/game.c +++ b/src/game.c @@ -4,7 +4,12 @@ static void server_on_client_disconnect(network_client c) { for (int i = 0; i < MAX_PLAYERS; i++) { player p = players[i]; - if (p.client.ConnectSocket == c.ConnectSocket) players[i].active = false; + if (p.client.ConnectSocket == c.ConnectSocket) { + //players[i].active = false; + players[i].connection_state = DISCONNECTED; + network_client_close(&players[i].client); + return; + } } } @@ -155,9 +160,18 @@ void update_server(platform_window* window) { case MESSAGE_GET_ID_UPSTREAM: { protocol_get_id_upstream* m = (protocol_get_id_upstream*)msg->message; u32 new_id = get_id_from_ip(msg->client); + + if (player_has_old_session(new_id)) { + rejoin_player(new_id, msg->client); + log_infox("Player connected to session / ip: %s id: %d", msg->client.ip, new_id); + } + else { + spawn_player(new_id, msg->client); + log_infox("Player rejoined session / ip: %s id: %d", msg->client.ip, new_id); + } + add_message_to_outgoing_queuex(create_protocol_get_id_down(new_id), msg->client); - spawn_player(new_id, msg->client); - log_infox("Player connected to server / ip: %s id: %d", msg->client.ip, new_id); + } break; case MESSAGE_USER_MOVED: { diff --git a/src/players.c b/src/players.c index 0889dab..21be388 100644 --- a/src/players.c +++ b/src/players.c @@ -26,6 +26,24 @@ int get_player_count() { return count; } +bool player_has_old_session(u32 id) { + for (int i = 0; i < MAX_PLAYERS; i++) { + if (players[i].id == id && players[i].connection_state == DISCONNECTED) { + return true; + } + } + return false; +} + +void rejoin_player(u32 id, network_client client) { + for (int i = 0; i < MAX_PLAYERS; i++) { + if (players[i].id == id) { + players[i].connection_state = CONNECTED; + players[i].client = client; + } + } +} + void spawn_player(u32 id, network_client client) { for (int i = 0; i < MAX_PLAYERS; i++) { if (players[i].active) continue; @@ -42,6 +60,7 @@ void spawn_player(u32 id, network_client client) { players[i].client = client; players[i].sprite = create_sprite(img_player_running, 22, 108, 136, 0.02f); players[i].direction = DIRECTION_DOWN; + players[i].connection_state = CONNECTED; gun g = get_gun_by_type(players[i].guntype); players[i].total_ammo = g.max_ammunition; -- cgit v1.2.3-70-g09d2