feat(wip): Room Property and Player Property

This commit is contained in:
2024-04-14 08:49:30 +03:00
parent 5c20fbafc1
commit d115c98b79
9 changed files with 109 additions and 19 deletions
+4
View File
@@ -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;
+2
View File
@@ -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,
}
}
@@ -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();
@@ -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);
@@ -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);
}
}
}
@@ -0,0 +1,49 @@
/*
* Copyright 2023 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 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);
}
}
@@ -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);
+4 -5
View File
@@ -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; }
@@ -37,13 +37,13 @@ public class RagonContext
public RagonContext(
INetworkConnection connection,
RagonServerConfiguration configuration,
IExecutor executor,
IRagonLobby lobby,
RagonScheduler scheduler)
RagonScheduler scheduler,
int limitBufferedEvents)
{
ConnectionStatus = ConnectionStatus.Unauthorized;
Configuration = configuration;
LimitBufferedEvents = limitBufferedEvents;
Connection = connection;
Executor = executor;
Lobby = lobby;
@@ -64,5 +64,4 @@ public class RagonContext
Room.AttachPlayer(RoomPlayer);
}
}
+5 -3
View File
@@ -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);