feat: plugin api for replication of data
This commit is contained in:
@@ -36,14 +36,18 @@ internal class RoomDataHandler: IHandler
|
||||
{
|
||||
var rawData = reader.RawData;
|
||||
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
|
||||
var player = _playerCache.GetPlayerByPeer(peerId);
|
||||
|
||||
if (player == null)
|
||||
RagonPlayer player = null;
|
||||
if (peerId != 10000)
|
||||
{
|
||||
RagonLog.Error($"Player with peerId:{peerId} not found");
|
||||
player = _playerCache.GetPlayerByPeer(peerId);
|
||||
if (player == null)
|
||||
{
|
||||
RagonLog.Error($"Player with peerId:{peerId} not found");
|
||||
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var headerSize = 3;
|
||||
|
||||
@@ -1,53 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using NLog;
|
||||
using Ragon.Server.Entity;
|
||||
using Ragon.Server.IO;
|
||||
using Ragon.Server.Plugin;
|
||||
using Ragon.Server.Room;
|
||||
|
||||
namespace Ragon.Relay;
|
||||
|
||||
public class RelayRoomPlugin: BaseRoomPlugin
|
||||
public class RelayRoomPlugin : BaseRoomPlugin
|
||||
{
|
||||
public void Tick(float dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnAttached()
|
||||
{
|
||||
Console.WriteLine("Room attached");
|
||||
}
|
||||
|
||||
public void OnDetached()
|
||||
{
|
||||
Console.WriteLine("Room detached");
|
||||
}
|
||||
|
||||
public bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
|
||||
{
|
||||
Console.WriteLine($"Entity created: {entity.Id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OnEntityRemove(RagonRoomPlayer destroyer, RagonEntity entity)
|
||||
{
|
||||
Console.WriteLine($"Entity destroyed: {entity.Id}");
|
||||
return true;
|
||||
}
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public override bool OnPlayerJoined(RagonRoomPlayer player)
|
||||
{
|
||||
// _logger.Trace($"Player {player.Name}|{player.Connection.Id} joined");
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool OnPlayerLeaved(RagonRoomPlayer player)
|
||||
{
|
||||
// _logger.Trace($"Player {player.Name}|{player.Connection.Id} leaved");
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool OnData(RagonRoomPlayer player, byte[] data)
|
||||
{
|
||||
Console.WriteLine("Data received");
|
||||
// _logger.Trace($"Data received from {player.Name}|{player.Connection.Id}");
|
||||
|
||||
// All Players
|
||||
// Room.ReplicateData(new Byte[] { 30, 40, 50 }, NetworkChannel.RELIABLE);
|
||||
// Selected Player
|
||||
// Room.ReplicateData(new byte[] { 10, 30, 40 }, new List<RagonRoomPlayer> { player }, NetworkChannel.RELIABLE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Array.Copy(data, 1, sendData, headerSize, dataSize);
|
||||
var pluginData = new byte[dataSize];
|
||||
Array.Copy(data, 1, pluginData, 0, dataSize);
|
||||
room.Plugin.OnData(player, pluginData);
|
||||
|
||||
room.Broadcast(sendData, channel);
|
||||
Array.Copy(data, 1, sendData, headerSize, dataSize);
|
||||
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);
|
||||
}
|
||||
@@ -29,6 +29,9 @@ public interface IRagonRoom
|
||||
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);
|
||||
IRagonEntity GetEntityById(ushort id);
|
||||
|
||||
@@ -144,7 +144,8 @@ 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);
|
||||
}
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user