feat(wip): player properties

This commit is contained in:
2024-04-29 09:12:42 +03:00
parent accd442388
commit b4cba20d82
14 changed files with 110 additions and 93 deletions
@@ -0,0 +1,13 @@
using Ragon.Protocol;
namespace Ragon.Client
{
public class PlayerDataHandler: IHandler
{
public void Handle(RagonBuffer reader)
{
}
}
}
@@ -0,0 +1,56 @@
/*
* 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 Ragon.Protocol;
namespace Ragon.Client;
internal class RawDataHandler: IHandler
{
private readonly RagonListenerList _listeners;
private readonly RagonPlayerCache _playerCache;
public RawDataHandler(
RagonPlayerCache playerCache,
RagonListenerList listeners)
{
_playerCache = playerCache;
_listeners = listeners;
}
public void Handle(RagonBuffer reader)
{
var rawData = reader.RawData;
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found");
_playerCache.Dump();
return;
}
var headerSize = 3;
var payload = new byte[rawData.Length - headerSize];
Array.Copy(rawData, headerSize, payload, 0, payload.Length);
_listeners.OnData(player, payload);
}
}
@@ -1,56 +1,12 @@
/*
* 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 Ragon.Protocol;
namespace Ragon.Client;
internal class RoomDataHandler: IHandler
namespace Ragon.Client
{
private readonly RagonListenerList _listeners;
private readonly RagonPlayerCache _playerCache;
public RoomDataHandler(
RagonPlayerCache playerCache,
RagonListenerList listeners)
public class RoomDataHandler: IHandler
{
_playerCache = playerCache;
_listeners = listeners;
}
public void Handle(RagonBuffer reader)
{
var rawData = reader.RawData;
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
var player = _playerCache.GetPlayerByPeer(peerId);
if (player == null)
{
RagonLog.Error($"Player with peerId:{peerId} not found");
_playerCache.Dump();
return;
}
var headerSize = 3;
var payload = new byte[rawData.Length - headerSize];
Array.Copy(rawData, headerSize, payload, 0, payload.Length);
_listeners.OnData(player, payload);
}
}
+1 -1
View File
@@ -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 RoomDataHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RawDataHandler(_playerCache, _listeners);
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
var protocolRaw = RagonVersion.Parse(protocol);
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Ragon.Server.Data;
public class RagonData
{
private byte[] _data = Array.Empty<byte>();
private byte[] _data;
public bool IsDirty { get; set; }
public byte[] Data
{
@@ -86,7 +86,6 @@ public sealed class AuthorizationOperation: BaseOperation
var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
@@ -5,7 +5,6 @@ using Ragon.Server.Lobby;
namespace Ragon.Server.Handler
{
public class PlayerDataOperation : BaseOperation
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
@@ -25,19 +24,7 @@ namespace Ragon.Server.Handler
var playerDataLen = Reader.ReadUShort();
var playerData = Reader.ReadBytes(playerDataLen);
var roomPlayer = context.RoomPlayer;
if (roomPlayer != null)
{
roomPlayer.UserData.Data = playerData;
return;
}
var lobbyPlayer = context.RoomPlayer;
if (lobbyPlayer != null)
{
lobbyPlayer.UserData.Data = playerData;
}
context.UserData.Data = playerData;
}
}
}
@@ -14,7 +14,6 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
@@ -28,22 +27,11 @@ public sealed class RoomDataOperation : BaseOperation
public override void Handle(RagonContext context, NetworkChannel channel)
{
var player = context.RoomPlayer;
var playerDataLen = Reader.ReadUShort();
var playerData = Reader.ReadBytes(playerDataLen);
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);
if (room != null)
room.UserData.Data = playerData;
}
}
@@ -31,14 +31,13 @@ public class RagonLobbyPlayer
public INetworkConnection Connection { get; }
public string Id { get; private set; }
public string Name { get; private set; }
public string Payload { get; private set; }
public RagonData UserData { get; private set; }
public RagonLobbyPlayer(INetworkConnection connection, string id, string name, byte[] payload)
public RagonLobbyPlayer(INetworkConnection connection, string id, string name, string payload)
{
Id = id;
Name = name;
Connection = connection;
UserData = new RagonData(payload);
Payload = payload;
}
}
@@ -59,8 +59,7 @@ public class RagonWebHookPlugin
var authorizationResponse = JsonConvert.DeserializeObject<AuthorizationResponse>(content);
if (authorizationResponse != null)
{
var bytes = Encoding.UTF8.GetBytes(authorizationResponse.Payload);
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, bytes);
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload);
context.SetPlayer(lobbyPlayer);
authorizationOperation.Approve(context);
+4 -1
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
using Ragon.Server.Data;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Time;
@@ -29,10 +30,11 @@ public class RagonContext
public int LimitBufferedEvents { get; private set; }
public IRagonLobby Lobby { get; private set; }
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
public RagonRoom? Room { get; private set; }
public RagonRoomPlayer? RoomPlayer { get; private set; }
public RagonData UserData { get; private set; }
public RagonScheduler Scheduler { get; private set; }
public RagonContext(
@@ -48,6 +50,7 @@ public class RagonContext
Executor = executor;
Lobby = lobby;
Scheduler = scheduler;
UserData = new RagonData(Array.Empty<byte>());
}
internal void SetPlayer(RagonLobbyPlayer player)
+18
View File
@@ -74,6 +74,7 @@ public class RagonServer : IRagonServer, INetworkListener
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
_scheduler.Run(new RagonActionTimer(SendRoomList, 1.0f));
_scheduler.Run(new RagonActionTimer(SendUserData, 0.2f));
_serverPlugin.OnAttached(this);
@@ -246,6 +247,23 @@ public class RagonServer : IRagonServer, INetworkListener
}
}
public void SendUserData()
{
foreach (var (_, value) in _contextsByPlayerId)
{
if (value.UserData.IsDirty)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
_writer.WriteUShort(value.Connection.Id);
_writer.WriteBytes(value.UserData.Data);
var sendData = _writer.ToArray();
_server.Broadcast(sendData, NetworkChannel.RELIABLE);
}
}
}
public BaseOperation ResolveHandler(RagonOperation operation)
{
return _handlers[(byte)operation];
+2
View File
@@ -15,6 +15,7 @@
*/
using Ragon.Protocol;
using Ragon.Server.Data;
using Ragon.Server.Entity;
using Ragon.Server.IO;
using Ragon.Server.Plugin;
@@ -30,6 +31,7 @@ public class RagonRoom : IRagonRoom, IRagonAction
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; }
public IRoomPlugin Plugin { get; private set; }
@@ -30,15 +30,12 @@ public class RagonRoomPlayer
public RagonRoom Room { get; private set; }
public RagonEntityCache Entities { get; private set; }
public RagonData UserData { get; private set; }
public RagonRoomPlayer(INetworkConnection connection, string id, string name)
{
Id = id;
Name = name;
Connection = connection;
Entities = new RagonEntityCache();
UserData = new RagonData(Array.Empty<byte>());
}
public void AttachEntity(RagonEntity entity)