added transfer ownership, limit buffered events

This commit is contained in:
2023-07-01 07:47:57 +03:00
parent 20662ae24d
commit 105457ffa0
24 changed files with 134 additions and 108 deletions
+17 -14
View File
@@ -20,7 +20,7 @@ using Ragon.Server.Room;
namespace Ragon.Server.Entity;
public class RagonEntity: IRagonEntity
public class RagonEntity : IRagonEntity
{
private static ushort _idGenerator = 100;
public ushort Id { get; private set; }
@@ -31,24 +31,26 @@ public class RagonEntity: IRagonEntity
public RagonAuthority Authority { get; private set; }
public RagonPayload Payload { get; private set; }
public IRagonEntityState State => _state;
private readonly List<RagonEvent> _bufferedEvents;
private readonly int _limitBufferedEvents;
private readonly RagonEntityState _state;
public RagonEntity(RagonEntityParameters parameters)
{
Id = _idGenerator++;
StaticId = parameters.StaticId;
Type = parameters.Type;
AttachId = parameters.AttachId;
Authority = parameters.Authority;
Payload = new RagonPayload();
_state = new RagonEntityState(this);
_bufferedEvents = new List<RagonEvent>();
_limitBufferedEvents = parameters.BufferedEvents;
}
public void Attach(RagonRoomPlayer owner)
{
Owner = owner;
@@ -56,8 +58,7 @@ public class RagonEntity: IRagonEntity
public void Detach()
{
}
}
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer)
{
@@ -88,7 +89,7 @@ public class RagonEntity: IRagonEntity
buffer.WriteUShort(Type);
buffer.WriteUShort(Id);
buffer.WriteUShort(Owner.Connection.Id);
Payload.Write(buffer);
var sendData = buffer.ToArray();
@@ -122,7 +123,7 @@ public class RagonEntity: IRagonEntity
buffer.WriteUShort(Payload.Size);
Payload.Write(buffer);
_state.Snapshot(buffer);
}
@@ -163,7 +164,9 @@ public class RagonEntity: IRagonEntity
return;
}
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner)
if (eventMode == RagonReplicationMode.Buffered &&
targetMode != RagonTarget.Owner &&
_bufferedEvents.Count < _limitBufferedEvents)
{
_bufferedEvents.Add(evnt);
}
@@ -221,7 +224,7 @@ public class RagonEntity: IRagonEntity
{
_state.AddProperty(property);
}
public void WriteState(RagonBuffer writer)
{
_state.Write(writer);
@@ -231,9 +234,9 @@ public class RagonEntity: IRagonEntity
{
if (Owner.Connection.Id != player.Connection.Id)
return false;
_state.Read(reader);
return true;
}
}
@@ -24,4 +24,5 @@ public ref struct RagonEntityParameters
public ushort StaticId;
public ushort AttachId;
public RagonAuthority Authority;
public int BufferedEvents;
}
@@ -28,16 +28,14 @@ public sealed class AuthorizationOperation: IRagonOperation
private Logger _logger = LogManager.GetCurrentClassLogger();
private readonly RagonWebHookPlugin _ragonWebHook;
private readonly RagonContextObserver _contextObserver;
private readonly Configuration _configuration;
private readonly RagonBuffer _writer;
public AuthorizationOperation(RagonWebHookPlugin ragonWebHook,
public AuthorizationOperation(
RagonWebHookPlugin ragonWebHook,
RagonContextObserver contextObserver,
RagonBuffer writer,
Configuration configuration)
RagonBuffer writer)
{
_ragonWebHook = ragonWebHook;
_configuration = configuration;
_contextObserver = contextObserver;
_writer = writer;
}
@@ -55,12 +53,13 @@ public sealed class AuthorizationOperation: IRagonOperation
_logger.Warn("Player already request authorization!");
return;
}
var configuration = context.Configuration;
var key = reader.ReadString();
var name = reader.ReadString();
var payload = reader.ReadString();
if (key == _configuration.ServerKey)
if (key == configuration.ServerKey)
{
if (_ragonWebHook.RequestAuthorization(context, name, payload))
return;
@@ -38,7 +38,8 @@ public sealed class EntityCreateOperation : IRagonOperation
Type = entityType,
Authority = eventAuthority,
AttachId = attachId,
StaticId = 0
StaticId = 0,
BufferedEvents = context.Configuration.LimitBufferedEvents,
};
var entity = new RagonEntity(entityParameters);
@@ -9,11 +9,11 @@ public sealed class EntityOwnershipOperation : IRagonOperation
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
{
var player = context.RoomPlayer;
var currentOwner = context.RoomPlayer;
var room = context.Room;
var entityId = reader.ReadUShort();
var playerId = reader.ReadUShort();
var playerPeerId = reader.ReadUShort();
if (!room.Entities.TryGetValue(entityId, out var entity))
{
@@ -21,29 +21,33 @@ public sealed class EntityOwnershipOperation : IRagonOperation
return;
}
if (entity.Owner.Connection.Id != player.Connection.Id)
if (entity.Owner.Connection.Id != currentOwner.Connection.Id)
{
_logger.Error($"Player not owner of entity with id {entityId}");
return;
}
if (!room.Players.TryGetValue(playerId, out var nextOwner))
if (!room.Players.TryGetValue(playerPeerId, out var nextOwner))
{
_logger.Error($"Player not found with id {entityId}");
return;
}
writer.Clear();
writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
writer.WriteUShort(nextOwner.Connection.Id);
writer.WriteUShort(1);
writer.WriteUShort(entity.Id);
player.Entities.Remove(entity);
currentOwner.Entities.Remove(entity);
nextOwner.Entities.Add(entity);
entity.Attach(nextOwner);
_logger.Trace($"Entity {entity.Id} next owner {nextOwner.Connection.Id}");
writer.Clear();
writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
writer.WriteUShort(playerPeerId);
writer.WriteUShort(1);
writer.WriteUShort(entity.Id);
var sendData = writer.ToArray();
foreach (var player in room.PlayerList)
player.Connection.Reliable.Send(sendData);
}
}
+1 -1
View File
@@ -91,7 +91,7 @@ public class RagonHttpServer
}
}
public void Start(Configuration configuration)
public void Start(RagonServerConfiguration configuration)
{
_cancellationTokenSource = new CancellationTokenSource();
_logger.Info($"Listen at http://0.0.0.0:{configuration.HttpPort}/");
@@ -31,7 +31,7 @@ public class RagonWebHookPlugin
private RagonServer _server;
private HttpClient _httpClient;
public RagonWebHookPlugin(RagonServer server, Configuration configuration)
public RagonWebHookPlugin(RagonServer server, RagonServerConfiguration configuration)
{
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
_httpClient = new HttpClient();
+3 -1
View File
@@ -26,7 +26,7 @@ public class RagonContext
public ConnectionStatus ConnectionStatus { get; set; }
public INetworkConnection Connection { get; }
public IExecutor Executor { get; private set; }
public RagonServerConfiguration Configuration { get; private set; }
public IRagonLobby Lobby { get; private set; }
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
@@ -37,11 +37,13 @@ public class RagonContext
public RagonContext(
INetworkConnection connection,
RagonServerConfiguration configuration,
IExecutor executor,
IRagonLobby lobby,
RagonScheduler scheduler)
{
ConnectionStatus = ConnectionStatus.Unauthorized;
Configuration = configuration;
Connection = connection;
Executor = executor;
Lobby = lobby;
+4 -4
View File
@@ -36,7 +36,7 @@ public class RagonServer : IRagonServer, INetworkListener
private readonly IServerPlugin _serverPlugin;
private readonly Thread _dedicatedThread;
private readonly Executor _executor;
private readonly Configuration _configuration;
private readonly RagonServerConfiguration _configuration;
private readonly RagonWebHookPlugin _webhooks;
private readonly RagonHttpServer _httpServer;
private readonly RagonBuffer _reader;
@@ -50,7 +50,7 @@ public class RagonServer : IRagonServer, INetworkListener
public RagonServer(
INetworkServer server,
IServerPlugin plugin,
Configuration configuration)
RagonServerConfiguration configuration)
{
_server = server;
_executor = _server.Executor;
@@ -74,7 +74,7 @@ public class RagonServer : IRagonServer, INetworkListener
_serverPlugin.OnAttached(this);
_handlers = new IRagonOperation[byte.MaxValue];
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer, configuration);
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer);
_handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation(plugin, _webhooks);
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_webhooks);
@@ -136,7 +136,7 @@ public class RagonServer : IRagonServer, INetworkListener
public void OnConnected(INetworkConnection connection)
{
var context = new RagonContext(connection, _executor, _lobby, _scheduler);
var context = new RagonContext(connection, _configuration, _executor, _lobby, _scheduler);
_logger.Trace($"Connected: {connection.Id}");
_contextsByConnection.Add(connection.Id, context);
@@ -31,7 +31,7 @@ public class WebHook
}
[Serializable]
public struct Configuration
public struct RagonServerConfiguration
{
public string ServerKey;
public string ServerType;
@@ -43,6 +43,7 @@ public struct Configuration
public int LimitConnections;
public int LimitPlayersPerRoom;
public int LimitRooms;
public int LimitBufferedEvents;
public Dictionary<string, string> WebHooks;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
@@ -53,12 +54,12 @@ public struct Configuration
{"websocket", Server.ServerType.WEBSOCKET}
};
public static Configuration Load(string filePath)
public static RagonServerConfiguration Load(string filePath)
{
CopyrightInfo();
var data = File.ReadAllText(filePath);
var configuration = JsonConvert.DeserializeObject<Configuration>(data);
var configuration = JsonConvert.DeserializeObject<RagonServerConfiguration>(data);
return configuration;
}