mirror of
https://github.com/AndrioCelos/TableturfBattleApp.git
synced 2026-10-25 11:06:52 -05:00
Rework sync procedure and add a loading indicator
The server will now send a sync message when the client connects via WebSocket. This removes the need to send another GET request to sync game data.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System.Web;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
namespace TableturfBattleServer;
|
||||
|
||||
internal class TableturfWebSocketBehaviour : WebSocketBehavior {
|
||||
public Guid GameID { get; set; }
|
||||
public Guid ClientToken { get; set; }
|
||||
@@ -13,7 +15,21 @@ internal class TableturfWebSocketBehaviour : WebSocketBehavior {
|
||||
this.GameID = gameID;
|
||||
if (args.TryGetValue("clientToken", out var clientTokenString) && Guid.TryParse(clientTokenString, out var clientToken))
|
||||
this.ClientToken = clientToken;
|
||||
|
||||
// Send an initial state payload.
|
||||
if (Program.games.TryGetValue(this.GameID, out var game)) {
|
||||
DTO.PlayerData? playerData = null;
|
||||
for (int i = 0; i < game.Players.Count; i++) {
|
||||
var player = game.Players[i];
|
||||
if (player.Token == this.ClientToken) {
|
||||
playerData = new(i, player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.Send(JsonConvert.SerializeObject(new DTO.WebSocketPayloadWithPlayerData<Game?>("sync", game, playerData)));
|
||||
} else
|
||||
this.Send(JsonConvert.SerializeObject(new DTO.WebSocketPayloadWithPlayerData<Game?>("sync", null, null)));
|
||||
}
|
||||
|
||||
internal void SendInternal(string data) => this.Send(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user