🚧 plugin system, webhook system

This commit is contained in:
2023-04-09 10:52:18 +04:00
parent f2edc94958
commit bfd6c1b54b
60 changed files with 762 additions and 267 deletions
@@ -16,40 +16,94 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Hander;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class AuthorizationOperation: IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly WebHookPlugin _webHook;
private readonly Configuration _configuration;
private readonly RagonBuffer _writer;
public AuthorizationOperation(
WebHookPlugin webHook,
RagonBuffer writer,
Configuration configuration)
{
_webHook = webHook;
_configuration = configuration;
_writer = writer;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Authorized)
if (context.ConnectionStatus == ConnectionStatus.Authorized)
{
_logger.Warn("Player already authorized");
_logger.Warn("Player already authorized!");
return;
}
if (context.ConnectionStatus == ConnectionStatus.InProcess)
{
_logger.Warn("Player already request authorization!");
return;
}
var key = reader.ReadString();
var playerName = reader.ReadString();
var additionalPayload = new RagonPayload();
additionalPayload.Read(reader);
context.LobbyPlayer.Name = playerName;
context.LobbyPlayer.AdditionalData = Array.Empty<byte>();
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized;
var name = reader.ReadString();
var payload = reader.ReadString();
if (key == _configuration.ServerKey)
{
if (_webHook.RequestAuthorization(context, name, payload))
return;
var lobbyPlayer = new RagonLobbyPlayer(Guid.NewGuid().ToString(), name, payload);
context.SetPlayer(lobbyPlayer);
Approve(context);
}
else
{
Reject(context);
}
}
public void Approve(RagonContext context)
{
context.ConnectionStatus = ConnectionStatus.Authorized;
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);
writer.Clear();
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
writer.WriteString(playerId);
writer.WriteString(playerName);
var sendData = writer.ToArray();
var sendData = _writer.ToArray();
context.Connection.Reliable.Send(sendData);
_logger.Trace($"Connection {context.Connection.Id} as {playerId}|{context.LobbyPlayer.Name} authorized");
_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}");
}
}
@@ -16,12 +16,13 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Entity;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class EntityCreateOperation : IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
@@ -32,7 +33,15 @@ public sealed class EntityCreateOperation : IRagonOperation
var eventAuthority = (RagonAuthority) reader.ReadByte();
var propertiesCount = reader.ReadUShort();
var entity = new RagonEntity(player, entityType, 0, attachId, eventAuthority);
var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = attachId,
StaticId = 0
};
var entity = new RagonEntity(entityParameters);
for (var i = 0; i < propertiesCount; i++)
{
var propertyType = reader.ReadBool();
@@ -40,13 +49,18 @@ public sealed class EntityCreateOperation : IRagonOperation
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
}
if (reader.Capacity > 0)
entity.Payload.Read(reader);
var roomPlugin = room.Plugin;
if (!roomPlugin.OnEntityCreate(player, entity))
return;
entity.Attach(player);
room.AttachEntity(entity);
player.AttachEntity(entity);
entity.Create();
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
@@ -16,12 +16,13 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Hander;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class EntityEventOperation : IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
@@ -16,12 +16,13 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Hander;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class EntityDestroyOperation: IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
@@ -17,29 +17,25 @@
using NLog;
using Ragon.Protocol;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class EntityStateOperation: IRagonOperation
{
private ILogger _logger = LogManager.GetCurrentClassLogger();
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
var room = context.Room;
var player = context.RoomPlayer;
var entitiesCount = reader.ReadUShort();
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
{
var entityId = reader.ReadUShort();
if (room.Entities.TryGetValue(entityId, out var entity) && entity.Owner.Connection.Id == context.Connection.Id)
{
entity.State.Read(reader);
room.Track(entity);
}
if (room.Entities.TryGetValue(entityId, out var entity))
entity.Read(player, reader);
else
{
_logger.Error($"Entity with Id {entityId} not found, replication interrupted");
}
}
}
}
@@ -0,0 +1,24 @@
/*
* 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.Server.Handler;
public interface IRagonOperation
{
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer);
}
@@ -16,17 +16,28 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Lobby;
using Ragon.Server.Plugin;
using Ragon.Server.Room;
namespace Ragon.Server;
namespace Ragon.Server.Hander;
public sealed class RoomCreateOperation: IRagonOperation
{
private RagonRoomParameters _roomParameters = new();
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonRoomParameters _roomParameters = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly WebHookPlugin _webHookPlugin;
public RoomCreateOperation(IServerPlugin serverPlugin, WebHookPlugin webHook)
{
_serverPlugin = serverPlugin;
_webHookPlugin = webHook;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
return;
@@ -62,17 +73,20 @@ public sealed class RoomCreateOperation: IRagonOperation
};
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);
var room = new RagonRoom(roomId, information);
context.Scheduler.Run(room);
context.Lobby.Persist(room);
context.SetRoom(room, roomPlayer);
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(room, player);
_webHookPlugin.RoomCreated(context, room);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with map {information.Map}");
JoinSuccess(player, room, writer);
JoinSuccess(roomPlayer, room, writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
}
@@ -84,9 +98,9 @@ public sealed class RoomCreateOperation: IRagonOperation
writer.WriteString(room.Id);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min);
writer.WriteUShort((ushort) room.Info.Max);
writer.WriteString(room.Info.Map);
writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteString(room.Map);
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
@@ -16,12 +16,21 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Web;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class RoomJoinOperation : IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly WebHookPlugin _webHookPlugin;
public RoomJoinOperation(IServerPlugin serverPlugin, WebHookPlugin plugin)
{
_serverPlugin = serverPlugin;
_webHookPlugin = plugin;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
@@ -30,42 +39,47 @@ public sealed class RoomJoinOperation : IRagonOperation
if (!context.Lobby.FindRoomById(roomId, out var existsRoom))
{
JoinFailed(lobbyPlayer, writer);
JoinFailed(context, writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}");
return;
}
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
JoinSuccess(player, existsRoom, writer);
if (!_serverPlugin.OnRoomJoin(player, existsRoom))
return;
_webHookPlugin.RoomJoined(context, existsRoom, player);
JoinSuccess(context, existsRoom, writer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
}
private void JoinSuccess(RagonRoomPlayer player, RagonRoom room, RagonBuffer writer)
private void JoinSuccess(RagonContext context, RagonRoom room, RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
writer.WriteString(room.Id);
writer.WriteString(player.Id);
writer.WriteString(context.RoomPlayer.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min);
writer.WriteUShort((ushort) room.Info.Max);
writer.WriteString(room.Info.Map);
writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteString(room.Map);
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
context.Connection.Reliable.Send(sendData);
}
private void JoinFailed(RagonLobbyPlayer player, RagonBuffer writer)
private void JoinFailed(RagonContext context, RagonBuffer writer)
{
writer.Clear();
writer.WriteOperation(RagonOperation.JOIN_FAILED);
writer.WriteString($"Room not exists");
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
context.Connection.Reliable.Send(sendData);
}
}
@@ -16,17 +16,26 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Web;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class RoomJoinOrCreateOperation : IRagonOperation
{
private RagonRoomParameters _roomParameters = new();
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonRoomParameters _roomParameters = new();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly WebHookPlugin _webHookPlugin;
public RoomJoinOrCreateOperation(IServerPlugin serverPlugin, WebHookPlugin plugin)
{
_serverPlugin = serverPlugin;
_webHookPlugin = plugin;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
{
_logger.Warn("Player not authorized for this request");
return;
@@ -39,9 +48,11 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
if (context.Lobby.FindRoomByMap(_roomParameters.Map, out var existsRoom))
{
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var player = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(existsRoom, player);
_webHookPlugin.RoomJoined(context, existsRoom, player);
JoinSuccess(player, existsRoom, writer);
}
else
@@ -53,14 +64,17 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
Min = _roomParameters.Min,
};
var room = new RagonRoom(roomId, information);
var roomPlayer = new RagonRoomPlayer(context.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
var roomPlugin = _serverPlugin.CreateRoomPlugin(information);
var room = new RagonRoom(roomId, information, roomPlugin);
_webHookPlugin.RoomCreated(context, room);
context.Lobby.Persist(room);
context.Scheduler.Run(room);
var roomPlayer = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
context.SetRoom(room, roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} {information}");
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} create room {room.Id} with map {information.Map}");
JoinSuccess(roomPlayer, room, writer);
}
@@ -73,9 +87,9 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
writer.WriteString(room.Id);
writer.WriteString(player.Id);
writer.WriteString(room.Owner.Id);
writer.WriteUShort((ushort) room.Info.Min);
writer.WriteUShort((ushort) room.Info.Max);
writer.WriteString(room.Info.Map);
writer.WriteUShort((ushort) room.PlayerMin);
writer.WriteUShort((ushort) room.PlayerMax);
writer.WriteString(room.Map);
var sendData = writer.ToArray();
player.Connection.Reliable.Send(sendData);
@@ -16,18 +16,30 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Plugin;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class RoomLeaveOperation: IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly IServerPlugin _serverPlugin;
private readonly WebHookPlugin _webHookPlugin;
public RoomLeaveOperation(IServerPlugin serverPlugin, WebHookPlugin plugin)
{
_serverPlugin = serverPlugin;
_webHookPlugin = plugin;
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
var room = context.Room;
var roomPlayer = context.RoomPlayer;
if (room != null)
{
{
_serverPlugin.OnRoomLeave(roomPlayer, room);
_webHookPlugin.RoomLeaved(context, room, roomPlayer);
context.Room?.DetachPlayer(roomPlayer);
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
}
@@ -18,11 +18,11 @@
using NLog;
using Ragon.Protocol;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public class SceneLoadOperation: IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
@@ -16,16 +16,23 @@
using NLog;
using Ragon.Protocol;
using Ragon.Server.Entity;
using Ragon.Server.Room;
namespace Ragon.Server;
namespace Ragon.Server.Handler;
public sealed class SceneLoadedOperation : IRagonOperation
{
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public SceneLoadedOperation()
{
}
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
return;
var owner = context.Room.Owner;
@@ -34,6 +41,7 @@ public sealed class SceneLoadedOperation : IRagonOperation
if (player == owner)
{
var statics = reader.ReadUShort();
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
{
@@ -41,20 +49,32 @@ public sealed class SceneLoadedOperation : IRagonOperation
var eventAuthority = (RagonAuthority)reader.ReadByte();
var staticId = reader.ReadUShort();
var propertiesCount = reader.ReadUShort();
var entity = new RagonEntity(player, entityType, staticId, 0, eventAuthority);
var entityParameters = new RagonEntityParameters()
{
Type = entityType,
Authority = eventAuthority,
AttachId = 0,
StaticId = staticId,
};
var entity = new RagonEntity(entityParameters);
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
{
var propertyType = reader.ReadBool();
var propertySize = reader.ReadUShort();
entity.State.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}";
var playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
var entityInfo = $"{entity.Id}:{entity.Type}";
_logger.Trace($"{playerInfo} created entity {entityInfo}");
entity.Attach(player);
room.AttachEntity(entity);
player.AttachEntity(entity);
}
@@ -123,7 +143,7 @@ public sealed class SceneLoadedOperation : IRagonOperation
writer.WriteString(roomPlayer.Id);
writer.WriteString(roomPlayer.Name);
}
var dynamicEntities = room.DynamicEntitiesList;
var dynamicEntitiesCount = (ushort)dynamicEntities.Count;
writer.WriteUShort(dynamicEntitiesCount);
@@ -135,7 +155,7 @@ public sealed class SceneLoadedOperation : IRagonOperation
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);