fix: stop tick room on delete

This commit is contained in:
2024-05-19 08:00:56 +03:00
parent 7a2196ff50
commit b84538b238
9 changed files with 84 additions and 20 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 (key == configuration.ServerKey)
{ {
if (_webhook.RequestAuthorization(context, name, payload)) if (_webhook.RequestAuthorization(context, payload))
return; return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload); var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) public void Persist(RagonRoom room)
{ {
room.Attach();
_rooms.Add(room); _rooms.Add(room);
_logger.Trace($"New room: {room.Id}"); _logger.Trace($"New room: {room.Id}");
@@ -87,6 +89,8 @@ public class LobbyInMemory : IRagonLobby
var result = false; var result = false;
if (room.Players.Count == 0) if (room.Players.Count == 0)
{ {
room.Detach();
_rooms.Remove(room); _rooms.Remove(room);
_logger.Trace($"Room {room.Id} removed"); _logger.Trace($"Room {room.Id} removed");
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ public class BaseRoomPlugin: IRoomPlugin
Room = room; Room = room;
} }
public virtual void OnDetached() public virtual void OnDetached(IRagonRoom room)
{ {
} }
+2 -2
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Tick(float dt);
void OnAttached(IRagonRoom room); void OnAttached(IRagonRoom room);
void OnDetached(); void OnDetached(IRagonRoom room);
bool OnPlayerJoined(RagonRoomPlayer player); bool OnPlayerJoined(RagonRoomPlayer player);
bool OnPlayerLeaved(RagonRoomPlayer player); bool OnPlayerLeaved(RagonRoomPlayer player);
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity); bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -39,11 +39,11 @@ public class RagonWebHookPlugin
_server = server; _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)) 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; var executor = context.Executor;
executor.Run(async () => executor.Run(async () =>
{ {
+28 -1
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,6 +32,8 @@ public class RagonRoom : IRagonRoom, IRagonAction
public int PlayerMin { get; private set; } public int PlayerMin { get; private set; }
public int PlayerCount => WaitPlayersList.Count; public int PlayerCount => WaitPlayersList.Count;
public bool IsDone { get; private set; }
public RagonData UserData { get; set; } public RagonData UserData { get; set; }
public RagonRoomPlayer Owner { get; private set; } public RagonRoomPlayer Owner { get; private set; }
public RagonBuffer Writer { get; } public RagonBuffer Writer { get; }
@@ -393,4 +395,29 @@ public class RagonRoom : IRagonRoom, IRagonAction
{ {
return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray(); 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;
}
} }
+3 -2
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 interface IRagonAction
{ {
public void Tick(float dt); public bool IsDone { get; }
public void Tick(float dt);
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com> * Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 public class RagonScheduler
{ {
private List<IRagonAction> _tasks; private List<IRagonAction> _tasks;
private List<IRagonAction> _toDelete;
public RagonScheduler() public RagonScheduler()
{ {
_tasks = new List<IRagonAction>(35); _tasks = new List<IRagonAction>(35);
_toDelete = new List<IRagonAction>(35);
} }
public void Run(IRagonAction task) public void Run(IRagonAction task)
@@ -38,6 +40,15 @@ public class RagonScheduler
public void Update(float dt) public void Update(float dt)
{ {
foreach (var task in _tasks) foreach (var task in _tasks)
{
task.Tick(dt); task.Tick(dt);
if (task.IsDone)
_toDelete.Add(task);
}
foreach (var action in _toDelete)
_tasks.Remove(action);
_toDelete.Clear();
} }
} }
+23 -2
View File
@@ -1,15 +1,36 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
*
* 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; using Ragon.Server.Time;
public class RagonActionTimer: IRagonAction public class RagonActionTimer : IRagonAction
{ {
public bool IsDone => _repeatCount == 0;
private Action _callback; private Action _callback;
private float _timer; private float _timer;
private float _time; private float _time;
private float _repeatCount;
public RagonActionTimer(Action callback, float timeInSeconds) public RagonActionTimer(Action callback, float timeInSeconds, int repeat = 1)
{ {
_callback = callback; _callback = callback;
_time = timeInSeconds * 1000; _time = timeInSeconds * 1000;
_repeatCount = repeat;
} }
public void Tick(float dt) public void Tick(float dt)