quantum-space-buddies/QSB/OwnershipSync/OwnershipManager.cs

94 lines
2.1 KiB
C#
Raw Normal View History

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
2023-05-08 18:30:59 +00:00
namespace QSB.OwnershipSync;
2022-03-03 03:46:33 +00:00
2023-05-08 18:30:59 +00:00
public static class OwnershipManager
2021-12-02 01:50:56 +00:00
{
2022-03-03 03:46:33 +00:00
#region host only
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
/// <summary>
/// whoever is first gets ownership
2022-03-03 03:46:33 +00:00
/// </summary>
2023-05-08 18:41:43 +00:00
private static readonly Dictionary<NetworkIdentity, List<uint>> _ownerQueue = new();
2022-01-15 05:18:05 +00:00
2023-05-08 18:41:43 +00:00
public static void RegisterOwnerQueue(this NetworkIdentity identity) => _ownerQueue.Add(identity, new List<uint>());
public static void UnregisterOwnerQueue(this NetworkIdentity identity) => _ownerQueue.Remove(identity);
2023-05-08 18:41:43 +00:00
public static void ServerUpdateOwnerQueue(this NetworkIdentity identity, uint id, OwnerQueueAction action)
2022-03-03 03:46:33 +00:00
{
2023-05-08 18:41:43 +00:00
var ownerQueue = _ownerQueue[identity];
var oldOwner = ownerQueue.Count != 0 ? ownerQueue[0] : uint.MaxValue;
2022-03-03 03:46:33 +00:00
switch (action)
2022-01-15 05:18:05 +00:00
{
2023-05-08 18:41:43 +00:00
case OwnerQueueAction.Add:
ownerQueue.SafeAdd(id);
2022-03-03 03:46:33 +00:00
break;
2023-05-08 18:41:43 +00:00
case OwnerQueueAction.Remove:
ownerQueue.Remove(id);
2022-03-03 03:46:33 +00:00
break;
2023-05-08 18:41:43 +00:00
case OwnerQueueAction.Force:
ownerQueue.Remove(id);
ownerQueue.Insert(0, id);
2022-03-03 03:46:33 +00:00
break;
2022-01-15 05:18:05 +00:00
}
2023-05-08 18:41:43 +00:00
var newOwner = ownerQueue.Count != 0 ? ownerQueue[0] : uint.MaxValue;
2022-03-03 03:46:33 +00:00
if (oldOwner != newOwner)
2022-01-15 05:18:05 +00:00
{
SetOwner(identity, newOwner);
2022-01-15 05:18:05 +00:00
}
2022-03-03 03:46:33 +00:00
}
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
/// <summary>
/// transfer ownership to a different client
2022-03-03 03:46:33 +00:00
/// </summary>
public static void OnDisconnect(NetworkConnectionToClient conn)
{
var id = conn.GetPlayerId();
2023-05-08 18:41:43 +00:00
foreach (var identity in _ownerQueue.Keys)
2022-01-15 05:18:05 +00:00
{
2023-05-08 18:41:43 +00:00
identity.ServerUpdateOwnerQueue(id, OwnerQueueAction.Remove);
2022-03-03 03:46:33 +00:00
}
}
2022-01-15 05:18:05 +00:00
public static void SetOwner(this NetworkIdentity identity, uint id)
2022-03-03 03:46:33 +00:00
{
var oldConn = identity.connectionToClient;
var newConn = id != uint.MaxValue ? id.GetNetworkConnection() : null;
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
if (oldConn == newConn)
{
return;
}
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
identity.RemoveClientAuthority();
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
if (newConn != null)
{
identity.AssignClientAuthority(newConn);
2022-01-15 05:18:05 +00:00
}
2022-03-03 03:46:33 +00:00
}
#endregion
2022-01-15 05:18:05 +00:00
2022-03-03 03:46:33 +00:00
#region any client
2022-01-15 05:18:05 +00:00
2023-05-08 18:41:43 +00:00
public static void UpdateOwnerQueue(this NetworkIdentity identity, OwnerQueueAction action)
2022-03-03 03:46:33 +00:00
{
2023-05-08 18:41:43 +00:00
if (action == OwnerQueueAction.Force && identity.isOwned)
2022-02-26 08:02:14 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2022-02-26 08:02:14 +00:00
}
2023-05-08 18:41:43 +00:00
new OwnerQueueMessage(identity.netId, action).Send();
2022-02-26 08:02:14 +00:00
}
2022-03-03 03:46:33 +00:00
#endregion
}