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