From d115c98b79279ce4e431c09b199b9540d48f2fa5 Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sun, 14 Apr 2024 08:49:30 +0300 Subject: [PATCH] feat(wip): Room Property and Player Property --- Ragon.Client/Sources/RagonPlayer.cs | 6 ++- Ragon.Protocol/Sources/RagonOperation.cs | 2 + .../Sources/Handler/AuthorizationOperation.cs | 6 ++- .../Sources/Handler/EntityCreateOperation.cs | 2 +- .../Sources/Handler/PlayerDataOperation.cs | 32 ++++++++++++ .../Sources/Handler/RoomRawDataOperation.cs | 49 +++++++++++++++++++ .../Sources/Handler/SceneLoadedOperation.cs | 2 +- Ragon.Server/Sources/RagonContext.cs | 21 ++++---- Ragon.Server/Sources/RagonServer.cs | 8 +-- 9 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 Ragon.Server/Sources/Handler/PlayerDataOperation.cs create mode 100644 Ragon.Server/Sources/Handler/RoomRawDataOperation.cs diff --git a/Ragon.Client/Sources/RagonPlayer.cs b/Ragon.Client/Sources/RagonPlayer.cs index c81ce99..9d7a842 100644 --- a/Ragon.Client/Sources/RagonPlayer.cs +++ b/Ragon.Client/Sources/RagonPlayer.cs @@ -14,6 +14,8 @@ * limitations under the License. */ +using Ragon.Protocol; + namespace Ragon.Client { [Serializable] @@ -25,6 +27,8 @@ namespace Ragon.Client public bool IsRoomOwner { get; set; } public bool IsLocal { get; set; } + public IRagonSerializable Data { get; set; } + public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal) { PeerId = peerId; @@ -32,6 +36,6 @@ namespace Ragon.Client IsLocal = isLocal; Name = name; Id = playerId; - } + } } } \ No newline at end of file diff --git a/Ragon.Protocol/Sources/RagonOperation.cs b/Ragon.Protocol/Sources/RagonOperation.cs index b7d6787..f5a9960 100644 --- a/Ragon.Protocol/Sources/RagonOperation.cs +++ b/Ragon.Protocol/Sources/RagonOperation.cs @@ -45,5 +45,7 @@ namespace Ragon.Protocol TRANSFER_ENTITY_OWNERSHIP = 24, TIMESTAMP_SYNCHRONIZATION = 25, ROOM_LIST_UPDATED = 26, + PLAYER_DATA_UPDATED = 27, + ROOM_DATA_UPDATED = 28, } } \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs index 6ed25bb..791944f 100644 --- a/Ragon.Server/Sources/Handler/AuthorizationOperation.cs +++ b/Ragon.Server/Sources/Handler/AuthorizationOperation.cs @@ -29,12 +29,14 @@ public sealed class AuthorizationOperation: BaseOperation private readonly RagonWebHookPlugin _webhook; private readonly RagonContextObserver _observer; private readonly RagonBuffer _writer; + private readonly RagonServerConfiguration _configuration; public AuthorizationOperation( RagonBuffer reader, RagonBuffer writer, RagonWebHookPlugin webhook, - RagonContextObserver observer): base(reader, writer) + RagonContextObserver observer, + RagonServerConfiguration configuration): base(reader, writer) { _webhook = webhook; _observer = observer; @@ -55,7 +57,7 @@ public sealed class AuthorizationOperation: BaseOperation return; } - var configuration = context.Configuration; + var configuration = _configuration; var key = Reader.ReadString(); var name = Reader.ReadString(); var payload = Reader.ReadString(); diff --git a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs b/Ragon.Server/Sources/Handler/EntityCreateOperation.cs index 546f5d8..7d5bf36 100644 --- a/Ragon.Server/Sources/Handler/EntityCreateOperation.cs +++ b/Ragon.Server/Sources/Handler/EntityCreateOperation.cs @@ -44,7 +44,7 @@ public sealed class EntityCreateOperation : BaseOperation Authority = eventAuthority, AttachId = attachId, StaticId = 0, - BufferedEvents = context.Configuration.LimitBufferedEvents, + BufferedEvents = context.LimitBufferedEvents, }; var entity = new RagonEntity(entityParameters); diff --git a/Ragon.Server/Sources/Handler/PlayerDataOperation.cs b/Ragon.Server/Sources/Handler/PlayerDataOperation.cs new file mode 100644 index 0000000..9dd5877 --- /dev/null +++ b/Ragon.Server/Sources/Handler/PlayerDataOperation.cs @@ -0,0 +1,32 @@ +using NLog; +using Ragon.Protocol; +using Ragon.Server.IO; +using Ragon.Server.Lobby; + +namespace Ragon.Server.Handler +{ + + public class PlayerDataOperation : BaseOperation + { + private readonly ILogger _logger = LogManager.GetCurrentClassLogger(); + + public PlayerDataOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + { + } + + public override void Handle(RagonContext context, NetworkChannel channel) + { + if (context.ConnectionStatus == ConnectionStatus.Unauthorized) + { + _logger.Warn($"Player {context.Connection.Id} not authorized for this request"); + return; + } + + var playerDataLen = Reader.ReadUShort(); + var playerData = Reader.ReadBytes(playerDataLen); + var player = context.RoomPlayer; + + // player.SetData(playerData); + } + } +} \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/RoomRawDataOperation.cs b/Ragon.Server/Sources/Handler/RoomRawDataOperation.cs new file mode 100644 index 0000000..134c89b --- /dev/null +++ b/Ragon.Server/Sources/Handler/RoomRawDataOperation.cs @@ -0,0 +1,49 @@ +/* + * Copyright 2023 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 NLog; +using Ragon.Protocol; +using Ragon.Server.IO; + +namespace Ragon.Server.Handler; + +public sealed class RoomRawDataOperation : BaseOperation +{ + public RoomRawDataOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) + { + } + + public override void Handle(RagonContext context, NetworkChannel channel) + { + var player = context.RoomPlayer; + var room = context.Room; + + var data = Reader.RawData; + var dataSize = data.Length - 1; + var headerSize = 3; + var size = headerSize + dataSize; + var sendData = new byte[size]; + var peerId = player.Connection.Id; + + sendData[0] = (byte)RagonOperation.REPLICATE_RAW_DATA; + sendData[1] = (byte)peerId; + sendData[2] = (byte)(peerId >> 8); + + Array.Copy(data, 1, sendData, headerSize, dataSize); + + room.Broadcast(sendData, channel); + } +} \ No newline at end of file diff --git a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs b/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs index dc0edc9..c81fbd4 100644 --- a/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs +++ b/Ragon.Server/Sources/Handler/SceneLoadedOperation.cs @@ -62,7 +62,7 @@ public sealed class SceneLoadedOperation : BaseOperation Authority = eventAuthority, AttachId = 0, StaticId = staticId, - BufferedEvents = context.Configuration.LimitBufferedEvents, + BufferedEvents = context.LimitBufferedEvents, }; var entity = new RagonEntity(entityParameters); diff --git a/Ragon.Server/Sources/RagonContext.cs b/Ragon.Server/Sources/RagonContext.cs index 3cf9e22..7badabf 100644 --- a/Ragon.Server/Sources/RagonContext.cs +++ b/Ragon.Server/Sources/RagonContext.cs @@ -26,7 +26,7 @@ public class RagonContext public ConnectionStatus ConnectionStatus { get; set; } public INetworkConnection Connection { get; } public IExecutor Executor { get; private set; } - public RagonServerConfiguration Configuration { get; private set; } + public int LimitBufferedEvents { get; private set; } public IRagonLobby Lobby { get; private set; } public RagonLobbyPlayer? LobbyPlayer { get; private set; } @@ -36,14 +36,14 @@ public class RagonContext public RagonScheduler Scheduler { get; private set; } public RagonContext( - INetworkConnection connection, - RagonServerConfiguration configuration, - IExecutor executor, - IRagonLobby lobby, - RagonScheduler scheduler) + INetworkConnection connection, + IExecutor executor, + IRagonLobby lobby, + RagonScheduler scheduler, + int limitBufferedEvents) { ConnectionStatus = ConnectionStatus.Unauthorized; - Configuration = configuration; + LimitBufferedEvents = limitBufferedEvents; Connection = connection; Executor = executor; Lobby = lobby; @@ -54,15 +54,14 @@ public class RagonContext { LobbyPlayer = player; } - + internal void SetRoom(RagonRoom room, RagonRoomPlayer player) { Room?.DetachPlayer(RoomPlayer); - + Room = room; RoomPlayer = player; - + Room.AttachPlayer(RoomPlayer); } - } \ No newline at end of file diff --git a/Ragon.Server/Sources/RagonServer.cs b/Ragon.Server/Sources/RagonServer.cs index db57d26..671db1f 100644 --- a/Ragon.Server/Sources/RagonServer.cs +++ b/Ragon.Server/Sources/RagonServer.cs @@ -78,7 +78,7 @@ public class RagonServer : IRagonServer, INetworkListener _serverPlugin.OnAttached(this); _handlers = new BaseOperation[byte.MaxValue]; - _handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _webhooks, contextObserver); + _handlers[(byte)RagonOperation.AUTHORIZE] = new AuthorizationOperation(_reader, _writer, _webhooks, contextObserver, configuration); _handlers[(byte)RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(_reader, _writer, plugin, _webhooks); _handlers[(byte)RagonOperation.CREATE_ROOM] = new RoomCreateOperation(_reader, _writer, plugin, _webhooks); _handlers[(byte)RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_reader, _writer, _webhooks); @@ -93,7 +93,9 @@ public class RagonServer : IRagonServer, INetworkListener _handlers[(byte)RagonOperation.TRANSFER_ENTITY_OWNERSHIP] = new EntityOwnershipOperation(_reader, _writer); _handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampSyncOperation(_reader, _writer); _handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventOperation(_reader, _writer); - _handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataOperation(_reader, _writer); + _handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomRawDataOperation(_reader, _writer); + _handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomDataOperation(_reader, _writer); + _handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerDataOperation(_reader, _writer); _logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}"); } @@ -150,7 +152,7 @@ public class RagonServer : IRagonServer, INetworkListener public void OnConnected(INetworkConnection connection) { - var context = new RagonContext(connection, _configuration, _executor, _lobby, _scheduler); + var context = new RagonContext(connection, _executor, _lobby, _scheduler, _configuration.LimitBufferedEvents); _logger.Trace($"Connected: {connection.Id}"); _contextsByConnection.Add(connection.Id, context);