feat: plugin api for replication of data

This commit is contained in:
2024-05-16 21:28:47 +03:00
parent 0ede864f40
commit 5634a182e6
8 changed files with 83 additions and 45 deletions
@@ -80,6 +80,7 @@ public sealed class RoomCreateOperation : BaseOperation
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
room.Plugin.OnAttached(room);
roomPlayer.OnAttached(room);
context.Scheduler.Run(room);
@@ -42,8 +42,11 @@ public sealed class RoomDataOperation : BaseOperation
sendData[1] = (byte)peerId;
sendData[2] = (byte)(peerId >> 8);
var pluginData = new byte[dataSize];
Array.Copy(data, 1, pluginData, 0, dataSize);
room.Plugin.OnData(player, pluginData);
Array.Copy(data, 1, sendData, headerSize, dataSize);
room.Broadcast(sendData, channel);
room.Broadcast(sendData, room.ReadyPlayersList, NetworkChannel.RELIABLE);
}
}
@@ -79,6 +79,8 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
room.Plugin.OnAttached(room);
context.Lobby.Persist(room);
context.Scheduler.Run(room);
context.SetRoom(room, roomPlayer);
@@ -28,4 +28,5 @@ public interface IRoomPlugin
bool OnPlayerLeaved(RagonRoomPlayer player);
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity);
bool OnData(RagonRoomPlayer player, byte[] data);
}
+3
View File
@@ -28,6 +28,9 @@ public interface IRagonRoom
public int PlayerMax { get; }
public int PlayerCount { get; }
public RagonData UserData { get; }
public void ReplicateData(byte[] data, NetworkChannel channel);
public void ReplicateData(byte[] data, List<RagonRoomPlayer> player, NetworkChannel channel);
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
RagonRoomPlayer GetPlayerById(string id);
+46 -7
View File
@@ -50,7 +50,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
private readonly HashSet<RagonEntity> _entitiesDirtySet;
private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
{
Id = roomId;
@@ -58,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);
@@ -114,7 +114,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
roomPlayer.Connection.Reliable.Send(sendData);
}
}
public void ReplicateEvent(
RagonRoomPlayer invoker,
RagonEvent evnt,
@@ -124,7 +124,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
{
var room = Owner.Room;
var buffer = room.Writer;
buffer.Clear();
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
buffer.WriteUShort(evnt.EventCode);
@@ -144,17 +144,18 @@ public class RagonRoom : IRagonRoom, IRagonAction
RagonTarget targetMode
)
{
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner && _bufferedEvents.Count < _limitBufferedEvents)
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();
@@ -192,6 +193,28 @@ public class RagonRoom : IRagonRoom, IRagonAction
}
}
}
public void ReplicateData(byte[] data, NetworkChannel channel)
{
ReplicateData(data, ReadyPlayersList, channel);
}
public void ReplicateData(byte[] data, List<RagonRoomPlayer> receivers,
NetworkChannel channel = NetworkChannel.RELIABLE)
{
var dataSize = data.Length;
var headerSize = 3;
var size = headerSize + dataSize;
var sendData = new byte[size];
var peerId = 10000; // Server Peer
sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA;
sendData[1] = (byte)peerId;
sendData[2] = (byte)(peerId >> 8);
Array.Copy(data, 0, sendData, headerSize, dataSize);
Broadcast(sendData, receivers, channel);
}
public void Tick(float dt)
{
@@ -275,6 +298,8 @@ public class RagonRoom : IRagonRoom, IRagonAction
player.OnDetached();
UpdateReadyPlayerList();
Plugin.OnPlayerLeaved(player);
}
}
@@ -317,6 +342,20 @@ public class RagonRoom : IRagonRoom, IRagonAction
}
}
public void Broadcast(byte[] data, List<RagonRoomPlayer> players, NetworkChannel channel = NetworkChannel.RELIABLE)
{
if (channel == NetworkChannel.RELIABLE)
{
foreach (var p in players)
p.Connection.Reliable.Send(data);
}
else
{
foreach (var p in players)
p.Connection.Unreliable.Send(data);
}
}
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
{
return Players[connection.Id];