feat: player/room user data now available on joined event

This commit is contained in:
2024-05-12 10:57:46 +03:00
parent a9e6a3e853
commit 646744c9a1
26 changed files with 328 additions and 143 deletions
+52 -9
View File
@@ -4,20 +4,63 @@ namespace Ragon.Server.Data;
public class RagonData
{
private byte[] _data;
public bool IsDirty { get; set; }
public byte[] Data
private readonly Dictionary<string, byte[]> _data = new Dictionary<string, byte[]>();
public bool IsDirty { get; private set; }
public RagonData()
{
get => _data;
set
}
public void Read(RagonBuffer buffer)
{
var len = buffer.ReadUShort();
for (int i = 0; i < len; i++)
{
_data = value;
IsDirty = true;
var key = buffer.ReadString();
var valueSize = buffer.ReadUShort();
if (valueSize > 0)
{
var value = buffer.ReadBytes(valueSize);
_data[key] = value;
}
else
{
_data[key] = Array.Empty<byte>();
}
}
}
public RagonData(byte[] data)
public void Write(RagonBuffer buffer)
{
_data = data;
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
}
var toDelete = _data
.Where(p => p.Value.Length == 0)
.Select(p => p.Key);
foreach (var prop in toDelete)
_data.Remove(prop);
IsDirty = false;
}
public void Snapshot(RagonBuffer buffer)
{
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
Console.WriteLine($"Key: {prop.Key} Value: {prop.Value.Length}");
}
}
}
@@ -26,15 +26,8 @@ namespace Ragon.Server.Handler
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return;
}
var playerUserData = Reader.ReadBytes(Reader.Capacity);
if (playerUserData.Length > _userDataLimit)
{
_logger.Warn($"Player {context.Connection.Id} exceeded user data limit");
return;
}
context.UserData.Data = playerUserData;
context.UserData.Read(Reader);
}
}
}
@@ -75,7 +75,7 @@ public sealed class RoomCreateOperation : BaseOperation
};
var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
@@ -103,11 +103,23 @@ public sealed class RoomCreateOperation : BaseOperation
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
room.UserData.Snapshot(writer);
writer.WriteUShort((ushort)room.PlayerList.Count);
foreach (var roomPlayer in room.PlayerList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
roomPlayer.Context.UserData.Snapshot(writer);
}
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
@@ -1,45 +1,46 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
namespace Ragon.Server.Handler;
public class RoomEventOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return;
}
var room = context.Room;
var player = context.RoomPlayer;
var eventId = Reader.ReadUShort();
var replicationMode = (RagonReplicationMode)Reader.ReadByte();
var eventMode = (RagonReplicationMode)Reader.ReadByte();
var targetMode = (RagonTarget)Reader.ReadByte();
var targetPlayerPeerId = (ushort)0;
if (targetMode == RagonTarget.Player)
targetPlayerPeerId = Reader.ReadUShort();
var @event = new RagonEvent(player, eventId);
@event.Read(Reader);
Writer.Clear();
Writer.WriteUShort(eventId);
Writer.WriteUShort(player.Connection.Id);
Writer.WriteUShort((ushort) replicationMode);
var sendData = Writer.ToArray();
if (targetMode == RagonTarget.Player && room.Players.TryGetValue(targetPlayerPeerId, out var targetPlayer))
{
targetPlayer.Connection.Reliable.Send(sendData);
room.ReplicateEvent(player, @event, eventMode, targetPlayer);
return;
}
foreach (var roomPlayer in room.ReadyPlayersList)
roomPlayer.Connection.Reliable.Send(sendData);
room.ReplicateEvent(player, @event, eventMode, targetMode);
}
}
@@ -46,7 +46,7 @@ public sealed class RoomJoinOperation : BaseOperation
return;
}
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
if (!existsRoom.Plugin.OnPlayerJoined(player))
@@ -56,6 +56,8 @@ public sealed class RoomJoinOperation : BaseOperation
JoinSuccess(context, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
}
@@ -64,12 +66,24 @@ public sealed class RoomJoinOperation : BaseOperation
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteString(context.RoomPlayer.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(context.RoomPlayer.Id);
writer.WriteString(room.Owner.Id);
room.UserData.Snapshot(writer);
writer.WriteUShort((ushort)room.PlayerList.Count);
foreach (var roomPlayer in room.PlayerList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
roomPlayer.Context.UserData.Snapshot(writer);
}
var sendData = writer.ToArray();
context.Connection.Reliable.Send(sendData);
}
@@ -52,7 +52,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
{
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
if (!existsRoom.Plugin.OnPlayerJoined(player))
return;
@@ -62,6 +62,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
JoinSuccess(player, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
}
else
{
@@ -72,7 +74,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
Min = _roomPayload.Min,
};
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
@@ -96,12 +98,24 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteString(room.Scene);
room.UserData.Snapshot(writer);
writer.WriteUShort((ushort)room.PlayerList.Count);
foreach (var roomPlayer in room.PlayerList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
roomPlayer.Context.UserData.Snapshot(writer);
}
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
@@ -42,16 +42,9 @@ public sealed class RoomUserDataOperation : BaseOperation
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return;
}
var roomUserData = Reader.ReadBytes(Reader.Capacity);
if (roomUserData.Length > _userDataLimit)
{
_logger.Warn("Room user data is too big");
return;
}
var room = context.Room;
if (room != null)
room.UserData.Data = roomUserData;
room.UserData.Read(Reader);
}
}
@@ -143,13 +143,6 @@ public sealed class SceneLoadedOperation : BaseOperation
{
writer.Clear();
writer.WriteOperation(RagonOperation.SNAPSHOT);
writer.WriteUShort((ushort)room.ReadyPlayersList.Count);
foreach (var roomPlayer in room.ReadyPlayersList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
}
var dynamicEntities = room.DynamicEntitiesList;
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
@@ -62,6 +62,12 @@ public class BaseRoomPlugin: IRoomPlugin
return true;
}
public virtual bool OnData(RagonRoomPlayer player, byte[] data)
{
return true;
}
#endregion
}
+2 -2
View File
@@ -30,7 +30,7 @@ public class RagonContext
public int LimitBufferedEvents { get; private set; }
public IRagonLobby Lobby { get; private set; }
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
public RagonRoom? Room { get; private set; }
public RagonRoom Room { get; private set; }
public RagonRoomPlayer? RoomPlayer { get; private set; }
public RagonData UserData { get; private set; }
public RagonScheduler Scheduler { get; private set; }
@@ -48,7 +48,7 @@ public class RagonContext
Executor = executor;
Lobby = lobby;
Scheduler = scheduler;
UserData = new RagonData(Array.Empty<byte>());
UserData = new RagonData();
}
internal void SetPlayer(RagonLobbyPlayer player)
+7 -9
View File
@@ -73,9 +73,9 @@ public class RagonServer : IRagonServer, INetworkListener
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
_scheduler.Run(new RagonActionTimer(SendRoomList, 1.0f));
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.2f));
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.2f));
_scheduler.Run(new RagonActionTimer(SendRoomList, 2.0f));
_scheduler.Run(new RagonActionTimer(SendPlayerUserData, 0.1f));
_scheduler.Run(new RagonActionTimer(SendRoomUserData, 0.1f));
_serverPlugin.OnAttached(this);
@@ -257,12 +257,11 @@ public class RagonServer : IRagonServer, INetworkListener
_writer.Clear();
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
_writer.WriteUShort(value.Connection.Id);
_writer.WriteBytes(value.UserData.Data);
value.UserData.Write(_writer);
var sendData = _writer.ToArray();
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
value.UserData.IsDirty = false;
}
}
}
@@ -275,12 +274,11 @@ public class RagonServer : IRagonServer, INetworkListener
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.ROOM_DATA_UPDATED);
_writer.WriteBytes(room.UserData.Data);
room.UserData.Write(_writer);
var sendData = _writer.ToArray();
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
room.UserData.IsDirty = false;
}
}
}
+103 -3
View File
@@ -17,6 +17,7 @@
using Ragon.Protocol;
using Ragon.Server.Data;
using Ragon.Server.Entity;
using Ragon.Server.Event;
using Ragon.Server.IO;
using Ragon.Server.Plugin;
using Ragon.Server.Time;
@@ -47,7 +48,9 @@ public class RagonRoom : IRagonRoom, IRagonAction
public List<RagonEntity> EntityList { get; private set; }
private readonly HashSet<RagonEntity> _entitiesDirtySet;
private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
{
Id = roomId;
@@ -55,7 +58,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
PlayerMax = info.Max;
PlayerMin = info.Min;
Plugin = roomPlugin;
Players = new Dictionary<ushort, RagonRoomPlayer>(info.Max);
WaitPlayersList = new List<RagonRoomPlayer>(info.Max);
ReadyPlayersList = new List<RagonRoomPlayer>(info.Max);
@@ -67,8 +70,10 @@ public class RagonRoom : IRagonRoom, IRagonAction
EntityList = new List<RagonEntity>();
_entitiesDirtySet = new HashSet<RagonEntity>();
_bufferedEvents = new List<RagonEvent>();
_limitBufferedEvents = 1000;
UserData = new RagonData(Array.Empty<byte>());
UserData = new RagonData();
Writer = new RagonBuffer();
}
@@ -93,6 +98,101 @@ public class RagonRoom : IRagonRoom, IRagonAction
_entitiesDirtySet.Remove(entity);
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer)
{
foreach (var evnt in _bufferedEvents)
{
Writer.Clear();
Writer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
Writer.WriteUShort(evnt.EventCode);
Writer.WriteUShort(evnt.Invoker.Connection.Id);
Writer.WriteByte((byte)RagonReplicationMode.Server);
evnt.Write(Writer);
var sendData = Writer.ToArray();
roomPlayer.Connection.Reliable.Send(sendData);
}
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonRoomPlayer targetPlayer
)
{
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evnt.EventCode);
buffer.WriteUShort(invoker.Connection.Id);
buffer.WriteByte((byte)eventMode);
evnt.Write(buffer);
var sendData = buffer.ToArray();
targetPlayer.Connection.Reliable.Send(sendData);
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
RagonReplicationMode eventMode,
RagonTarget targetMode
)
{
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner && _bufferedEvents.Count < _limitBufferedEvents)
{
_bufferedEvents.Add(evnt);
}
Writer.Clear();
Writer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
Writer.WriteUShort(evnt.EventCode);
Writer.WriteUShort(invoker.Connection.Id);
Writer.WriteByte((byte)eventMode);
evnt.Write(Writer);
var sendData = Writer.ToArray();
switch (targetMode)
{
case RagonTarget.Owner:
{
Owner.Connection.Reliable.Send(sendData);
break;
}
case RagonTarget.ExceptOwner:
{
foreach (var roomPlayer in ReadyPlayersList)
{
if (roomPlayer.Connection.Id != Owner.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.ExceptInvoker:
{
foreach (var roomPlayer in ReadyPlayersList)
{
if (roomPlayer.Connection.Id != invoker.Connection.Id)
roomPlayer.Connection.Reliable.Send(sendData);
}
break;
}
case RagonTarget.All:
{
Broadcast(sendData);
break;
}
}
}
public void Tick(float dt)
{
var entities = (ushort)_entitiesDirtySet.Count;
@@ -21,4 +21,5 @@ public ref struct RoomInformation
public string Scene;
public int Min;
public int Max;
public int BufferedEventsLimit;
}
+4 -2
View File
@@ -23,6 +23,7 @@ namespace Ragon.Server.Room;
public class RagonRoomPlayer
{
public INetworkConnection Connection { get; }
public RagonContext Context { get; }
public string Id { get; }
public string Name { get; }
public bool IsLoaded { get; private set; }
@@ -30,11 +31,12 @@ public class RagonRoomPlayer
public RagonRoom Room { get; private set; }
public RagonEntityCache Entities { get; private set; }
public RagonRoomPlayer(INetworkConnection connection, string id, string name)
public RagonRoomPlayer(RagonContext context, string id, string name)
{
Id = id;
Name = name;
Connection = connection;
Context = context;
Connection = context.Connection;
Entities = new RagonEntityCache();
}