From 6886808132b0dc54bc727176288252a9ec9b9284 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sun, 5 May 2024 15:45:28 +0300 Subject: [PATCH] feat(wip): room properties --- Ragon.Client/Sources/Entity/RagonEntity.cs | 1 - .../Sources/Handler/RoomDataHandler.cs | 8 +++++++ ...awDataHandler.cs => RoomRawDataHandler.cs} | 4 ++-- Ragon.Client/Sources/RagonClient.cs | 2 +- Ragon.Client/Sources/RagonRoom.cs | 22 ++++++++++++------- Ragon.Protocol/Sources/RagonBuffer.cs | 1 + Ragon.Server/Sources/Room/IRagonRoom.cs | 2 ++ Ragon.Server/Sources/Room/RagonRoom.cs | 16 +++++++++++++- 8 files changed, 43 insertions(+), 13 deletions(-) rename Ragon.Client/Sources/Handler/{RawDataHandler.cs => RoomRawDataHandler.cs} (95%) diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Entity/RagonEntity.cs index afb2e9b..930a15c 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Entity/RagonEntity.cs @@ -55,7 +55,6 @@ namespace Ragon.Client public ushort Id { get; private set; } public ushort Type { get; private set; } - public RagonAuthority Authority { get; private set; } public RagonPlayer Owner { get; private set; } public RagonEntityState State { get; private set; } diff --git a/Ragon.Client/Sources/Handler/RoomDataHandler.cs b/Ragon.Client/Sources/Handler/RoomDataHandler.cs index 8aadbef..f46a19e 100644 --- a/Ragon.Client/Sources/Handler/RoomDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomDataHandler.cs @@ -4,9 +4,17 @@ namespace Ragon.Client { public class RoomDataHandler: IHandler { + private readonly RagonClient _client; + public RoomDataHandler(RagonClient client) + { + _client = client; + } + public void Handle(RagonBuffer reader) { + var len = reader.ReadUShort(); + _client.Room?.Data(reader); } } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/RawDataHandler.cs b/Ragon.Client/Sources/Handler/RoomRawDataHandler.cs similarity index 95% rename from Ragon.Client/Sources/Handler/RawDataHandler.cs rename to Ragon.Client/Sources/Handler/RoomRawDataHandler.cs index 93188e1..bb4ebe4 100644 --- a/Ragon.Client/Sources/Handler/RawDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomRawDataHandler.cs @@ -19,12 +19,12 @@ using Ragon.Protocol; namespace Ragon.Client; -internal class RawDataHandler: IHandler +internal class RoomRawDataHandler: IHandler { private readonly RagonListenerList _listeners; private readonly RagonPlayerCache _playerCache; - public RawDataHandler( + public RoomRawDataHandler( RagonPlayerCache playerCache, RagonListenerList listeners) { diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index 4ec10bd..c593c74 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -121,7 +121,7 @@ namespace Ragon.Client _handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache); _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 RawDataHandler(_playerCache, _listeners); + _handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomRawDataHandler(_playerCache, _listeners); _handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners); var protocolRaw = RagonVersion.Parse(protocol); diff --git a/Ragon.Client/Sources/RagonRoom.cs b/Ragon.Client/Sources/RagonRoom.cs index bf8d7c9..ef1806f 100644 --- a/Ragon.Client/Sources/RagonRoom.cs +++ b/Ragon.Client/Sources/RagonRoom.cs @@ -46,20 +46,21 @@ namespace Ragon.Client _callback = null!; } } - + private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer); - private RagonClient _client; - private RagonScene _scene; - private RagonEntityCache _entityCache; - private RagonPlayerCache _playerCache; - private RoomParameters _parameters; - + private readonly RagonClient _client; + private readonly RagonScene _scene; + private readonly RagonEntityCache _entityCache; + private readonly RagonPlayerCache _playerCache; + private readonly RoomParameters _parameters; + private readonly Dictionary _properties = new(); + public string Id => _parameters.RoomId; public int MinPlayers => _parameters.Min; public int MaxPlayers => _parameters.Max; public string Scene => _scene.Name; - + public IReadOnlyList Players => _playerCache.Players; public RagonPlayer Local => _playerCache.Local; public RagonPlayer Owner => _playerCache.Owner; @@ -100,6 +101,11 @@ namespace Ragon.Client RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined"); } + internal void Data(RagonBuffer buffer) + { + + } + public IDisposable OnEvent(Action callback) where TEvent : IRagonEvent, new() { var t = new TEvent(); diff --git a/Ragon.Protocol/Sources/RagonBuffer.cs b/Ragon.Protocol/Sources/RagonBuffer.cs index 6b3e431..40932f1 100644 --- a/Ragon.Protocol/Sources/RagonBuffer.cs +++ b/Ragon.Protocol/Sources/RagonBuffer.cs @@ -282,6 +282,7 @@ namespace Ragon.Protocol _write += numBits; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint Read(int numBits = 16) diff --git a/Ragon.Server/Sources/Room/IRagonRoom.cs b/Ragon.Server/Sources/Room/IRagonRoom.cs index de2aacb..9f42df9 100644 --- a/Ragon.Server/Sources/Room/IRagonRoom.cs +++ b/Ragon.Server/Sources/Room/IRagonRoom.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using Ragon.Server.Data; using Ragon.Server.Entity; using Ragon.Server.IO; @@ -26,6 +27,7 @@ public interface IRagonRoom public int PlayerMin { get; } public int PlayerMax { get; } public int PlayerCount { get; } + public RagonData UserData { get; } RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection); RagonRoomPlayer GetPlayerById(string id); diff --git a/Ragon.Server/Sources/Room/RagonRoom.cs b/Ragon.Server/Sources/Room/RagonRoom.cs index ce66157..7a5ea9a 100644 --- a/Ragon.Server/Sources/Room/RagonRoom.cs +++ b/Ragon.Server/Sources/Room/RagonRoom.cs @@ -30,7 +30,7 @@ public class RagonRoom : IRagonRoom, IRagonAction public int PlayerMax { get; private set; } public int PlayerMin { get; private set; } public int PlayerCount => WaitPlayersList.Count; - + public RagonData UserData { get; set; } public RagonRoomPlayer Owner { get; private set; } public RagonBuffer Writer { get; } @@ -110,6 +110,20 @@ public class RagonRoom : IRagonRoom, IRagonAction foreach (var roomPlayer in ReadyPlayersList) roomPlayer.Connection.Unreliable.Send(sendData); } + + if (UserData.IsDirty) + { + Writer.Clear(); + Writer.WriteOperation(RagonOperation.ROOM_DATA_UPDATED); + Writer.WriteUShort((ushort)UserData.Data.Length); + Writer.WriteBytes(UserData.Data); + + var sendData = Writer.ToArray(); + foreach (var roomPlayer in ReadyPlayersList) + roomPlayer.Connection.Reliable.Send(sendData); + + UserData.IsDirty = false; + } } public void AttachPlayer(RagonRoomPlayer player)