diff --git a/src/CommandHandlers/JoinRoomHandler.cs b/src/CommandHandlers/JoinRoomHandler.cs index 8408f03..8f7c1d7 100644 --- a/src/CommandHandlers/JoinRoomHandler.cs +++ b/src/CommandHandlers/JoinRoomHandler.cs @@ -7,32 +7,11 @@ namespace sodoffmmo.CommandHandlers; [ExtensionCommandHandler("JA")] class JoinRoomHandler : ICommandHandler { - Room room; - Client client; public void Handle(Client client, NetworkObject receivedObject) { string roomName = receivedObject.Get("p").Get("rn"); - client.LeaveRoom(); - client.InvalidatePlayerData(); - room = Room.GetOrAdd(roomName); + Room room = Room.GetOrAdd(roomName); Console.WriteLine($"Join Room: {roomName} RoomID: {room.Id} IID: {client.ClientID}"); - this.client = client; - - client.Send(room.RespondJoinRoom()); - client.Send(room.SubscribeRoom()); - UpdatePlayerUserVariables(); - client.Room = room; - } - - private void UpdatePlayerUserVariables() { - foreach (Client c in room.Clients) { - NetworkObject cmd = new(); - NetworkObject obj = new(); - cmd.Add("c", "SUV"); - obj.Add("MID", c.ClientID); - cmd.Add("p", obj); - client.Send(NetworkObject.WrapObject(1, 13, cmd).Serialize()); - } - + client.JoinRoom(room); } } diff --git a/src/Core/Client.cs b/src/Core/Client.cs index 4c17fb8..efb88fa 100644 --- a/src/Core/Client.cs +++ b/src/Core/Client.cs @@ -58,6 +58,26 @@ public class Client { Room = null; } } + + public void JoinRoom(Room room) { + LeaveRoom(); + InvalidatePlayerData(); + Room = room; + Send(Room.RespondJoinRoom()); + Send(Room.SubscribeRoom()); + UpdatePlayerUserVariables(); + } + + private void UpdatePlayerUserVariables() { + foreach (Client c in Room.Clients) { + NetworkObject cmd = new(); + NetworkObject obj = new(); + cmd.Add("c", "SUV"); + obj.Add("MID", c.ClientID); + cmd.Add("p", obj); + Send(NetworkObject.WrapObject(1, 13, cmd).Serialize()); + } + } public void InvalidatePlayerData() { PlayerData = new(); diff --git a/src/Core/GauntletRoom.cs b/src/Core/GauntletRoom.cs index a9fa90f..d427271 100644 --- a/src/Core/GauntletRoom.cs +++ b/src/Core/GauntletRoom.cs @@ -124,15 +124,8 @@ public class GauntletRoom : Room { if (room is null) room = GauntletRoom.Get(); - room.AddPlayer(client); // must be call before InvalidatePlayerData - we need uid - - client.LeaveRoom(); - client.InvalidatePlayerData(); - - client.Send(room.RespondJoinRoom()); - client.Send(room.SubscribeRoom()); + room.AddPlayer(client); // must be call before JoinRoom (before InvalidatePlayerData) - we need uid + client.JoinRoom(room); room.SendUJR(); - - client.Room = room; } } diff --git a/src/Core/Room.cs b/src/Core/Room.cs index 173f311..f05156c 100644 --- a/src/Core/Room.cs +++ b/src/Core/Room.cs @@ -3,7 +3,6 @@ using sodoffmmo.Data; namespace sodoffmmo.Core; public class Room { - static int id = 2; static Dictionary rooms = new(); List clients = new();