2022-01-15 05:18:05 +00:00
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.Messaging;
|
2021-12-02 01:50:56 +00:00
|
|
|
|
using QSB.Utility;
|
2021-12-07 15:56:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-12-02 01:50:56 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
namespace QSB.AuthoritySync
|
2021-12-02 01:50:56 +00:00
|
|
|
|
{
|
2022-02-27 12:40:44 +00:00
|
|
|
|
public static class AuthorityManager
|
|
|
|
|
{
|
|
|
|
|
#region host only
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// whoever is first gets authority
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly Dictionary<NetworkIdentity, List<uint>> _authQueue = new();
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
public static void RegisterAuthQueue(this NetworkIdentity identity) => _authQueue.Add(identity, new List<uint>());
|
|
|
|
|
public static void UnregisterAuthQueue(this NetworkIdentity identity) => _authQueue.Remove(identity);
|
2022-02-25 06:04:54 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
public static void ServerUpdateAuthQueue(this NetworkIdentity identity, uint id, AuthQueueAction action)
|
2022-01-15 05:18:05 +00:00
|
|
|
|
{
|
2022-02-27 12:40:44 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2022-01-15 05:18:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// transfer authority to a different client
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void OnDisconnect(NetworkConnectionToClient conn)
|
2022-01-15 05:18:05 +00:00
|
|
|
|
{
|
2022-02-27 12:40:44 +00:00
|
|
|
|
var id = conn.GetPlayerId();
|
|
|
|
|
foreach (var identity in _authQueue.Keys)
|
|
|
|
|
{
|
|
|
|
|
identity.ServerUpdateAuthQueue(id, AuthQueueAction.Remove);
|
|
|
|
|
}
|
2022-01-15 05:18:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
public static void SetAuthority(this NetworkIdentity identity, uint id)
|
2022-01-15 05:18:05 +00:00
|
|
|
|
{
|
2022-02-27 12:40:44 +00:00
|
|
|
|
var oldConn = identity.connectionToClient;
|
|
|
|
|
var newConn = id != uint.MaxValue ? id.GetNetworkConnection() : null;
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
if (oldConn == newConn)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
identity.RemoveClientAuthority();
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
if (newConn != null)
|
|
|
|
|
{
|
|
|
|
|
identity.AssignClientAuthority(newConn);
|
|
|
|
|
}
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
// DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - "
|
|
|
|
|
// + $"set authority to {id}");
|
2022-01-15 05:18:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
#endregion
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
#region any client
|
2022-01-15 05:18:05 +00:00
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
public static void UpdateAuthQueue(this NetworkIdentity identity, AuthQueueAction action)
|
2022-02-26 08:02:14 +00:00
|
|
|
|
{
|
2022-02-27 12:40:44 +00:00
|
|
|
|
if (action == AuthQueueAction.Force && identity.hasAuthority)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new AuthQueueMessage(identity.netId, action).Send();
|
2022-02-26 08:02:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 12:40:44 +00:00
|
|
|
|
#endregion
|
2022-02-26 08:02:14 +00:00
|
|
|
|
}
|
2022-02-25 06:04:54 +00:00
|
|
|
|
}
|