2020-09-29 20:34:46 +00:00
|
|
|
|
using OWML.Common;
|
2020-12-14 16:24:52 +00:00
|
|
|
|
using QSB.Events;
|
2020-09-29 20:34:46 +00:00
|
|
|
|
using QSB.Utility;
|
2020-09-04 19:54:34 +00:00
|
|
|
|
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-09-04 19:54:34 +00:00
|
|
|
|
|
2020-11-03 17:56:48 +00:00
|
|
|
|
namespace QSB.OrbSync.Events
|
2020-09-04 19:54:34 +00:00
|
|
|
|
{
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public class OrbUserEvent : QSBEvent<WorldObjectMessage>
|
|
|
|
|
{
|
|
|
|
|
public override EventType Type => EventType.OrbUser;
|
2020-09-04 19:54:34 +00:00
|
|
|
|
|
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-09-04 19:54:34 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void Handler(int id) => SendEvent(CreateMessage(id));
|
2020-09-04 19:54:34 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private WorldObjectMessage CreateMessage(int id) => new WorldObjectMessage
|
|
|
|
|
{
|
|
|
|
|
AboutId = LocalPlayerId,
|
|
|
|
|
ObjectId = id
|
|
|
|
|
};
|
2020-09-04 19:54:34 +00:00
|
|
|
|
|
2020-12-11 22:42:21 +00:00
|
|
|
|
public override void OnReceiveRemote(bool server, WorldObjectMessage message)
|
2020-12-14 21:20:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (server)
|
|
|
|
|
{
|
|
|
|
|
HandleServer(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HandleClient(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-14 20:41:56 +00:00
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private static void HandleServer(WorldObjectMessage message)
|
|
|
|
|
{
|
|
|
|
|
var fromPlayer = QSBNetworkServer.connections.First(x => x.GetPlayer().PlayerId == message.FromId);
|
|
|
|
|
if (QSBWorldSync.OrbSyncList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - OrbSyncList is empty. (ID {message.ObjectId})", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (fromPlayer == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite("Error - FromPlayer is null!", MessageType.Error);
|
|
|
|
|
}
|
|
|
|
|
var orbSync = QSBWorldSync.OrbSyncList
|
|
|
|
|
.First(x => x.AttachedOrb == QSBWorldSync.OldOrbList[message.ObjectId]);
|
|
|
|
|
if (orbSync == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - No orb found for user event. (ID {message.ObjectId})", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var orbIdentity = orbSync.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);
|
|
|
|
|
orbSync.enabled = true;
|
|
|
|
|
}
|
2020-12-11 22:42:21 +00:00
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private static void HandleClient(WorldObjectMessage message)
|
|
|
|
|
{
|
|
|
|
|
if (QSBWorldSync.OrbSyncList.Count < message.ObjectId)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite(
|
|
|
|
|
$"Error - Orb id {message.ObjectId} out of range of orb sync list {QSBWorldSync.OrbSyncList.Count}.",
|
|
|
|
|
MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var orb = QSBWorldSync.OrbSyncList
|
|
|
|
|
.First(x => x.AttachedOrb == QSBWorldSync.OldOrbList[message.ObjectId]);
|
|
|
|
|
orb.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|