quantum-space-buddies/QSB/Player/Events/PlayerReadyEvent.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2020-08-24 15:51:12 +01:00
using OWML.Common;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2020-12-14 16:28:03 +00:00
using QSB.Messaging;
2020-11-03 22:29:23 +00:00
using QSB.SectorSync;
2020-08-24 15:51:12 +01:00
using QSB.Utility;
using System.Linq;
2020-08-10 11:45:24 +01:00
2020-11-03 21:33:48 +00:00
namespace QSB.Player.Events
2020-08-10 11:45:24 +01:00
{
2020-12-02 21:23:01 +00:00
public class PlayerReadyEvent : QSBEvent<ToggleMessage>
{
public override EventType Type => EventType.PlayerReady;
2020-08-10 11:45:24 +01:00
2020-12-02 21:23:01 +00:00
public override void SetupListener() => GlobalMessenger<bool>.AddListener(EventNames.QSBPlayerReady, Handler);
public override void CloseListener() => GlobalMessenger<bool>.RemoveListener(EventNames.QSBPlayerReady, Handler);
2020-08-15 20:32:58 +01:00
2020-12-02 21:23:01 +00:00
private void Handler(bool ready) => SendEvent(CreateMessage(ready));
2020-08-15 21:52:43 +02:00
2020-12-02 21:23:01 +00:00
private ToggleMessage CreateMessage(bool ready) => new ToggleMessage
{
AboutId = LocalPlayerId,
ToggleValue = ready
};
2020-08-10 18:17:54 +02:00
2020-12-11 22:42:21 +00:00
public override void OnReceiveRemote(bool server, ToggleMessage message)
2020-12-14 21:41:56 +01:00
{
if (server)
{
HandleServer(message);
}
else
{
HandleClient(message);
}
}
private static void HandleServer(ToggleMessage message)
{
DebugLog.DebugWrite($"Get ready event from {message.FromId}", MessageType.Success);
QSBPlayerManager.GetPlayer(message.AboutId).IsReady = message.ToggleValue;
GlobalMessenger.FireEvent(EventNames.QSBServerSendPlayerStates);
}
private void HandleClient(ToggleMessage message)
{
DebugLog.DebugWrite($"Get ready event from {message.FromId}", MessageType.Success);
if (!QSBPlayerManager.PlayerExists(message.FromId))
{
DebugLog.ToConsole(
"Error - Got ready event for non-existent player! Did we not send a PlayerStatesRequestEvent? Or was it not handled?",
MessageType.Error);
return;
}
foreach (var item in QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
.Where(x => x != null && x.IsReady && x.ReferenceSector != null && x.PlayerId == LocalPlayerId))
{
GlobalMessenger<uint, QSBSector>.FireEvent(EventNames.QSBSectorChange, item.NetId.Value, item.ReferenceSector);
}
}
}
2020-12-03 08:28:05 +00:00
}