quantum-space-buddies/QSB/OrbSync/Events/OrbUserEvent.cs

80 lines
2.5 KiB
C#
Raw Normal View History

2020-09-29 20:34:46 +00:00
using OWML.Common;
2020-11-03 21:42:14 +00:00
using QSB.EventsCore;
2020-09-29 20:34:46 +00:00
using QSB.Utility;
using QSB.WorldSync;
2020-11-03 22:18:22 +00:00
using QSB.WorldSync.Events;
2020-12-04 22:15:41 +00:00
using QuantumUNET;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Components;
2020-09-05 12:24:51 +00:00
using System.Linq;
2020-11-03 17:56:48 +00:00
namespace QSB.OrbSync.Events
{
2020-12-02 21:29:53 +00:00
public class OrbUserEvent : QSBEvent<WorldObjectMessage>
{
public override EventType Type => EventType.OrbUser;
2020-12-02 21:29:53 +00:00
public override void SetupListener() => GlobalMessenger<int>.AddListener(EventNames.QSBOrbUser, Handler);
public override void CloseListener() => GlobalMessenger<int>.RemoveListener(EventNames.QSBOrbUser, Handler);
2020-12-02 21:29:53 +00:00
private void Handler(int id) => SendEvent(CreateMessage(id));
2020-12-02 21:29:53 +00:00
private WorldObjectMessage CreateMessage(int id) => new WorldObjectMessage
{
AboutId = LocalPlayerId,
ObjectId = id
};
2020-12-02 21:29:53 +00:00
public override void OnServerReceive(WorldObjectMessage message)
{
var fromPlayer = QSBNetworkServer.connections.First(x => x.GetPlayer().PlayerId == message.FromId);
2020-12-11 13:14:58 +00:00
if (QSBWorldSync.OrbSyncList.Count == 0)
2020-12-02 21:29:53 +00:00
{
DebugLog.ToConsole($"Error - OrbSyncList is empty. (ID {message.ObjectId})", MessageType.Error);
return;
}
if (fromPlayer == null)
{
DebugLog.DebugWrite("Error - FromPlayer is null!", MessageType.Error);
}
2020-12-11 13:14:58 +00:00
var orb = QSBWorldSync.OrbSyncList
.First(x => x.AttachedOrb == QSBWorldSync.OldOrbList[message.ObjectId]);
2020-12-02 21:29:53 +00:00
if (orb == null)
{
DebugLog.ToConsole($"Error - No orb found for user event. (ID {message.ObjectId})", MessageType.Error);
return;
}
var orbIdentity = orb.GetComponent<QSBNetworkIdentity>();
if (orbIdentity == null)
{
DebugLog.ToConsole($"Error - Orb identity is null. (ID {message.ObjectId})", MessageType.Error);
return;
}
if (orbIdentity.ClientAuthorityOwner != null && orbIdentity.ClientAuthorityOwner != fromPlayer)
{
orbIdentity.RemoveClientAuthority(orbIdentity.ClientAuthorityOwner);
}
orbIdentity.AssignClientAuthority(fromPlayer);
orb.enabled = true;
}
2020-09-06 08:07:31 +00:00
2020-12-02 21:29:53 +00:00
public override void OnReceiveRemote(WorldObjectMessage message)
{
2020-12-11 13:14:58 +00:00
if (QSBWorldSync.OrbSyncList.Count < message.ObjectId)
2020-12-02 21:29:53 +00:00
{
2020-12-11 13:14:58 +00:00
DebugLog.DebugWrite($"Error - Orb id {message.ObjectId} out of range of orb sync list {QSBWorldSync.OrbSyncList.Count}.", MessageType.Error);
2020-12-02 21:29:53 +00:00
return;
}
2020-12-11 13:14:58 +00:00
var orb = QSBWorldSync.OrbSyncList
.First(x => x.AttachedOrb == QSBWorldSync.OldOrbList[message.ObjectId]);
2020-12-02 21:29:53 +00:00
orb.enabled = true;
}
2020-12-02 21:29:53 +00:00
public override void OnReceiveLocal(WorldObjectMessage message)
{
if (QSBNetworkServer.active)
{
OnServerReceive(message);
}
}
}
2020-12-03 08:28:05 +00:00
}