quantum-space-buddies/QSB/AuthoritySync/AuthorityManager.cs
2022-01-14 22:08:06 -08:00

182 lines
4.3 KiB
C#

using Mirror;
using QSB.Messaging;
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Components;
using System.Collections.Generic;
using System.Linq;
namespace QSB.AuthoritySync
{
public static class AuthorityManager2
{
#region host only
/// <summary>
/// whoever is first gets authority
/// </summary>
private static readonly Dictionary<NetworkIdentity, List<uint>> _authQueue = new();
public static void RegisterAuthQueue(this NetworkIdentity identity) => _authQueue.Add(identity, new List<uint>());
public static void UnregisterAuthQueue(this NetworkIdentity identity) => _authQueue.Remove(identity);
public static void UpdateAuthQueue(this NetworkIdentity identity, uint id, AuthQueueAction action)
{
var authQueue = _authQueue[identity];
var oldOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
switch (action)
{
case AuthQueueAction.Add:
authQueue.SafeAdd(id);
break;
case AuthQueueAction.Remove:
authQueue.Remove(id);
break;
case AuthQueueAction.Force:
authQueue.Remove(id);
authQueue.Insert(0, id);
break;
}
var newOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
if (oldOwner != newOwner)
{
SetAuthority(identity, newOwner);
}
}
/// <summary>
/// transfer authority to a different client
/// </summary>
public static void OnDisconnect(uint id)
{
foreach (var identity in _authQueue.Keys)
{
identity.UpdateAuthQueue(id, AuthQueueAction.Remove);
}
}
public static void SetAuthority(this NetworkIdentity identity, uint id)
{
var oldConn = identity.connectionToClient;
var newConn = id != uint.MaxValue
? NetworkServer.connections.Values.First(x => x.GetPlayerId() == id)
: null;
if (oldConn == newConn)
{
return;
}
identity.RemoveClientAuthority();
if (newConn != null)
{
identity.AssignClientAuthority(newConn);
}
// DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - "
// + $"set authority to {id}");
}
#endregion
#region any client
public static void SendAuthQueueMessage(this NetworkIdentity identity, AuthQueueAction action) =>
new AuthQueueMessage(identity.netId, action).Send();
#endregion
}
public static class AuthorityManager
{
#region host only
/// <summary>
/// whoever is first gets authority
/// </summary>
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);
public static void UpdateAuthQueue(this QNetworkIdentity identity, uint id, AuthQueueAction action)
{
var authQueue = _authQueue[identity];
var oldOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
switch (action)
{
case AuthQueueAction.Add:
authQueue.SafeAdd(id);
break;
case AuthQueueAction.Remove:
authQueue.Remove(id);
break;
case AuthQueueAction.Force:
authQueue.Remove(id);
authQueue.Insert(0, id);
break;
}
var newOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
if (oldOwner != newOwner)
{
SetAuthority(identity, newOwner);
}
}
/// <summary>
/// transfer authority to a different client
/// </summary>
public static void OnDisconnect(uint id)
{
foreach (var identity in _authQueue.Keys)
{
identity.UpdateAuthQueue(id, AuthQueueAction.Remove);
}
}
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);
}
// DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - "
// + $"set authority to {id}");
}
#endregion
#region any client
public static void SendAuthQueueMessage(this QNetworkIdentity identity, AuthQueueAction action) =>
throw new System.NotImplementedException();
#endregion
}
}