remove player from suspend tracker when they leave

This commit is contained in:
JohnCorby 2021-11-30 21:21:25 -08:00
parent 43d9dc56cd
commit 1934e89040
2 changed files with 71 additions and 50 deletions

View File

@ -1,8 +1,4 @@
using System.Linq;
using QSB.Events;
using QSB.Player;
using QSB.Utility;
using QuantumUNET;
using QSB.Events;
using QuantumUNET.Components;
namespace QSB.SuspendableSync
@ -35,50 +31,7 @@ namespace QSB.SuspendableSync
return;
}
var unsuspendedFor = SuspendableManager._unsuspendedFor[message.Identity];
var suspended = !unsuspendedFor.Contains(message.FromId);
if (message.Suspended == suspended)
{
return;
}
if (!message.Suspended)
{
unsuspendedFor.Add(message.FromId);
}
else
{
unsuspendedFor.Remove(message.FromId);
}
var newOwner = unsuspendedFor.Count != 0 ? unsuspendedFor[0] : uint.MaxValue;
SetAuthority(message.Identity, newOwner);
}
private static void SetAuthority(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($"{QSBPlayerManager.LocalPlayerId}.{identity.NetId}:{identity.gameObject.name} - set authority to {id}");
SuspendableManager.SetSuspended(message.FromId, message.Identity, message.Suspended);
}
}
}

View File

@ -1,4 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using QSB.Player;
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Components;
namespace QSB.SuspendableSync
@ -6,9 +10,73 @@ namespace QSB.SuspendableSync
/// all of this is host only
public static class SuspendableManager
{
internal static readonly Dictionary<QNetworkIdentity, List<uint>> _unsuspendedFor = new();
private static readonly Dictionary<QNetworkIdentity, List<uint>> _unsuspendedFor = new();
static SuspendableManager() => QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
private static void OnPlayerLeave(uint id)
{
if (!QSBCore.IsHost)
{
return;
}
foreach (var (identity, _) in _unsuspendedFor)
{
SetSuspended(id, identity, false);
}
}
public static void Register(QNetworkIdentity identity) => _unsuspendedFor.Add(identity, new List<uint>());
public static void Unregister(QNetworkIdentity identity) => _unsuspendedFor.Remove(identity);
public static void SetSuspended(uint id, QNetworkIdentity identity, bool suspended)
{
var unsuspendedFor = _unsuspendedFor[identity];
var oldSuspended = !unsuspendedFor.Contains(id);
if (suspended == oldSuspended)
{
return;
}
if (!suspended)
{
unsuspendedFor.Add(id);
}
else
{
unsuspendedFor.Remove(id);
}
var newOwner = unsuspendedFor.Count != 0 ? unsuspendedFor[0] : uint.MaxValue;
SetAuthority(identity, newOwner);
}
private static void SetAuthority(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($"{QSBPlayerManager.LocalPlayerId}.{identity.NetId}:{identity.gameObject.name} - set authority to {id}");
}
}
}