quantum-space-buddies/QSB/AuthoritySync/AuthorityManager.cs

97 lines
2.2 KiB
C#
Raw Normal View History

2021-12-22 09:16:44 +00:00
using QSB.Messaging;
2021-12-02 01:50:56 +00:00
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Components;
2021-12-07 15:56:08 +00:00
using System.Collections.Generic;
using System.Linq;
2021-12-02 01:50:56 +00:00
namespace QSB.AuthoritySync
{
public static class AuthorityManager
{
#region host only
2021-12-18 04:11:38 +00:00
/// <summary>
2021-12-02 01:50:56 +00:00
/// whoever is first gets authority
2021-12-18 04:11:38 +00:00
/// </summary>
2021-12-02 01:50:56 +00:00
private static readonly Dictionary<QNetworkIdentity, List<uint>> _authQueue = new();
public static void RegisterAuthQueue(this QNetworkIdentity identity) => _authQueue.Add(identity, new List<uint>());
public static void UnregisterAuthQueue(this QNetworkIdentity identity) => _authQueue.Remove(identity);
2021-12-18 04:11:38 +00:00
public static void UpdateAuthQueue(this QNetworkIdentity identity, uint id, AuthQueueAction action)
2021-12-02 01:50:56 +00:00
{
var authQueue = _authQueue[identity];
2021-12-18 07:28:29 +00:00
var oldOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
2021-12-02 01:50:56 +00:00
2021-12-18 04:11:38 +00:00
switch (action)
2021-12-02 01:50:56 +00:00
{
2021-12-18 04:11:38 +00:00
case AuthQueueAction.Add:
2021-12-18 07:28:29 +00:00
authQueue.SafeAdd(id);
2021-12-18 04:11:38 +00:00
break;
case AuthQueueAction.Remove:
2021-12-18 07:28:29 +00:00
authQueue.Remove(id);
2021-12-18 04:11:38 +00:00
break;
case AuthQueueAction.Force:
authQueue.Remove(id);
authQueue.Insert(0, id);
break;
2021-12-02 01:50:56 +00:00
}
var newOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
2021-12-18 07:28:29 +00:00
if (oldOwner != newOwner)
{
SetAuthority(identity, newOwner);
}
2021-12-02 01:50:56 +00:00
}
2021-12-18 04:11:38 +00:00
/// <summary>
2021-12-02 01:50:56 +00:00
/// transfer authority to a different client
2021-12-18 04:11:38 +00:00
/// </summary>
2021-12-02 01:50:56 +00:00
public static void OnDisconnect(uint id)
{
foreach (var identity in _authQueue.Keys)
{
2021-12-18 04:11:38 +00:00
identity.UpdateAuthQueue(id, AuthQueueAction.Remove);
2021-12-02 01:50:56 +00:00
}
}
public static void SetAuthority(this QNetworkIdentity identity, uint id)
{
var oldConn = identity.ClientAuthorityOwner;
var newConn = id != uint.MaxValue
? QNetworkServer.connections.First(x => x.GetPlayerId() == id)
: null;
if (oldConn == newConn)
{
return;
}
if (oldConn != null)
{
identity.RemoveClientAuthority(oldConn);
}
if (newConn != null)
{
identity.AssignClientAuthority(newConn);
}
2021-12-02 21:40:13 +00:00
// DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - "
2021-12-07 15:56:08 +00:00
// + $"set authority to {id}");
2021-12-02 01:50:56 +00:00
}
#endregion
#region any client
2021-12-23 02:37:24 +00:00
public static void SendAuthQueueMessage(this QNetworkIdentity identity, AuthQueueAction action) =>
2021-12-23 01:13:54 +00:00
new AuthQueueMessage(identity.NetId, action).Send();
2021-12-02 01:50:56 +00:00
#endregion
}
}