From fc28f512bafa985f1b2d3f803244ae3c96d258a0 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Fri, 14 Apr 2023 14:32:04 +0400 Subject: [PATCH] :recycle: plugin api --- .../Handler/AuthorizeSuccessHandler.cs | 3 +- ...stroyHandler.cs => EntityRemoveHandler.cs} | 4 +-- Ragon.Client/Sources/RagonClient.cs | 2 +- Ragon.Client/Sources/RagonListenerList.cs | 2 +- Ragon.Server/Sources/Entity/IRagonEntity.cs | 16 +++++++++ .../Sources/Entity/IRagonEntityState.cs | 8 +++++ Ragon.Server/Sources/Entity/RagonEntity.cs | 29 +++++++++------ .../Sources/Entity/RagonEntityState.cs | 10 +++--- .../Sources/Handler/EntityCreateOperation.cs | 2 +- .../Sources/Handler/EntityStateOperation.cs | 8 +++-- .../Sources/Handler/RoomJoinOperation.cs | 13 ++++--- .../Sources/Handler/RoomLeaveOperation.cs | 18 +++++----- .../Sources/Handler/SceneLoadedOperation.cs | 2 +- Ragon.Server/Sources/IRagonServer.cs | 5 +-- Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs | 36 +++++++++---------- .../Sources/Plugin/BaseServerPlugin.cs | 30 +++------------- Ragon.Server/Sources/Plugin/IRoomPlugin.cs | 6 ++-- Ragon.Server/Sources/Plugin/IServerPlugin.cs | 2 -- Ragon.Server/Sources/RagonServer.cs | 26 +++++++++++--- Ragon.Server/Sources/Room/IRagonRoom.cs | 3 ++ Ragon.Server/Sources/Room/RagonRoom.cs | 27 +++++++++++--- 21 files changed, 155 insertions(+), 97 deletions(-) rename Ragon.Client/Sources/Handler/{EntityDestroyHandler.cs => EntityRemoveHandler.cs} (90%) create mode 100644 Ragon.Server/Sources/Entity/IRagonEntity.cs create mode 100644 Ragon.Server/Sources/Entity/IRagonEntityState.cs diff --git a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs index 867fc29..9a4a2d8 100644 --- a/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs +++ b/Ragon.Client/Sources/Handler/AuthorizeSuccessHandler.cs @@ -32,7 +32,8 @@ internal class AuthorizeSuccessHandler: Handler { var playerId = buffer.ReadString(); var playerName = buffer.ReadString(); + var playerPayload = buffer.ReadString(); - _listenerList.OnAuthorizationSuccess(playerId, playerName); + _listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/EntityDestroyHandler.cs b/Ragon.Client/Sources/Handler/EntityRemoveHandler.cs similarity index 90% rename from Ragon.Client/Sources/Handler/EntityDestroyHandler.cs rename to Ragon.Client/Sources/Handler/EntityRemoveHandler.cs index 2d8639c..25abafe 100644 --- a/Ragon.Client/Sources/Handler/EntityDestroyHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityRemoveHandler.cs @@ -19,11 +19,11 @@ using Ragon.Protocol; namespace Ragon.Client; -internal class EntityDestroyHandler: Handler +internal class EntityRemoveHandler: Handler { private readonly RagonEntityCache _entityCache; - public EntityDestroyHandler(RagonEntityCache entityCache) + public EntityRemoveHandler(RagonEntityCache entityCache) { _entityCache = entityCache; } diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index a059782..6c71258 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -95,7 +95,7 @@ namespace Ragon.Client _handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listenerList); _handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listenerList); _handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache); - _handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityDestroyHandler(_entityCache); + _handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache); _handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache); _handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(this, _playerCache, _entityCache); _handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, _listenerList, _entityCache, _playerCache); diff --git a/Ragon.Client/Sources/RagonListenerList.cs b/Ragon.Client/Sources/RagonListenerList.cs index 494d746..134bd62 100644 --- a/Ragon.Client/Sources/RagonListenerList.cs +++ b/Ragon.Client/Sources/RagonListenerList.cs @@ -150,7 +150,7 @@ namespace Ragon.Client _playerLeftListeners.Remove(listener); } - public void OnAuthorizationSuccess(string playerId, string playerName) + public void OnAuthorizationSuccess(string playerId, string playerName, string payload) { foreach (var listener in _authorizationListeners) listener.OnAuthorizationSuccess(_client, playerId, playerName); diff --git a/Ragon.Server/Sources/Entity/IRagonEntity.cs b/Ragon.Server/Sources/Entity/IRagonEntity.cs new file mode 100644 index 0000000..bd61eba --- /dev/null +++ b/Ragon.Server/Sources/Entity/IRagonEntity.cs @@ -0,0 +1,16 @@ +using Ragon.Protocol; +using Ragon.Server.Room; + +namespace Ragon.Server.Entity; + +public interface IRagonEntity +{ + public ushort Id { get; } + public ushort Type { get; } + public ushort StaticId { get; } + public ushort AttachId { get; } + public RagonRoomPlayer Owner { get; } + public RagonAuthority Authority { get; } + public RagonPayload Payload { get; } + public IRagonEntityState State { get; } +} \ No newline at end of file diff --git a/Ragon.Server/Sources/Entity/IRagonEntityState.cs b/Ragon.Server/Sources/Entity/IRagonEntityState.cs new file mode 100644 index 0000000..95d303c --- /dev/null +++ b/Ragon.Server/Sources/Entity/IRagonEntityState.cs @@ -0,0 +1,8 @@ +using Ragon.Protocol; + +namespace Ragon.Server.Entity; + +public interface IRagonEntityState +{ + +} \ No newline at end of file diff --git a/Ragon.Server/Sources/Entity/RagonEntity.cs b/Ragon.Server/Sources/Entity/RagonEntity.cs index da75e00..ad4e6e4 100644 --- a/Ragon.Server/Sources/Entity/RagonEntity.cs +++ b/Ragon.Server/Sources/Entity/RagonEntity.cs @@ -20,7 +20,7 @@ using Ragon.Server.Room; namespace Ragon.Server.Entity; -public class RagonEntity +public class RagonEntity: IRagonEntity { private static ushort _idGenerator = 100; public ushort Id { get; private set; } @@ -30,8 +30,10 @@ public class RagonEntity public RagonRoomPlayer Owner { get; private set; } public RagonAuthority Authority { get; private set; } public RagonPayload Payload { get; private set; } - public RagonEntityState State { get; private set; } + public IRagonEntityState State => _state; + private readonly List _bufferedEvents; + private readonly RagonEntityState _state; public RagonEntity(RagonEntityParameters parameters) { @@ -41,10 +43,9 @@ public class RagonEntity Type = parameters.Type; AttachId = parameters.AttachId; Authority = parameters.Authority; - - State = new RagonEntityState(this); Payload = new RagonPayload(); + _state = new RagonEntityState(this); _bufferedEvents = new List(); } @@ -121,7 +122,8 @@ public class RagonEntity buffer.WriteUShort(Payload.Size); Payload.Write(buffer); - State.Snapshot(buffer); + + _state.Snapshot(buffer); } public void ReplicateEvent( @@ -215,16 +217,23 @@ public class RagonEntity } } - public void Write(RagonBuffer writer) + public void AddProperty(RagonProperty property) { - State.Write(writer); + _state.AddProperty(property); + } + + public void WriteState(RagonBuffer writer) + { + _state.Write(writer); } - public void Read(RagonRoomPlayer player, RagonBuffer reader) + public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader) { if (Owner.Connection.Id != player.Connection.Id) - return; + return false; - State.Read(reader); + _state.Read(reader); + + return true; } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Entity/RagonEntityState.cs b/Ragon.Server/Sources/Entity/RagonEntityState.cs index 140d7af..8df9dab 100644 --- a/Ragon.Server/Sources/Entity/RagonEntityState.cs +++ b/Ragon.Server/Sources/Entity/RagonEntityState.cs @@ -19,14 +19,16 @@ using Ragon.Protocol; namespace Ragon.Server.Entity; -public class RagonEntityState +public class RagonEntityState: IRagonEntityState { private List _properties; private RagonEntity _entity; - + private RagonBuffer _buffer; + public RagonEntityState(RagonEntity entity, int capacity = 10) { _entity = entity; + _buffer = new RagonBuffer(8); _properties = new List(capacity); } @@ -65,8 +67,8 @@ public class RagonEntityState { foreach (var property in _properties) { - var hasPayload = property.IsFixed || !property.IsFixed && property.Size > 0; - if (hasPayload) + var hasPayloadOrFixed = property.IsFixed || property is { IsFixed: false, Size: > 0 }; + if (hasPayloadOrFixed) { buffer.WriteBool(true); property.Write(buffer); diff --git a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs b/Ragon.Server/Sources/Handler/EntityCreateOperation.cs index 8fb83a6..eaf9c88 100644 --- a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs +++ b/Ragon.Server/Sources/Handler/EntityCreateOperation.cs @@ -47,7 +47,7 @@ public sealed class EntityCreateOperation : IRagonOperation var propertyType = reader.ReadBool(); var propertySize = reader.ReadUShort(); - entity.State.AddProperty(new RagonProperty(propertySize, propertyType)); + entity.AddProperty(new RagonProperty(propertySize, propertyType)); } if (reader.Capacity > 0) diff --git a/Ragon.Server/Sources/Handler/EntityStateOperation.cs b/Ragon.Server/Sources/Handler/EntityStateOperation.cs index c9ec700..639ebde 100644 --- a/Ragon.Server/Sources/Handler/EntityStateOperation.cs +++ b/Ragon.Server/Sources/Handler/EntityStateOperation.cs @@ -32,10 +32,14 @@ public sealed class EntityStateOperation: IRagonOperation for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++) { var entityId = reader.ReadUShort(); - if (room.Entities.TryGetValue(entityId, out var entity)) - entity.Read(player, reader); + if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, reader)) + { + room.Track(entity); + } else + { _logger.Error($"Entity with Id {entityId} not found, replication interrupted"); + } } } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/RoomJoinOperation.cs b/Ragon.Server/Sources/Handler/RoomJoinOperation.cs index c9f2cb6..9359b81 100644 --- a/Ragon.Server/Sources/Handler/RoomJoinOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomJoinOperation.cs @@ -25,13 +25,12 @@ namespace Ragon.Server.Handler; public sealed class RoomJoinOperation : IRagonOperation { private readonly Logger _logger = LogManager.GetCurrentClassLogger(); - private readonly IServerPlugin _serverPlugin; - private readonly RagonWebHookPlugin _ragonWebHookPlugin; + + private readonly RagonWebHookPlugin _webHook; - public RoomJoinOperation(IServerPlugin serverPlugin, RagonWebHookPlugin plugin) + public RoomJoinOperation(RagonWebHookPlugin plugin) { - _serverPlugin = serverPlugin; - _ragonWebHookPlugin = plugin; + _webHook = plugin; } public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) @@ -50,10 +49,10 @@ public sealed class RoomJoinOperation : IRagonOperation var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name); context.SetRoom(existsRoom, player); - if (!_serverPlugin.OnRoomJoin(player, existsRoom)) + if (!existsRoom.Plugin.OnPlayerJoined(player)) return; - _ragonWebHookPlugin.RoomJoined(context, existsRoom, player); + _webHook.RoomJoined(context, existsRoom, player); JoinSuccess(context, existsRoom, writer); diff --git a/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs b/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs index 4de5f6c..0acb494 100644 --- a/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs +++ b/Ragon.Server/Sources/Handler/RoomLeaveOperation.cs @@ -24,12 +24,10 @@ namespace Ragon.Server.Handler; public sealed class RoomLeaveOperation: IRagonOperation { private readonly Logger _logger = LogManager.GetCurrentClassLogger(); - private readonly IServerPlugin _serverPlugin; - private readonly RagonWebHookPlugin _ragonWebHookPlugin; - public RoomLeaveOperation(IServerPlugin serverPlugin, RagonWebHookPlugin plugin) + private readonly RagonWebHookPlugin _webHook; + public RoomLeaveOperation(RagonWebHookPlugin plugin) { - _serverPlugin = serverPlugin; - _ragonWebHookPlugin = plugin; + _webHook = plugin; } public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer) @@ -39,9 +37,13 @@ public sealed class RoomLeaveOperation: IRagonOperation if (room != null) { - _serverPlugin.OnRoomLeave(roomPlayer, room); - _ragonWebHookPlugin.RoomLeaved(context, room, roomPlayer); - context.Room?.DetachPlayer(roomPlayer); + var plugin = room.Plugin; + + plugin.OnPlayerLeaved(roomPlayer); + room.DetachPlayer(roomPlayer); + + _webHook.RoomLeaved(context, room, roomPlayer); + _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}"); } } diff --git a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs b/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs index f489887..d16ee0c 100644 --- a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs +++ b/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs @@ -64,7 +64,7 @@ public sealed class SceneLoadedOperation : IRagonOperation { var propertyType = reader.ReadBool(); var propertySize = reader.ReadUShort(); - entity.State.AddProperty(new RagonProperty(propertySize, propertyType)); + entity.AddProperty(new RagonProperty(propertySize, propertyType)); } var roomPlugin = room.Plugin; diff --git a/Ragon.Server/Sources/IRagonServer.cs b/Ragon.Server/Sources/IRagonServer.cs index 0dcba94..cffc960 100644 --- a/Ragon.Server/Sources/IRagonServer.cs +++ b/Ragon.Server/Sources/IRagonServer.cs @@ -15,11 +15,12 @@ */ using Ragon.Server.IO; +using Ragon.Server.Lobby; namespace Ragon.Server; public interface IRagonServer { - RagonContext? ResolveContext(INetworkConnection connection); - RagonContext? ResolveContext(string id); + RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection); + RagonLobbyPlayer? GetPlayerById(string id); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs index 62626d0..f903309 100644 --- a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs @@ -22,44 +22,44 @@ namespace Ragon.Server.Plugin; public class BaseRoomPlugin: IRoomPlugin { - private IRagonRoom _ragonRoom; - - public RagonRoomPlayer GetPlayerById(string id) - { - var player = _ragonRoom.GetPlayerById(id); - return player; - } - - public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection) - { - var player = _ragonRoom.GetPlayerByConnection(connection); - return player; - } + public IRagonRoom Room { get; private set; } public virtual void OnAttached(IRagonRoom room) { - _ragonRoom = room; + Room = room; } public virtual void OnDetached() { } - - #region VIRTUAL + #region VIRTUAL + + public virtual bool OnPlayerJoined(RagonRoomPlayer player) + { + return true; + } + + public virtual bool OnPlayerLeaved(RagonRoomPlayer player) + { + return true; + } + public virtual void Tick(float dt) { } - public virtual bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity) + public virtual bool OnEntityCreate(RagonRoomPlayer creator, IRagonEntity entity) { + return true; } - public virtual bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity) + public virtual bool OnEntityRemove(RagonRoomPlayer remover, IRagonEntity entity) { + return true; } diff --git a/Ragon.Server/Sources/Plugin/BaseServerPlugin.cs b/Ragon.Server/Sources/Plugin/BaseServerPlugin.cs index fa69c1c..88b8183 100644 --- a/Ragon.Server/Sources/Plugin/BaseServerPlugin.cs +++ b/Ragon.Server/Sources/Plugin/BaseServerPlugin.cs @@ -22,26 +22,14 @@ namespace Ragon.Server.Plugin; public class BaseServerPlugin: IServerPlugin { - private IRagonServer _ragonServer; + public IRagonServer Server { get; protected set; } - public RagonLobbyPlayer? GetPlayerById(string id) + public virtual void OnAttached(IRagonServer server) { - var context = _ragonServer.ResolveContext(id); - return context?.LobbyPlayer; + Server = server; } - public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection) - { - var context = _ragonServer.ResolveContext(connection); - return context?.LobbyPlayer; - } - - public void OnAttached(IRagonServer server) - { - _ragonServer = server; - } - - public void OnDetached() + public virtual void OnDetached() { } @@ -56,16 +44,6 @@ public class BaseServerPlugin: IServerPlugin return true; } - public virtual bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room) - { - return true; - } - - public virtual bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room) - { - return true; - } - public virtual bool OnCommand(string command, string payload) { return true; diff --git a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs index 679707e..1634979 100644 --- a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs @@ -24,6 +24,8 @@ public interface IRoomPlugin void Tick(float dt); void OnAttached(IRagonRoom room); void OnDetached(); - bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity); - bool OnEntityRemove(RagonRoomPlayer remover, RagonEntity entity); + bool OnPlayerJoined(RagonRoomPlayer player); + bool OnPlayerLeaved(RagonRoomPlayer player); + bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity); + bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Plugin/IServerPlugin.cs b/Ragon.Server/Sources/Plugin/IServerPlugin.cs index cad278e..eaf5fce 100644 --- a/Ragon.Server/Sources/Plugin/IServerPlugin.cs +++ b/Ragon.Server/Sources/Plugin/IServerPlugin.cs @@ -26,8 +26,6 @@ public interface IServerPlugin void OnDetached(); bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room); bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room); - bool OnRoomLeave(RagonRoomPlayer player, RagonRoom room); - bool OnRoomJoin(RagonRoomPlayer player, RagonRoom room); bool OnCommand(string command, string payload); IRoomPlugin CreateRoomPlugin(RoomInformation information); } \ No newline at end of file diff --git a/Ragon.Server/Sources/RagonServer.cs b/Ragon.Server/Sources/RagonServer.cs index d49c336..8dba18f 100644 --- a/Ragon.Server/Sources/RagonServer.cs +++ b/Ragon.Server/Sources/RagonServer.cs @@ -77,8 +77,8 @@ public class RagonServer : IRagonServer, INetworkListener _handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer, configuration); _handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(plugin, _webhooks); _handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation(plugin, _webhooks); - _handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(plugin, _webhooks); - _handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(plugin, _webhooks); + _handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_webhooks); + _handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_webhooks); _handlers[(byte) RagonOperation.LOAD_SCENE] = new SceneLoadOperation(); _handlers[(byte) RagonOperation.SCENE_LOADED] = new SceneLoadedOperation(); _handlers[(byte) RagonOperation.CREATE_ENTITY] = new EntityCreateOperation(); @@ -198,7 +198,23 @@ public class RagonServer : IRagonServer, INetworkListener _logger.Error(ex); } } - public IRagonOperation ResolveOperation(RagonOperation operation) => _handlers[(byte)operation]; - public RagonContext? ResolveContext(INetworkConnection connection) => _contextsByConnection.TryGetValue(connection.Id, out var context) ? context : null; - public RagonContext? ResolveContext(string playerId) => _contextsByPlayerId.TryGetValue(playerId, out var context) ? context : null; + + public IRagonOperation ResolveOperation(RagonOperation operation) + { + return _handlers[(byte)operation]; + } + + public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection) + { + return _contextsByConnection.TryGetValue(connection.Id, out var context) ? + context.LobbyPlayer : + null; + } + + public RagonLobbyPlayer? GetPlayerById(string playerId) + { + return _contextsByPlayerId.TryGetValue(playerId, out var context) ? + context.LobbyPlayer : + null; + } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Room/IRagonRoom.cs b/Ragon.Server/Sources/Room/IRagonRoom.cs index 53c4a5d..54c4134 100644 --- a/Ragon.Server/Sources/Room/IRagonRoom.cs +++ b/Ragon.Server/Sources/Room/IRagonRoom.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using Ragon.Server.Entity; using Ragon.Server.IO; namespace Ragon.Server.Room; @@ -22,4 +23,6 @@ public interface IRagonRoom { RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection); RagonRoomPlayer GetPlayerById(string id); + IRagonEntity GetEntityById(ushort id); + IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer id); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Room/RagonRoom.cs b/Ragon.Server/Sources/Room/RagonRoom.cs index e5018dd..07a7755 100644 --- a/Ragon.Server/Sources/Room/RagonRoom.cs +++ b/Ragon.Server/Sources/Room/RagonRoom.cs @@ -100,7 +100,7 @@ public class RagonRoom : IRagonRoom, IRagonAction Writer.WriteUShort(entities); foreach (var entity in _entitiesDirtySet) - entity.Write(Writer); + entity.WriteState(Writer); _entitiesDirtySet.Clear(); @@ -205,7 +205,26 @@ public class RagonRoom : IRagonRoom, IRagonAction foreach (var readyPlayer in ReadyPlayersList) readyPlayer.Connection.Reliable.Send(data); } - - public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection) => Players[connection.Id]; - public RagonRoomPlayer GetPlayerById(string id) => PlayerList.First(p => p.Id == id); + + public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection) + { + return Players[connection.Id]; + } + + public RagonRoomPlayer? GetPlayerById(string id) + { + return PlayerList.FirstOrDefault(p => p.Id == id); + } + + public IRagonEntity? GetEntityById(ushort id) + { + return Entities.TryGetValue(id, out var entity) ? + entity : + null; + } + + public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player) + { + return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray(); + } } \ No newline at end of file