From fa6ace4dc80a7fe43a89f1d808cc631d3713d23f Mon Sep 17 00:00:00 2001 From: Edmand46 Date: Fri, 16 Dec 2022 23:36:51 +0400 Subject: [PATCH] wip --- .../IScheduleTask.cs => Action/IAction.cs} | 2 +- Ragon.Core/Action/Loop.cs | 27 +++++++++++++++++++ Ragon.Core/Application.cs | 25 +++++++++-------- Ragon.Core/Game/Entity.cs | 2 +- Ragon.Core/Game/Room.cs | 10 +++---- Ragon.Core/HandlerRegistry.cs | 2 +- ...AuthHandler.cs => AuthorizationHandler.cs} | 2 +- Ragon.Core/Handlers/RoomCreateHandler.cs | 4 ++- .../Handlers/RoomJoinOrCreateHandler.cs | 4 ++- Ragon.Core/PlayerContext.cs | 2 +- Ragon.Core/Time/Scheduler.cs | 27 ------------------- Ragon.Relay/Program.cs | 7 ----- 12 files changed, 54 insertions(+), 60 deletions(-) rename Ragon.Core/{Time/IScheduleTask.cs => Action/IAction.cs} (63%) create mode 100644 Ragon.Core/Action/Loop.cs rename Ragon.Core/Handlers/{AuthHandler.cs => AuthorizationHandler.cs} (95%) delete mode 100644 Ragon.Core/Time/Scheduler.cs diff --git a/Ragon.Core/Time/IScheduleTask.cs b/Ragon.Core/Action/IAction.cs similarity index 63% rename from Ragon.Core/Time/IScheduleTask.cs rename to Ragon.Core/Action/IAction.cs index 94e7e92..a8b0017 100644 --- a/Ragon.Core/Time/IScheduleTask.cs +++ b/Ragon.Core/Action/IAction.cs @@ -1,6 +1,6 @@ namespace Ragon.Core.Time; -public interface IScheduleTask +public interface IAction { public void Tick(); } \ No newline at end of file diff --git a/Ragon.Core/Action/Loop.cs b/Ragon.Core/Action/Loop.cs new file mode 100644 index 0000000..b310cdc --- /dev/null +++ b/Ragon.Core/Action/Loop.cs @@ -0,0 +1,27 @@ +namespace Ragon.Core.Time; + +public class Loop +{ + private List _tasks; + + public Loop() + { + _tasks = new List(35); + } + + public void Run(IAction task) + { + _tasks.Add(task); + } + + public void Stop(IAction task) + { + _tasks.Remove(task); + } + + public void Tick() + { + foreach (var task in _tasks) + task.Tick(); + } +} \ No newline at end of file diff --git a/Ragon.Core/Application.cs b/Ragon.Core/Application.cs index 58405d7..abc0c9e 100644 --- a/Ragon.Core/Application.cs +++ b/Ragon.Core/Application.cs @@ -10,25 +10,24 @@ namespace Ragon.Core; public class Application : INetworkListener { - private Logger _logger = LogManager.GetCurrentClassLogger(); - - private INetworkServer _server; - private Thread _dedicatedThread; - private Configuration _configuration; - private HandlerRegistry _handlerRegistry; - private ILobby _lobby; - private Scheduler _scheduler; - private Dictionary _contexts = new(); + private readonly Logger _logger = LogManager.GetCurrentClassLogger(); + private readonly INetworkServer _server; + private readonly Thread _dedicatedThread; + private readonly Configuration _configuration; + private readonly HandlerRegistry _handlerRegistry; + private readonly ILobby _lobby; + private readonly Loop _loop; + private readonly Dictionary _contexts; public Application(Configuration configuration) { _configuration = configuration; _dedicatedThread = new Thread(Execute); _dedicatedThread.IsBackground = true; - + _contexts = new Dictionary(); _handlerRegistry = new HandlerRegistry(); _lobby = new LobbyInMemory(); - _scheduler = new Scheduler(); + _loop = new Loop(); if (configuration.Socket == "enet") _server = new ENetServer(); @@ -40,7 +39,7 @@ public class Application : INetworkListener { while (true) { - _scheduler.Tick(); + _loop.Tick(); _server.Poll(); Thread.Sleep((int) 1000.0f / _configuration.SendRate); @@ -71,7 +70,7 @@ public class Application : INetworkListener { var context = new PlayerContext(connection, new LobbyPlayer(connection)); context.Lobby = _lobby; - context.Scheduler = _scheduler; + context.Loop = _loop; _logger.Trace($"Connected {connection.Id}"); _contexts.Add(connection.Id, context); diff --git a/Ragon.Core/Game/Entity.cs b/Ragon.Core/Game/Entity.cs index 9252a47..465c841 100644 --- a/Ragon.Core/Game/Entity.cs +++ b/Ragon.Core/Game/Entity.cs @@ -78,7 +78,7 @@ public class Entity player.Connection.ReliableChannel.Send(sendData); } - public void Destroy() + public void Destroy(byte[] payload) { var room = Owner.Room; var serializer = room.Writer; diff --git a/Ragon.Core/Game/Room.cs b/Ragon.Core/Game/Room.cs index b3270a3..6018a81 100644 --- a/Ragon.Core/Game/Room.cs +++ b/Ragon.Core/Game/Room.cs @@ -1,8 +1,9 @@ using Ragon.Common; +using Ragon.Core.Time; namespace Ragon.Core.Game; -public class Room +public class Room: IAction { public string Id { get; } public RoomInformation Info { get; } @@ -22,15 +23,12 @@ public class Room private RagonSerializer _writer; public RagonSerializer Writer => _writer; - public PluginBase Plugin { get; set; } - public Room(string roomId, RoomInformation info, PluginBase plugin) + public Room(string roomId, RoomInformation info) { Id = roomId; Info = info; - Plugin = plugin; - Players = new Dictionary(info.Max); WaitPlayersList = new List(info.Max); ReadyPlayersList = new List(info.Max); @@ -68,7 +66,7 @@ public class Room DynamicEntitiesList.Remove(entity); _entitiesDirtySet.Remove(entity); - entity.Destroy(); + entity.Destroy(payload); currentOwner.Entities.Remove(entity); } diff --git a/Ragon.Core/HandlerRegistry.cs b/Ragon.Core/HandlerRegistry.cs index 3d37f36..b6acf25 100644 --- a/Ragon.Core/HandlerRegistry.cs +++ b/Ragon.Core/HandlerRegistry.cs @@ -27,7 +27,7 @@ public sealed class HandlerRegistry _reader = new RagonSerializer(2048); _writer = new RagonSerializer(2048); - _authorizationHandler = new AuthHandler(); + _authorizationHandler = new AuthorizationHandler(); _joinOrCreateHandler = new JoinOrCreateHandler(); _sceneLoadedHandler = new SceneLoadedHandler(); _createHandler = new CreateHandler(); diff --git a/Ragon.Core/Handlers/AuthHandler.cs b/Ragon.Core/Handlers/AuthorizationHandler.cs similarity index 95% rename from Ragon.Core/Handlers/AuthHandler.cs rename to Ragon.Core/Handlers/AuthorizationHandler.cs index d11e520..6ab9bc2 100644 --- a/Ragon.Core/Handlers/AuthHandler.cs +++ b/Ragon.Core/Handlers/AuthorizationHandler.cs @@ -4,7 +4,7 @@ using Ragon.Core.Lobby; namespace Ragon.Core.Handlers; -public sealed class AuthHandler: IHandler +public sealed class AuthorizationHandler: IHandler { private Logger _logger = LogManager.GetCurrentClassLogger(); diff --git a/Ragon.Core/Handlers/RoomCreateHandler.cs b/Ragon.Core/Handlers/RoomCreateHandler.cs index 54459cc..950508d 100644 --- a/Ragon.Core/Handlers/RoomCreateHandler.cs +++ b/Ragon.Core/Handlers/RoomCreateHandler.cs @@ -50,7 +50,7 @@ public sealed class CreateHandler: IHandler var lobbyPlayer = context.LobbyPlayer; var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); - var room = new Room(roomId, information, new PluginBase()); + var room = new Room(roomId, information); room.AddPlayer(roomPlayer); context.Room?.RemovePlayer(context.RoomPlayer); @@ -62,6 +62,8 @@ public sealed class CreateHandler: IHandler JoinSuccess(roomPlayer, room, writer); + context.Loop.Run(room); + _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}"); } diff --git a/Ragon.Core/Handlers/RoomJoinOrCreateHandler.cs b/Ragon.Core/Handlers/RoomJoinOrCreateHandler.cs index 1661777..e154c2b 100644 --- a/Ragon.Core/Handlers/RoomJoinOrCreateHandler.cs +++ b/Ragon.Core/Handlers/RoomJoinOrCreateHandler.cs @@ -44,7 +44,7 @@ public sealed class JoinOrCreateHandler : IHandler Min = _roomParameters.Min, }; - var room = new Room(roomId, information, new PluginBase()); + var room = new Room(roomId, information); context.Lobby.Persist(room); var roomPlayer = new RoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name); @@ -57,6 +57,8 @@ public sealed class JoinOrCreateHandler : IHandler _logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}"); JoinSuccess(roomPlayer, room, writer); + + context.Loop.Run(room); } } diff --git a/Ragon.Core/PlayerContext.cs b/Ragon.Core/PlayerContext.cs index 12393e4..bf2e740 100644 --- a/Ragon.Core/PlayerContext.cs +++ b/Ragon.Core/PlayerContext.cs @@ -9,7 +9,7 @@ namespace Ragon.Core; public class PlayerContext: IDisposable { public INetworkConnection Connection { get; } - public Scheduler Scheduler; + public Loop Loop; public ILobby Lobby { get; set; } public LobbyPlayer LobbyPlayer { private set; get; } public Room? Room { get; set; } diff --git a/Ragon.Core/Time/Scheduler.cs b/Ragon.Core/Time/Scheduler.cs deleted file mode 100644 index b787ea9..0000000 --- a/Ragon.Core/Time/Scheduler.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace Ragon.Core.Time; - -public class Scheduler -{ - private List _tasks; - - public Scheduler() - { - _tasks = new List(35); - } - - public void Add(IScheduleTask task) - { - _tasks.Add(task); - } - - public void Remove(IScheduleTask task) - { - _tasks.Remove(task); - } - - public void Tick() - { - foreach (var task in _tasks) - task.Tick(); - } -} \ No newline at end of file diff --git a/Ragon.Relay/Program.cs b/Ragon.Relay/Program.cs index b8cde5c..2c9e8fd 100755 --- a/Ragon.Relay/Program.cs +++ b/Ragon.Relay/Program.cs @@ -5,13 +5,6 @@ using Ragon.Core; namespace Ragon.Relay { - [StructLayout(LayoutKind.Sequential)] - struct Serializer - { - [FieldOffset(0)] public Guid Uuid; - [FieldOffset(0)] public long Long0; - [FieldOffset(0)] public long Long1; - } class Program { static void Main(string[] args)