RespondJoinRoom in AddClient as atomic operation

to avoid missed player in rooms due to joining race condition
This commit is contained in:
Robert Paciorek 2024-03-01 02:34:43 +00:00
parent fb2596d3be
commit bfbd9adb82
3 changed files with 6 additions and 3 deletions

View File

@ -47,7 +47,7 @@ class SetUserVariablesHandler : ICommandHandler {
client.PlayerData.Fp = suvData.Get<string>("FP"); client.PlayerData.Fp = suvData.Get<string>("FP");
Console.WriteLine($"SUV {client.Room.Name} IID: {client.ClientID}"); Console.WriteLine($"SUV {client.Room.Name} IID: {client.ClientID}");
client.Room.AddClient(client);
UpdatePlayersInRoom(); UpdatePlayersInRoom();
SendSUVToPlayerInRoom(); SendSUVToPlayerInRoom();
} }

View File

@ -63,7 +63,7 @@ public class Client {
LeaveRoom(); LeaveRoom();
InvalidatePlayerData(); InvalidatePlayerData();
Room = room; Room = room;
Send(Room.RespondJoinRoom()); Room.AddClient(this);
Send(Room.SubscribeRoom()); Send(Room.SubscribeRoom());
UpdatePlayerUserVariables(); UpdatePlayerUserVariables();
} }

View File

@ -6,7 +6,7 @@ public class Room {
static Dictionary<string, Room> rooms = new(); static Dictionary<string, Room> rooms = new();
List<Client> clients = new(); List<Client> clients = new();
public object roomLock = new object(); protected object roomLock = new object();
public int Id { get; private set; } public int Id { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
@ -38,6 +38,9 @@ public class Room {
public void AddClient(Client client) { public void AddClient(Client client) {
lock (roomLock) { lock (roomLock) {
client.Send(RespondJoinRoom());
// NOTE: send RespondJoinRoom() and add client to clients as atomic operation
// to make sure to client get full list of players in room
clients.Add(client); clients.Add(client);
} }
} }