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
+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();
}