feat: replication level
This commit is contained in:
@@ -7,6 +7,7 @@ namespace Ragon.Common
|
|||||||
AUTHORIZED_FAILED,
|
AUTHORIZED_FAILED,
|
||||||
|
|
||||||
JOIN_OR_CREATE_ROOM,
|
JOIN_OR_CREATE_ROOM,
|
||||||
|
CREATE_ROOM,
|
||||||
JOIN_ROOM,
|
JOIN_ROOM,
|
||||||
LEAVE_ROOM,
|
LEAVE_ROOM,
|
||||||
OWNERSHIP_CHANGED,
|
OWNERSHIP_CHANGED,
|
||||||
|
|||||||
@@ -94,8 +94,6 @@ namespace Ragon.Core
|
|||||||
_allPlayers = _players.Select(p => p.Key).ToArray();
|
_allPlayers = _players.Select(p => p.Key).ToArray();
|
||||||
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
_readyPlayers = _players.Where(p => p.Value.IsLoaded).Select(p => p.Key).ToArray();
|
||||||
|
|
||||||
var isOwnershipChange = player.PeerId == _owner;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
_plugin.OnPlayerLeaved(player);
|
_plugin.OnPlayerLeaved(player);
|
||||||
|
|
||||||
@@ -114,34 +112,12 @@ namespace Ragon.Core
|
|||||||
Broadcast(_readyPlayers, sendData);
|
Broadcast(_readyPlayers, sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_allPlayers.Length > 0 && isOwnershipChange)
|
|
||||||
{
|
|
||||||
var newRoomOwnerId = _allPlayers[0];
|
|
||||||
var newRoomOwner = _players[newRoomOwnerId];
|
|
||||||
|
|
||||||
_owner = newRoomOwnerId;
|
|
||||||
|
|
||||||
{
|
|
||||||
_plugin.OnOwnershipChanged(newRoomOwner);
|
|
||||||
|
|
||||||
_serializer.Clear();
|
|
||||||
_serializer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
|
||||||
_serializer.WriteString(newRoomOwner.Id);
|
|
||||||
|
|
||||||
var sendData = _serializer.ToArray();
|
|
||||||
Broadcast(_readyPlayers, sendData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_entitiesAll = _entities.Values.ToArray();
|
_entitiesAll = _entities.Values.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessEvent(uint peerId, ReadOnlySpan<byte> rawData)
|
public void ProcessEvent(uint peerId, RagonOperation operation, ReadOnlySpan<byte> payloadRawData)
|
||||||
{
|
{
|
||||||
var operation = (RagonOperation) rawData[0];
|
|
||||||
var payloadRawData = rawData.Slice(1, rawData.Length - 1);
|
|
||||||
|
|
||||||
_serializer.Clear();
|
_serializer.Clear();
|
||||||
_serializer.FromSpan(ref payloadRawData);
|
_serializer.FromSpan(ref payloadRawData);
|
||||||
|
|
||||||
@@ -193,6 +169,21 @@ namespace Ragon.Core
|
|||||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case RagonOperation.LOAD_SCENE:
|
||||||
|
{
|
||||||
|
var sceneName = _serializer.ReadString();
|
||||||
|
_readyPlayers = Array.Empty<uint>();
|
||||||
|
_entitiesAll = Array.Empty<Entity>();
|
||||||
|
_entities.Clear();
|
||||||
|
|
||||||
|
_serializer.Clear();
|
||||||
|
_serializer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||||
|
_serializer.WriteString(sceneName);
|
||||||
|
|
||||||
|
var sendData = _serializer.ToArray();
|
||||||
|
Broadcast(_allPlayers, sendData, DeliveryType.Reliable);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case RagonOperation.REPLICATE_EVENT:
|
case RagonOperation.REPLICATE_EVENT:
|
||||||
{
|
{
|
||||||
var evntId = _serializer.ReadUShort();
|
var evntId = _serializer.ReadUShort();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using Ragon.Common;
|
||||||
using ENet;
|
using ENet;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
@@ -29,10 +29,10 @@ namespace Ragon.Core
|
|||||||
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
||||||
var dispatcher = new Dispatcher();
|
var dispatcher = new Dispatcher();
|
||||||
_dispatcherInternal = dispatcher;
|
_dispatcherInternal = dispatcher;
|
||||||
|
|
||||||
ThreadDispatcher = dispatcher;
|
ThreadDispatcher = dispatcher;
|
||||||
Server = new ENetServer(this);
|
Server = new ENetServer(this);
|
||||||
|
|
||||||
_deltaTime = 1000.0f / configuration.SendRate;
|
_deltaTime = 1000.0f / configuration.SendRate;
|
||||||
|
|
||||||
_roomManager = new RoomManager(factory, this);
|
_roomManager = new RoomManager(factory, this);
|
||||||
@@ -69,7 +69,7 @@ namespace Ragon.Core
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Server.Process();
|
Server.Process();
|
||||||
|
|
||||||
_dispatcherInternal.Process();
|
_dispatcherInternal.Process();
|
||||||
|
|
||||||
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
|
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
|
||||||
@@ -109,14 +109,13 @@ namespace Ragon.Core
|
|||||||
evnt.Packet.CopyTo(dataRaw);
|
evnt.Packet.CopyTo(dataRaw);
|
||||||
|
|
||||||
var data = new ReadOnlySpan<byte>(dataRaw);
|
var data = new ReadOnlySpan<byte>(dataRaw);
|
||||||
|
var operation = (RagonOperation) data[0];
|
||||||
|
var payload = data.Slice(1, data.Length - 1);
|
||||||
|
|
||||||
if (_roomManager.RoomsBySocket.TryGetValue(peerId, out var room))
|
if (_roomManager.RoomsBySocket.TryGetValue(peerId, out var room))
|
||||||
{
|
room.ProcessEvent(peerId, operation, payload);
|
||||||
room.ProcessEvent(peerId, data);
|
|
||||||
}
|
_lobby.ProcessEvent(peerId, operation, payload);
|
||||||
else
|
|
||||||
{
|
|
||||||
_lobby.ProcessEvent(peerId, data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using NLog;
|
||||||
using Ragon.Common;
|
using Ragon.Common;
|
||||||
|
|
||||||
namespace Ragon.Core;
|
namespace Ragon.Core;
|
||||||
|
|
||||||
public class Lobby : ILobby
|
public class Lobby : ILobby
|
||||||
{
|
{
|
||||||
|
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly RagonSerializer _serializer;
|
private readonly RagonSerializer _serializer;
|
||||||
private readonly RoomManager _roomManager;
|
private readonly RoomManager _roomManager;
|
||||||
private readonly AuthorizationManager _authorizationManager;
|
private readonly AuthorizationManager _authorizationManager;
|
||||||
@@ -19,30 +21,50 @@ public class Lobby : ILobby
|
|||||||
_authorizationManager = new AuthorizationManager(provider, gameThread, this, _serializer);
|
_authorizationManager = new AuthorizationManager(provider, gameThread, this, _serializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessEvent(uint peerId, ReadOnlySpan<byte> data)
|
public void ProcessEvent(uint peerId, RagonOperation op, ReadOnlySpan<byte> payload)
|
||||||
{
|
{
|
||||||
var op = (RagonOperation) data[0];
|
|
||||||
var payload = data.Slice(1, data.Length - 1);
|
|
||||||
|
|
||||||
_serializer.Clear();
|
_serializer.Clear();
|
||||||
_serializer.FromSpan(ref payload);
|
_serializer.FromSpan(ref payload);
|
||||||
|
|
||||||
|
if (op == RagonOperation.AUTHORIZE)
|
||||||
|
{
|
||||||
|
var key = _serializer.ReadString();
|
||||||
|
var playerName = _serializer.ReadString();
|
||||||
|
var protocol = _serializer.ReadByte();
|
||||||
|
_authorizationManager.OnAuthorization(peerId, key, playerName, protocol);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var player = _authorizationManager.GetPlayer(peerId);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
_logger.Warn($"Peer not authorized {peerId} trying to {op}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (op)
|
switch (op)
|
||||||
{
|
{
|
||||||
case RagonOperation.AUTHORIZE:
|
|
||||||
{
|
|
||||||
var key = _serializer.ReadString();
|
|
||||||
var playerName = _serializer.ReadString();
|
|
||||||
var protocol = _serializer.ReadByte();
|
|
||||||
_authorizationManager.OnAuthorization(peerId, key, playerName, protocol);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case RagonOperation.JOIN_ROOM:
|
case RagonOperation.JOIN_ROOM:
|
||||||
{
|
{
|
||||||
var roomId = _serializer.ReadString();
|
var roomId = _serializer.ReadString();
|
||||||
var player = _authorizationManager.GetPlayer(peerId);
|
|
||||||
if (player != null)
|
if (_roomManager.RoomsBySocket.ContainsKey(peerId))
|
||||||
_roomManager.Join(player, roomId, Array.Empty<byte>());
|
_roomManager.Left(player, Array.Empty<byte>());
|
||||||
|
|
||||||
|
_roomManager.Join(player, roomId, Array.Empty<byte>());
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonOperation.CREATE_ROOM:
|
||||||
|
{
|
||||||
|
var min = _serializer.ReadUShort();
|
||||||
|
var max = _serializer.ReadUShort();
|
||||||
|
var map = _serializer.ReadString();
|
||||||
|
|
||||||
|
if (_roomManager.RoomsBySocket.ContainsKey(peerId))
|
||||||
|
_roomManager.Left(player, Array.Empty<byte>());
|
||||||
|
|
||||||
|
_roomManager.Create(player, map, min, max, Array.Empty<byte>());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RagonOperation.JOIN_OR_CREATE_ROOM:
|
case RagonOperation.JOIN_OR_CREATE_ROOM:
|
||||||
@@ -50,16 +72,16 @@ public class Lobby : ILobby
|
|||||||
var min = _serializer.ReadUShort();
|
var min = _serializer.ReadUShort();
|
||||||
var max = _serializer.ReadUShort();
|
var max = _serializer.ReadUShort();
|
||||||
var map = _serializer.ReadString();
|
var map = _serializer.ReadString();
|
||||||
var player = _authorizationManager.GetPlayer(peerId);
|
|
||||||
if (player != null)
|
if (_roomManager.RoomsBySocket.ContainsKey(peerId))
|
||||||
_roomManager.JoinOrCreate(player, map, min, max, Array.Empty<byte>());
|
_roomManager.Left(player, Array.Empty<byte>());
|
||||||
|
|
||||||
|
_roomManager.JoinOrCreate(player, map, min, max, Array.Empty<byte>());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RagonOperation.LEAVE_ROOM:
|
case RagonOperation.LEAVE_ROOM:
|
||||||
{
|
{
|
||||||
var player = _authorizationManager.GetPlayer(peerId);
|
_roomManager.Left(player, Array.Empty<byte>());
|
||||||
if (player != null)
|
|
||||||
_roomManager.Left(player, Array.Empty<byte>());
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,20 @@ public class RoomManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Create(Player player, string map, int min, int max, byte[] payload)
|
||||||
|
{
|
||||||
|
var plugin = _factory.CreatePlugin(map);
|
||||||
|
if (plugin == null)
|
||||||
|
throw new NullReferenceException($"Plugin for map {map} is null");
|
||||||
|
|
||||||
|
var room = new GameRoom(_gameThread, plugin, map, min, max);
|
||||||
|
room.Joined(player, payload);
|
||||||
|
room.Start();
|
||||||
|
|
||||||
|
_roomsBySocket.Add(player.PeerId, room);
|
||||||
|
_rooms.Add(room);
|
||||||
|
}
|
||||||
|
|
||||||
public void JoinOrCreate(Player player, string map, int min, int max, byte[] payload)
|
public void JoinOrCreate(Player player, string map, int min, int max, byte[] payload)
|
||||||
{
|
{
|
||||||
if (_rooms.Count > 0)
|
if (_rooms.Count > 0)
|
||||||
@@ -77,6 +91,8 @@ public class RoomManager
|
|||||||
room.Stop();
|
room.Stop();
|
||||||
_rooms.Remove(room);
|
_rooms.Remove(room);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_gameThread.Server.Send(player.PeerId, new byte[] { (byte) RagonOperation.LEAVE_ROOM }, DeliveryType.Reliable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user