🚧 pass-through raw data, refactoring

This commit is contained in:
2023-10-07 19:30:52 +03:00
parent 8788cb0fcf
commit e1a3ea45e2
29 changed files with 434 additions and 219 deletions
@@ -23,20 +23,20 @@ using Ragon.Server.Room;
namespace Ragon.Server.Handler;
public sealed class RoomCreateOperation: IRagonOperation
public sealed class RoomCreateOperation : BaseOperation
{
private readonly RagonRoomParameters _roomParameters = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
public RoomCreateOperation(IServerPlugin serverPlugin, RagonWebHookPlugin ragonWebHook)
public RoomCreateOperation(RagonBuffer reader, RagonBuffer writer, IServerPlugin serverPlugin, RagonWebHookPlugin ragonWebHook) : base(reader, writer)
{
_serverPlugin = serverPlugin;
_ragonWebHookPlugin = ragonWebHook;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
public override void Handle(RagonContext context, byte[] data)
{
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
@@ -44,56 +44,56 @@ public sealed class RoomCreateOperation: IRagonOperation
return;
}
var custom = reader.ReadBool();
var custom = Reader.ReadBool();
var roomId = Guid.NewGuid().ToString();
if (custom)
{
roomId = reader.ReadString();
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();
{
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;
return;
}
}
_roomParameters.Deserialize(reader);
_roomParameters.Deserialize(Reader);
var information = new RoomInformation()
{
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
Scene = _roomParameters.Scene,
Max = _roomParameters.Max,
Min = _roomParameters.Min,
};
var lobbyPlayer = context.LobbyPlayer;
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
roomPlayer.OnAttached(room);
context.Scheduler.Run(room);
context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
_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);
JoinSuccess(roomPlayer, room, Writer);
_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();
@@ -101,8 +101,8 @@ public sealed class RoomCreateOperation: IRagonOperation
writer.WriteString(room.Id);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteUShort((ushort)room.PlayerMin);
writer.WriteUShort((ushort)room.PlayerMax);
writer.WriteString(room.Scene);
var sendData = writer.ToArray();