refactoring: removed external dependencies from server, removed webhooks from server

This commit is contained in:
2024-05-19 08:54:49 +03:00
parent b84538b238
commit e9dc45265a
53 changed files with 642 additions and 1049 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
* 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.
@@ -14,105 +14,103 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
namespace Ragon.Server.Handler;
public sealed class AuthorizationOperation: BaseOperation
namespace Ragon.Server.Handler
{
private Logger _logger = LogManager.GetCurrentClassLogger();
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,
RagonServerConfiguration configuration): base(reader, writer)
public sealed class AuthorizationOperation : BaseOperation
{
_webhook = webhook;
_observer = observer;
_writer = writer;
_configuration = configuration;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Authorized)
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(AuthorizationOperation));
private readonly IServerPlugin _serverPlugin;
private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer;
public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer,
IServerPlugin serverPlugin,
RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer)
{
_logger.Warn("Player already authorized!");
return;
_serverPlugin = serverPlugin;
_configuration = configuration;
_observer = observer;
_writer = writer;
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
public override void Handle(RagonContext context, NetworkChannel channel)
{
_logger.Warn("Player already request authorization!");
return;
}
var configuration = _configuration;
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
if (key == configuration.ServerKey)
{
if (_webhook.RequestAuthorization(context, payload))
if (context.ConnectionStatus == ConnectionStatus.Authorized)
{
_logger.Warning("Player already authorized!");
return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
{
_logger.Warning("Player already request authorization!");
return;
}
var configuration = _configuration;
var key = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
if (key == configuration.ServerKey)
{
var authorizeViaWebHook = _serverPlugin.OnAuthorize(new ConnectionRequest() { PeerID = context.Connection.Id });
if (authorizeViaWebHook)
return;
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
else
{
Reject(context);
}
}
else
public void Approve(RagonContext context)
{
Reject(context);
context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
_writer.WriteString(playerId);
_writer.WriteString(playerName);
_writer.WriteString(playerPayload);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
}
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}");
}
}
public void Approve(RagonContext context)
{
context.ConnectionStatus = ConnectionStatus.Authorized;
_observer.OnAuthorized(context);
var playerId = context.LobbyPlayer.Id;
var playerName = context.LobbyPlayer.Name;
var playerPayload = context.LobbyPlayer.Payload;
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
_writer.WriteString(playerId);
_writer.WriteString(playerName);
_writer.WriteString(playerPayload);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
}
public void Reject(RagonContext context)
{
_writer.Clear();
_writer.WriteOperation(RagonOperation.AUTHORIZED_FAILED);
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
context.Connection.Close();
_logger.Trace($"Connection {context.Connection.Id}");
}
}
@@ -14,17 +14,17 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityCreateOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityCreateOperation));
public EntityCreateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
@@ -14,16 +14,16 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityEventOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityEventOperation));
public EntityEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
@@ -37,7 +37,7 @@ public sealed class EntityEventOperation : BaseOperation
if (!room.Entities.TryGetValue(entityId, out var ent))
{
_logger.Warn($"Entity not found for event with Id {entityId}");
_logger.Warning($"Entity not found for event with Id {entityId}");
return;
}
@@ -15,15 +15,15 @@
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityOwnershipOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityOwnershipOperation));
public EntityOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
@@ -14,16 +14,16 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityDestroyOperation: BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityDestroyOperation));
public EntityDestroyOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
@@ -14,15 +14,15 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class EntityStateOperation: BaseOperation
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(EntityStateOperation));
public EntityStateOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
@@ -1,15 +1,15 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler
{
public class PlayerUserDataOperation : BaseOperation
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(PlayerUserDataOperation));
private readonly int _userDataLimit;
public PlayerUserDataOperation(
RagonBuffer reader,
RagonBuffer writer,
@@ -23,10 +23,10 @@ namespace Ragon.Server.Handler
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
_logger.Warning($"Player {context.Connection.Id} not authorized for this request");
return;
}
context.UserData.Read(Reader);
}
}
@@ -14,114 +14,114 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server.Handler;
public sealed class RoomCreateOperation : BaseOperation
namespace Ragon.Server.Handler
{
private readonly RagonRoomPayload _roomPayload = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public RoomCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin, RagonWebHookPlugin ragonWebHook) : base(reader, writer)
public sealed class RoomCreateOperation : BaseOperation
{
_serverPlugin = serverPlugin;
_ragonWebHookPlugin = ragonWebHook;
}
private IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomCreateOperation));
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
private readonly RagonRoomParameters _roomParameters = new();
private readonly IServerPlugin _serverPlugin;
public RoomCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin) : base(reader,
writer)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return;
_serverPlugin = serverPlugin;
}
var custom = Reader.ReadBool();
var roomId = Guid.NewGuid().ToString();
if (custom)
public override void Handle(RagonContext context, NetworkChannel channel)
{
roomId = Reader.ReadString();
if (context.Lobby.FindRoomById(roomId, out _))
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
Writer.Clear();
Writer.WriteOperation(RagonOperation.JOIN_FAILED);
Writer.WriteString($"Room with id {roomId} already exists");
var sendData = Writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist");
_logger.Warning($"Player {context.Connection.Id} not authorized for this request");
return;
}
var custom = Reader.ReadBool();
var roomId = Guid.NewGuid().ToString();
if (custom)
{
roomId = Reader.ReadString();
if (context.Lobby.FindRoomById(roomId, out _))
{
Writer.Clear();
Writer.WriteOperation(RagonOperation.JOIN_FAILED);
Writer.WriteString($"Room with id {roomId} already exists");
var sendData = Writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace(
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} join failed to room {roomId}, room already exist");
return;
}
}
_roomParameters.Deserialize(Reader);
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
};
var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
room.Plugin.OnAttached(room);
roomPlayer.OnAttached(room);
context.Scheduler.Run(room);
context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
_logger.Trace(
$"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
JoinSuccess(roomPlayer, room, Writer);
roomPlugin.OnPlayerJoined(roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
}
_roomPayload.Deserialize(Reader);
var information = new RoomInformation()
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
{
Scene = _roomPayload.Scene,
Max = _roomPayload.Max,
Min = _roomPayload.Min,
};
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
room.UserData.Snapshot(writer);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
room.Plugin.OnAttached(room);
roomPlayer.OnAttached(room);
writer.WriteUShort((ushort)room.PlayerList.Count);
foreach (var roomPlayer in room.PlayerList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
context.Scheduler.Run(room);
context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
roomPlayer.Context.UserData.Snapshot(writer);
}
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with scene {information.Scene}");
JoinSuccess(roomPlayer, room, Writer);
roomPlugin.OnPlayerJoined(roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
}
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
room.UserData.Snapshot(writer);
writer.WriteUShort((ushort)room.PlayerList.Count);
foreach (var roomPlayer in room.PlayerList)
{
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
roomPlayer.Context.UserData.Snapshot(writer);
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
}
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
}
}
@@ -14,7 +14,6 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
@@ -1,14 +1,15 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Event;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public class RoomEventOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomEventOperation));
public RoomEventOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
}
@@ -17,7 +18,7 @@ public class RoomEventOperation : BaseOperation
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
_logger.Warning($"Player {context.Connection.Id} not authorized for this request");
return;
}
@@ -14,24 +14,21 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Logging;
using Ragon.Server.Room;
namespace Ragon.Server.Handler;
public sealed class RoomJoinOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _webHook;
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomJoinOperation));
public RoomJoinOperation(RagonBuffer reader, RagonBuffer writer, RagonWebHookPlugin plugin) : base(reader, writer)
public RoomJoinOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
_webHook = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
@@ -49,8 +46,6 @@ public sealed class RoomJoinOperation : BaseOperation
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
_webHook.RoomJoined(context, existsRoom, player);
JoinSuccess(context, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
@@ -14,50 +14,46 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Room;
namespace Ragon.Server.Handler;
public sealed class RoomJoinOrCreateOperation : BaseOperation
{
private readonly RagonRoomPayload _roomPayload = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomJoinOrCreateOperation));
private readonly RagonRoomParameters _roomParameters = new();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public RoomJoinOrCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin, RagonWebHookPlugin plugin): base(reader, writer)
public RoomJoinOrCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin): base(reader, writer)
{
_serverPlugin = serverPlugin;
_ragonWebHookPlugin = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn("Player not authorized for this request");
_logger.Warning("Player not authorized for this request");
return;
}
var roomId = Guid.NewGuid().ToString();
var lobbyPlayer = context.LobbyPlayer;
_roomPayload.Deserialize(Reader);
_roomParameters.Deserialize(Reader);
if (context.Lobby.FindRoomByScene(_roomPayload.Scene, out var existsRoom))
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
{
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
JoinSuccess(player, existsRoom, Writer);
existsRoom.RestoreBufferedEvents(player);
@@ -68,16 +64,16 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
{
var information = new RoomInformation()
{
Scene = _roomPayload.Scene,
Max = _roomPayload.Max,
Min = _roomPayload.Min,
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
};
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
_serverPlugin.OnRoomCreate(lobbyPlayer, room);
room.Plugin.OnAttached(room);
@@ -14,22 +14,18 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Plugin;
using Ragon.Server.Plugin.Web;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class RoomLeaveOperation: BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _webHook;
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomLeaveOperation));
public RoomLeaveOperation(RagonBuffer reader, RagonBuffer writer, RagonWebHookPlugin plugin): base(reader, writer)
public RoomLeaveOperation(RagonBuffer reader, RagonBuffer writer): base(reader, writer)
{
_webHook = plugin;
}
public override void Handle(RagonContext context, NetworkChannel channel)
@@ -44,8 +40,6 @@ public sealed class RoomLeaveOperation: BaseOperation
plugin.OnPlayerLeaved(roomPlayer);
room.DetachPlayer(roomPlayer);
_webHook.RoomLeaved(context, room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
}
}
@@ -13,16 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class RoomOwnershipOperation : BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomOwnershipOperation));
public RoomOwnershipOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
@@ -14,16 +14,16 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public sealed class RoomUserDataOperation : BaseOperation
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(RoomUserDataOperation));
private readonly int _userDataLimit;
public RoomUserDataOperation(
@@ -39,7 +39,7 @@ public sealed class RoomUserDataOperation : BaseOperation
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
_logger.Warning($"Player {context.Connection.Id} not authorized for this request");
return;
}
@@ -14,15 +14,15 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.IO;
using Ragon.Server.Logging;
namespace Ragon.Server.Handler;
public class SceneLoadOperation: BaseOperation
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadOperation));
public SceneLoadOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer) {}
@@ -35,7 +35,7 @@ public class SceneLoadOperation: BaseOperation
if (roomOwner.Connection.Id != currentPlayer.Connection.Id)
{
_logger.Warn("Only owner can change scene!");
_logger.Warning("Only owner can change scene!");
return;
}
@@ -14,150 +14,151 @@
* limitations under the License.
*/
using NLog;
using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
using Ragon.Server.Room;
namespace Ragon.Server.Handler;
public sealed class SceneLoadedOperation : BaseOperation
namespace Ragon.Server.Handler
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer): base(reader, writer)
public sealed class SceneLoadedOperation : BaseOperation
{
}
public override void Handle(RagonContext context, NetworkChannel channel)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
return;
private readonly IRagonLogger _logger = LoggerManager.GetLogger(nameof(SceneLoadedOperation));
var owner = context.Room.Owner;
var player = context.RoomPlayer;
var room = context.Room;
if (player.IsLoaded)
public SceneLoadedOperation(RagonBuffer reader, RagonBuffer writer) : base(reader, writer)
{
_logger.Warn($"Player {player.Name}:{player.Connection.Id} already ready");
return;
}
if (player == owner)
public override void Handle(RagonContext context, NetworkChannel channel)
{
var statics = Reader.ReadUShort();
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
return;
var owner = context.Room.Owner;
var player = context.RoomPlayer;
var room = context.Room;
if (player.IsLoaded)
{
var entityType = Reader.ReadUShort();
var eventAuthority = (RagonAuthority)Reader.ReadByte();
var staticId = Reader.ReadUShort();
var propertiesCount = Reader.ReadUShort();
_logger.Warning($"Player {player.Name}:{player.Connection.Id} already ready");
return;
}
var entityParameters = new RagonEntityParameters()
if (player == owner)
{
var statics = Reader.ReadUShort();
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
{
Type = entityType,
Authority = eventAuthority,
AttachId = 0,
StaticId = staticId,
BufferedEvents = context.LimitBufferedEvents,
};
var entityType = Reader.ReadUShort();
var eventAuthority = (RagonAuthority)Reader.ReadByte();
var staticId = Reader.ReadUShort();
var propertiesCount = Reader.ReadUShort();
var entity = new RagonEntity(entityParameters);
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
{
var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort();
entity.AddProperty(new RagonProperty(propertySize, propertyType));
var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = 0,
StaticId = staticId,
BufferedEvents = context.LimitBufferedEvents,
};
var entity = new RagonEntity(entityParameters);
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
{
var propertyType = Reader.ReadBool();
var propertySize = Reader.ReadUShort();
entity.AddProperty(new RagonProperty(propertySize, propertyType));
}
var roomPlugin = room.Plugin;
if (!roomPlugin.OnEntityCreate(player, entity)) continue;
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
var entityInfo = $"{entity.Id}:{entity.Type}";
_logger.Trace($"{playerInfo} created static entity {entityInfo}");
entity.Attach(player);
room.AttachEntity(entity);
player.AttachEntity(entity);
}
var roomPlugin = room.Plugin;
if (!roomPlugin.OnEntityCreate(player, entity)) continue;
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
var entityInfo = $"{entity.Id}:{entity.Type}";
_logger.Trace($"{playerInfo} created static entity {entityInfo}");
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
entity.Attach(player);
room.AttachEntity(entity);
player.AttachEntity(entity);
room.WaitPlayersList.Add(player);
foreach (var roomPlayer in room.WaitPlayersList)
{
DispatchPlayerJoinExcludePlayer(room, roomPlayer, Writer);
roomPlayer.SetReady();
}
room.UpdateReadyPlayerList();
DispatchSnapshot(room, room.WaitPlayersList, Writer);
room.WaitPlayersList.Clear();
}
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} loaded");
room.WaitPlayersList.Add(player);
foreach (var roomPlayer in room.WaitPlayersList)
else if (owner.IsLoaded)
{
DispatchPlayerJoinExcludePlayer(room, roomPlayer, Writer);
player.SetReady();
roomPlayer.SetReady();
DispatchPlayerJoinExcludePlayer(room, player, Writer);
room.UpdateReadyPlayerList();
DispatchSnapshot(room, new List<RagonRoomPlayer>() { player }, Writer);
foreach (var entity in room.EntityList)
entity.RestoreBufferedEvents(player, Writer);
}
else
{
_logger.Trace($"Player {player.Connection.Id}|{context.LobbyPlayer.Name} waiting owner of room");
room.WaitPlayersList.Add(player);
}
room.UpdateReadyPlayerList();
DispatchSnapshot(room, room.WaitPlayersList, Writer);
room.WaitPlayersList.Clear();
}
else if (owner.IsLoaded)
private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer)
{
player.SetReady();
writer.Clear();
writer.WriteOperation(RagonOperation.PLAYER_JOINED);
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
DispatchPlayerJoinExcludePlayer(room, player, Writer);
room.UpdateReadyPlayerList();
DispatchSnapshot(room, new List<RagonRoomPlayer>() { player }, Writer);
foreach (var entity in room.EntityList)
entity.RestoreBufferedEvents(player, Writer);
var sendData = writer.ToArray();
foreach (var awaiter in room.ReadyPlayersList)
{
if (awaiter != roomPlayer)
awaiter.Connection.Reliable.Send(sendData);
}
}
else
private void DispatchSnapshot(RagonRoom room, List<RagonRoomPlayer> receviersList, RagonBuffer writer)
{
_logger.Trace($"Player {player.Connection.Id}|{context.LobbyPlayer.Name} waiting owner of room");
room.WaitPlayersList.Add(player);
writer.Clear();
writer.WriteOperation(RagonOperation.SNAPSHOT);
var dynamicEntities = room.DynamicEntitiesList;
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
writer.WriteUShort(dynamicEntitiesCount);
foreach (var entity in dynamicEntities)
entity.Snapshot(writer);
var staticEntities = room.StaticEntitiesList;
var staticEntitiesCount = (ushort)staticEntities.Count;
writer.WriteUShort(staticEntitiesCount);
foreach (var entity in staticEntities)
entity.Snapshot(writer);
var sendData = writer.ToArray();
foreach (var player in receviersList)
player.Connection.Reliable.Send(sendData);
}
}
private void DispatchPlayerJoinExcludePlayer(RagonRoom room, RagonRoomPlayer roomPlayer, RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.PLAYER_JOINED);
writer.WriteUShort(roomPlayer.Connection.Id);
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
var sendData = writer.ToArray();
foreach (var awaiter in room.ReadyPlayersList)
{
if (awaiter != roomPlayer)
awaiter.Connection.Reliable.Send(sendData);
}
}
private void DispatchSnapshot(RagonRoom room, List<RagonRoomPlayer> receviersList, RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.SNAPSHOT);
var dynamicEntities = room.DynamicEntitiesList;
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
writer.WriteUShort(dynamicEntitiesCount);
foreach (var entity in dynamicEntities)
entity.Snapshot(writer);
var staticEntities = room.StaticEntitiesList;
var staticEntitiesCount = (ushort)staticEntities.Count;
writer.WriteUShort(staticEntitiesCount);
foreach (var entity in staticEntities)
entity.Snapshot(writer);
var sendData = writer.ToArray();
foreach (var player in receviersList)
player.Connection.Reliable.Send(sendData);
}
}