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-08-24 15:51:12 +01:00
|
|
|
|
using QSB.Utility;
|
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:20:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (server)
|
|
|
|
|
{
|
|
|
|
|
HandleServer(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HandleClient(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-14 21:41:56 +01:00
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private static void HandleServer(ToggleMessage message)
|
|
|
|
|
{
|
2021-08-08 20:04:15 +01:00
|
|
|
|
DebugLog.DebugWrite($"[SERVER] Get ready event from {message.FromId}", MessageType.Success);
|
2021-04-18 18:46:55 +01:00
|
|
|
|
QSBPlayerManager.GetPlayer(message.AboutId).PlayerStates.IsReady = message.ToggleValue;
|
2021-08-08 20:04:15 +01:00
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBPlayerInformation);
|
2020-12-14 21:20:53 +00:00
|
|
|
|
}
|
2020-12-14 21:41:56 +01:00
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private void HandleClient(ToggleMessage message)
|
|
|
|
|
{
|
2021-08-08 20:04:15 +01:00
|
|
|
|
DebugLog.DebugWrite($"[CLIENT] Get ready event from {message.FromId}", MessageType.Success);
|
2020-12-14 21:20:53 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|