feat: multi project support, maintaince

This commit is contained in:
2025-10-04 15:08:53 +03:00
parent 5136f08dab
commit e78e8048ff
18 changed files with 152 additions and 66 deletions
@@ -19,6 +19,7 @@ using Ragon.Server.IO;
using Ragon.Server.Lobby;
using Ragon.Server.Logging;
using Ragon.Server.Plugin;
using Ragon.Server.Project;
namespace Ragon.Server.Handler
{
@@ -30,19 +31,22 @@ namespace Ragon.Server.Handler
private readonly RagonContextObserver _observer;
private readonly RagonServerConfiguration _configuration;
private readonly RagonBuffer _writer;
private readonly ProjectRegistry _projectRegistry;
public AuthorizationOperation(RagonBuffer reader,
RagonBuffer writer,
IRagonServer server,
IServerPlugin serverPlugin,
RagonContextObserver observer,
RagonServerConfiguration configuration) : base(reader, writer)
RagonServerConfiguration configuration,
ProjectRegistry projectRegistry) : base(reader, writer)
{
_serverPlugin = serverPlugin;
_configuration = configuration;
_observer = observer;
_writer = writer;
_server = server;
_projectRegistry = projectRegistry;
}
public override void Handle(RagonContext context, NetworkChannel channel)
@@ -59,31 +63,45 @@ namespace Ragon.Server.Handler
return;
}
var configuration = _configuration;
var key = Reader.ReadString();
var projectKey = Reader.ReadString();
var name = Reader.ReadString();
var payload = Reader.ReadString();
if (key == configuration.ServerKey)
if (!_projectRegistry.ValidateKey(projectKey))
{
var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaPlugin)
return;
var id = Guid.NewGuid().ToString();
Approve(context, new ConnectionResponse(id, name, payload));
}
else
{
_logger.Warning($"Invalid key for connection {context.Connection.Id}");
_logger.Warning($"Invalid project key from connection {context.Connection.Id}");
Reject(context);
return;
}
if (!_projectRegistry.CanConnect(projectKey))
{
_logger.Warning($"Connection limit reached for project key: {projectKey}");
Reject(context);
return;
}
var authorizeViaPlugin = _serverPlugin.OnAuthorize(new ConnectionRequest(_server, context.Connection.Id, payload));
if (authorizeViaPlugin)
return;
var project = _projectRegistry.GetOrCreateProject(projectKey);
if (project == null)
{
_logger.Warning($"Failed to create project for key: {projectKey}");
Reject(context);
return;
}
var id = Guid.NewGuid().ToString();
Approve(context, new ConnectionResponse(id, name, payload), project.Id);
_projectRegistry.RegisterConnection(project.Id);
}
public void Approve(RagonContext context, ConnectionResponse result)
public void Approve(RagonContext context, ConnectionResponse result, int projectId)
{
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload);
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, result.Id, result.Name, result.Payload, projectId);
context.SetPlayer(lobbyPlayer);
context.ConnectionStatus = ConnectionStatus.Authorized;
@@ -102,7 +120,7 @@ namespace Ragon.Server.Handler
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name}");
_logger.Trace($"Approved {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} for project {projectId}");
}
public void Reject(RagonContext context)
@@ -27,7 +27,7 @@ namespace Ragon.Server.Handler
return;
}
context.UserData.Read(Reader);
context.UserData.Read(Reader, _userDataLimit);
}
}
}
@@ -88,7 +88,7 @@ namespace Ragon.Server.Handler
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
var room = new RagonRoom(roomId, information, roomPlugin, lobbyPlayer.ProjectId);
room.Plugin.OnAttached(room);
roomPlayer.OnAttached(room);
@@ -43,6 +43,13 @@ public sealed class RoomJoinOperation : BaseOperation
return;
}
if (existsRoom.ProjectId != lobbyPlayer.ProjectId)
{
JoinFailed(context, Writer);
_logger.Warning($"Player {context.Connection.Id}|{lobbyPlayer.Name} tried to join room from different project");
return;
}
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
@@ -57,6 +57,12 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
if (context.Lobby.FindRoomByScene(_roomParameters.Scene, out var existsRoom))
{
if (existsRoom.ProjectId != lobbyPlayer.ProjectId)
{
_logger.Warning($"Player {context.Connection.Id}|{lobbyPlayer.Name} tried to join room from different project");
return;
}
var player = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
@@ -81,7 +87,7 @@ public sealed class RoomJoinOrCreateOperation : BaseOperation
var roomPlayer = new RagonRoomPlayer(context, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
var room = new RagonRoom(roomId, information, roomPlugin, lobbyPlayer.ProjectId);
_serverPlugin.OnRoomCreate(lobbyPlayer, room);
@@ -45,6 +45,6 @@ public sealed class RoomUserDataOperation : BaseOperation
var room = context.Room;
if (room != null)
room.UserData.Read(Reader);
room.UserData.Read(Reader, _userDataLimit);
}
}