94 lines
2.2 KiB
C#
Raw Normal View History

2022-01-14 21:18:05 -08:00
using Mirror;
using QSB.Messaging;
2021-12-01 17:50:56 -08:00
using QSB.Utility;
2021-12-07 15:56:08 +00:00
using System.Collections.Generic;
using System.Linq;
2021-12-01 17:50:56 -08:00
namespace QSB.AuthoritySync
{
2022-01-14 22:36:13 -08:00
public static class AuthorityManager
2022-01-14 21:18:05 -08:00
{
#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>
2022-01-14 22:36:13 -08:00
public static void OnDisconnect(NetworkConnection conn)
2022-01-14 21:18:05 -08:00
{
2022-01-14 22:36:13 -08:00
var id = conn.GetPlayerId();
2022-01-14 21:18:05 -08:00
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
2022-01-16 00:58:30 -08:00
? NetworkServer.connections.Values.FirstOrDefault(x => x.GetPlayerId() == id)
2022-01-14 21:18:05 -08:00
: 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
}
2021-12-01 17:50:56 -08:00
}