feat: replication level
This commit is contained in:
@@ -94,8 +94,6 @@ namespace Ragon.Core
|
||||
_allPlayers = _players.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);
|
||||
|
||||
@@ -114,34 +112,12 @@ namespace Ragon.Core
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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.FromSpan(ref payloadRawData);
|
||||
|
||||
@@ -193,6 +169,21 @@ namespace Ragon.Core
|
||||
Broadcast(_readyPlayers, sendData, DeliveryType.Reliable);
|
||||
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:
|
||||
{
|
||||
var evntId = _serializer.ReadUShort();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using Ragon.Common;
|
||||
using ENet;
|
||||
using NLog;
|
||||
|
||||
@@ -29,10 +29,10 @@ namespace Ragon.Core
|
||||
var authorizationProvider = factory.CreateAuthorizationProvider(configuration);
|
||||
var dispatcher = new Dispatcher();
|
||||
_dispatcherInternal = dispatcher;
|
||||
|
||||
|
||||
ThreadDispatcher = dispatcher;
|
||||
Server = new ENetServer(this);
|
||||
|
||||
|
||||
_deltaTime = 1000.0f / configuration.SendRate;
|
||||
|
||||
_roomManager = new RoomManager(factory, this);
|
||||
@@ -69,7 +69,7 @@ namespace Ragon.Core
|
||||
while (true)
|
||||
{
|
||||
Server.Process();
|
||||
|
||||
|
||||
_dispatcherInternal.Process();
|
||||
|
||||
var elapsedMilliseconds = _gameLoopTimer.ElapsedMilliseconds;
|
||||
@@ -109,14 +109,13 @@ namespace Ragon.Core
|
||||
evnt.Packet.CopyTo(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))
|
||||
{
|
||||
room.ProcessEvent(peerId, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
_lobby.ProcessEvent(peerId, data);
|
||||
}
|
||||
room.ProcessEvent(peerId, operation, payload);
|
||||
|
||||
_lobby.ProcessEvent(peerId, operation, payload);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user