suspension functionality

This commit is contained in:
JohnCorby 2021-11-30 16:57:04 -08:00
parent 3e28cbfee1
commit 9ed1a1caa8
4 changed files with 128 additions and 4 deletions

View File

@ -101,5 +101,6 @@
public static string QSBLearnLaunchCodes = "QSBLearnLaunchCodes";
public static string QSBSatelliteRepairTick = "QSBSatelliteRepairTick";
public static string QSBSatelliteRepaired = "QSBSatelliteRepairTick";
public static string QSBSuspensionChange = "QSBSuspensionChange";
}
}

View File

@ -1,4 +1,5 @@
using OWML.Common;
using System.Collections.Generic;
using OWML.Common;
using QSB.Anglerfish.Events;
using QSB.Animation.NPC.Events;
using QSB.Animation.Player.Events;
@ -21,6 +22,7 @@ using QSB.ShipSync.Events;
using QSB.ShipSync.Events.Component;
using QSB.ShipSync.Events.Hull;
using QSB.StatueSync.Events;
using QSB.SuspensionAuthoritySync;
using QSB.TimeSync.Events;
using QSB.Tools.FlashlightTool.Events;
using QSB.Tools.ProbeLauncherTool.Events;
@ -32,9 +34,6 @@ using QSB.Tools.TranslatorTool.TranslationSync.Events;
using QSB.Utility;
using QSB.Utility.Events;
using QSB.ZeroGCaveSync.Events;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QSB.Events
{
@ -112,6 +111,7 @@ namespace QSB.Events
new IdentifyFrequencyEvent(),
new IdentifySignalEvent(),
new NpcAnimationEvent(),
new SuspensionChangeEvent(),
// Ship
new FlyShipEvent(),
new HatchEvent(),

View File

@ -0,0 +1,97 @@
using System.Collections.Generic;
using System.Linq;
using QSB.Events;
using QSB.Utility;
using QuantumUNET;
using QuantumUNET.Components;
namespace QSB.SuspensionAuthoritySync
{
public class SuspensionChangeEvent : QSBEvent<SuspensionChangeMessage>
{
private static Dictionary<QNetworkIdentity, List<uint>> _potentialOwners;
public override void SetupListener()
{
if (QSBCore.IsHost)
{
_potentialOwners = new Dictionary<QNetworkIdentity, List<uint>>();
}
GlobalMessenger<QNetworkIdentity, bool>.AddListener(EventNames.QSBSuspensionChange, Handler);
}
public override void CloseListener()
{
if (QSBCore.IsHost)
{
_potentialOwners = null;
}
GlobalMessenger<QNetworkIdentity, bool>.RemoveListener(EventNames.QSBSuspensionChange, Handler);
}
private void Handler(QNetworkIdentity identity, bool suspended) => SendEvent(CreateMessage(identity, suspended));
private SuspensionChangeMessage CreateMessage(QNetworkIdentity identity, bool suspended) => new()
{
OnlySendToHost = true,
Identity = identity,
Suspended = suspended
};
public override void OnReceiveLocal(bool isHost, SuspensionChangeMessage message) => OnReceive(message);
public override void OnReceiveRemote(bool isHost, SuspensionChangeMessage message) => OnReceive(message);
private static void OnReceive(SuspensionChangeMessage message)
{
if (!QSBCore.WorldObjectsReady)
{
return;
}
// init owner list for this identity
if (!_potentialOwners.TryGetValue(message.Identity, out var potentialOwners))
{
potentialOwners = new List<uint>();
_potentialOwners[message.Identity] = potentialOwners;
}
if (!message.Suspended)
{
potentialOwners.Add(message.FromId);
}
else
{
potentialOwners.Remove(message.FromId);
}
var newOwner = potentialOwners.Count != 0 ? potentialOwners[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($"{identity.NetId}:{identity.gameObject.name} - set authority to {id}");
}
}
}

View File

@ -0,0 +1,26 @@
using QSB.Messaging;
using QuantumUNET.Components;
using QuantumUNET.Transport;
namespace QSB.SuspensionAuthoritySync
{
public class SuspensionChangeMessage : PlayerMessage
{
public QNetworkIdentity Identity;
public bool Suspended;
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Identity = reader.ReadNetworkIdentity();
Suspended = reader.ReadBoolean();
}
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Identity);
writer.Write(Suspended);
}
}
}