Compare commits

...

7 Commits

Author SHA1 Message Date
edmand46 b39bd8bd0a fix: updated workflows 2024-04-14 19:23:08 +03:00
edmand46 12b80c546c fix: updated workflows 2024-04-14 19:14:57 +03:00
edmand46 5c20fbafc1 Merge pull request #15 from edmand46/feat/rooms
feat: Room List
2024-04-13 18:27:28 +03:00
edmand46 bd0c6df5ea fix: timer, providing api for listeners 2024-04-13 18:25:39 +03:00
edmand46 c4811ef052 feat(wip): list rooms 2024-04-13 17:43:23 +03:00
edmand46 d82964526c feat(wip): room list support 2024-04-13 16:17:31 +03:00
edmand46 acedaef270 ♻️ unified event api for room and entities 2024-04-07 17:15:52 +03:00
23 changed files with 306 additions and 93 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Build
shell: bash
run: |
@@ -19,9 +19,9 @@ using Ragon.Protocol;
namespace Ragon.Client;
public struct RagonRoomInformation
public struct RoomParameters
{
public RagonRoomInformation(string roomId, string playerId, string ownerId, ushort min, ushort max)
public RoomParameters(string roomId, string playerId, string ownerId, ushort min, ushort max)
{
RoomId = roomId;
PlayerId = playerId;
@@ -39,6 +39,8 @@ public struct RagonRoomInformation
internal class JoinSuccessHandler : IHandler
{
private readonly RagonListenerList _listenerList;
private readonly RagonPlayerCache _playerCache;
private readonly RagonEntityCache _entityCache;
@@ -67,7 +69,7 @@ internal class JoinSuccessHandler : IHandler
var sceneName = reader.ReadString();
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
var roomInfo = new RagonRoomInformation(roomId, localId, ownerId, min, max);
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
_playerCache.SetOwnerAndLocal(ownerId, localId);
@@ -0,0 +1,43 @@
using Ragon.Protocol;
namespace Ragon.Client;
internal class RoomListHandler: IHandler
{
private RagonListenerList _listenerList;
private RagonSession _session;
public RoomListHandler(RagonSession session, RagonListenerList list)
{
_session = session;
_listenerList = list;
}
public void Handle(RagonBuffer reader)
{
var roomCount = reader.ReadUShort();
var roomList = new RagonRoomInformation[roomCount];
for (int i = 0; i < roomCount; i++)
{
var id = reader.ReadString();
var scene = reader.ReadString();
var maxPlayers = reader.ReadUShort();
var minPlayers = reader.ReadUShort();
var players = reader.ReadUShort();
var roomInfo = new RagonRoomInformation()
{
Id = id,
Scene = scene,
PlayerCount = players,
PlayerMax = maxPlayers,
PlayerMin = minPlayers,
Properties = new Dictionary<string, byte[]>()
};
roomList[i] = roomInfo;
}
_listenerList.OnRoomList(roomList);
}
}
@@ -0,0 +1,6 @@
namespace Ragon.Client;
public interface IRagonRoomListListener
{
public void OnRoomListUpdate(IReadOnlyList<RagonRoomInformation> roomsInfos);
}
+45 -43
View File
@@ -29,7 +29,7 @@ namespace Ragon.Client
private RagonBuffer _writeBuffer;
private RagonRoom _room;
private RagonSession _session;
private RagonListenerList listeners;
private RagonListenerList _listeners;
private RagonPlayerCache _playerCache;
private RagonEntityCache _entityCache;
private RagonEventCache _eventCache;
@@ -56,7 +56,7 @@ namespace Ragon.Client
public RagonClient(INetworkConnection connection, int rate)
{
listeners = new RagonListenerList(this);
_listeners = new RagonListenerList(this);
_connection = connection;
_connection.OnData += OnData;
@@ -104,25 +104,26 @@ namespace Ragon.Client
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
_handlers = new IHandler[byte.MaxValue];
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, listeners);
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(listeners);
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(listeners);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, listeners, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, listeners);
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, listeners);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, listeners);
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners);
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners);
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
_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(_playerCache, _entityCache);
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, listeners, _entityCache, _playerCache, _entityListener);
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener);
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, listeners);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
var protocolRaw = RagonVersion.Parse(protocol);
_connection.Connect(address, port, protocolRaw);
}
@@ -152,7 +153,7 @@ namespace Ragon.Client
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
}
listeners.Update();
_listeners.Update();
_connection.Update();
}
@@ -166,31 +167,32 @@ namespace Ragon.Client
_connection.Dispose();
}
public void AddListener(IRagonListener listener) => listeners.Add(listener);
public void AddListener(IRagonAuthorizationListener listener) => listeners.Add(listener);
public void AddListener(IRagonConnectionListener listener) => listeners.Add(listener);
public void AddListener(IRagonFailedListener listener) => listeners.Add(listener);
public void AddListener(IRagonJoinListener listener) => listeners.Add(listener);
public void AddListener(IRagonLeftListener listener) => listeners.Add(listener);
public void AddListener(IRagonOwnershipChangedListener listener) => listeners.Add(listener);
public void AddListener(IRagonPlayerJoinListener listener) => listeners.Add(listener);
public void AddListener(IRagonPlayerLeftListener listener) => listeners.Add(listener);
public void AddListener(IRagonSceneListener listener) => listeners.Add(listener);
public void AddListener(IRagonSceneRequestListener listener) => listeners.Add(listener);
public void AddListener(IRagonDataListener listener) => listeners.Add(listener);
public void RemoveListener(IRagonListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonAuthorizationListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonConnectionListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonFailedListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonJoinListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonLeftListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonOwnershipChangedListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonPlayerJoinListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonPlayerLeftListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonSceneListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonSceneRequestListener listener) => listeners.Remove(listener);
public void RemoveListener(IRagonDataListener listener) => listeners.Remove(listener);
public void AddListener(IRagonListener listener) => _listeners.Add(listener);
public void AddListener(IRagonAuthorizationListener listener) => _listeners.Add(listener);
public void AddListener(IRagonConnectionListener listener) => _listeners.Add(listener);
public void AddListener(IRagonFailedListener listener) => _listeners.Add(listener);
public void AddListener(IRagonJoinListener listener) => _listeners.Add(listener);
public void AddListener(IRagonLeftListener listener) => _listeners.Add(listener);
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener);
public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener);
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
public void RemoveListener(IRagonListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonAuthorizationListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonConnectionListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonFailedListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonJoinListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonLeftListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonSceneListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonSceneRequestListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener);
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
#endregion
@@ -234,7 +236,7 @@ namespace Ragon.Client
{
RagonLog.Trace("Connected");
listeners.OnConnected();
_listeners.OnConnected();
_status = RagonStatus.CONNECTED;
}
@@ -242,7 +244,7 @@ namespace Ragon.Client
{
RagonLog.Trace($"Disconnected: {reason}");
listeners.OnDisconnected(reason);
_listeners.OnDisconnected(reason);
_status = RagonStatus.DISCONNECTED;
}
+17
View File
@@ -32,6 +32,7 @@ namespace Ragon.Client
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
private readonly List<IRagonDataListener> _dataListeners = new();
private readonly List<IRagonRoomListListener> _roomListListeners = new();
private readonly List<Action> _delayedActions = new();
public RagonListenerList(RagonClient client)
@@ -131,6 +132,11 @@ namespace Ragon.Client
_playerLeftListeners.Add(listener);
}
public void Add(IRagonRoomListListener listener)
{
_roomListListeners.Add(listener);
}
public void Remove(IRagonDataListener listener)
{
_delayedActions.Add(() => _dataListeners.Remove(listener));
@@ -185,6 +191,11 @@ namespace Ragon.Client
{
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
}
public void Remove(IRagonRoomListListener listener)
{
_delayedActions.Add(() => _roomListListeners.Remove(listener));
}
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
{
@@ -263,5 +274,11 @@ namespace Ragon.Client
foreach (var listener in _dataListeners)
listener.OnData(player, data);
}
public void OnRoomList(RagonRoomInformation[] roomInfos)
{
foreach (var listListener in _roomListListeners)
listListener.OnRoomListUpdate(roomInfos);
}
}
}
+35 -21
View File
@@ -20,17 +20,44 @@ namespace Ragon.Client
{
public class RagonRoom: IDisposable
{
private class EventSubscription : IDisposable
{
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
private Action<RagonPlayer, IRagonEvent> _callback;
public EventSubscription(
List<Action<RagonPlayer, IRagonEvent>> callbacks,
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
Action<RagonPlayer, IRagonEvent> callback)
{
_callbacks = callbacks;
_localCallbacks = localCallbacks;
_callback = callback;
}
public void Dispose()
{
_callbacks?.Remove(_callback);
_localCallbacks?.Remove(_callback);
_callbacks = null!;
_localCallbacks = null!;
_callback = null!;
}
}
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
private RagonClient _client;
private RagonScene _scene;
private RagonEntityCache _entityCache;
private RagonPlayerCache _playerCache;
private RagonRoomInformation _information;
private RoomParameters _parameters;
public string Id => _information.RoomId;
public int MinPlayers => _information.Min;
public int MaxPlayers => _information.Max;
public string Id => _parameters.RoomId;
public int MinPlayers => _parameters.Min;
public int MaxPlayers => _parameters.Max;
public string Scene => _scene.Name;
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
@@ -44,11 +71,11 @@ namespace Ragon.Client
public RagonRoom(RagonClient client,
RagonEntityCache entityCache,
RagonPlayerCache playerCache,
RagonRoomInformation information,
RoomParameters parameters,
RagonScene scene)
{
_client = client;
_information = information;
_parameters = parameters;
_entityCache = entityCache;
_playerCache = playerCache;
_scene = scene;
@@ -73,11 +100,10 @@ namespace Ragon.Client
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
}
public Action<RagonPlayer, IRagonEvent> OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
if (!_listeners.TryGetValue(eventCode, out var callbacks))
@@ -106,19 +132,7 @@ namespace Ragon.Client
});
}
return action;
}
public void OffEvent<TEvent>(Action<RagonPlayer, IRagonEvent> callback) where TEvent : IRagonEvent, new()
{
var t = new TEvent();
var eventCode = _client.Event.GetEventCode(t);
if (_listeners.TryGetValue(eventCode, out var callbacks))
callbacks.Remove(callback);
if (_localListeners.TryGetValue(eventCode, out var localCallbacks))
localCallbacks.Remove(callback);
return new EventSubscription(callbacks, localCallbacks, action);
}
public void LoadScene(string sceneName) => _scene.Load(sceneName);
@@ -0,0 +1,12 @@
namespace Ragon.Client
{
public struct RagonRoomInformation
{
public string Id;
public string Scene;
public int PlayerMax;
public int PlayerMin;
public int PlayerCount;
public Dictionary<string, byte[]> Properties;
}
}
+5 -5
View File
@@ -31,11 +31,11 @@ namespace Ragon.Client
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
{
var parameters = new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
var parameters = new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
CreateOrJoin(parameters);
}
public void CreateOrJoin(RagonRoomParameters parameters)
public void CreateOrJoin(RagonRoomPayload parameters)
{
_buffer.Clear();
_buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
@@ -48,15 +48,15 @@ namespace Ragon.Client
public void Create(string sceneName, int minPlayers, int maxPlayers)
{
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
Create(null, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
}
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
{
Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
Create(roomId, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
}
public void Create(string roomId, RagonRoomParameters parameters)
public void Create(string roomId, RagonRoomPayload parameters)
{
_buffer.Clear();
_buffer.WriteOperation(RagonOperation.CREATE_ROOM);
+1
View File
@@ -44,5 +44,6 @@ namespace Ragon.Protocol
TRANSFER_ROOM_OWNERSHIP = 23,
TRANSFER_ENTITY_OWNERSHIP = 24,
TIMESTAMP_SYNCHRONIZATION = 25,
ROOM_LIST_UPDATED = 26,
}
}
@@ -17,7 +17,7 @@
namespace Ragon.Protocol
{
public class RagonRoomParameters: IRagonSerializable
public class RagonRoomPayload: IRagonSerializable
{
public string Scene { get; set; }
public int Min { get; set; }
+1 -1
View File
@@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>Ragon.Relay</RootNamespace>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@@ -26,7 +26,7 @@ namespace Ragon.Server.Handler;
public sealed class RoomCreateOperation : BaseOperation
{
private readonly RagonRoomParameters _roomParameters = new();
private readonly RagonRoomPayload _roomPayload = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
@@ -65,13 +65,13 @@ public sealed class RoomCreateOperation : BaseOperation
}
}
_roomParameters.Deserialize(Reader);
_roomPayload.Deserialize(Reader);
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
Scene = _roomPayload.Scene,
Max = _roomPayload.Max,
Min = _roomPayload.Min,
};
var lobbyPlayer = context.LobbyPlayer;
@@ -80,6 +80,9 @@ public sealed class RoomCreateOperation : BaseOperation
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
if (!roomPlugin.OnPlayerJoined(roomPlayer))
return;
roomPlayer.OnAttached(room);
context.Scheduler.Run(room);
@@ -26,7 +26,7 @@ namespace Ragon.Server.Handler;
public sealed class RoomJoinOrCreateOperation : BaseOperation
{
private readonly RagonRoomParameters _roomParameters = new();
private readonly RagonRoomPayload _roomPayload = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
@@ -48,11 +48,15 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
var roomId = Guid.NewGuid().ToString();
var lobbyPlayer = context.LobbyPlayer;
_roomParameters.Deserialize(Reader);
_roomPayload.Deserialize(Reader);
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
{
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
if (!existsRoom.Plugin.OnPlayerJoined(player))
return;
context.SetRoom(existsRoom, player);
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
@@ -63,15 +67,18 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
{
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
Scene = _roomPayload.Scene,
Max = _roomPayload.Max,
Min = _roomPayload.Min,
};
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
if (!roomPlugin.OnPlayerJoined(roomPlayer))
return;
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
context.Lobby.Persist(room);
@@ -21,6 +21,7 @@ namespace Ragon.Server.Lobby;
public interface IRagonLobby
{
public IReadOnlyList<IRagonRoom> Rooms { get; }
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
public bool FindRoomByScene(string sceneName, [MaybeNullWhen(false)] out RagonRoom room);
public void Persist(RagonRoom room);
@@ -0,0 +1,33 @@
using Ragon.Protocol;
using Ragon.Server.Room;
namespace Ragon.Server.Lobby;
public class RagonLobbyDispatcher
{
private IRagonLobby _lobby;
public RagonLobbyDispatcher(IRagonLobby lobby)
{
_lobby = lobby;
}
public void Write(RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.ROOM_LIST_UPDATED);
var rooms = _lobby.Rooms;
writer.WriteUShort((ushort)rooms.Count);
for (int i = 0; i < rooms.Count; i++)
{
var room = rooms[i];
writer.WriteString(room.Id);
writer.WriteString(room.Scene);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerCount);
}
}
}
@@ -25,6 +25,8 @@ public class LobbyInMemory : IRagonLobby
private readonly List<RagonRoom> _rooms = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public IReadOnlyList<IRagonRoom> Rooms => _rooms.AsReadOnly();
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room)
{
foreach (var existRagonRoom in _rooms)
+5 -4
View File
@@ -2,8 +2,9 @@ namespace Ragon.Server.Logging;
public interface IRagonLogger
{
public void Warning(string tag, string message);
public void Info(string tag, string message);
public void Error(string tag, string message);
public void Trace(string tag, string message);
public void Warning(string message);
public void Info(string message);
public void Error(string message);
public void Error(Exception ex);
public void Trace(string message);
}
@@ -0,0 +1,6 @@
namespace Ragon.Server.Logging;
public interface IRagonLoggerFactory
{
public IRagonLogger GetLogger(string tag);
}
@@ -0,0 +1,17 @@
namespace Ragon.Server.Logging
{
public class LoggerManager
{
private static IRagonLoggerFactory _factory = null!;
public static void SetLoggerFactory(IRagonLoggerFactory loggerFactory)
{
_factory = loggerFactory;
}
public static IRagonLogger GetLogger(string tag)
{
return _factory.GetLogger(tag);
}
}
}
+19 -3
View File
@@ -45,6 +45,7 @@ public class RagonServer : IRagonServer, INetworkListener
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
private readonly Stopwatch _timer;
private readonly RagonLobbyDispatcher _lobbySerializer;
private readonly long _tickRate = 0;
public RagonServer(
@@ -59,6 +60,7 @@ public class RagonServer : IRagonServer, INetworkListener
_contextsByConnection = new Dictionary<ushort, RagonContext>();
_contextsByPlayerId = new Dictionary<string, RagonContext>();
_lobby = new LobbyInMemory();
_lobbySerializer = new RagonLobbyDispatcher(_lobby);
_scheduler = new RagonScheduler();
_webhooks = new RagonWebHookPlugin(this, configuration);
_dedicatedThread = new Thread(Execute);
@@ -68,9 +70,11 @@ public class RagonServer : IRagonServer, INetworkListener
_writer = new RagonBuffer();
_tickRate = 1000 / _configuration.ServerTickRate;
_timer = new Stopwatch();
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
_scheduler.Run(new RagonActionTimer(SendRoomList, 1.0f));
_serverPlugin.OnAttached(this);
_handlers = new BaseOperation[byte.MaxValue];
@@ -107,7 +111,7 @@ public class RagonServer : IRagonServer, INetworkListener
if (_timer.ElapsedMilliseconds > _tickRate)
{
_timer.Restart();
_scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f);
_scheduler.Update(_tickRate);
SendTimestamp();
}
@@ -228,6 +232,18 @@ public class RagonServer : IRagonServer, INetworkListener
_server.Broadcast(sendData, NetworkChannel.UNRELIABLE);
}
public void SendRoomList()
{
_lobbySerializer.Write(_writer);
var sendData = _writer.ToArray();
foreach (var (_, value) in _contextsByPlayerId)
{
if (value.Room == null) // If only in lobby, then send room list data
value.Connection.Reliable.Send(sendData);
}
}
public BaseOperation ResolveHandler(RagonOperation operation)
{
return _handlers[(byte)operation];
+6
View File
@@ -21,6 +21,12 @@ namespace Ragon.Server.Room;
public interface IRagonRoom
{
public string Id { get; }
public string Scene { get; }
public int PlayerMin { get; }
public int PlayerMax { get; }
public int PlayerCount { get; }
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
RagonRoomPlayer GetPlayerById(string id);
IRagonEntity GetEntityById(ushort id);
@@ -0,0 +1,24 @@
using Ragon.Server.Time;
public class RagonActionTimer: IRagonAction
{
private Action _callback;
private float _timer;
private float _time;
public RagonActionTimer(Action callback, float timeInSeconds)
{
_callback = callback;
_time = timeInSeconds * 1000;
}
public void Tick(float dt)
{
_timer += dt;
if (_timer >= _time)
{
_callback?.Invoke();
_timer = 0;
}
}
}