feat: player/room user data now available on joined event
This commit is contained in:
@@ -71,15 +71,15 @@ namespace Ragon.Client
|
||||
internal bool PropertiesChanged => _propertiesChanged;
|
||||
internal ushort SceneId => _sceneId;
|
||||
|
||||
private ushort _sceneId;
|
||||
private readonly ushort _sceneId;
|
||||
private bool _propertiesChanged;
|
||||
|
||||
private RagonPayload _spawnPayload;
|
||||
private RagonPayload _destroyPayload;
|
||||
|
||||
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
|
||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||
private readonly Dictionary<int, OnEventDelegate> _events = new();
|
||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
|
||||
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
|
||||
|
||||
public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
|
||||
{
|
||||
@@ -125,7 +125,14 @@ namespace Ragon.Client
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void Prepare(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer player, RagonPayload payload)
|
||||
public void Prepare(
|
||||
RagonClient client,
|
||||
ushort entityId,
|
||||
ushort entityType,
|
||||
bool hasAuthority,
|
||||
RagonPlayer player,
|
||||
RagonPayload payload
|
||||
)
|
||||
{
|
||||
Type = entityType;
|
||||
Id = entityId;
|
||||
|
||||
@@ -39,8 +39,6 @@ public struct RoomParameters
|
||||
|
||||
internal class JoinSuccessHandler : IHandler
|
||||
{
|
||||
|
||||
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
@@ -62,19 +60,39 @@ internal class JoinSuccessHandler : IHandler
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var roomId = reader.ReadString();
|
||||
var localId = reader.ReadString();
|
||||
var ownerId = reader.ReadString();
|
||||
var min = reader.ReadUShort();
|
||||
var max = reader.ReadUShort();
|
||||
var sceneName = reader.ReadString();
|
||||
var localId = reader.ReadString();
|
||||
var ownerId = reader.ReadString();
|
||||
|
||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||
|
||||
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
|
||||
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
|
||||
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
|
||||
|
||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||
_client.AssignRoom(room);
|
||||
room.UserData.Read(reader);
|
||||
|
||||
var playersCount = reader.ReadUShort();
|
||||
RagonLog.Trace("Players: " + playersCount);
|
||||
for (var i = 0; i < playersCount; i++)
|
||||
{
|
||||
var playerPeerId = reader.ReadUShort();
|
||||
var playerId = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
|
||||
var player = _playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||
|
||||
player.UserData.Read(reader);
|
||||
|
||||
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
||||
}
|
||||
|
||||
_client.AssignRoom(room);
|
||||
_client.SetStatus(RagonStatus.ROOM);
|
||||
|
||||
_listenerList.OnJoined();
|
||||
_listenerList.OnSceneRequest(sceneName);
|
||||
}
|
||||
}
|
||||
@@ -46,19 +46,6 @@ internal class SnapshotHandler : IHandler
|
||||
public void Handle(RagonBuffer buffer)
|
||||
{
|
||||
var entities = new List<RagonEntity>();
|
||||
var playersCount = buffer.ReadUShort();
|
||||
RagonLog.Trace("Players: " + playersCount);
|
||||
for (var i = 0; i < playersCount; i++)
|
||||
{
|
||||
var playerPeerId = buffer.ReadUShort();
|
||||
var playerId = buffer.ReadString();
|
||||
var playerName = buffer.ReadString();
|
||||
|
||||
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||
|
||||
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
||||
}
|
||||
|
||||
var dynamicEntities = buffer.ReadUShort();
|
||||
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
|
||||
for (var i = 0; i < dynamicEntities; i++)
|
||||
@@ -123,12 +110,6 @@ internal class SnapshotHandler : IHandler
|
||||
entities.Add(entity);
|
||||
}
|
||||
|
||||
if (_client.Status == RagonStatus.LOBBY)
|
||||
{
|
||||
_client.SetStatus(RagonStatus.ROOM);
|
||||
_listenerList.OnJoined();
|
||||
}
|
||||
|
||||
foreach (var entity in entities)
|
||||
entity.Attach();
|
||||
|
||||
|
||||
@@ -18,5 +18,5 @@ namespace Ragon.Client;
|
||||
|
||||
public interface IRagonDataListener
|
||||
{
|
||||
public void OnData(RagonPlayer player, byte[] data);
|
||||
public void OnData(RagonClient client, RagonPlayer player, byte[] data);
|
||||
}
|
||||
@@ -2,5 +2,5 @@ namespace Ragon.Client;
|
||||
|
||||
public interface IRagonRoomListListener
|
||||
{
|
||||
public void OnRoomListUpdate(IReadOnlyList<RagonRoomInformation> roomsInfos);
|
||||
public void OnRoomListUpdate(RagonClient client, IReadOnlyList<RagonRoomInformation> roomsInfos);
|
||||
}
|
||||
@@ -298,13 +298,13 @@ namespace Ragon.Client
|
||||
public void OnData(RagonPlayer player, byte[] data)
|
||||
{
|
||||
foreach (var listener in _dataListeners)
|
||||
listener.OnData(player, data);
|
||||
listener.OnData(_client, player, data);
|
||||
}
|
||||
|
||||
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
||||
{
|
||||
foreach (var listListener in _roomListListeners)
|
||||
listListener.OnRoomListUpdate(roomInfos);
|
||||
listListener.OnRoomListUpdate(_client, roomInfos);
|
||||
}
|
||||
|
||||
public void OnRoomUserData(IReadOnlyList<string> changes)
|
||||
|
||||
@@ -52,10 +52,10 @@ public sealed class RagonPlayerCache
|
||||
_localId = localId;
|
||||
}
|
||||
|
||||
public void AddPlayer(ushort peerId, string playerId, string playerName)
|
||||
public RagonPlayer AddPlayer(ushort peerId, string playerId, string playerName)
|
||||
{
|
||||
if (_playersById.ContainsKey(playerId))
|
||||
return;
|
||||
return null;
|
||||
|
||||
var isOwner = playerId == _ownerId;
|
||||
var isLocal = playerId == _localId;
|
||||
@@ -73,6 +73,8 @@ public sealed class RagonPlayerCache
|
||||
_players.Add(player);
|
||||
_playersById.Add(player.Id, player);
|
||||
_playersByConnection.Add(player.PeerId, player);
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
public void RemovePlayer(string playerId)
|
||||
|
||||
@@ -104,7 +104,7 @@ namespace Ragon.Client
|
||||
if (_events.TryGetValue(eventCode, out var evnt))
|
||||
evnt?.Invoke(caller, buffer);
|
||||
else
|
||||
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
||||
RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined");
|
||||
}
|
||||
|
||||
internal void HandleUserData(RagonBuffer buffer)
|
||||
|
||||
@@ -80,6 +80,8 @@ public class RagonScene
|
||||
buffer.WriteByte((byte)replicationMode);
|
||||
buffer.WriteByte((byte)target);
|
||||
|
||||
evnt.Serialize(buffer);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonUserData: IUserData
|
||||
public class RagonUserData : IUserData
|
||||
{
|
||||
public byte[] this[string key]
|
||||
{
|
||||
@@ -49,13 +49,12 @@ namespace Ragon.Client
|
||||
|
||||
public bool Dirty => _localChanges.Count > 0;
|
||||
|
||||
private readonly List<string> _localChanges = new ();
|
||||
private readonly List<string> _localChanges = new();
|
||||
private readonly Dictionary<string, bool> _changesCache = new();
|
||||
private readonly Dictionary<string, byte[]> _properties = new();
|
||||
|
||||
public RagonUserData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IReadOnlyList<string> Read(RagonBuffer buffer)
|
||||
@@ -70,7 +69,6 @@ namespace Ragon.Client
|
||||
{
|
||||
var value = buffer.ReadBytes(valueSize);
|
||||
_properties[key] = value;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -91,7 +89,7 @@ namespace Ragon.Client
|
||||
buffer.WriteString(propertyChanged);
|
||||
if (_properties.TryGetValue(propertyChanged, out var property))
|
||||
{
|
||||
buffer.WriteUShort((ushort) property.Length);
|
||||
buffer.WriteUShort((ushort)property.Length);
|
||||
buffer.WriteBytes(property);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Ragon.Server;
|
||||
using Ragon.Server.Entity;
|
||||
using Ragon.Server.Plugin;
|
||||
using Ragon.Server.Room;
|
||||
@@ -34,4 +33,10 @@ public class RelayRoomPlugin: BaseRoomPlugin
|
||||
Console.WriteLine($"Entity destroyed: {entity.Id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool OnData(RagonRoomPlayer player, byte[] data)
|
||||
{
|
||||
Console.WriteLine("Data received");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ namespace Ragon.Server.ENetServer;
|
||||
|
||||
public sealed class ENetConnection: INetworkConnection
|
||||
{
|
||||
private static ushort _iterator = 0;
|
||||
public ushort Id { get; }
|
||||
public INetworkChannel Reliable { get; private set; }
|
||||
public INetworkChannel Unreliable { get; private set; }
|
||||
@@ -30,7 +31,8 @@ public sealed class ENetConnection: INetworkConnection
|
||||
{
|
||||
_peer = peer;
|
||||
|
||||
Id = (ushort) peer.ID;
|
||||
// Id = (ushort) peer.ID;
|
||||
Id = _iterator++;
|
||||
Reliable = new ENetReliableChannel(peer, NetworkChannel.RELIABLE);
|
||||
Unreliable = new ENetUnreliableChannel(peer, NetworkChannel.UNRELIABLE);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
_data = value;
|
||||
IsDirty = true;
|
||||
var len = buffer.ReadUShort();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,14 +27,7 @@ namespace Ragon.Server.Handler
|
||||
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,22 +1,31 @@
|
||||
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;
|
||||
|
||||
@@ -26,20 +35,12 @@ public class RoomEventOperation : BaseOperation
|
||||
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,11 +66,23 @@ 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,11 +98,23 @@ 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);
|
||||
|
||||
@@ -43,15 +43,8 @@ public sealed class RoomUserDataOperation : BaseOperation
|
||||
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;
|
||||
|
||||
@@ -63,5 +63,11 @@ public class BaseRoomPlugin: IRoomPlugin
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool OnData(RagonRoomPlayer player, byte[] data)
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,6 +48,8 @@ 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)
|
||||
{
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user