From e4f664b5575293f71e551aa719a5fb9edf0bca46 Mon Sep 17 00:00:00 2001 From: Edmand46 Date: Sun, 28 Aug 2022 19:31:07 +0400 Subject: [PATCH] refactor: renamed plugin api --- .../Source/Plugins/EmptyPlugin.cs | 19 +++++++--- Ragon/Sources/Game/GameRoom.cs | 5 ++- Ragon/Sources/Plugin/PluginBase.cs | 36 +++++++++---------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Ragon.SimpleServer/Source/Plugins/EmptyPlugin.cs b/Ragon.SimpleServer/Source/Plugins/EmptyPlugin.cs index 6f70b81..8c69455 100755 --- a/Ragon.SimpleServer/Source/Plugins/EmptyPlugin.cs +++ b/Ragon.SimpleServer/Source/Plugins/EmptyPlugin.cs @@ -1,4 +1,5 @@ -using Ragon.Core; +using NLog.Fluent; +using Ragon.Core; namespace Game.Source { @@ -16,13 +17,23 @@ namespace Game.Source } public override void OnPlayerJoined(Player player) - { - // _logger.Info($"Player({player.PlayerName}) joined to Room({GameRoom.Id})"); + { + // Logger.Info($"Player({player.PlayerName}) joined to Room({Room.Id})"); } public override void OnPlayerLeaved(Player player) { - // _logger.Info($"Player({player.PlayerName}) left from Room({GameRoom.Id})"); + // Logger.Info($"Player({player.PlayerName}) left from Room({Room.Id})"); + } + + public override void OnEntityCreated(Player player, Entity entity) + { + // Logger.Info($"Player({player.PlayerName}) create entity {entity.EntityId}:{entity.EntityType}"); + } + + public override void OnEntityDestroyed(Player player, Entity entity) + { + // Logger.Info($"Player({player.PlayerName}) destroy entity {entity.EntityId}:{entity.EntityType}"); } } } \ No newline at end of file diff --git a/Ragon/Sources/Game/GameRoom.cs b/Ragon/Sources/Game/GameRoom.cs index c6e06b4..50247a0 100755 --- a/Ragon/Sources/Game/GameRoom.cs +++ b/Ragon/Sources/Game/GameRoom.cs @@ -212,7 +212,7 @@ namespace Ragon.Core _serializer.Clear(); _serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); _serializer.WriteUShort(evntId); - _serializer.WriteUShort((ushort) peerId); + _serializer.WriteUShort(peerId); _serializer.WriteByte(evntMode); _serializer.WriteUShort(entityId); _serializer.WriteData(ref payload); @@ -242,7 +242,6 @@ namespace Ragon.Core break; } } - break; } case RagonOperation.LOAD_SCENE: @@ -332,7 +331,7 @@ namespace Ragon.Core player.EntitiesIds.Add(entity.EntityId); var ownerId = (ushort) peerId; - + _entities.Add(entity.EntityId, entity); _entitiesAll = _entities.Values.ToArray(); diff --git a/Ragon/Sources/Plugin/PluginBase.cs b/Ragon/Sources/Plugin/PluginBase.cs index 52f92a9..8d644b8 100755 --- a/Ragon/Sources/Plugin/PluginBase.cs +++ b/Ragon/Sources/Plugin/PluginBase.cs @@ -8,21 +8,20 @@ namespace Ragon.Core public class PluginBase { private delegate void SubscribeDelegate(Player player, ref ReadOnlySpan data); - private delegate void SubscribeEntityDelegate(Player player, Entity entity, ref ReadOnlySpan data); private Dictionary _globalEvents = new(); private Dictionary> _entityEvents = new(); private readonly RagonSerializer _serializer = new(); - protected IGameRoom GameRoom { get; private set; } = null!; + protected IGameRoom Room { get; private set; } = null!; protected ILogger Logger = null!; public void Attach(GameRoom gameRoom) { Logger = LogManager.GetLogger($"Plugin<{GetType().Name}>"); - GameRoom = gameRoom; + Room = gameRoom; _globalEvents.Clear(); _entityEvents.Clear(); @@ -34,7 +33,7 @@ namespace Ragon.Core _entityEvents.Clear(); } - public void Subscribe(ushort evntCode, Action action) where T : IRagonSerializable, new() + public void OnEvent(ushort evntCode, Action action) where T : IRagonSerializable, new() { if (_globalEvents.ContainsKey(evntCode)) { @@ -58,7 +57,7 @@ namespace Ragon.Core }); } - public void Subscribe(ushort evntCode, Action action) + public void OnEvent(ushort evntCode, Action action) { if (_globalEvents.ContainsKey(evntCode)) { @@ -69,7 +68,7 @@ namespace Ragon.Core _globalEvents.Add(evntCode, (Player player, ref ReadOnlySpan raw) => { action.Invoke(player); }); } - public void Subscribe(Entity entity, ushort evntCode, Action action) where T : IRagonSerializable, new() + public void OnEvent(Entity entity, ushort evntCode, Action action) where T : IRagonSerializable, new() { if (_entityEvents.ContainsKey(entity.EntityId)) { @@ -116,7 +115,7 @@ namespace Ragon.Core } } - public void Subscribe(Entity entity, ushort evntCode, Action action) + public void OnEvent(Entity entity, ushort evntCode, Action action) { if (_entityEvents.ContainsKey(entity.EntityId)) { @@ -150,8 +149,9 @@ namespace Ragon.Core if (!_entityEvents[entityId].ContainsKey(evntCode)) return false; - var player = GameRoom.GetPlayerById(peerId); - var entity = GameRoom.GetEntityById(entityId); + var player = Room.GetPlayerById(peerId); + var entity = Room.GetEntityById(entityId); + _entityEvents[entityId][evntCode].Invoke(player, entity, ref payload); return true; @@ -161,7 +161,7 @@ namespace Ragon.Core { if (_globalEvents.ContainsKey(evntCode)) { - var player = GameRoom.GetPlayerById(peerId); + var player = Room.GetPlayerById(peerId); _globalEvents[evntCode].Invoke(player, ref payload); return true; } @@ -169,7 +169,7 @@ namespace Ragon.Core return false; } - public void SendEvent(Player player, uint eventCode, IRagonSerializable payload) + public void ReplicateEvent(Player player, uint eventCode, IRagonSerializable payload) { _serializer.Clear(); _serializer.WriteOperation(RagonOperation.REPLICATE_EVENT); @@ -177,10 +177,10 @@ namespace Ragon.Core payload.Serialize(_serializer); var sendData = _serializer.ToArray(); - GameRoom.Send(player.PeerId, sendData); + Room.Send(player.PeerId, sendData); } - public void BroadcastEvent(ushort eventCode, IRagonSerializable payload) + public void ReplicateEvent(ushort eventCode, IRagonSerializable payload) { _serializer.Clear(); _serializer.WriteOperation(RagonOperation.REPLICATE_EVENT); @@ -188,10 +188,10 @@ namespace Ragon.Core payload.Serialize(_serializer); var sendData = _serializer.ToArray(); - GameRoom.Broadcast(sendData, DeliveryType.Reliable); + Room.Broadcast(sendData, DeliveryType.Reliable); } - public void SendEntityEvent(Player player, Entity entity, IRagonSerializable payload) + public void ReplicateEntityEvent(Player player, Entity entity, IRagonSerializable payload) { _serializer.Clear(); _serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); @@ -200,10 +200,10 @@ namespace Ragon.Core payload.Serialize(_serializer); var sendData = _serializer.ToArray(); - GameRoom.Send(player.PeerId, sendData, DeliveryType.Reliable); + Room.Send(player.PeerId, sendData, DeliveryType.Reliable); } - public void BroadcastEntityEvent(Entity entity, IRagonSerializable payload) + public void ReplicateEntityEvent(Entity entity, IRagonSerializable payload) { _serializer.Clear(); _serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT); @@ -212,7 +212,7 @@ namespace Ragon.Core payload.Serialize(_serializer); var sendData = _serializer.ToArray(); - GameRoom.Broadcast(sendData); + Room.Broadcast(sendData); }