mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-02-22 12:39:51 +00:00
suspension functionality
This commit is contained in:
parent
3e28cbfee1
commit
9ed1a1caa8
@ -101,5 +101,6 @@
|
||||
public static string QSBLearnLaunchCodes = "QSBLearnLaunchCodes";
|
||||
public static string QSBSatelliteRepairTick = "QSBSatelliteRepairTick";
|
||||
public static string QSBSatelliteRepaired = "QSBSatelliteRepairTick";
|
||||
public static string QSBSuspensionChange = "QSBSuspensionChange";
|
||||
}
|
||||
}
|
||||
|
@ -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(),
|
||||
|
97
QSB/SuspensionAuthoritySync/SuspensionChangeEvent.cs
Normal file
97
QSB/SuspensionAuthoritySync/SuspensionChangeEvent.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
26
QSB/SuspensionAuthoritySync/SuspensionChangeMessage.cs
Normal file
26
QSB/SuspensionAuthoritySync/SuspensionChangeMessage.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user