From b84538b238c11ca5759db2ae64417b1fe7c82cdc Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sun, 19 May 2024 08:00:56 +0300 Subject: [PATCH] fix: stop tick room on delete --- .../Sources/Handler/AuthorizationOperation.cs | 4 +-- .../Sources/Lobby/RagonLobbyInMemory.cs | 6 +++- Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs | 8 ++--- Ragon.Server/Sources/Plugin/IRoomPlugin.cs | 4 +-- .../Sources/Plugin/Web/RagonWebHookPlugin.cs | 6 ++-- Ragon.Server/Sources/Room/RagonRoom.cs | 29 ++++++++++++++++++- Ragon.Server/Sources/Time/IRagonAction.cs | 5 ++-- .../Sources/Time/RagonActionExecutor.cs | 15 ++++++++-- Ragon.Server/Sources/Time/RagonActionTimer.cs | 27 +++++++++++++++-- 9 files changed, 84 insertions(+), 20 deletions(-) diff --git a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs index 4bd37f3..e463725 100644 --- a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs +++ b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,7 +65,7 @@ public sealed class AuthorizationOperation: BaseOperation if (key == configuration.ServerKey) { - if (_webhook.RequestAuthorization(context, name, payload)) + if (_webhook.RequestAuthorization(context, payload)) return; var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload); diff --git a/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs b/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs index 42073b6..a2e2ea6 100644 --- a/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs +++ b/Ragon.Server/Sources/Lobby/RagonLobbyInMemory.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,6 +75,8 @@ public class LobbyInMemory : IRagonLobby public void Persist(RagonRoom room) { + room.Attach(); + _rooms.Add(room); _logger.Trace($"New room: {room.Id}"); @@ -87,6 +89,8 @@ public class LobbyInMemory : IRagonLobby var result = false; if (room.Players.Count == 0) { + room.Detach(); + _rooms.Remove(room); _logger.Trace($"Room {room.Id} removed"); diff --git a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs index 32ed035..6de1100 100644 --- a/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/BaseRoomPlugin.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,12 +28,12 @@ public class BaseRoomPlugin: IRoomPlugin { Room = room; } - - public virtual void OnDetached() + + public virtual void OnDetached(IRagonRoom room) { } - + #region VIRTUAL public virtual bool OnPlayerJoined(RagonRoomPlayer player) diff --git a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs index 84f7ffa..bbb0469 100644 --- a/Ragon.Server/Sources/Plugin/IRoomPlugin.cs +++ b/Ragon.Server/Sources/Plugin/IRoomPlugin.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ public interface IRoomPlugin { void Tick(float dt); void OnAttached(IRagonRoom room); - void OnDetached(); + void OnDetached(IRagonRoom room); bool OnPlayerJoined(RagonRoomPlayer player); bool OnPlayerLeaved(RagonRoomPlayer player); bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity); diff --git a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs index d6c88fc..c1d6f4f 100644 --- a/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs +++ b/Ragon.Server/Sources/Plugin/Web/RagonWebHookPlugin.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,11 +39,11 @@ public class RagonWebHookPlugin _server = server; } - public bool RequestAuthorization(RagonContext context, string name, string password) + public bool RequestAuthorization(RagonContext context, string payload) { if (_webHooks.TryGetValue("authorization-request", out var value)) { - var httpContent = new StringContent(""); + var httpContent = new StringContent(payload, Encoding.UTF8, "application/json"); var executor = context.Executor; executor.Run(async () => { diff --git a/Ragon.Server/Sources/Room/RagonRoom.cs b/Ragon.Server/Sources/Room/RagonRoom.cs index dfeb668..7c40f3c 100644 --- a/Ragon.Server/Sources/Room/RagonRoom.cs +++ b/Ragon.Server/Sources/Room/RagonRoom.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,8 @@ public class RagonRoom : IRagonRoom, IRagonAction public int PlayerMax { get; private set; } public int PlayerMin { get; private set; } public int PlayerCount => WaitPlayersList.Count; + + public bool IsDone { get; private set; } public RagonData UserData { get; set; } public RagonRoomPlayer Owner { get; private set; } @@ -393,4 +395,29 @@ public class RagonRoom : IRagonRoom, IRagonAction { return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray(); } + + public void Attach() + { + Plugin.OnAttached(this); + } + + public void Detach() + { + Plugin.OnDetached(this); + + Players.Clear(); + WaitPlayersList.Clear(); + ReadyPlayersList.Clear(); + PlayerList.Clear(); + + Entities.Clear(); + DynamicEntitiesList.Clear(); + StaticEntitiesList.Clear(); + EntityList.Clear(); + + _entitiesDirtySet.Clear(); + _bufferedEvents.Clear(); + + IsDone = true; + } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Time/IRagonAction.cs b/Ragon.Server/Sources/Time/IRagonAction.cs index 36ec070..4c5d14d 100644 --- a/Ragon.Server/Sources/Time/IRagonAction.cs +++ b/Ragon.Server/Sources/Time/IRagonAction.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,5 +18,6 @@ namespace Ragon.Server.Time; public interface IRagonAction { - public void Tick(float dt); + public bool IsDone { get; } + public void Tick(float dt); } \ No newline at end of file diff --git a/Ragon.Server/Sources/Time/RagonActionExecutor.cs b/Ragon.Server/Sources/Time/RagonActionExecutor.cs index 538b709..76e2d2d 100644 --- a/Ragon.Server/Sources/Time/RagonActionExecutor.cs +++ b/Ragon.Server/Sources/Time/RagonActionExecutor.cs @@ -1,5 +1,5 @@ /* - * Copyright 2023 Eduard Kargin + * Copyright 2023-2024 Eduard Kargin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,12 @@ namespace Ragon.Server.Time; public class RagonScheduler { private List _tasks; - + private List _toDelete; + public RagonScheduler() { _tasks = new List(35); + _toDelete = new List(35); } public void Run(IRagonAction task) @@ -38,6 +40,15 @@ public class RagonScheduler public void Update(float dt) { foreach (var task in _tasks) + { task.Tick(dt); + if (task.IsDone) + _toDelete.Add(task); + } + + foreach (var action in _toDelete) + _tasks.Remove(action); + + _toDelete.Clear(); } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Time/RagonActionTimer.cs b/Ragon.Server/Sources/Time/RagonActionTimer.cs index d8af36b..9981691 100644 --- a/Ragon.Server/Sources/Time/RagonActionTimer.cs +++ b/Ragon.Server/Sources/Time/RagonActionTimer.cs @@ -1,15 +1,36 @@ +/* + * Copyright 2023-2024 Eduard Kargin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + using Ragon.Server.Time; -public class RagonActionTimer: IRagonAction +public class RagonActionTimer : IRagonAction { + public bool IsDone => _repeatCount == 0; + private Action _callback; private float _timer; private float _time; - - public RagonActionTimer(Action callback, float timeInSeconds) + private float _repeatCount; + + public RagonActionTimer(Action callback, float timeInSeconds, int repeat = 1) { _callback = callback; _time = timeInSeconds * 1000; + _repeatCount = repeat; } public void Tick(float dt)