add Client.JoinRoom()

This commit is contained in:
Robert Paciorek 2024-02-28 12:08:49 +00:00
parent e7e2c21be6
commit 9b896f6326
4 changed files with 24 additions and 33 deletions

View File

@ -7,32 +7,11 @@ namespace sodoffmmo.CommandHandlers;
[ExtensionCommandHandler("JA")] [ExtensionCommandHandler("JA")]
class JoinRoomHandler : ICommandHandler class JoinRoomHandler : ICommandHandler
{ {
Room room;
Client client;
public void Handle(Client client, NetworkObject receivedObject) public void Handle(Client client, NetworkObject receivedObject)
{ {
string roomName = receivedObject.Get<NetworkObject>("p").Get<string>("rn"); string roomName = receivedObject.Get<NetworkObject>("p").Get<string>("rn");
client.LeaveRoom(); Room room = Room.GetOrAdd(roomName);
client.InvalidatePlayerData();
room = Room.GetOrAdd(roomName);
Console.WriteLine($"Join Room: {roomName} RoomID: {room.Id} IID: {client.ClientID}"); Console.WriteLine($"Join Room: {roomName} RoomID: {room.Id} IID: {client.ClientID}");
this.client = client; client.JoinRoom(room);
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());
}
} }
} }

View File

@ -58,6 +58,26 @@ public class Client {
Room = null; 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() { public void InvalidatePlayerData() {
PlayerData = new(); PlayerData = new();

View File

@ -124,15 +124,8 @@ public class GauntletRoom : Room {
if (room is null) if (room is null)
room = GauntletRoom.Get(); room = GauntletRoom.Get();
room.AddPlayer(client); // must be call before InvalidatePlayerData - we need uid room.AddPlayer(client); // must be call before JoinRoom (before InvalidatePlayerData) - we need uid
client.JoinRoom(room);
client.LeaveRoom();
client.InvalidatePlayerData();
client.Send(room.RespondJoinRoom());
client.Send(room.SubscribeRoom());
room.SendUJR(); room.SendUJR();
client.Room = room;
} }
} }

View File

@ -3,7 +3,6 @@ using sodoffmmo.Data;
namespace sodoffmmo.Core; namespace sodoffmmo.Core;
public class Room { public class Room {
static int id = 2;
static Dictionary<string, Room> rooms = new(); static Dictionary<string, Room> rooms = new();
List<Client> clients = new(); List<Client> clients = new();