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")]
class JoinRoomHandler : ICommandHandler
{
Room room;
Client client;
public void Handle(Client client, NetworkObject receivedObject)
{
string roomName = receivedObject.Get<NetworkObject>("p").Get<string>("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);
}
}

View File

@ -59,6 +59,26 @@ public class Client {
}
}
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();
}

View File

@ -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;
}
}

View File

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