97 lines
2.2 KiB
C#
Raw Normal View History

2021-12-22 01:16:44 -08:00
using QSB.Messaging;
2021-12-01 17:50:56 -08: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-01 17:50:56 -08:00
namespace QSB.AuthoritySync
{
public static class AuthorityManager
{
#region host only
2021-12-17 20:11:38 -08:00
/// <summary>
2021-12-01 17:50:56 -08:00
/// whoever is first gets authority
2021-12-17 20:11:38 -08:00
/// </summary>
2021-12-01 17:50:56 -08: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-17 20:11:38 -08:00
public static void UpdateAuthQueue(this QNetworkIdentity identity, uint id, AuthQueueAction action)
2021-12-01 17:50:56 -08:00
{
var authQueue = _authQueue[identity];
2021-12-17 23:28:29 -08:00
var oldOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
2021-12-01 17:50:56 -08:00
2021-12-17 20:11:38 -08:00
switch (action)
2021-12-01 17:50:56 -08:00
{
2021-12-17 20:11:38 -08:00
case AuthQueueAction.Add:
2021-12-17 23:28:29 -08:00
authQueue.SafeAdd(id);
2021-12-17 20:11:38 -08:00
break;
case AuthQueueAction.Remove:
2021-12-17 23:28:29 -08:00
authQueue.Remove(id);
2021-12-17 20:11:38 -08:00
break;
case AuthQueueAction.Force:
authQueue.Remove(id);
authQueue.Insert(0, id);
break;
2021-12-01 17:50:56 -08:00
}
var newOwner = authQueue.Count != 0 ? authQueue[0] : uint.MaxValue;
2021-12-17 23:28:29 -08:00
if (oldOwner != newOwner)
{
SetAuthority(identity, newOwner);
}
2021-12-01 17:50:56 -08:00
}
2021-12-17 20:11:38 -08:00
/// <summary>
2021-12-01 17:50:56 -08:00
/// transfer authority to a different client
2021-12-17 20:11:38 -08:00
/// </summary>
2021-12-01 17:50:56 -08:00
public static void OnDisconnect(uint id)
{
foreach (var identity in _authQueue.Keys)
{
2021-12-17 20:11:38 -08:00
identity.UpdateAuthQueue(id, AuthQueueAction.Remove);
2021-12-01 17:50:56 -08: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 13:40:13 -08:00
// DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - "
2021-12-07 15:56:08 +00:00
// + $"set authority to {id}");
2021-12-01 17:50:56 -08:00
}
#endregion
#region any client
2021-12-22 18:37:24 -08:00
public static void SendAuthQueueMessage(this QNetworkIdentity identity, AuthQueueAction action) =>
2021-12-22 17:13:54 -08:00
new AuthQueueMessage(identity.NetId, action).Send();
2021-12-01 17:50:56 -08:00
#endregion
}
}