summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-11-01 20:06:23 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2023-11-01 20:06:23 +0100
commit9ae166e78fb751186de8986f042fb02d112d51f6 (patch)
tree168a9ded0f03c4dd336c68426af4c77970df522e /src
parent4a5353adf6798043c144eb9014341bbffac3c42c (diff)
disconnect and reconnect working
Diffstat (limited to 'src')
-rw-r--r--src/game.c20
-rw-r--r--src/players.c19
2 files changed, 36 insertions, 3 deletions
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;