Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1406b17d62 | |||
| 105457ffa0 | |||
| 20662ae24d | |||
| 6c441d9dee | |||
| 907bd2611e | |||
| 91d8516ac9 | |||
| ecdafeab00 | |||
| 88baff9fee | |||
| fdb41649b2 | |||
| aa607a7eb9 | |||
| efebf4ceda | |||
| 17d1b7307d | |||
| fc28f512ba | |||
| 6c4a51534a | |||
| c91551ae08 | |||
| e1a9ad476c | |||
| 24c9aa2043 | |||
| bfd6c1b54b | |||
| f2edc94958 | |||
| b8dfc4cf41 | |||
| bd7713bfcb | |||
| 043523d712 |
@@ -4,3 +4,5 @@
|
|||||||
obj
|
obj
|
||||||
bin
|
bin
|
||||||
*.user
|
*.user
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
Ragon is fully free, small and high perfomance room based game server with plugin based architecture.
|
Ragon is fully free, small and high perfomance room based game server with plugin based architecture.
|
||||||
|
|
||||||
<a href="https://www.ragon-server.com/docs/installation">Documentation</a>
|
<a href="https://www.ragon-server.com/docs/overview">Documentation</a>
|
||||||
|
|
||||||
### Features:
|
### Features:
|
||||||
- Effective
|
- Effective
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client.Simulation;
|
namespace Ragon.Client.Simulation;
|
||||||
|
|
||||||
public class Game : IRagonListener
|
public class Game : IRagonListener
|
||||||
@@ -18,7 +20,7 @@ public class Game : IRagonListener
|
|||||||
public void OnConnected(RagonClient client)
|
public void OnConnected(RagonClient client)
|
||||||
{
|
{
|
||||||
RagonLog.Trace("Connected");
|
RagonLog.Trace("Connected");
|
||||||
_client.Session.AuthorizeWithKey("defaultkey", "Player Eduard", Array.Empty<byte>());
|
_client.Session.AuthorizeWithKey("defaultkey", "Player Eduard");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAuthorizationSuccess(RagonClient client, string playerId, string playerName)
|
public void OnAuthorizationSuccess(RagonClient client, string playerId, string playerName)
|
||||||
@@ -63,7 +65,7 @@ public class Game : IRagonListener
|
|||||||
RagonLog.Trace("Left");
|
RagonLog.Trace("Left");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisconnected(RagonClient client)
|
public void OnDisconnected(RagonClient client, RagonDisconnect ragonDisconnect)
|
||||||
{
|
{
|
||||||
RagonLog.Trace("Disconnected");
|
RagonLog.Trace("Disconnected");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
using ENet;
|
using ENet;
|
||||||
|
using Ragon.Protocol;
|
||||||
using Event = ENet.Event;
|
using Event = ENet.Event;
|
||||||
using EventType = ENet.EventType;
|
using EventType = ENet.EventType;
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public Action<byte[]> OnData { get; set; }
|
public Action<byte[]> OnData { get; set; }
|
||||||
public Action OnConnected { get; set; }
|
public Action OnConnected { get; set; }
|
||||||
public Action<DisconnectReason> OnDisconnected { get; set; }
|
public Action<RagonDisconnect> OnDisconnected { get; set; }
|
||||||
public ulong BytesSent { get; }
|
public ulong BytesSent { get; }
|
||||||
public ulong BytesReceived { get; }
|
public ulong BytesReceived { get; }
|
||||||
public int Ping { get; }
|
public int Ping { get; }
|
||||||
@@ -98,10 +99,10 @@ namespace Ragon.Client
|
|||||||
OnConnected?.Invoke();
|
OnConnected?.Invoke();
|
||||||
break;
|
break;
|
||||||
case EventType.Disconnect:
|
case EventType.Disconnect:
|
||||||
OnDisconnected?.Invoke(DisconnectReason.MANUAL);
|
OnDisconnected?.Invoke(RagonDisconnect.SERVER);
|
||||||
break;
|
break;
|
||||||
case EventType.Timeout:
|
case EventType.Timeout:
|
||||||
OnDisconnected?.Invoke(DisconnectReason.TIMEOUT);
|
OnDisconnected?.Invoke(RagonDisconnect.TIMEOUT);
|
||||||
break;
|
break;
|
||||||
case EventType.Receive:
|
case EventType.Receive:
|
||||||
var data = new byte[_netEvent.Packet.Length];
|
var data = new byte[_netEvent.Packet.Length];
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
using ENet;
|
using ENet;
|
||||||
|
using Ragon.Protocol;
|
||||||
using Event = ENet.Event;
|
using Event = ENet.Event;
|
||||||
using EventType = ENet.EventType;
|
using EventType = ENet.EventType;
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public Action<byte[]> OnData { get; set; }
|
public Action<byte[]> OnData { get; set; }
|
||||||
public Action OnConnected { get; set; }
|
public Action OnConnected { get; set; }
|
||||||
public Action<DisconnectReason> OnDisconnected { get; set; }
|
public Action<RagonDisconnect> OnDisconnected { get; set; }
|
||||||
public ulong BytesSent { get; }
|
public ulong BytesSent { get; }
|
||||||
public ulong BytesReceived { get; }
|
public ulong BytesReceived { get; }
|
||||||
public int Ping { get; }
|
public int Ping { get; }
|
||||||
@@ -99,10 +100,10 @@ namespace Ragon.Client
|
|||||||
OnConnected?.Invoke();
|
OnConnected?.Invoke();
|
||||||
break;
|
break;
|
||||||
case EventType.Disconnect:
|
case EventType.Disconnect:
|
||||||
OnDisconnected?.Invoke(DisconnectReason.MANUAL);
|
OnDisconnected?.Invoke(RagonDisconnect.SERVER);
|
||||||
break;
|
break;
|
||||||
case EventType.Timeout:
|
case EventType.Timeout:
|
||||||
OnDisconnected?.Invoke(DisconnectReason.TIMEOUT);
|
OnDisconnected?.Invoke(RagonDisconnect.TIMEOUT);
|
||||||
break;
|
break;
|
||||||
case EventType.Receive:
|
case EventType.Receive:
|
||||||
var data = new byte[_netEvent.Packet.Length];
|
var data = new byte[_netEvent.Packet.Length];
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<DebugType>none</DebugType>
|
<DebugType>none</DebugType>
|
||||||
<OutputPath>/Users/edmand46/RagonProjects/ragon-unity-sdk/Assets/Ragon/Runtime/Plugins</OutputPath>
|
<OutputPath>/Users/edmand46/RagonProjects/ragon-oss-sdk/Assets/Ragon/Plugins/</OutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
|
|||||||
@@ -92,12 +92,12 @@ namespace Ragon.Client
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetSpawnPayload<T>() where T : IRagonPayload, new()
|
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
||||||
{
|
{
|
||||||
return GetPayload<T>(_spawnPayload);
|
return GetPayload<T>(_spawnPayload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetDestroyPayload<T>() where T : IRagonPayload, new()
|
public T GetDetachPayload<T>() where T : IRagonPayload, new()
|
||||||
{
|
{
|
||||||
return GetPayload<T>(_destroyPayload);
|
return GetPayload<T>(_destroyPayload);
|
||||||
}
|
}
|
||||||
@@ -105,6 +105,12 @@ namespace Ragon.Client
|
|||||||
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
||||||
where TEvent : IRagonEvent, new()
|
where TEvent : IRagonEvent, new()
|
||||||
{
|
{
|
||||||
|
if (!IsAttached)
|
||||||
|
{
|
||||||
|
RagonLog.Error("Entity not attached");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var evntId = _client.Event.GetEventCode(evnt);
|
var evntId = _client.Event.GetEventCode(evnt);
|
||||||
var buffer = _client.Buffer;
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
@@ -114,7 +120,7 @@ namespace Ragon.Client
|
|||||||
buffer.WriteUShort(evntId);
|
buffer.WriteUShort(evntId);
|
||||||
buffer.WriteByte((byte) replicationMode);
|
buffer.WriteByte((byte) replicationMode);
|
||||||
buffer.WriteByte((byte) RagonTarget.Player);
|
buffer.WriteByte((byte) RagonTarget.Player);
|
||||||
buffer.WriteUShort((ushort) target.PeerId);
|
buffer.WriteUShort(target.PeerId);
|
||||||
|
|
||||||
evnt.Serialize(buffer);
|
evnt.Serialize(buffer);
|
||||||
|
|
||||||
@@ -128,6 +134,12 @@ namespace Ragon.Client
|
|||||||
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
|
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
|
||||||
where TEvent : IRagonEvent, new()
|
where TEvent : IRagonEvent, new()
|
||||||
{
|
{
|
||||||
|
if (!IsAttached)
|
||||||
|
{
|
||||||
|
RagonLog.Error("Entity not attached");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (target != RagonTarget.ExceptOwner)
|
if (target != RagonTarget.ExceptOwner)
|
||||||
{
|
{
|
||||||
if (replicationMode == RagonReplicationMode.Local)
|
if (replicationMode == RagonReplicationMode.Local)
|
||||||
@@ -167,8 +179,10 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
if (_events.ContainsKey(eventCode))
|
if (_events.ContainsKey(eventCode))
|
||||||
{
|
{
|
||||||
RagonLog.Warn($"Event already {eventCode} subscribed");
|
_events.Remove(eventCode);
|
||||||
return;
|
_localEvents.Remove(eventCode);
|
||||||
|
|
||||||
|
RagonLog.Warn($"Event already {eventCode} subscribed, removed old one!");
|
||||||
}
|
}
|
||||||
|
|
||||||
_localEvents.Add(eventCode, (player, eventData) => { callback.Invoke(player, (TEvent) eventData); });
|
_localEvents.Add(eventCode, (player, eventData) => { callback.Invoke(player, (TEvent) eventData); });
|
||||||
@@ -196,8 +210,10 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
if (_events.ContainsKey(eventCode))
|
if (_events.TryGetValue(eventCode, out var evnt))
|
||||||
_events[eventCode]?.Invoke(caller, buffer);
|
evnt?.Invoke(caller, buffer);
|
||||||
|
else
|
||||||
|
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void TrackChangedProperty(RagonProperty property)
|
internal void TrackChangedProperty(RagonProperty property)
|
||||||
@@ -210,7 +226,7 @@ namespace Ragon.Client
|
|||||||
var prevOwner = Owner;
|
var prevOwner = Owner;
|
||||||
|
|
||||||
Owner = player;
|
Owner = player;
|
||||||
HasAuthority = player.PeerId == _client.Room.Local.PeerId;
|
HasAuthority = player.IsLocal;
|
||||||
|
|
||||||
OwnershipChanged?.Invoke(prevOwner, player);
|
OwnershipChanged?.Invoke(prevOwner, player);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public sealed class RagonEntityState
|
|||||||
{
|
{
|
||||||
var changed = buffer.ReadBool();
|
var changed = buffer.ReadBool();
|
||||||
if (changed)
|
if (changed)
|
||||||
property.Deserialize(buffer);
|
property.Read(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,22 +29,25 @@ namespace Ragon.Client
|
|||||||
public bool IsFixed => _fixed;
|
public bool IsFixed => _fixed;
|
||||||
public int Size => _size;
|
public int Size => _size;
|
||||||
|
|
||||||
private bool _fixed;
|
private RagonBuffer _propertyBuffer;
|
||||||
private string _name;
|
|
||||||
protected bool _invokeLocal;
|
|
||||||
|
|
||||||
private RagonEntity _entity;
|
private RagonEntity _entity;
|
||||||
private bool _dirty;
|
private bool _dirty;
|
||||||
private int _size;
|
private int _size;
|
||||||
private int _ticks;
|
private int _ticks;
|
||||||
private int _priority;
|
private int _priority;
|
||||||
|
private bool _fixed;
|
||||||
|
private string _name;
|
||||||
|
|
||||||
|
protected bool InvokeLocal;
|
||||||
|
|
||||||
protected RagonProperty(int priority, bool invokeLocal)
|
protected RagonProperty(int priority, bool invokeLocal)
|
||||||
{
|
{
|
||||||
_size = 0;
|
_size = 0;
|
||||||
_priority = priority;
|
_priority = priority;
|
||||||
_fixed = false;
|
_fixed = false;
|
||||||
_invokeLocal = invokeLocal;
|
_propertyBuffer = new RagonBuffer();
|
||||||
|
|
||||||
|
InvokeLocal = invokeLocal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetName(string name)
|
public void SetName(string name)
|
||||||
@@ -60,7 +63,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
protected void InvokeChanged()
|
protected void InvokeChanged()
|
||||||
{
|
{
|
||||||
if (!_invokeLocal)
|
if (!InvokeLocal)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Changed?.Invoke();
|
Changed?.Invoke();
|
||||||
@@ -97,20 +100,38 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
internal void Write(RagonBuffer buffer)
|
internal void Write(RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
|
_propertyBuffer.Clear();
|
||||||
|
|
||||||
if (_fixed)
|
if (_fixed)
|
||||||
{
|
{
|
||||||
Serialize(buffer);
|
Serialize(_propertyBuffer);
|
||||||
|
|
||||||
|
buffer.FromBuffer(_propertyBuffer, _size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var sizeOffset = buffer.WriteOffset;
|
Serialize(_propertyBuffer);
|
||||||
buffer.Write(0, 16);
|
|
||||||
var propOffset = buffer.WriteOffset;
|
|
||||||
|
|
||||||
Serialize(buffer);
|
var propertySize = (ushort) _propertyBuffer.WriteOffset;
|
||||||
|
buffer.WriteUShort(propertySize);;
|
||||||
|
buffer.FromBuffer(_propertyBuffer, propertySize);
|
||||||
|
}
|
||||||
|
|
||||||
var propSize = (uint)(buffer.WriteOffset - propOffset);
|
internal void Read(RagonBuffer buffer)
|
||||||
buffer.Write(propSize, 16, sizeOffset);
|
{
|
||||||
|
_propertyBuffer.Clear();
|
||||||
|
|
||||||
|
if (_fixed)
|
||||||
|
{
|
||||||
|
buffer.ToBuffer(_propertyBuffer, _size);
|
||||||
|
|
||||||
|
Deserialize(_propertyBuffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var propSize = buffer.ReadUShort();
|
||||||
|
buffer.ToBuffer(_propertyBuffer, propSize);
|
||||||
|
Deserialize(_propertyBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Serialize(RagonBuffer buffer)
|
public virtual void Serialize(RagonBuffer buffer)
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ internal class AuthorizeSuccessHandler: Handler
|
|||||||
{
|
{
|
||||||
var playerId = buffer.ReadString();
|
var playerId = buffer.ReadString();
|
||||||
var playerName = buffer.ReadString();
|
var playerName = buffer.ReadString();
|
||||||
|
var playerPayload = buffer.ReadString();
|
||||||
|
|
||||||
_listenerList.OnAuthorizationSuccess(playerId, playerName);
|
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ internal class EntityCreateHandler : Handler
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.OnCreate(attachId, entityType, 0, entityId, hasAuthority);
|
var entity = _entityCache.OnCreate(attachId, entityType, 0, entityId, hasAuthority);
|
||||||
entity.Attach(_client, entityId, entityType, hasAuthority, player, payload);
|
entity.Attach(_client, entityId, entityType, hasAuthority, player, payload);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ internal class EntityEventHandler : Handler
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player.IsMe && executionMode == RagonReplicationMode.LocalAndServer)
|
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_entityCache.OnEvent(player, entityId, eventCode, buffer);
|
_entityCache.OnEvent(player, entityId, eventCode, buffer);
|
||||||
|
|||||||
+7
-8
@@ -19,13 +19,13 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class OwnershipHandler: Handler
|
internal class EntityOwnershipHandler: Handler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
public OwnershipHandler(
|
public EntityOwnershipHandler(
|
||||||
RagonListenerList listenerList,
|
RagonListenerList listenerList,
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
RagonEntityCache entityCache)
|
RagonEntityCache entityCache)
|
||||||
@@ -37,17 +37,16 @@ internal class OwnershipHandler: Handler
|
|||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
var newOwnerId = buffer.ReadString();
|
var newOwnerId = buffer.ReadUShort();
|
||||||
var player = _playerCache.GetPlayerById(newOwnerId);
|
|
||||||
|
|
||||||
_playerCache.OnOwnershipChanged(newOwnerId);
|
|
||||||
_listenerList.OnOwnershipChanged(player);
|
|
||||||
|
|
||||||
var entities = buffer.ReadUShort();
|
var entities = buffer.ReadUShort();
|
||||||
|
|
||||||
|
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
||||||
for (var i = 0; i < entities; i++)
|
for (var i = 0; i < entities; i++)
|
||||||
{
|
{
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = buffer.ReadUShort();
|
||||||
_entityCache.OnOwnershipChanged(player, entityId);
|
_entityCache.OnOwnershipChanged(player, entityId);
|
||||||
|
|
||||||
|
RagonLog.Trace("Entity changed owner: " + entityId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-2
@@ -19,11 +19,11 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class EntityDestroyHandler: Handler
|
internal class EntityRemoveHandler: Handler
|
||||||
{
|
{
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
public EntityDestroyHandler(RagonEntityCache entityCache)
|
public EntityRemoveHandler(RagonEntityCache entityCache)
|
||||||
{
|
{
|
||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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.Client;
|
||||||
|
|
||||||
|
internal class OwnershipRoomHandler: Handler
|
||||||
|
{
|
||||||
|
private readonly RagonListenerList _listenerList;
|
||||||
|
private readonly RagonPlayerCache _playerCache;
|
||||||
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
|
public OwnershipRoomHandler(
|
||||||
|
RagonListenerList listenerList,
|
||||||
|
RagonPlayerCache playerCache,
|
||||||
|
RagonEntityCache entityCache)
|
||||||
|
{
|
||||||
|
_listenerList = listenerList;
|
||||||
|
_playerCache = playerCache;
|
||||||
|
_entityCache = entityCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var newOwnerId = buffer.ReadUShort();
|
||||||
|
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
||||||
|
|
||||||
|
_playerCache.OnOwnershipChanged(newOwnerId);
|
||||||
|
_listenerList.OnOwnershipChanged(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
@@ -64,7 +65,7 @@ internal class SnapshotHandler : Handler
|
|||||||
var payload = new RagonPayload(payloadSize);
|
var payload = new RagonPayload(payloadSize);
|
||||||
payload.Read(buffer);
|
payload.Read(buffer);
|
||||||
|
|
||||||
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.OnCreate(0, entityType, 0, entityId, hasAuthority);
|
var entity = _entityCache.OnCreate(0, entityType, 0, entityId, hasAuthority);
|
||||||
|
|
||||||
entity.Read(buffer);
|
entity.Read(buffer);
|
||||||
@@ -85,7 +86,7 @@ internal class SnapshotHandler : Handler
|
|||||||
var payload = new RagonPayload(payloadSize);
|
var payload = new RagonPayload(payloadSize);
|
||||||
payload.Read(buffer);
|
payload.Read(buffer);
|
||||||
|
|
||||||
var hasAuthority = _playerCache.LocalPlayer.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.OnCreate(0, entityType, staticId, entityId, hasAuthority);
|
var entity = _entityCache.OnCreate(0, entityType, staticId, entityId, hasAuthority);
|
||||||
|
|
||||||
entity.Read(buffer);
|
entity.Read(buffer);
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public interface INetworkConnection: IRagonConnection
|
public interface INetworkConnection: IRagonConnection
|
||||||
@@ -23,7 +25,7 @@ public interface INetworkConnection: IRagonConnection
|
|||||||
public INetworkChannel Unreliable { get; }
|
public INetworkChannel Unreliable { get; }
|
||||||
public Action<byte[]> OnData { get; set; }
|
public Action<byte[]> OnData { get; set; }
|
||||||
public Action OnConnected { get; set; }
|
public Action OnConnected { get; set; }
|
||||||
public Action<DisconnectReason> OnDisconnected { get; set; }
|
public Action<RagonDisconnect> OnDisconnected { get; set; }
|
||||||
public ulong BytesSent { get; }
|
public ulong BytesSent { get; }
|
||||||
public ulong BytesReceived { get; }
|
public ulong BytesReceived { get; }
|
||||||
public int Ping { get; }
|
public int Ping { get; }
|
||||||
|
|||||||
@@ -14,10 +14,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public interface IRagonConnectedListener
|
public interface IRagonConnectionListener
|
||||||
{
|
{
|
||||||
void OnConnected(RagonClient client);
|
void OnConnected(RagonClient client);
|
||||||
void OnDisconnected(RagonClient client);
|
void OnDisconnected(RagonClient client, RagonDisconnect reason);
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
public interface IRagonListener :
|
public interface IRagonListener :
|
||||||
IRagonAuthorizationListener,
|
IRagonAuthorizationListener,
|
||||||
IRagonConnectedListener,
|
IRagonConnectionListener,
|
||||||
IRagonFailedListener,
|
IRagonFailedListener,
|
||||||
IRagonJoinListener,
|
IRagonJoinListener,
|
||||||
IRagonLeftListener,
|
IRagonLeftListener,
|
||||||
|
|||||||
@@ -87,23 +87,19 @@ namespace Ragon.Client
|
|||||||
_handlers = new Handler[byte.MaxValue];
|
_handlers = new Handler[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(_listenerList);
|
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(_listenerList);
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listenerList);
|
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listenerList);
|
||||||
_handlers[(byte)RagonOperation.JOIN_SUCCESS] =
|
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _readBuffer, _listenerList, _playerCache, _entityCache);
|
||||||
new JoinSuccessHandler(this, _readBuffer, _listenerList, _playerCache, _entityCache);
|
|
||||||
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listenerList);
|
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listenerList);
|
||||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listenerList, _entityCache);
|
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listenerList, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_CHANGED] =
|
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(_listenerList, _playerCache, _entityCache);
|
||||||
new OwnershipHandler(_listenerList, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(_listenerList, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listenerList);
|
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listenerList);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_LEAVED] =
|
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listenerList);
|
||||||
new PlayerLeftHandler(_entityCache, _playerCache, _listenerList);
|
|
||||||
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listenerList);
|
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listenerList);
|
||||||
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.DESTROY_ENTITY] = new EntityDestroyHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] =
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(this, _playerCache, _entityCache);
|
||||||
new EntityEventHandler(this, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, _listenerList, _entityCache, _playerCache);
|
||||||
_handlers[(byte)RagonOperation.SNAPSHOT] =
|
|
||||||
new SnapshotHandler(this, _listenerList, _entityCache, _playerCache);
|
|
||||||
|
|
||||||
var protocolRaw = RagonVersion.Parse(protocol);
|
var protocolRaw = RagonVersion.Parse(protocol);
|
||||||
_connection.Connect(address, port, protocolRaw);
|
_connection.Connect(address, port, protocolRaw);
|
||||||
@@ -114,10 +110,13 @@ namespace Ragon.Client
|
|||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
_room.Cleanup();
|
_room.Cleanup();
|
||||||
_connection.Disconnect();
|
_connection.Disconnect();
|
||||||
OnDisconnected(DisconnectReason.MANUAL);
|
|
||||||
|
OnDisconnected(RagonDisconnect.MANUAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(float dt)
|
public void Update(float dt)
|
||||||
|
{
|
||||||
|
if (_status != RagonStatus.DISCONNECTED)
|
||||||
{
|
{
|
||||||
_replicationTime += dt;
|
_replicationTime += dt;
|
||||||
if (_replicationTime >= _replicationRate)
|
if (_replicationTime >= _replicationRate)
|
||||||
@@ -127,6 +126,8 @@ namespace Ragon.Client
|
|||||||
}
|
}
|
||||||
|
|
||||||
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
||||||
|
}
|
||||||
|
|
||||||
_connection.Update();
|
_connection.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +140,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void AddListener(IRagonListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonListener listener) => _listenerList.Add(listener);
|
||||||
public void AddListener(IRagonAuthorizationListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonAuthorizationListener listener) => _listenerList.Add(listener);
|
||||||
public void AddListener(IRagonConnectedListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonConnectionListener listener) => _listenerList.Add(listener);
|
||||||
public void AddListener(IRagonFailedListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonFailedListener listener) => _listenerList.Add(listener);
|
||||||
public void AddListener(IRagonJoinListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonJoinListener listener) => _listenerList.Add(listener);
|
||||||
public void AddListener(IRagonLeftListener listener) => _listenerList.Add(listener);
|
public void AddListener(IRagonLeftListener listener) => _listenerList.Add(listener);
|
||||||
@@ -150,7 +151,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void RemoveListener(IRagonListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonListener listener) => _listenerList.Remove(listener);
|
||||||
public void RemoveListener(IRagonAuthorizationListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonAuthorizationListener listener) => _listenerList.Remove(listener);
|
||||||
public void RemoveListener(IRagonConnectedListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonConnectionListener listener) => _listenerList.Remove(listener);
|
||||||
public void RemoveListener(IRagonFailedListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonFailedListener listener) => _listenerList.Remove(listener);
|
||||||
public void RemoveListener(IRagonJoinListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonJoinListener listener) => _listenerList.Remove(listener);
|
||||||
public void RemoveListener(IRagonLeftListener listener) => _listenerList.Remove(listener);
|
public void RemoveListener(IRagonLeftListener listener) => _listenerList.Remove(listener);
|
||||||
@@ -181,11 +182,11 @@ namespace Ragon.Client
|
|||||||
_status = RagonStatus.CONNECTED;
|
_status = RagonStatus.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisconnected(DisconnectReason reason)
|
private void OnDisconnected(RagonDisconnect reason)
|
||||||
{
|
{
|
||||||
RagonLog.Trace($"Disconnected: {reason}");
|
RagonLog.Trace($"Disconnected: {reason}");
|
||||||
|
|
||||||
_listenerList.OnDisconnected();
|
_listenerList.OnDisconnected(reason);
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public sealed class RagonEntityCache
|
|||||||
|
|
||||||
public void Create(RagonEntity entity, IRagonPayload? spawnPayload)
|
public void Create(RagonEntity entity, IRagonPayload? spawnPayload)
|
||||||
{
|
{
|
||||||
var attachId = (ushort) (_playerCache.LocalPlayer.PeerId + _localEntitiesCounter++) ;
|
var attachId = (ushort)(_playerCache.Local.PeerId + _localEntitiesCounter++);
|
||||||
var buffer = _client.Buffer;
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
@@ -71,6 +71,19 @@ public sealed class RagonEntityCache
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Transfer(RagonEntity entity, RagonPlayer player)
|
||||||
|
{
|
||||||
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
|
buffer.Clear();
|
||||||
|
buffer.WriteOperation(RagonOperation.TRANSFER_ENTITY_OWNERSHIP);
|
||||||
|
buffer.WriteUShort(entity.Id);
|
||||||
|
buffer.WriteUShort(player.PeerId);
|
||||||
|
|
||||||
|
var sendData = buffer.ToArray();
|
||||||
|
_client.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
public void Destroy(RagonEntity entity, IRagonPayload? destroyPayload)
|
public void Destroy(RagonEntity entity, IRagonPayload? destroyPayload)
|
||||||
{
|
{
|
||||||
if (!entity.IsAttached)
|
if (!entity.IsAttached)
|
||||||
@@ -78,10 +91,11 @@ public sealed class RagonEntityCache
|
|||||||
RagonLog.Warn("Can't destroy object, he is not created");
|
RagonLog.Warn("Can't destroy object, he is not created");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = _client.Buffer;
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
buffer.WriteOperation(RagonOperation.DESTROY_ENTITY);
|
buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
|
||||||
buffer.WriteUShort(entity.Id);
|
buffer.WriteUShort(entity.Id);
|
||||||
|
|
||||||
destroyPayload?.Serialize(buffer);
|
destroyPayload?.Serialize(buffer);
|
||||||
@@ -225,8 +239,17 @@ public sealed class RagonEntityCache
|
|||||||
internal void OnOwnershipChanged(RagonPlayer player, ushort entityId)
|
internal void OnOwnershipChanged(RagonPlayer player, ushort entityId)
|
||||||
{
|
{
|
||||||
if (_entityMap.TryGetValue(entityId, out var entity))
|
if (_entityMap.TryGetValue(entityId, out var entity))
|
||||||
entity.OnOwnershipChanged(player);
|
{
|
||||||
|
if (player.IsLocal)
|
||||||
|
_entityList.Add(entity);
|
||||||
else
|
else
|
||||||
|
_entityList.Remove(entity);
|
||||||
|
|
||||||
|
entity.OnOwnershipChanged(player);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
RagonLog.Warn($"Entity {entityId} not found!");
|
RagonLog.Warn($"Entity {entityId} not found!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -26,8 +26,12 @@ public class RagonEventCache
|
|||||||
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
|
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
|
||||||
{
|
{
|
||||||
var type = typeof(TEvent);
|
var type = typeof(TEvent);
|
||||||
var evntCode = _eventsRegistryByType[type];
|
if (!_eventsRegistryByType.TryGetValue(type, out var eventCode))
|
||||||
return evntCode;
|
{
|
||||||
|
RagonLog.Error($"Event with type {type} not registered");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return eventCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Register<T>() where T : IRagonEvent, new()
|
public void Register<T>() where T : IRagonEvent, new()
|
||||||
|
|||||||
@@ -14,13 +14,15 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
internal class RagonListenerList
|
internal class RagonListenerList
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly List<IRagonAuthorizationListener> _authorizationListeners = new();
|
private readonly List<IRagonAuthorizationListener> _authorizationListeners = new();
|
||||||
private readonly List<IRagonConnectedListener> _connectionListeners = new();
|
private readonly List<IRagonConnectionListener> _connectionListeners = new();
|
||||||
private readonly List<IRagonFailedListener> _failedListeners = new();
|
private readonly List<IRagonFailedListener> _failedListeners = new();
|
||||||
private readonly List<IRagonJoinListener> _joinListeners = new();
|
private readonly List<IRagonJoinListener> _joinListeners = new();
|
||||||
private readonly List<IRagonLeftListener> _leftListeners = new();
|
private readonly List<IRagonLeftListener> _leftListeners = new();
|
||||||
@@ -65,7 +67,7 @@ namespace Ragon.Client
|
|||||||
_authorizationListeners.Add(listener);
|
_authorizationListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add(IRagonConnectedListener listener)
|
public void Add(IRagonConnectionListener listener)
|
||||||
{
|
{
|
||||||
_connectionListeners.Add(listener);
|
_connectionListeners.Add(listener);
|
||||||
}
|
}
|
||||||
@@ -110,7 +112,7 @@ namespace Ragon.Client
|
|||||||
_authorizationListeners.Remove(listener);
|
_authorizationListeners.Remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(IRagonConnectedListener listener)
|
public void Remove(IRagonConnectionListener listener)
|
||||||
{
|
{
|
||||||
_connectionListeners.Remove(listener);
|
_connectionListeners.Remove(listener);
|
||||||
}
|
}
|
||||||
@@ -150,7 +152,7 @@ namespace Ragon.Client
|
|||||||
_playerLeftListeners.Remove(listener);
|
_playerLeftListeners.Remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnAuthorizationSuccess(string playerId, string playerName)
|
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
|
||||||
{
|
{
|
||||||
foreach (var listener in _authorizationListeners)
|
foreach (var listener in _authorizationListeners)
|
||||||
listener.OnAuthorizationSuccess(_client, playerId, playerName);
|
listener.OnAuthorizationSuccess(_client, playerId, playerName);
|
||||||
@@ -210,10 +212,10 @@ namespace Ragon.Client
|
|||||||
listener.OnConnected(_client);
|
listener.OnConnected(_client);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisconnected()
|
public void OnDisconnected(RagonDisconnect disconnect)
|
||||||
{
|
{
|
||||||
foreach (var listener in _connectionListeners)
|
foreach (var listener in _connectionListeners)
|
||||||
listener.OnDisconnected(_client);
|
listener.OnDisconnected(_client, disconnect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,13 +23,13 @@ namespace Ragon.Client
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public ushort PeerId { get; set; }
|
public ushort PeerId { get; set; }
|
||||||
public bool IsRoomOwner { get; set; }
|
public bool IsRoomOwner { get; set; }
|
||||||
public bool IsMe { get; set; }
|
public bool IsLocal { get; set; }
|
||||||
|
|
||||||
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isMe)
|
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
|
||||||
{
|
{
|
||||||
PeerId = peerId;
|
PeerId = peerId;
|
||||||
IsRoomOwner = isRoomOwner;
|
IsRoomOwner = isRoomOwner;
|
||||||
IsMe = isMe;
|
IsLocal = isLocal;
|
||||||
Name = name;
|
Name = name;
|
||||||
Id = playerId;
|
Id = playerId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,12 +18,13 @@ namespace Ragon.Client;
|
|||||||
|
|
||||||
public sealed class RagonPlayerCache
|
public sealed class RagonPlayerCache
|
||||||
{
|
{
|
||||||
private List<RagonPlayer> _players = new List<RagonPlayer>();
|
private readonly List<RagonPlayer> _players = new();
|
||||||
private Dictionary<string, RagonPlayer> _playersById = new();
|
private readonly Dictionary<string, RagonPlayer> _playersById = new();
|
||||||
private Dictionary<ushort, RagonPlayer> _playersByConnection = new();
|
private readonly Dictionary<ushort, RagonPlayer> _playersByConnection = new();
|
||||||
|
|
||||||
|
public IReadOnlyList<RagonPlayer> Players => _players;
|
||||||
public RagonPlayer Owner { get; private set; }
|
public RagonPlayer Owner { get; private set; }
|
||||||
public RagonPlayer LocalPlayer { get; private set; }
|
public RagonPlayer Local { get; private set; }
|
||||||
public bool IsRoomOwner => _ownerId == _localId;
|
public bool IsRoomOwner => _ownerId == _localId;
|
||||||
|
|
||||||
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId];
|
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId];
|
||||||
@@ -50,8 +51,8 @@ public sealed class RagonPlayerCache
|
|||||||
|
|
||||||
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
|
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
|
||||||
|
|
||||||
if (player.IsMe)
|
if (player.IsLocal)
|
||||||
LocalPlayer = player;
|
Local = player;
|
||||||
|
|
||||||
if (player.IsRoomOwner)
|
if (player.IsRoomOwner)
|
||||||
Owner = player;
|
Owner = player;
|
||||||
@@ -70,13 +71,15 @@ public sealed class RagonPlayerCache
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnOwnershipChanged(string playerId)
|
public void OnOwnershipChanged(ushort playerPeerId)
|
||||||
{
|
{
|
||||||
foreach (var player in _players)
|
foreach (var player in _players)
|
||||||
{
|
{
|
||||||
if (player.Id == playerId)
|
if (player.PeerId == playerPeerId)
|
||||||
|
{
|
||||||
Owner = player;
|
Owner = player;
|
||||||
player.IsRoomOwner = player.Id == playerId;
|
Owner.IsRoomOwner = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ namespace Ragon.Client
|
|||||||
public int MinPlayers => _information.Min;
|
public int MinPlayers => _information.Min;
|
||||||
public int MaxPlayers => _information.Max;
|
public int MaxPlayers => _information.Max;
|
||||||
|
|
||||||
public RagonPlayer Local => _playerCache.LocalPlayer;
|
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
||||||
|
public RagonPlayer Local => _playerCache.Local;
|
||||||
public RagonPlayer Owner => _playerCache.Owner;
|
public RagonPlayer Owner => _playerCache.Owner;
|
||||||
|
|
||||||
public RagonRoom(RagonClient client,
|
public RagonRoom(RagonClient client,
|
||||||
@@ -55,6 +56,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
||||||
public void CreateEntity(RagonEntity entity, IRagonPayload? payload) => _entityCache.Create(entity, payload);
|
public void CreateEntity(RagonEntity entity, IRagonPayload? payload) => _entityCache.Create(entity, payload);
|
||||||
|
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
|
||||||
|
|
||||||
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
|
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
|
||||||
public void DestroyEntity(RagonEntity entityId, IRagonPayload? payload) => _entityCache.Destroy(entityId, payload);
|
public void DestroyEntity(RagonEntity entityId, IRagonPayload? payload) => _entityCache.Destroy(entityId, payload);
|
||||||
|
|||||||
@@ -93,13 +93,13 @@ namespace Ragon.Client
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AuthorizeWithKey(string key, string playerName, byte[] additonalData)
|
public void AuthorizeWithKey(string key, string playerName, string payload = "")
|
||||||
{
|
{
|
||||||
_buffer.Clear();
|
_buffer.Clear();
|
||||||
_buffer.WriteOperation(RagonOperation.AUTHORIZE);
|
_buffer.WriteOperation(RagonOperation.AUTHORIZE);
|
||||||
_buffer.WriteString(key);
|
_buffer.WriteString(key);
|
||||||
_buffer.WriteString(playerName);
|
_buffer.WriteString(playerName);
|
||||||
_buffer.WriteBytes(additonalData);
|
_buffer.WriteString(payload);
|
||||||
|
|
||||||
var sendData = _buffer.ToArray();
|
var sendData = _buffer.ToArray();
|
||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Ragon.Client.Compressor;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client.Utils;
|
||||||
|
|
||||||
|
public static class CompressorExtension
|
||||||
|
{
|
||||||
|
public static float Read(this FloatCompressor compressor, RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Write(this FloatCompressor compressor, RagonBuffer buffer, float value)
|
||||||
|
{
|
||||||
|
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float Read(this IntCompressor compressor, RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Write(this IntCompressor compressor, RagonBuffer buffer, int value)
|
||||||
|
{
|
||||||
|
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -321,6 +321,7 @@ namespace Ragon.Protocol
|
|||||||
_read += size;
|
_read += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteSpan(ref ReadOnlySpan<uint> data, int size)
|
public void WriteSpan(ref ReadOnlySpan<uint> data, int size)
|
||||||
{
|
{
|
||||||
var used = _write & 0x0000001F;
|
var used = _write & 0x0000001F;
|
||||||
@@ -352,6 +353,20 @@ namespace Ragon.Protocol
|
|||||||
_write = 0;
|
_write = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void FromBuffer(RagonBuffer buffer, int size)
|
||||||
|
{
|
||||||
|
ReadOnlySpan<uint> data = buffer._buckets.AsSpan();
|
||||||
|
WriteSpan(ref data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void ToBuffer(RagonBuffer buffer, int size)
|
||||||
|
{
|
||||||
|
var data = buffer._buckets.AsSpan();
|
||||||
|
ReadSpan(ref data, size);
|
||||||
|
}
|
||||||
|
|
||||||
public void FromArray(byte[] data)
|
public void FromArray(byte[] data)
|
||||||
{
|
{
|
||||||
var length = data.Length;
|
var length = data.Length;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Ragon.Protocol
|
||||||
|
{
|
||||||
|
public enum RagonDisconnect
|
||||||
|
{
|
||||||
|
MANUAL,
|
||||||
|
TIMEOUT,
|
||||||
|
SERVER,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,7 +26,8 @@ namespace Ragon.Protocol
|
|||||||
CREATE_ROOM,
|
CREATE_ROOM,
|
||||||
JOIN_ROOM,
|
JOIN_ROOM,
|
||||||
LEAVE_ROOM,
|
LEAVE_ROOM,
|
||||||
OWNERSHIP_CHANGED,
|
OWNERSHIP_ENTITY_CHANGED,
|
||||||
|
OWNERSHIP_ROOM_CHANGED,
|
||||||
JOIN_SUCCESS,
|
JOIN_SUCCESS,
|
||||||
JOIN_FAILED,
|
JOIN_FAILED,
|
||||||
LOAD_SCENE,
|
LOAD_SCENE,
|
||||||
@@ -34,9 +35,11 @@ namespace Ragon.Protocol
|
|||||||
PLAYER_JOINED,
|
PLAYER_JOINED,
|
||||||
PLAYER_LEAVED,
|
PLAYER_LEAVED,
|
||||||
CREATE_ENTITY,
|
CREATE_ENTITY,
|
||||||
DESTROY_ENTITY,
|
REMOVE_ENTITY,
|
||||||
SNAPSHOT,
|
SNAPSHOT,
|
||||||
REPLICATE_ENTITY_STATE,
|
REPLICATE_ENTITY_STATE,
|
||||||
REPLICATE_ENTITY_EVENT,
|
REPLICATE_ENTITY_EVENT,
|
||||||
|
TRANSFER_ROOM_OWNERSHIP,
|
||||||
|
TRANSFER_ENTITY_OWNERSHIP,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Ragon.Server.ENet\Ragon.Server.ENet.csproj" />
|
<ProjectReference Include="..\Ragon.Server.ENet\Ragon.Server.ENet.csproj" />
|
||||||
<ProjectReference Include="..\Ragon.Server.DotNetWebSockets\Ragon.Server.DotNetWebSockets.csproj" />
|
<ProjectReference Include="..\Ragon.Server.WebSocketServer\Ragon.Server.WebSocketServer.csproj" />
|
||||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
|
public class KickPlayerCommand
|
||||||
|
{
|
||||||
|
public string Id;
|
||||||
|
}
|
||||||
@@ -17,8 +17,9 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Server;
|
using Ragon.Server;
|
||||||
using Ragon.Server.ENet;
|
using Ragon.Server.ENet;
|
||||||
using Ragon.Server.DotNetWebsockets;
|
using Ragon.Server.WebSocketServer;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
|
||||||
namespace Ragon.Relay;
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
@@ -29,24 +30,22 @@ public class Relay
|
|||||||
var logger = LogManager.GetLogger("Ragon.Relay");
|
var logger = LogManager.GetLogger("Ragon.Relay");
|
||||||
logger.Info("Relay Application");
|
logger.Info("Relay Application");
|
||||||
|
|
||||||
var configuration = Configuration.Load("relay.config.json");
|
var configuration = RagonServerConfiguration.Load("relay.config.json");
|
||||||
var serverType = Configuration.GetServerType(configuration.ServerType);
|
var serverType = RagonServerConfiguration.GetServerType(configuration.ServerType);
|
||||||
|
|
||||||
INetworkServer server = null;
|
INetworkServer networkServer = new ENetServer();
|
||||||
|
IServerPlugin plugin = new RelayServerPlugin();
|
||||||
switch (serverType)
|
switch (serverType)
|
||||||
{
|
{
|
||||||
case ServerType.ENET:
|
case ServerType.ENET:
|
||||||
server = new ENetServer();
|
networkServer = new ENetServer();
|
||||||
break;
|
break;
|
||||||
case ServerType.WEBSOCKET:
|
case ServerType.WEBSOCKET:
|
||||||
server = new DotNetWebSocketServer();
|
networkServer = new WebSocketServer();
|
||||||
break;
|
|
||||||
default:
|
|
||||||
server = new ENetServer();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var relay = new RagonServer(server, configuration);
|
var relay = new RagonServer(networkServer, plugin, configuration);
|
||||||
logger.Info("Started");
|
logger.Info("Started");
|
||||||
relay.Start();
|
relay.Start();
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using Ragon.Server;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
|
public class RelayRoomPlugin: BaseRoomPlugin
|
||||||
|
{
|
||||||
|
public void Tick(float dt)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnAttached()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Room attached");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnDetached()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Room detached");
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool OnEntityCreate(RagonRoomPlayer creator, RagonEntity entity)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Entity created: {entity.Id}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool OnEntityRemove(RagonRoomPlayer destroyer, RagonEntity entity)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Entity destroyed: {entity.Id}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
namespace Ragon.Relay;
|
||||||
|
|
||||||
|
public class RelayServerPlugin: BaseServerPlugin
|
||||||
|
{
|
||||||
|
public override bool OnCommand(string command, string payload)
|
||||||
|
{
|
||||||
|
Console.WriteLine(command);
|
||||||
|
if (command == "kick-player")
|
||||||
|
{
|
||||||
|
var commandPayload = JsonConvert.DeserializeObject<KickPlayerCommand>(payload);
|
||||||
|
var player = Server.GetPlayerById(commandPayload.Id);
|
||||||
|
if (player != null)
|
||||||
|
player.Connection.Close();
|
||||||
|
else
|
||||||
|
Console.WriteLine($"Player not found with Id {commandPayload.Id}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,18 @@
|
|||||||
"serverType": "enet",
|
"serverType": "enet",
|
||||||
"serverTickRate": 30,
|
"serverTickRate": 30,
|
||||||
"gameProtocol": "1.0.0",
|
"gameProtocol": "1.0.0",
|
||||||
"port": 5001,
|
"port": 5000,
|
||||||
|
"httpPort": 5001,
|
||||||
|
"httpKey": "defaultkey",
|
||||||
"limitConnections": 4095,
|
"limitConnections": 4095,
|
||||||
"limitPlayersPerRoom": 20,
|
"limitPlayersPerRoom": 20,
|
||||||
"limitRooms": 200
|
"limitRooms": 200,
|
||||||
|
"limitBufferedEvents": 50,
|
||||||
|
"webHooks":
|
||||||
|
{
|
||||||
|
"room-created": "http://127.0.0.1:3000/service/create-room",
|
||||||
|
"room-removed": "http://127.0.0.1:3000/service/remove-room",
|
||||||
|
"room-joined": "http://127.0.0.1:3000/service/join-room",
|
||||||
|
"room-leaved": "http://127.0.0.1:3000/service/leave-room"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using ENet;
|
using ENet;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.ENet;
|
namespace Ragon.Server.ENet;
|
||||||
|
|
||||||
@@ -23,11 +24,19 @@ public sealed class ENetConnection: INetworkConnection
|
|||||||
public ushort Id { get; }
|
public ushort Id { get; }
|
||||||
public INetworkChannel Reliable { get; private set; }
|
public INetworkChannel Reliable { get; private set; }
|
||||||
public INetworkChannel Unreliable { get; private set; }
|
public INetworkChannel Unreliable { get; private set; }
|
||||||
|
private Peer _peer;
|
||||||
|
|
||||||
public ENetConnection(Peer peer)
|
public ENetConnection(Peer peer)
|
||||||
{
|
{
|
||||||
|
_peer = peer;
|
||||||
|
|
||||||
Id = (ushort) peer.ID;
|
Id = (ushort) peer.ID;
|
||||||
Reliable = new ENetReliableChannel(peer, 0);
|
Reliable = new ENetReliableChannel(peer, 0);
|
||||||
Unreliable = new ENetUnreliableChannel(peer, 1);
|
Unreliable = new ENetUnreliableChannel(peer, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
_peer.Disconnect(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using ENet;
|
using ENet;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.ENet;
|
namespace Ragon.Server.ENet;
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
using ENet;
|
using ENet;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.ENet
|
namespace Ragon.Server.ENet
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using ENet;
|
using ENet;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.ENet;
|
namespace Ragon.Server.ENet;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>Ragon.WebSockets</RootNamespace>
|
<RootNamespace>Ragon.WebSockets</RootNamespace>
|
||||||
+8
-2
@@ -14,10 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Net.WebSockets;
|
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.DotNetWebsockets;
|
namespace Ragon.Server.WebSocketServer;
|
||||||
|
|
||||||
public sealed class WebSocketConnection : INetworkConnection
|
public sealed class WebSocketConnection : INetworkConnection
|
||||||
{
|
{
|
||||||
@@ -43,6 +44,11 @@ public sealed class WebSocketConnection : INetworkConnection
|
|||||||
Unreliable = unreliableChannel;
|
Unreliable = unreliableChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task Flush()
|
public async Task Flush()
|
||||||
{
|
{
|
||||||
foreach (var channel in _channels)
|
foreach (var channel in _channels)
|
||||||
+2
-2
@@ -15,9 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using Ragon.Server;
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.DotNetWebsockets;
|
namespace Ragon.Server.WebSocketServer;
|
||||||
|
|
||||||
public class WebSocketReliableChannel : INetworkChannel
|
public class WebSocketReliableChannel : INetworkChannel
|
||||||
{
|
{
|
||||||
+4
-3
@@ -18,10 +18,11 @@ using System.Net;
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
namespace Ragon.Server.DotNetWebsockets;
|
namespace Ragon.Server.WebSocketServer;
|
||||||
|
|
||||||
public class DotNetWebSocketServer : INetworkServer
|
public class WebSocketServer : INetworkServer
|
||||||
{
|
{
|
||||||
public Executor Executor => _executor;
|
public Executor Executor => _executor;
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ public class DotNetWebSocketServer : INetworkServer
|
|||||||
private List<WebSocketConnection> _activeConnections;
|
private List<WebSocketConnection> _activeConnections;
|
||||||
private CancellationTokenSource _cancellationTokenSource;
|
private CancellationTokenSource _cancellationTokenSource;
|
||||||
|
|
||||||
public DotNetWebSocketServer()
|
public WebSocketServer()
|
||||||
{
|
{
|
||||||
_sequencer = new Stack<ushort>();
|
_sequencer = new Stack<ushort>();
|
||||||
_connections = Array.Empty<WebSocketConnection>();
|
_connections = Array.Empty<WebSocketConnection>();
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
|
public interface IRagonEntity
|
||||||
|
{
|
||||||
|
public ushort Id { get; }
|
||||||
|
public ushort Type { get; }
|
||||||
|
public ushort StaticId { get; }
|
||||||
|
public ushort AttachId { get; }
|
||||||
|
public RagonRoomPlayer Owner { get; }
|
||||||
|
public RagonAuthority Authority { get; }
|
||||||
|
public RagonPayload Payload { get; }
|
||||||
|
public IRagonEntityState State { get; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
|
public interface IRagonEntityState
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,10 +16,11 @@
|
|||||||
|
|
||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonEntity
|
public class RagonEntity : IRagonEntity
|
||||||
{
|
{
|
||||||
private static ushort _idGenerator = 100;
|
private static ushort _idGenerator = 100;
|
||||||
public ushort Id { get; private set; }
|
public ushort Id { get; private set; }
|
||||||
@@ -29,30 +30,36 @@ public class RagonEntity
|
|||||||
public RagonRoomPlayer Owner { get; private set; }
|
public RagonRoomPlayer Owner { get; private set; }
|
||||||
public RagonAuthority Authority { get; private set; }
|
public RagonAuthority Authority { get; private set; }
|
||||||
public RagonPayload Payload { get; private set; }
|
public RagonPayload Payload { get; private set; }
|
||||||
public RagonEntityState State { get; private set; }
|
public IRagonEntityState State => _state;
|
||||||
|
|
||||||
private readonly List<RagonEvent> _bufferedEvents;
|
private readonly List<RagonEvent> _bufferedEvents;
|
||||||
|
private readonly int _limitBufferedEvents;
|
||||||
|
private readonly RagonEntityState _state;
|
||||||
|
|
||||||
public RagonEntity(RagonRoomPlayer owner, ushort type, ushort staticId, ushort attachId, RagonAuthority eventAuthority)
|
public RagonEntity(RagonEntityParameters parameters)
|
||||||
{
|
{
|
||||||
Owner = owner;
|
|
||||||
StaticId = staticId;
|
|
||||||
Type = type;
|
|
||||||
AttachId = attachId;
|
|
||||||
Id = _idGenerator++;
|
Id = _idGenerator++;
|
||||||
Authority = eventAuthority;
|
|
||||||
State = new RagonEntityState(this);
|
StaticId = parameters.StaticId;
|
||||||
|
Type = parameters.Type;
|
||||||
|
AttachId = parameters.AttachId;
|
||||||
|
Authority = parameters.Authority;
|
||||||
Payload = new RagonPayload();
|
Payload = new RagonPayload();
|
||||||
|
|
||||||
|
_state = new RagonEntityState(this);
|
||||||
_bufferedEvents = new List<RagonEvent>();
|
_bufferedEvents = new List<RagonEvent>();
|
||||||
|
_limitBufferedEvents = parameters.BufferedEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Attach(RagonRoomPlayer owner)
|
||||||
public void SetOwner(RagonRoomPlayer owner)
|
|
||||||
{
|
{
|
||||||
Owner = owner;
|
Owner = owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Detach()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer)
|
public void RestoreBufferedEvents(RagonRoomPlayer roomPlayer, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
foreach (var evnt in _bufferedEvents)
|
foreach (var evnt in _bufferedEvents)
|
||||||
@@ -96,7 +103,7 @@ public class RagonEntity
|
|||||||
var buffer = room.Writer;
|
var buffer = room.Writer;
|
||||||
|
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
buffer.WriteOperation(RagonOperation.DESTROY_ENTITY);
|
buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
|
||||||
buffer.WriteUShort(Id);
|
buffer.WriteUShort(Id);
|
||||||
|
|
||||||
Payload.Write(buffer);
|
Payload.Write(buffer);
|
||||||
@@ -116,7 +123,8 @@ public class RagonEntity
|
|||||||
|
|
||||||
buffer.WriteUShort(Payload.Size);
|
buffer.WriteUShort(Payload.Size);
|
||||||
Payload.Write(buffer);
|
Payload.Write(buffer);
|
||||||
State.Snapshot(buffer);
|
|
||||||
|
_state.Snapshot(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReplicateEvent(
|
public void ReplicateEvent(
|
||||||
@@ -156,7 +164,9 @@ public class RagonEntity
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner)
|
if (eventMode == RagonReplicationMode.Buffered &&
|
||||||
|
targetMode != RagonTarget.Owner &&
|
||||||
|
_bufferedEvents.Count < _limitBufferedEvents)
|
||||||
{
|
{
|
||||||
_bufferedEvents.Add(evnt);
|
_bufferedEvents.Add(evnt);
|
||||||
}
|
}
|
||||||
@@ -209,4 +219,24 @@ public class RagonEntity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddProperty(RagonProperty property)
|
||||||
|
{
|
||||||
|
_state.AddProperty(property);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteState(RagonBuffer writer)
|
||||||
|
{
|
||||||
|
_state.Write(writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryReadState(RagonRoomPlayer player, RagonBuffer reader)
|
||||||
|
{
|
||||||
|
if (Owner.Connection.Id != player.Connection.Id)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_state.Read(reader);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+7
-29
@@ -14,37 +14,15 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using Ragon.Client;
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
using Xunit.Abstractions;
|
|
||||||
|
|
||||||
namespace Ragon.Core.Tests;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonXUnitLogger: IRagonLogger
|
public ref struct RagonEntityParameters
|
||||||
{
|
{
|
||||||
private ITestOutputHelper _outputHelper;
|
public ushort Type;
|
||||||
public RagonXUnitLogger(ITestOutputHelper outputHelper)
|
public ushort StaticId;
|
||||||
{
|
public ushort AttachId;
|
||||||
_outputHelper = outputHelper;
|
public RagonAuthority Authority;
|
||||||
}
|
public int BufferedEvents;
|
||||||
|
|
||||||
public void Warn(string message)
|
|
||||||
{
|
|
||||||
_outputHelper.WriteLine($"[Warn] {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Trace(string message)
|
|
||||||
{
|
|
||||||
_outputHelper.WriteLine($"[Trace] {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Info(string message)
|
|
||||||
{
|
|
||||||
_outputHelper.WriteLine($"[Info] {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Error(string message)
|
|
||||||
{
|
|
||||||
_outputHelper.WriteLine($"[Error] {message}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -17,16 +17,18 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonEntityState
|
public class RagonEntityState: IRagonEntityState
|
||||||
{
|
{
|
||||||
private List<RagonProperty> _properties;
|
private List<RagonProperty> _properties;
|
||||||
private RagonEntity _entity;
|
private RagonEntity _entity;
|
||||||
|
private RagonBuffer _buffer;
|
||||||
|
|
||||||
public RagonEntityState(RagonEntity entity, int capacity = 10)
|
public RagonEntityState(RagonEntity entity, int capacity = 10)
|
||||||
{
|
{
|
||||||
_entity = entity;
|
_entity = entity;
|
||||||
|
_buffer = new RagonBuffer(8);
|
||||||
_properties = new List<RagonProperty>(capacity);
|
_properties = new List<RagonProperty>(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,8 +67,8 @@ public class RagonEntityState
|
|||||||
{
|
{
|
||||||
foreach (var property in _properties)
|
foreach (var property in _properties)
|
||||||
{
|
{
|
||||||
var hasPayload = property.IsFixed || !property.IsFixed && property.Size > 0;
|
var hasPayloadOrFixed = property.IsFixed || property is { IsFixed: false, Size: > 0 };
|
||||||
if (hasPayload)
|
if (hasPayloadOrFixed)
|
||||||
{
|
{
|
||||||
buffer.WriteBool(true);
|
buffer.WriteBool(true);
|
||||||
property.Write(buffer);
|
property.Write(buffer);
|
||||||
|
|||||||
@@ -14,10 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonEvent
|
public class RagonEvent
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonPayload
|
public class RagonPayload
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,11 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using System.ComponentModel;
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Entity;
|
||||||
|
|
||||||
public class RagonProperty : RagonPayload
|
public class RagonProperty : RagonPayload
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,40 +16,97 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
|
||||||
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class AuthorizationOperation: IRagonOperation
|
public sealed class AuthorizationOperation: IRagonOperation
|
||||||
{
|
{
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly RagonWebHookPlugin _ragonWebHook;
|
||||||
|
private readonly RagonContextObserver _contextObserver;
|
||||||
|
private readonly RagonBuffer _writer;
|
||||||
|
|
||||||
|
public AuthorizationOperation(
|
||||||
|
RagonWebHookPlugin ragonWebHook,
|
||||||
|
RagonContextObserver contextObserver,
|
||||||
|
RagonBuffer writer)
|
||||||
|
{
|
||||||
|
_ragonWebHook = ragonWebHook;
|
||||||
|
_contextObserver = contextObserver;
|
||||||
|
_writer = writer;
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var key = reader.ReadString();
|
if (context.ConnectionStatus == ConnectionStatus.InProcess)
|
||||||
var playerName = reader.ReadString();
|
{
|
||||||
var additionalPayload = new RagonPayload();
|
_logger.Warn("Player already request authorization!");
|
||||||
additionalPayload.Read(reader);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
context.LobbyPlayer.Name = playerName;
|
var configuration = context.Configuration;
|
||||||
context.LobbyPlayer.AdditionalData = Array.Empty<byte>();
|
var key = reader.ReadString();
|
||||||
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized;
|
var name = reader.ReadString();
|
||||||
|
var payload = reader.ReadString();
|
||||||
|
|
||||||
|
if (key == configuration.ServerKey)
|
||||||
|
{
|
||||||
|
if (_ragonWebHook.RequestAuthorization(context, name, payload))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, Guid.NewGuid().ToString(), name, payload);
|
||||||
|
context.SetPlayer(lobbyPlayer);
|
||||||
|
|
||||||
|
Approve(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Reject(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Approve(RagonContext context)
|
||||||
|
{
|
||||||
|
context.ConnectionStatus = ConnectionStatus.Authorized;
|
||||||
|
|
||||||
|
_contextObserver.OnAuthorized(context);
|
||||||
|
|
||||||
var playerId = context.LobbyPlayer.Id;
|
var playerId = context.LobbyPlayer.Id;
|
||||||
|
var playerName = context.LobbyPlayer.Name;
|
||||||
|
var playerPayload = context.LobbyPlayer.Payload;
|
||||||
|
|
||||||
writer.Clear();
|
_writer.Clear();
|
||||||
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
_writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
||||||
writer.WriteString(playerId);
|
_writer.WriteString(playerId);
|
||||||
writer.WriteString(playerName);
|
_writer.WriteString(playerName);
|
||||||
|
_writer.WriteString(playerPayload);
|
||||||
|
|
||||||
var sendData = writer.ToArray();
|
var sendData = _writer.ToArray();
|
||||||
context.Connection.Reliable.Send(sendData);
|
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 NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class EntityCreateOperation : IRagonOperation
|
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)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
@@ -32,18 +33,32 @@ public sealed class EntityCreateOperation : IRagonOperation
|
|||||||
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
||||||
var propertiesCount = reader.ReadUShort();
|
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,
|
||||||
|
BufferedEvents = context.Configuration.LimitBufferedEvents,
|
||||||
|
};
|
||||||
|
|
||||||
|
var entity = new RagonEntity(entityParameters);
|
||||||
for (var i = 0; i < propertiesCount; i++)
|
for (var i = 0; i < propertiesCount; i++)
|
||||||
{
|
{
|
||||||
var propertyType = reader.ReadBool();
|
var propertyType = reader.ReadBool();
|
||||||
var propertySize = reader.ReadUShort();
|
var propertySize = reader.ReadUShort();
|
||||||
|
|
||||||
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
|
entity.AddProperty(new RagonProperty(propertySize, propertyType));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reader.Capacity > 0)
|
if (reader.Capacity > 0)
|
||||||
entity.Payload.Read(reader);
|
entity.Payload.Read(reader);
|
||||||
|
|
||||||
|
var roomPlugin = room.Plugin;
|
||||||
|
if (!roomPlugin.OnEntityCreate(player, entity))
|
||||||
|
return;
|
||||||
|
|
||||||
|
entity.Attach(player);
|
||||||
room.AttachEntity(entity);
|
room.AttachEntity(entity);
|
||||||
player.AttachEntity(entity);
|
player.AttachEntity(entity);
|
||||||
|
|
||||||
|
|||||||
@@ -16,12 +16,13 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class EntityEventOperation : IRagonOperation
|
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)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using NLog;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
|
public sealed class EntityOwnershipOperation : IRagonOperation
|
||||||
|
{
|
||||||
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
|
{
|
||||||
|
var currentOwner = context.RoomPlayer;
|
||||||
|
var room = context.Room;
|
||||||
|
|
||||||
|
var entityId = reader.ReadUShort();
|
||||||
|
var playerPeerId = reader.ReadUShort();
|
||||||
|
|
||||||
|
if (!room.Entities.TryGetValue(entityId, out var entity))
|
||||||
|
{
|
||||||
|
_logger.Error($"Entity not found with id {entityId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity.Owner.Connection.Id != currentOwner.Connection.Id)
|
||||||
|
{
|
||||||
|
_logger.Error($"Player not owner of entity with id {entityId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!room.Players.TryGetValue(playerPeerId, out var nextOwner))
|
||||||
|
{
|
||||||
|
_logger.Error($"Player not found with id {entityId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-2
@@ -16,12 +16,13 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class EntityDestroyOperation: IRagonOperation
|
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)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
@@ -17,23 +17,23 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class EntityStateOperation: IRagonOperation
|
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)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
|
var player = context.RoomPlayer;
|
||||||
var entitiesCount = reader.ReadUShort();
|
var entitiesCount = reader.ReadUShort();
|
||||||
|
|
||||||
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
|
for (var entityIndex = 0; entityIndex < entitiesCount; entityIndex++)
|
||||||
{
|
{
|
||||||
var entityId = reader.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
if (room.Entities.TryGetValue(entityId, out var entity))
|
if (room.Entities.TryGetValue(entityId, out var entity) && entity.TryReadState(player, reader))
|
||||||
{
|
{
|
||||||
entity.State.Read(reader);
|
|
||||||
room.Track(entity);
|
room.Track(entity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public interface IRagonOperation
|
public interface IRagonOperation
|
||||||
{
|
{
|
||||||
@@ -16,17 +16,29 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class RoomCreateOperation: IRagonOperation
|
public sealed class RoomCreateOperation: IRagonOperation
|
||||||
{
|
{
|
||||||
private RagonRoomParameters _roomParameters = new();
|
private readonly RagonRoomParameters _roomParameters = new();
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly IServerPlugin _serverPlugin;
|
||||||
|
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
||||||
|
|
||||||
|
public RoomCreateOperation(IServerPlugin serverPlugin, RagonWebHookPlugin ragonWebHook)
|
||||||
|
{
|
||||||
|
_serverPlugin = serverPlugin;
|
||||||
|
_ragonWebHookPlugin = ragonWebHook;
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
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");
|
_logger.Warn($"Player {context.Connection.Id} not authorized for this request");
|
||||||
return;
|
return;
|
||||||
@@ -62,17 +74,22 @@ public sealed class RoomCreateOperation: IRagonOperation
|
|||||||
};
|
};
|
||||||
|
|
||||||
var lobbyPlayer = context.LobbyPlayer;
|
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);
|
||||||
|
|
||||||
|
roomPlayer.OnAttached(room);
|
||||||
|
|
||||||
var room = new RagonRoom(roomId, information);
|
|
||||||
context.Scheduler.Run(room);
|
context.Scheduler.Run(room);
|
||||||
context.Lobby.Persist(room);
|
context.Lobby.Persist(room);
|
||||||
|
context.SetRoom(room, roomPlayer);
|
||||||
|
|
||||||
var player = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
||||||
context.SetRoom(room, player);
|
|
||||||
|
|
||||||
_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}");
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to room {room.Id}");
|
||||||
}
|
}
|
||||||
@@ -84,9 +101,9 @@ public sealed class RoomCreateOperation: IRagonOperation
|
|||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteString(player.Id);
|
writer.WriteString(player.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
writer.WriteUShort((ushort) room.Info.Min);
|
writer.WriteUShort((ushort) room.PlayerMin);
|
||||||
writer.WriteUShort((ushort) room.Info.Max);
|
writer.WriteUShort((ushort) room.PlayerMax);
|
||||||
writer.WriteString(room.Info.Map);
|
writer.WriteString(room.Map);
|
||||||
|
|
||||||
var sendData = writer.ToArray();
|
var sendData = writer.ToArray();
|
||||||
player.Connection.Reliable.Send(sendData);
|
player.Connection.Reliable.Send(sendData);
|
||||||
|
|||||||
@@ -16,12 +16,22 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class RoomJoinOperation : IRagonOperation
|
public sealed class RoomJoinOperation : IRagonOperation
|
||||||
{
|
{
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
|
private readonly RagonWebHookPlugin _webHook;
|
||||||
|
|
||||||
|
public RoomJoinOperation(RagonWebHookPlugin plugin)
|
||||||
|
{
|
||||||
|
_webHook = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
@@ -30,42 +40,47 @@ public sealed class RoomJoinOperation : IRagonOperation
|
|||||||
|
|
||||||
if (!context.Lobby.FindRoomById(roomId, out var existsRoom))
|
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}");
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} failed to join room {roomId}");
|
||||||
return;
|
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);
|
context.SetRoom(existsRoom, player);
|
||||||
|
|
||||||
JoinSuccess(player, existsRoom, writer);
|
if (!existsRoom.Plugin.OnPlayerJoined(player))
|
||||||
|
return;
|
||||||
|
|
||||||
|
_webHook.RoomJoined(context, existsRoom, player);
|
||||||
|
|
||||||
|
JoinSuccess(context, existsRoom, writer);
|
||||||
|
|
||||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} joined to {existsRoom.Id}");
|
_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.Clear();
|
||||||
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
writer.WriteOperation(RagonOperation.JOIN_SUCCESS);
|
||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteString(player.Id);
|
writer.WriteString(context.RoomPlayer.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
writer.WriteUShort((ushort) room.Info.Min);
|
writer.WriteUShort((ushort) room.PlayerMin);
|
||||||
writer.WriteUShort((ushort) room.Info.Max);
|
writer.WriteUShort((ushort) room.PlayerMax);
|
||||||
writer.WriteString(room.Info.Map);
|
writer.WriteString(room.Map);
|
||||||
|
|
||||||
var sendData = writer.ToArray();
|
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.Clear();
|
||||||
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
writer.WriteOperation(RagonOperation.JOIN_FAILED);
|
||||||
writer.WriteString($"Room not exists");
|
writer.WriteString($"Room not exists");
|
||||||
|
|
||||||
var sendData = writer.ToArray();
|
var sendData = writer.ToArray();
|
||||||
player.Connection.Reliable.Send(sendData);
|
context.Connection.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,17 +16,29 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
||||||
{
|
{
|
||||||
private RagonRoomParameters _roomParameters = new();
|
private readonly RagonRoomParameters _roomParameters = new();
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly IServerPlugin _serverPlugin;
|
||||||
|
private readonly RagonWebHookPlugin _ragonWebHookPlugin;
|
||||||
|
|
||||||
|
public RoomJoinOrCreateOperation(IServerPlugin serverPlugin, RagonWebHookPlugin plugin)
|
||||||
|
{
|
||||||
|
_serverPlugin = serverPlugin;
|
||||||
|
_ragonWebHookPlugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
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");
|
_logger.Warn("Player not authorized for this request");
|
||||||
return;
|
return;
|
||||||
@@ -39,9 +51,11 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
|||||||
|
|
||||||
if (context.Lobby.FindRoomByMap(_roomParameters.Map, out var existsRoom))
|
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);
|
context.SetRoom(existsRoom, player);
|
||||||
|
|
||||||
|
_ragonWebHookPlugin.RoomJoined(context, existsRoom, player);
|
||||||
|
|
||||||
JoinSuccess(player, existsRoom, writer);
|
JoinSuccess(player, existsRoom, writer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -53,14 +67,17 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
|||||||
Min = _roomParameters.Min,
|
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);
|
||||||
|
|
||||||
|
_ragonWebHookPlugin.RoomCreated(context, room, roomPlayer);
|
||||||
|
|
||||||
context.Lobby.Persist(room);
|
context.Lobby.Persist(room);
|
||||||
context.Scheduler.Run(room);
|
context.Scheduler.Run(room);
|
||||||
|
|
||||||
var roomPlayer = new RagonRoomPlayer(lobbyPlayer.Connection, lobbyPlayer.Id, lobbyPlayer.Name);
|
|
||||||
context.SetRoom(room, roomPlayer);
|
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);
|
JoinSuccess(roomPlayer, room, writer);
|
||||||
}
|
}
|
||||||
@@ -73,9 +90,9 @@ public sealed class RoomJoinOrCreateOperation : IRagonOperation
|
|||||||
writer.WriteString(room.Id);
|
writer.WriteString(room.Id);
|
||||||
writer.WriteString(player.Id);
|
writer.WriteString(player.Id);
|
||||||
writer.WriteString(room.Owner.Id);
|
writer.WriteString(room.Owner.Id);
|
||||||
writer.WriteUShort((ushort) room.Info.Min);
|
writer.WriteUShort((ushort) room.PlayerMin);
|
||||||
writer.WriteUShort((ushort) room.Info.Max);
|
writer.WriteUShort((ushort) room.PlayerMax);
|
||||||
writer.WriteString(room.Info.Map);
|
writer.WriteString(room.Map);
|
||||||
|
|
||||||
var sendData = writer.ToArray();
|
var sendData = writer.ToArray();
|
||||||
player.Connection.Reliable.Send(sendData);
|
player.Connection.Reliable.Send(sendData);
|
||||||
|
|||||||
@@ -16,19 +16,34 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class RoomLeaveOperation: IRagonOperation
|
public sealed class RoomLeaveOperation: IRagonOperation
|
||||||
{
|
{
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly RagonWebHookPlugin _webHook;
|
||||||
|
public RoomLeaveOperation(RagonWebHookPlugin plugin)
|
||||||
|
{
|
||||||
|
_webHook = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
var roomPlayer = context.RoomPlayer;
|
var roomPlayer = context.RoomPlayer;
|
||||||
|
|
||||||
if (room != null)
|
if (room != null)
|
||||||
{
|
{
|
||||||
context.Room?.DetachPlayer(roomPlayer);
|
var plugin = room.Plugin;
|
||||||
|
|
||||||
|
plugin.OnPlayerLeaved(roomPlayer);
|
||||||
|
room.DetachPlayer(roomPlayer);
|
||||||
|
|
||||||
|
_webHook.RoomLeaved(context, room, roomPlayer);
|
||||||
|
|
||||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
|
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} leaved from {room.Id}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using NLog;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
|
public sealed class RoomOwnershipOperation : IRagonOperation
|
||||||
|
{
|
||||||
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,11 +18,11 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public class SceneLoadOperation: IRagonOperation
|
public class SceneLoadOperation: IRagonOperation
|
||||||
{
|
{
|
||||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,16 +16,24 @@
|
|||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Handler;
|
||||||
|
|
||||||
public sealed class SceneLoadedOperation : IRagonOperation
|
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)
|
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer)
|
||||||
{
|
{
|
||||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Unauthorized)
|
if (context.ConnectionStatus == ConnectionStatus.Unauthorized)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var owner = context.Room.Owner;
|
var owner = context.Room.Owner;
|
||||||
@@ -34,6 +42,7 @@ public sealed class SceneLoadedOperation : IRagonOperation
|
|||||||
|
|
||||||
if (player == owner)
|
if (player == owner)
|
||||||
{
|
{
|
||||||
|
|
||||||
var statics = reader.ReadUShort();
|
var statics = reader.ReadUShort();
|
||||||
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
for (var staticIndex = 0; staticIndex < statics; staticIndex++)
|
||||||
{
|
{
|
||||||
@@ -42,19 +51,31 @@ public sealed class SceneLoadedOperation : IRagonOperation
|
|||||||
var staticId = reader.ReadUShort();
|
var staticId = reader.ReadUShort();
|
||||||
var propertiesCount = 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++)
|
for (var propertyIndex = 0; propertyIndex < propertiesCount; propertyIndex++)
|
||||||
{
|
{
|
||||||
var propertyType = reader.ReadBool();
|
var propertyType = reader.ReadBool();
|
||||||
var propertySize = reader.ReadUShort();
|
var propertySize = reader.ReadUShort();
|
||||||
entity.State.AddProperty(new RagonProperty(propertySize, propertyType));
|
entity.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 playerInfo = $"Player {context.Connection.Id}|{context.LobbyPlayer.Name}";
|
||||||
var entityInfo = $"{entity.Id}:{entity.Type}";
|
var entityInfo = $"{entity.Id}:{entity.Type}";
|
||||||
|
|
||||||
_logger.Trace($"{playerInfo} created entity {entityInfo}");
|
_logger.Trace($"{playerInfo} created entity {entityInfo}");
|
||||||
|
|
||||||
|
entity.Attach(player);
|
||||||
room.AttachEntity(entity);
|
room.AttachEntity(entity);
|
||||||
player.AttachEntity(entity);
|
player.AttachEntity(entity);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* 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 NLog;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Http;
|
||||||
|
|
||||||
|
public class RagonHttpServer
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
|
||||||
|
private readonly IExecutor _executor;
|
||||||
|
private readonly IServerPlugin _serverPlugin;
|
||||||
|
private HttpListener _httpListener;
|
||||||
|
private CancellationTokenSource _cancellationTokenSource;
|
||||||
|
|
||||||
|
public RagonHttpServer(IExecutor executor, IServerPlugin serverPlugin)
|
||||||
|
{
|
||||||
|
_serverPlugin = serverPlugin;
|
||||||
|
_executor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void StartAccept(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var context = await _httpListener.GetContextAsync();
|
||||||
|
|
||||||
|
if (context.Request.HttpMethod != "POST")
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 404;
|
||||||
|
context.Response.ContentLength64 = 0;
|
||||||
|
context.Response.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = context.Request;
|
||||||
|
var reader = new StreamReader(request.InputStream, request.ContentEncoding);
|
||||||
|
var rawJson = await reader.ReadToEndAsync();
|
||||||
|
var httpCommand = JsonDocument.Parse(rawJson);
|
||||||
|
if (httpCommand != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var command = httpCommand.RootElement.GetProperty("command");
|
||||||
|
var payload = httpCommand.RootElement.GetProperty("payload");
|
||||||
|
|
||||||
|
if (_serverPlugin.OnCommand(command.GetString() ?? "none", payload.GetRawText()))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
context.Response.ContentLength64 = 0;
|
||||||
|
context.Response.Close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 403;
|
||||||
|
context.Response.ContentLength64 = 0;
|
||||||
|
context.Response.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(ex);
|
||||||
|
|
||||||
|
context.Response.StatusCode = 505;
|
||||||
|
context.Response.ContentLength64 = 0;
|
||||||
|
context.Response.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.StatusCode = 403;
|
||||||
|
context.Response.ContentLength64 = 0;
|
||||||
|
context.Response.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start(RagonServerConfiguration configuration)
|
||||||
|
{
|
||||||
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
_logger.Info($"Listen at http://0.0.0.0:{configuration.HttpPort}/");
|
||||||
|
|
||||||
|
_httpListener = new HttpListener();
|
||||||
|
_httpListener.Prefixes.Add($"http://127.0.0.1:{configuration.HttpPort}/");
|
||||||
|
_httpListener.Start();
|
||||||
|
|
||||||
|
_executor.Run(() => StartAccept(_cancellationTokenSource.Token), TaskCreationOptions.LongRunning);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
_cancellationTokenSource.Cancel();
|
||||||
|
_httpListener.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,23 +16,28 @@
|
|||||||
|
|
||||||
using System.Threading.Channels;
|
using System.Threading.Channels;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public class Executor : TaskScheduler, IExecutor
|
public class Executor : TaskScheduler, IExecutor
|
||||||
{
|
{
|
||||||
private ChannelReader<Task> _reader;
|
private readonly ChannelReader<Task> _reader;
|
||||||
private ChannelWriter<Task> _writer;
|
private readonly ChannelWriter<Task> _writer;
|
||||||
private Queue<Task> _pendingTasks;
|
private readonly Queue<Task> _pendingTasks;
|
||||||
private TaskFactory _taskFactory;
|
private readonly TaskFactory _taskFactory;
|
||||||
|
|
||||||
public void Run(Action action)
|
public Task Run(Action action, TaskCreationOptions task = TaskCreationOptions.None)
|
||||||
{
|
{
|
||||||
_taskFactory.StartNew(action);
|
return _taskFactory.StartNew(action, task);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Executor()
|
public Executor()
|
||||||
{
|
{
|
||||||
var channel = Channel.CreateUnbounded<Task>();
|
var channel = Channel.CreateUnbounded<Task>(new UnboundedChannelOptions()
|
||||||
|
{
|
||||||
|
SingleReader = true,
|
||||||
|
SingleWriter = true,
|
||||||
|
});
|
||||||
|
|
||||||
_reader = channel.Reader;
|
_reader = channel.Reader;
|
||||||
_writer = channel.Writer;
|
_writer = channel.Writer;
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public interface IExecutor
|
public interface IExecutor
|
||||||
{
|
{
|
||||||
public void Run(Action action);
|
public Task Run(Action action, TaskCreationOptions task = TaskCreationOptions.None);
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public interface INetworkChannel
|
public interface INetworkChannel
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,11 +14,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public interface INetworkConnection
|
public interface INetworkConnection
|
||||||
{
|
{
|
||||||
public ushort Id { get; }
|
public ushort Id { get; }
|
||||||
public INetworkChannel Reliable { get; }
|
public INetworkChannel Reliable { get; }
|
||||||
public INetworkChannel Unreliable { get; }
|
public INetworkChannel Unreliable { get; }
|
||||||
|
public void Close();
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public interface INetworkListener
|
public interface INetworkListener
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public interface INetworkServer
|
public interface INetworkServer
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.IO;
|
||||||
|
|
||||||
public struct NetworkConfiguration
|
public struct NetworkConfiguration
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Server;
|
||||||
|
|
||||||
|
public class RagonContextObserver
|
||||||
|
{
|
||||||
|
private Dictionary<string, RagonContext> _contexts;
|
||||||
|
public RagonContextObserver(Dictionary<string, RagonContext> contexts)
|
||||||
|
{
|
||||||
|
_contexts = contexts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnAuthorized(RagonContext context)
|
||||||
|
{
|
||||||
|
_contexts.Add(context.LobbyPlayer.Id, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.IO;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
|
||||||
|
namespace Ragon.Server;
|
||||||
|
|
||||||
|
public interface IRagonServer
|
||||||
|
{
|
||||||
|
RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection);
|
||||||
|
RagonLobbyPlayer? GetPlayerById(string id);
|
||||||
|
}
|
||||||
@@ -15,13 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Lobby;
|
||||||
|
|
||||||
public interface IRagonLobby
|
public interface IRagonLobby
|
||||||
{
|
{
|
||||||
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
public bool FindRoomById(string roomId, [MaybeNullWhen(false)] out RagonRoom room);
|
||||||
public bool FindRoomByMap(string map, [MaybeNullWhen(false)] out RagonRoom room);
|
public bool FindRoomByMap(string map, [MaybeNullWhen(false)] out RagonRoom room);
|
||||||
public void Persist(RagonRoom room);
|
public void Persist(RagonRoom room);
|
||||||
public void RemoveIfEmpty(RagonRoom room);
|
public bool RemoveIfEmpty(RagonRoom room);
|
||||||
}
|
}
|
||||||
@@ -16,8 +16,9 @@
|
|||||||
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Lobby;
|
||||||
|
|
||||||
public class LobbyInMemory : IRagonLobby
|
public class LobbyInMemory : IRagonLobby
|
||||||
{
|
{
|
||||||
@@ -28,8 +29,7 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
{
|
{
|
||||||
foreach (var existRagonRoom in _rooms)
|
foreach (var existRagonRoom in _rooms)
|
||||||
{
|
{
|
||||||
var info = existRagonRoom.Info;
|
if (existRagonRoom.Id == RagonRoomId && existRagonRoom.PlayerMin < existRagonRoom.PlayerMax)
|
||||||
if (existRagonRoom.Id == RagonRoomId && info.Min < info.Max)
|
|
||||||
{
|
{
|
||||||
room = existRagonRoom;
|
room = existRagonRoom;
|
||||||
return true;
|
return true;
|
||||||
@@ -44,8 +44,7 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
{
|
{
|
||||||
foreach (var existsRoom in _rooms)
|
foreach (var existsRoom in _rooms)
|
||||||
{
|
{
|
||||||
var info = existsRoom.Info;
|
if (existsRoom.Map == map && existsRoom.PlayerCount < existsRoom.PlayerMax)
|
||||||
if (info.Map == map && existsRoom.Players.Count < info.Max)
|
|
||||||
{
|
{
|
||||||
room = existsRoom;
|
room = existsRoom;
|
||||||
return true;
|
return true;
|
||||||
@@ -62,18 +61,23 @@ public class LobbyInMemory : IRagonLobby
|
|||||||
_logger.Trace($"New room: {room.Id}");
|
_logger.Trace($"New room: {room.Id}");
|
||||||
|
|
||||||
foreach (var r in _rooms)
|
foreach (var r in _rooms)
|
||||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count} Entities: {r.Entities.Count}");
|
_logger.Trace($"Room: {r.Id} Map: {r.Map} Players: {r.Players.Count} Entities: {r.Entities.Count}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveIfEmpty(RagonRoom room)
|
public bool RemoveIfEmpty(RagonRoom room)
|
||||||
{
|
{
|
||||||
|
var result = false;
|
||||||
if (room.Players.Count == 0)
|
if (room.Players.Count == 0)
|
||||||
{
|
{
|
||||||
_rooms.Remove(room);
|
_rooms.Remove(room);
|
||||||
_logger.Trace($"Room {room.Id} removed");
|
_logger.Trace($"Room {room.Id} removed");
|
||||||
|
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var r in _rooms)
|
foreach (var r in _rooms)
|
||||||
_logger.Trace($"Room: {r.Id} {r.Info} Players: {r.Players.Count} Entities: {r.Entities.Count}");
|
_logger.Trace($"Room: {r.Id} Map: {r.Map} Players: {r.Players.Count} Entities: {r.Entities.Count}");
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,28 +14,29 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server;
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
public enum LobbyPlayerStatus
|
namespace Ragon.Server.Lobby;
|
||||||
|
|
||||||
|
public enum ConnectionStatus
|
||||||
{
|
{
|
||||||
Unauthorized,
|
Unauthorized,
|
||||||
|
InProcess,
|
||||||
Authorized,
|
Authorized,
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RagonLobbyPlayer
|
public class RagonLobbyPlayer
|
||||||
{
|
{
|
||||||
|
public INetworkConnection Connection { get; }
|
||||||
public string Id { get; private set; }
|
public string Id { get; private set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; private set; }
|
||||||
public byte[] AdditionalData { get; set; }
|
public string Payload { get; private set; }
|
||||||
public LobbyPlayerStatus Status { get; set; }
|
|
||||||
public INetworkConnection Connection { get; private set; }
|
|
||||||
|
|
||||||
public RagonLobbyPlayer(INetworkConnection connection)
|
public RagonLobbyPlayer(INetworkConnection connection, string id, string name, string payload)
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid().ToString();
|
Id = id;
|
||||||
|
Name = name;
|
||||||
|
Payload = payload;
|
||||||
Connection = connection;
|
Connection = connection;
|
||||||
Status = LobbyPlayerStatus.Unauthorized;
|
|
||||||
Name = "None";
|
|
||||||
AdditionalData = Array.Empty<byte>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Entity;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
public class BaseRoomPlugin: IRoomPlugin
|
||||||
|
{
|
||||||
|
public IRagonRoom Room { get; private set; }
|
||||||
|
|
||||||
|
public virtual void OnAttached(IRagonRoom room)
|
||||||
|
{
|
||||||
|
Room = room;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnDetached()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#region VIRTUAL
|
||||||
|
|
||||||
|
public virtual bool OnPlayerJoined(RagonRoomPlayer player)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnPlayerLeaved(RagonRoomPlayer player)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Tick(float dt)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnEntityCreate(RagonRoomPlayer creator, IRagonEntity entity)
|
||||||
|
{
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnEntityRemove(RagonRoomPlayer remover, IRagonEntity entity)
|
||||||
|
{
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.IO;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
public class BaseServerPlugin: IServerPlugin
|
||||||
|
{
|
||||||
|
public IRagonServer Server { get; protected set; }
|
||||||
|
|
||||||
|
public virtual void OnAttached(IRagonServer server)
|
||||||
|
{
|
||||||
|
Server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnDetached()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool OnCommand(string command, string payload)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRoomPlugin CreateRoomPlugin(RoomInformation information)
|
||||||
|
{
|
||||||
|
return new BaseRoomPlugin();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Entity;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
public interface IRoomPlugin
|
||||||
|
{
|
||||||
|
void Tick(float dt);
|
||||||
|
void OnAttached(IRagonRoom room);
|
||||||
|
void OnDetached();
|
||||||
|
bool OnPlayerJoined(RagonRoomPlayer player);
|
||||||
|
bool OnPlayerLeaved(RagonRoomPlayer player);
|
||||||
|
bool OnEntityCreate(RagonRoomPlayer player, IRagonEntity entity);
|
||||||
|
bool OnEntityRemove(RagonRoomPlayer player, IRagonEntity entity);
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Http;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin;
|
||||||
|
|
||||||
|
public interface IServerPlugin
|
||||||
|
{
|
||||||
|
void OnAttached(IRagonServer server);
|
||||||
|
void OnDetached();
|
||||||
|
bool OnRoomCreate(RagonLobbyPlayer player, RagonRoom room);
|
||||||
|
bool OnRoomRemove(RagonLobbyPlayer player, RagonRoom room);
|
||||||
|
bool OnCommand(string command, string payload);
|
||||||
|
IRoomPlugin CreateRoomPlugin(RoomInformation information);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class PlayerDto
|
||||||
|
{
|
||||||
|
public string Id { get; set;}
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public PlayerDto(RagonRoomPlayer ragonRoomPlayer)
|
||||||
|
{
|
||||||
|
Id = ragonRoomPlayer.Id;
|
||||||
|
Name = ragonRoomPlayer.Name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RoomDto
|
||||||
|
{
|
||||||
|
public string Id { get; set;}
|
||||||
|
public int PlayerMin { get; set; }
|
||||||
|
public int PlayerMax { get; set; }
|
||||||
|
public int PlayerCount { get; set; }
|
||||||
|
public PlayerDto[] Players { get; set; }
|
||||||
|
|
||||||
|
public RoomDto(RagonRoom room)
|
||||||
|
{
|
||||||
|
Id = room.Id;
|
||||||
|
PlayerMin = room.PlayerMin;
|
||||||
|
PlayerMax = room.PlayerMax;
|
||||||
|
PlayerCount = room.PlayerCount;
|
||||||
|
|
||||||
|
Players = room.PlayerList.Select(p => new PlayerDto(p)).ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* 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 System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Handler;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
public class RagonWebHookPlugin
|
||||||
|
{
|
||||||
|
private Dictionary<string, string> _webHooks;
|
||||||
|
|
||||||
|
private RagonServer _server;
|
||||||
|
private HttpClient _httpClient;
|
||||||
|
|
||||||
|
public RagonWebHookPlugin(RagonServer server, RagonServerConfiguration configuration)
|
||||||
|
{
|
||||||
|
_webHooks = new Dictionary<string, string>(configuration.WebHooks);
|
||||||
|
_httpClient = new HttpClient();
|
||||||
|
_server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RequestAuthorization(RagonContext context, string name, string password)
|
||||||
|
{
|
||||||
|
if (_webHooks.TryGetValue("authorization-request", out var value))
|
||||||
|
{
|
||||||
|
var httpContent = new StringContent("");
|
||||||
|
var executor = context.Executor;
|
||||||
|
executor.Run(async () =>
|
||||||
|
{
|
||||||
|
var authorizationOperation = (AuthorizationOperation) _server.ResolveOperation(RagonOperation.AUTHORIZE);
|
||||||
|
var response = await _httpClient.PostAsync(new Uri(value), httpContent);
|
||||||
|
if (response.StatusCode != HttpStatusCode.OK)
|
||||||
|
{
|
||||||
|
authorizationOperation.Reject(context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
|
var authorizationResponse = JsonConvert.DeserializeObject<AuthorizationResponse>(content);
|
||||||
|
if (authorizationResponse != null)
|
||||||
|
{
|
||||||
|
var lobbyPlayer = new RagonLobbyPlayer(context.Connection, authorizationResponse.Id, authorizationResponse.Name, authorizationResponse.Payload);
|
||||||
|
|
||||||
|
context.SetPlayer(lobbyPlayer);
|
||||||
|
authorizationOperation.Approve(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
authorizationOperation.Reject(context);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RoomCreated(RagonContext context, RagonRoom room, RagonRoomPlayer player)
|
||||||
|
{
|
||||||
|
if (_webHooks.TryGetValue("room-created", out var value) && !string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
var request = new RoomCreatedRequest()
|
||||||
|
{
|
||||||
|
Room = new RoomDto(room),
|
||||||
|
Player = new PlayerDto(player)
|
||||||
|
};
|
||||||
|
var content = JsonContent.Create(request);
|
||||||
|
var executor = context.Executor;
|
||||||
|
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RoomRemoved(RagonContext context, RagonRoom ragonRoom)
|
||||||
|
{
|
||||||
|
if (_webHooks.TryGetValue("room-removed", out var value) && !string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
var request = new RoomRemovedRequest()
|
||||||
|
{
|
||||||
|
Room = new RoomDto(ragonRoom)
|
||||||
|
};
|
||||||
|
var content = JsonContent.Create(request);
|
||||||
|
var executor = context.Executor;
|
||||||
|
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RoomJoined(RagonContext context, RagonRoom existsRoom, RagonRoomPlayer player)
|
||||||
|
{
|
||||||
|
if (_webHooks.TryGetValue("room-joined", out var value) && !string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
var request = new RoomJoinedRequest()
|
||||||
|
{
|
||||||
|
Room = new RoomDto(existsRoom),
|
||||||
|
Player = new PlayerDto(player)
|
||||||
|
};
|
||||||
|
var content = JsonContent.Create(request);
|
||||||
|
var executor = context.Executor;
|
||||||
|
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RoomLeaved(RagonContext context, RagonRoom room, RagonRoomPlayer roomPlayer)
|
||||||
|
{
|
||||||
|
if (_webHooks.TryGetValue("room-leaved", out var value) && !string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
var request = new RoomLeavedRequest()
|
||||||
|
{
|
||||||
|
Room = new RoomDto(room),
|
||||||
|
Player = new PlayerDto(roomPlayer)
|
||||||
|
};
|
||||||
|
var content = JsonContent.Create(request);
|
||||||
|
var executor = context.Executor;
|
||||||
|
executor.Run(() => _httpClient.PostAsync(new Uri(value), content, CancellationToken.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-1
@@ -14,4 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
global using Xunit;
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class AuthorizationRequest
|
||||||
|
{
|
||||||
|
public string Name;
|
||||||
|
public string Token;
|
||||||
|
}
|
||||||
@@ -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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RoomCreatedRequest
|
||||||
|
{
|
||||||
|
public RoomDto Room { get; set; }
|
||||||
|
public PlayerDto Player { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
public class RoomJoinedRequest
|
||||||
|
{
|
||||||
|
public RoomDto Room { get; set; }
|
||||||
|
public PlayerDto Player { get; set; }
|
||||||
|
}
|
||||||
@@ -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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RoomLeavedRequest
|
||||||
|
{
|
||||||
|
public RoomDto Room { get; set; }
|
||||||
|
public PlayerDto Player { get; set; }
|
||||||
|
}
|
||||||
+5
-4
@@ -14,10 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Client;
|
|
||||||
|
|
||||||
public enum DisconnectReason
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RoomRemovedRequest
|
||||||
{
|
{
|
||||||
MANUAL,
|
public RoomDto Room { get; set; }
|
||||||
TIMEOUT,
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Server.Plugin.Web;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class AuthorizationResponse
|
||||||
|
{
|
||||||
|
public string Id;
|
||||||
|
public string Name;
|
||||||
|
public string Payload;
|
||||||
|
}
|
||||||
@@ -14,31 +14,45 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using Ragon.Core.Time;
|
using Ragon.Server.IO;
|
||||||
using Ragon.Server;
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
using Ragon.Server.Room;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server;
|
||||||
|
|
||||||
public class RagonContext
|
public class RagonContext
|
||||||
{
|
{
|
||||||
|
public ConnectionStatus ConnectionStatus { get; set; }
|
||||||
public INetworkConnection Connection { get; }
|
public INetworkConnection Connection { get; }
|
||||||
public IExecutor Executor { get; private set; }
|
public IExecutor Executor { get; private set; }
|
||||||
|
public RagonServerConfiguration Configuration { get; private set; }
|
||||||
public IRagonLobby Lobby { get; private set; }
|
public IRagonLobby Lobby { get; private set; }
|
||||||
public RagonLobbyPlayer LobbyPlayer { get; private set; }
|
public RagonLobbyPlayer? LobbyPlayer { get; private set; }
|
||||||
|
|
||||||
public RagonRoom? Room { get; private set; }
|
public RagonRoom? Room { get; private set; }
|
||||||
public RagonRoomPlayer? RoomPlayer { get; private set; }
|
public RagonRoomPlayer? RoomPlayer { get; private set; }
|
||||||
|
|
||||||
public RagonScheduler Scheduler { get; private set; }
|
public RagonScheduler Scheduler { get; private set; }
|
||||||
|
|
||||||
public RagonContext(INetworkConnection connection, IExecutor executor, IRagonLobby lobby, RagonScheduler scheduler, RagonLobbyPlayer lobbyPlayer)
|
public RagonContext(
|
||||||
|
INetworkConnection connection,
|
||||||
|
RagonServerConfiguration configuration,
|
||||||
|
IExecutor executor,
|
||||||
|
IRagonLobby lobby,
|
||||||
|
RagonScheduler scheduler)
|
||||||
{
|
{
|
||||||
|
ConnectionStatus = ConnectionStatus.Unauthorized;
|
||||||
|
Configuration = configuration;
|
||||||
Connection = connection;
|
Connection = connection;
|
||||||
Executor = executor;
|
Executor = executor;
|
||||||
Lobby = lobby;
|
Lobby = lobby;
|
||||||
Scheduler = scheduler;
|
Scheduler = scheduler;
|
||||||
LobbyPlayer = lobbyPlayer;
|
}
|
||||||
|
|
||||||
|
internal void SetPlayer(RagonLobbyPlayer player)
|
||||||
|
{
|
||||||
|
LobbyPlayer = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SetRoom(RagonRoom room, RagonRoomPlayer player)
|
internal void SetRoom(RagonRoom room, RagonRoomPlayer player)
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server;
|
||||||
|
|
||||||
public class RagonEntityCache
|
public class RagonEntityCache
|
||||||
|
|||||||
@@ -16,56 +16,77 @@
|
|||||||
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Ragon.Core.Time;
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
using Ragon.Server;
|
using Ragon.Server.Handler;
|
||||||
|
using Ragon.Server.Http;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Lobby;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Plugin.Web;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server;
|
||||||
|
|
||||||
public class RagonServer : INetworkListener
|
public class RagonServer : IRagonServer, INetworkListener
|
||||||
{
|
{
|
||||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||||
private readonly INetworkServer _server;
|
private readonly INetworkServer _server;
|
||||||
|
private readonly IRagonOperation[] _handlers;
|
||||||
|
private readonly IRagonLobby _lobby;
|
||||||
|
private readonly IServerPlugin _serverPlugin;
|
||||||
private readonly Thread _dedicatedThread;
|
private readonly Thread _dedicatedThread;
|
||||||
private readonly Executor _executor;
|
private readonly Executor _executor;
|
||||||
private readonly Configuration _configuration;
|
private readonly RagonServerConfiguration _configuration;
|
||||||
private readonly IRagonOperation[] _handlers;
|
private readonly RagonWebHookPlugin _webhooks;
|
||||||
|
private readonly RagonHttpServer _httpServer;
|
||||||
private readonly RagonBuffer _reader;
|
private readonly RagonBuffer _reader;
|
||||||
private readonly RagonBuffer _writer;
|
private readonly RagonBuffer _writer;
|
||||||
private readonly IRagonLobby _lobby;
|
|
||||||
private readonly RagonScheduler _scheduler;
|
private readonly RagonScheduler _scheduler;
|
||||||
private readonly Dictionary<ushort, RagonContext> _contexts;
|
private readonly Dictionary<ushort, RagonContext> _contextsByConnection;
|
||||||
private long _tickrate = 0;
|
private readonly Dictionary<string, RagonContext> _contextsByPlayerId;
|
||||||
private Stopwatch _timer;
|
private readonly Stopwatch _timer;
|
||||||
|
private readonly long _tickRate = 0;
|
||||||
|
|
||||||
public RagonServer(INetworkServer server, Configuration configuration)
|
public RagonServer(
|
||||||
|
INetworkServer server,
|
||||||
|
IServerPlugin plugin,
|
||||||
|
RagonServerConfiguration configuration)
|
||||||
{
|
{
|
||||||
_server = server;
|
_server = server;
|
||||||
_executor = _server.Executor;
|
_executor = _server.Executor;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_dedicatedThread = new Thread(Execute);
|
_serverPlugin = plugin;
|
||||||
_dedicatedThread.IsBackground = true;
|
_contextsByConnection = new Dictionary<ushort, RagonContext>();
|
||||||
_contexts = new Dictionary<ushort, RagonContext>();
|
_contextsByPlayerId = new Dictionary<string, RagonContext>();
|
||||||
_lobby = new LobbyInMemory();
|
_lobby = new LobbyInMemory();
|
||||||
_scheduler = new RagonScheduler();
|
_scheduler = new RagonScheduler();
|
||||||
|
_webhooks = new RagonWebHookPlugin(this, configuration);
|
||||||
|
_dedicatedThread = new Thread(Execute);
|
||||||
|
_dedicatedThread.IsBackground = true;
|
||||||
|
_httpServer = new RagonHttpServer(_executor, plugin);
|
||||||
_reader = new RagonBuffer();
|
_reader = new RagonBuffer();
|
||||||
_writer = new RagonBuffer();
|
_writer = new RagonBuffer();
|
||||||
_tickrate = 1000 / _configuration.ServerTickRate;
|
_tickRate = 1000 / _configuration.ServerTickRate;
|
||||||
_timer = new Stopwatch();
|
_timer = new Stopwatch();
|
||||||
|
|
||||||
|
var contextObserver = new RagonContextObserver(_contextsByPlayerId);
|
||||||
|
|
||||||
|
_serverPlugin.OnAttached(this);
|
||||||
|
|
||||||
_handlers = new IRagonOperation[byte.MaxValue];
|
_handlers = new IRagonOperation[byte.MaxValue];
|
||||||
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation();
|
_handlers[(byte) RagonOperation.AUTHORIZE] = new AuthorizationOperation(_webhooks, contextObserver, _writer);
|
||||||
_handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation();
|
_handlers[(byte) RagonOperation.JOIN_OR_CREATE_ROOM] = new RoomJoinOrCreateOperation(plugin, _webhooks);
|
||||||
_handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation();
|
_handlers[(byte) RagonOperation.CREATE_ROOM] = new RoomCreateOperation(plugin, _webhooks);
|
||||||
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation();
|
_handlers[(byte) RagonOperation.JOIN_ROOM] = new RoomJoinOperation(_webhooks);
|
||||||
_handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation();
|
_handlers[(byte) RagonOperation.LEAVE_ROOM] = new RoomLeaveOperation(_webhooks);
|
||||||
_handlers[(byte) RagonOperation.LOAD_SCENE] = new SceneLoadOperation();
|
_handlers[(byte) RagonOperation.LOAD_SCENE] = new SceneLoadOperation();
|
||||||
_handlers[(byte) RagonOperation.SCENE_LOADED] = new SceneLoadedOperation();
|
_handlers[(byte) RagonOperation.SCENE_LOADED] = new SceneLoadedOperation();
|
||||||
_handlers[(byte) RagonOperation.CREATE_ENTITY] = new EntityCreateOperation();
|
_handlers[(byte) RagonOperation.CREATE_ENTITY] = new EntityCreateOperation();
|
||||||
_handlers[(byte) RagonOperation.DESTROY_ENTITY] = new EntityDestroyOperation();
|
_handlers[(byte) RagonOperation.REMOVE_ENTITY] = new EntityDestroyOperation();
|
||||||
_handlers[(byte) RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventOperation();
|
_handlers[(byte) RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventOperation();
|
||||||
_handlers[(byte) RagonOperation.REPLICATE_ENTITY_STATE] = new EntityStateOperation();
|
_handlers[(byte) RagonOperation.REPLICATE_ENTITY_STATE] = new EntityStateOperation();
|
||||||
|
_handlers[(byte) RagonOperation.TRANSFER_ROOM_OWNERSHIP] = new EntityOwnershipOperation();
|
||||||
|
_handlers[(byte) RagonOperation.TRANSFER_ENTITY_OWNERSHIP] = new EntityOwnershipOperation();
|
||||||
|
|
||||||
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
|
_logger.Trace($"Server Tick Rate: {_configuration.ServerTickRate}");
|
||||||
}
|
}
|
||||||
@@ -75,13 +96,13 @@ public class RagonServer : INetworkListener
|
|||||||
_timer.Start();
|
_timer.Start();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (_timer.ElapsedMilliseconds > _tickrate)
|
if (_timer.ElapsedMilliseconds > _tickRate)
|
||||||
{
|
{
|
||||||
_executor.Update();
|
_scheduler.Update(_timer.ElapsedMilliseconds / 1000.0f);
|
||||||
_scheduler.Update();
|
|
||||||
_timer.Restart();
|
_timer.Restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_executor.Update();
|
||||||
_server.Update();
|
_server.Update();
|
||||||
Thread.Sleep(1);
|
Thread.Sleep(1);
|
||||||
}
|
}
|
||||||
@@ -97,6 +118,7 @@ public class RagonServer : INetworkListener
|
|||||||
Port = _configuration.Port,
|
Port = _configuration.Port,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
_httpServer.Start(_configuration);
|
||||||
_server.Start(this, networkConfiguration);
|
_server.Start(this, networkConfiguration);
|
||||||
|
|
||||||
if (executeInDedicatedThread)
|
if (executeInDedicatedThread)
|
||||||
@@ -107,31 +129,32 @@ public class RagonServer : INetworkListener
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
_serverPlugin.OnDetached();
|
||||||
_server.Stop();
|
_server.Stop();
|
||||||
_dedicatedThread.Interrupt();
|
_dedicatedThread.Interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnConnected(INetworkConnection connection)
|
public void OnConnected(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
var lobbyPlayer = new RagonLobbyPlayer(connection);
|
var context = new RagonContext(connection, _configuration, _executor, _lobby, _scheduler);
|
||||||
var context = new RagonContext(connection, _executor, _lobby, _scheduler, lobbyPlayer);
|
|
||||||
|
|
||||||
_logger.Trace($"Connected: {connection.Id}");
|
_logger.Trace($"Connected: {connection.Id}");
|
||||||
_contexts.Add(connection.Id, context);
|
_contextsByConnection.Add(connection.Id, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnDisconnected(INetworkConnection connection)
|
public void OnDisconnected(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
if (_contexts.Remove(connection.Id, out var context))
|
if (_contextsByConnection.Remove(connection.Id, out var context))
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
if (room != null)
|
if (room != null)
|
||||||
{
|
{
|
||||||
room.DetachPlayer(context.RoomPlayer);
|
room.DetachPlayer(context.RoomPlayer);
|
||||||
_lobby.RemoveIfEmpty(room);
|
if (_lobby.RemoveIfEmpty(room))
|
||||||
|
_webhooks.RoomRemoved(context, room);
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Trace($"Disconnected: {connection.Id}|{context.LobbyPlayer.Name}|{context.LobbyPlayer.Id}");
|
_logger.Trace($"Disconnected: {connection.Id}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -141,7 +164,7 @@ public class RagonServer : INetworkListener
|
|||||||
|
|
||||||
public void OnTimeout(INetworkConnection connection)
|
public void OnTimeout(INetworkConnection connection)
|
||||||
{
|
{
|
||||||
if (_contexts.Remove(connection.Id, out var context))
|
if (_contextsByConnection.Remove(connection.Id, out var context))
|
||||||
{
|
{
|
||||||
var room = context.Room;
|
var room = context.Room;
|
||||||
if (room != null)
|
if (room != null)
|
||||||
@@ -162,13 +185,12 @@ public class RagonServer : INetworkListener
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_contexts.TryGetValue(connection.Id, out var context))
|
if (_contextsByConnection.TryGetValue(connection.Id, out var context))
|
||||||
{
|
{
|
||||||
_writer.Clear();
|
_writer.Clear();
|
||||||
_reader.Clear();
|
_reader.Clear();
|
||||||
_reader.FromArray(data);
|
_reader.FromArray(data);
|
||||||
|
|
||||||
// Console.WriteLine($"{string.Join(",", data.Select(d => d.ToString()))}");
|
|
||||||
var operation = _reader.ReadByte();
|
var operation = _reader.ReadByte();
|
||||||
_handlers[operation].Handle(context, _reader, _writer);
|
_handlers[operation].Handle(context, _reader, _writer);
|
||||||
}
|
}
|
||||||
@@ -178,4 +200,23 @@ public class RagonServer : INetworkListener
|
|||||||
_logger.Error(ex);
|
_logger.Error(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IRagonOperation ResolveOperation(RagonOperation operation)
|
||||||
|
{
|
||||||
|
return _handlers[(byte)operation];
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonLobbyPlayer? GetPlayerByConnection(INetworkConnection connection)
|
||||||
|
{
|
||||||
|
return _contextsByConnection.TryGetValue(connection.Id, out var context) ?
|
||||||
|
context.LobbyPlayer :
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonLobbyPlayer? GetPlayerById(string playerId)
|
||||||
|
{
|
||||||
|
return _contextsByPlayerId.TryGetValue(playerId, out var context) ?
|
||||||
|
context.LobbyPlayer :
|
||||||
|
null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -25,26 +25,44 @@ public enum ServerType
|
|||||||
WEBSOCKET,
|
WEBSOCKET,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class WebHook
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public struct Configuration
|
public struct RagonServerConfiguration
|
||||||
{
|
{
|
||||||
public string ServerKey;
|
public string ServerKey;
|
||||||
public string ServerType;
|
public string ServerType;
|
||||||
public ushort ServerTickRate;
|
public ushort ServerTickRate;
|
||||||
public string GameProtocol;
|
public string GameProtocol;
|
||||||
public ushort Port;
|
public ushort Port;
|
||||||
|
public ushort HttpPort;
|
||||||
|
public string HttpKey;
|
||||||
public int LimitConnections;
|
public int LimitConnections;
|
||||||
public int LimitPlayersPerRoom;
|
public int LimitPlayersPerRoom;
|
||||||
public int LimitRooms;
|
public int LimitRooms;
|
||||||
|
public int LimitBufferedEvents;
|
||||||
|
public Dictionary<string, string> WebHooks;
|
||||||
|
|
||||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||||
private static readonly string ServerVersion = "1.1.0-rc";
|
private static readonly string ServerVersion = "1.2.0-rc";
|
||||||
private static Dictionary<string, ServerType> _serverTypes = new Dictionary<string, ServerType>()
|
private static Dictionary<string, ServerType> _serverTypes = new Dictionary<string, ServerType>()
|
||||||
{
|
{
|
||||||
{"enet", Server.ServerType.ENET},
|
{"enet", Server.ServerType.ENET},
|
||||||
{"websocket", Server.ServerType.WEBSOCKET}
|
{"websocket", Server.ServerType.WEBSOCKET}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static RagonServerConfiguration Load(string filePath)
|
||||||
|
{
|
||||||
|
CopyrightInfo();
|
||||||
|
|
||||||
|
var data = File.ReadAllText(filePath);
|
||||||
|
var configuration = JsonConvert.DeserializeObject<RagonServerConfiguration>(data);
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
private static void CopyrightInfo()
|
private static void CopyrightInfo()
|
||||||
{
|
{
|
||||||
Logger.Info($"Server Version: {ServerVersion}");
|
Logger.Info($"Server Version: {ServerVersion}");
|
||||||
@@ -53,20 +71,12 @@ public struct Configuration
|
|||||||
Logger.Info($"Processors: {Environment.ProcessorCount}");
|
Logger.Info($"Processors: {Environment.ProcessorCount}");
|
||||||
Logger.Info($"Runtime Version: {Environment.Version}");
|
Logger.Info($"Runtime Version: {Environment.Version}");
|
||||||
Logger.Info("==================================");
|
Logger.Info("==================================");
|
||||||
Logger.Info("| |");
|
Logger.Info(@" ___ _ ___ ___ _ _ ");
|
||||||
Logger.Info("| Ragon |");
|
Logger.Info(@" | _ \ /_\ / __|/ _ \| \| |");
|
||||||
Logger.Info("| |");
|
Logger.Info(@" | / / _ \ (_ | (_) | .` |");
|
||||||
|
Logger.Info(@" |_|_\/_/ \_\___|\___/|_|\_|");
|
||||||
Logger.Info("==================================");
|
Logger.Info("==================================");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Configuration Load(string filePath)
|
|
||||||
{
|
|
||||||
CopyrightInfo();
|
|
||||||
|
|
||||||
var data = File.ReadAllText(filePath);
|
|
||||||
var configuration = JsonConvert.DeserializeObject<Configuration>(data);
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ServerType GetServerType(string type) => _serverTypes[type];
|
public static ServerType GetServerType(string type) => _serverTypes[type];
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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.Server.Entity;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
|
||||||
|
namespace Ragon.Server.Room;
|
||||||
|
|
||||||
|
public interface IRagonRoom
|
||||||
|
{
|
||||||
|
RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection);
|
||||||
|
RagonRoomPlayer GetPlayerById(string id);
|
||||||
|
IRagonEntity GetEntityById(ushort id);
|
||||||
|
IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer id);
|
||||||
|
}
|
||||||
@@ -14,17 +14,26 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using Ragon.Core.Time;
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
using Ragon.Server.Entity;
|
||||||
|
using Ragon.Server.IO;
|
||||||
|
using Ragon.Server.Plugin;
|
||||||
|
using Ragon.Server.Time;
|
||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server.Room;
|
||||||
|
|
||||||
public class RagonRoom: IRagonAction
|
public class RagonRoom : IRagonRoom, IRagonAction
|
||||||
{
|
{
|
||||||
public string Id { get; private set; }
|
public string Id { get; private set; }
|
||||||
public RoomInformation Info { get; private set; }
|
public string Map { get; private set; }
|
||||||
|
public int PlayerMax { get; private set; }
|
||||||
|
public int PlayerMin { get; private set; }
|
||||||
|
public int PlayerCount => WaitPlayersList.Count;
|
||||||
|
|
||||||
public RagonRoomPlayer Owner { get; private set; }
|
public RagonRoomPlayer Owner { get; private set; }
|
||||||
public RagonBuffer Writer { get; }
|
public RagonBuffer Writer { get; }
|
||||||
|
public IRoomPlugin Plugin { get; private set; }
|
||||||
|
|
||||||
public Dictionary<ushort, RagonRoomPlayer> Players { get; private set; }
|
public Dictionary<ushort, RagonRoomPlayer> Players { get; private set; }
|
||||||
public List<RagonRoomPlayer> WaitPlayersList { get; private set; }
|
public List<RagonRoomPlayer> WaitPlayersList { get; private set; }
|
||||||
public List<RagonRoomPlayer> ReadyPlayersList { get; private set; }
|
public List<RagonRoomPlayer> ReadyPlayersList { get; private set; }
|
||||||
@@ -37,10 +46,13 @@ public class RagonRoom: IRagonAction
|
|||||||
|
|
||||||
private readonly HashSet<RagonEntity> _entitiesDirtySet;
|
private readonly HashSet<RagonEntity> _entitiesDirtySet;
|
||||||
|
|
||||||
public RagonRoom(string roomId, RoomInformation info)
|
public RagonRoom(string roomId, RoomInformation info, IRoomPlugin roomPlugin)
|
||||||
{
|
{
|
||||||
Id = roomId;
|
Id = roomId;
|
||||||
Info = info;
|
Map = info.Map;
|
||||||
|
PlayerMax = info.Max;
|
||||||
|
PlayerMin = info.Min;
|
||||||
|
Plugin = roomPlugin;
|
||||||
|
|
||||||
Players = new Dictionary<ushort, RagonRoomPlayer>(info.Max);
|
Players = new Dictionary<ushort, RagonRoomPlayer>(info.Max);
|
||||||
WaitPlayersList = new List<RagonRoomPlayer>(info.Max);
|
WaitPlayersList = new List<RagonRoomPlayer>(info.Max);
|
||||||
@@ -78,7 +90,7 @@ public class RagonRoom: IRagonAction
|
|||||||
_entitiesDirtySet.Remove(entity);
|
_entitiesDirtySet.Remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Tick()
|
public void Tick(float dt)
|
||||||
{
|
{
|
||||||
var entities = (ushort)_entitiesDirtySet.Count;
|
var entities = (ushort)_entitiesDirtySet.Count;
|
||||||
if (entities > 0)
|
if (entities > 0)
|
||||||
@@ -88,7 +100,7 @@ public class RagonRoom: IRagonAction
|
|||||||
Writer.WriteUShort(entities);
|
Writer.WriteUShort(entities);
|
||||||
|
|
||||||
foreach (var entity in _entitiesDirtySet)
|
foreach (var entity in _entitiesDirtySet)
|
||||||
entity.State.Write(Writer);
|
entity.WriteState(Writer);
|
||||||
|
|
||||||
_entitiesDirtySet.Clear();
|
_entitiesDirtySet.Clear();
|
||||||
|
|
||||||
@@ -141,15 +153,15 @@ public class RagonRoom: IRagonAction
|
|||||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||||
|
|
||||||
Writer.Clear();
|
Writer.Clear();
|
||||||
Writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
Writer.WriteOperation(RagonOperation.OWNERSHIP_ENTITY_CHANGED);
|
||||||
Writer.WriteString(Owner.Id);
|
Writer.WriteUShort(Owner.Connection.Id);
|
||||||
Writer.WriteUShort((ushort)entitiesToUpdate.Count);
|
Writer.WriteUShort((ushort)entitiesToUpdate.Count);
|
||||||
|
|
||||||
foreach (var entity in entitiesToUpdate)
|
foreach (var entity in entitiesToUpdate)
|
||||||
{
|
{
|
||||||
Writer.WriteUShort(entity.Id);
|
Writer.WriteUShort(entity.Id);
|
||||||
|
|
||||||
entity.SetOwner(nextOwner);
|
entity.Attach(nextOwner);
|
||||||
nextOwner.Entities.Add(entity);
|
nextOwner.Entities.Add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,12 +182,7 @@ public class RagonRoom: IRagonAction
|
|||||||
|
|
||||||
public void UpdateMap(string sceneName)
|
public void UpdateMap(string sceneName)
|
||||||
{
|
{
|
||||||
Info = new RoomInformation()
|
Map = sceneName;
|
||||||
{
|
|
||||||
Max = Info.Max,
|
|
||||||
Min = Info.Min,
|
|
||||||
Map = sceneName,
|
|
||||||
};
|
|
||||||
|
|
||||||
DynamicEntitiesList.Clear();
|
DynamicEntitiesList.Clear();
|
||||||
StaticEntitiesList.Clear();
|
StaticEntitiesList.Clear();
|
||||||
@@ -198,4 +205,26 @@ public class RagonRoom: IRagonAction
|
|||||||
foreach (var readyPlayer in ReadyPlayersList)
|
foreach (var readyPlayer in ReadyPlayersList)
|
||||||
readyPlayer.Connection.Reliable.Send(data);
|
readyPlayer.Connection.Reliable.Send(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RagonRoomPlayer GetPlayerByConnection(INetworkConnection connection)
|
||||||
|
{
|
||||||
|
return Players[connection.Id];
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonRoomPlayer? GetPlayerById(string id)
|
||||||
|
{
|
||||||
|
return PlayerList.FirstOrDefault(p => p.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRagonEntity? GetEntityById(ushort id)
|
||||||
|
{
|
||||||
|
return Entities.TryGetValue(id, out var entity) ?
|
||||||
|
entity :
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRagonEntity[] GetEntitiesOfPlayer(RagonRoomPlayer player)
|
||||||
|
{
|
||||||
|
return EntityList.Where(e => e.Owner.Connection.Id == player.Connection.Id).ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -16,14 +16,9 @@
|
|||||||
|
|
||||||
namespace Ragon.Server;
|
namespace Ragon.Server;
|
||||||
|
|
||||||
public class RoomInformation
|
public ref struct RoomInformation
|
||||||
{
|
{
|
||||||
public string Map { get; init; } = "none";
|
public string Map;
|
||||||
public int Min { get; init; }
|
public int Min;
|
||||||
public int Max { get; init; }
|
public int Max;
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"Map: {Map} Count: {Min}/{Max}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user