quantum-space-buddies/QSB/Events/PlayerLeaveEvent.cs

38 lines
1.3 KiB
C#
Raw Normal View History

2020-08-09 12:19:51 +01:00
using QSB.Messaging;
using QSB.Utility;
using System.Linq;
namespace QSB.Events
{
2020-08-10 18:17:54 +02:00
public class PlayerLeaveEvent : QSBEvent<PlayerLeaveMessage>
2020-08-09 12:19:51 +01:00
{
public override MessageType Type => MessageType.PlayerLeave;
public override void SetupListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<uint, uint[]>.AddListener(EventNames.QSBPlayerLeave, Handler);
2020-08-09 12:19:51 +01:00
}
2020-08-15 20:32:58 +01:00
public override void CloseListener()
{
2020-08-15 21:52:43 +02:00
GlobalMessenger<uint, uint[]>.RemoveListener(EventNames.QSBPlayerLeave, Handler);
2020-08-15 20:32:58 +01:00
}
2020-08-18 22:37:27 +02:00
private void Handler(uint playerId, uint[] netIds) => SendEvent(CreateMessage(playerId, netIds));
2020-08-15 21:52:43 +02:00
2020-08-18 22:37:27 +02:00
private PlayerLeaveMessage CreateMessage(uint playerId, uint[] netIds) => new PlayerLeaveMessage
2020-08-10 18:17:54 +02:00
{
2020-08-18 22:37:27 +02:00
AboutId = playerId,
NetIds = netIds
2020-08-10 18:17:54 +02:00
};
2020-08-10 14:40:06 +01:00
public override void OnReceiveRemote(PlayerLeaveMessage message)
2020-08-09 12:19:51 +01:00
{
2020-08-17 16:51:56 +01:00
var playerName = PlayerRegistry.GetPlayer(message.AboutId).Name;
DebugLog.ToConsole($"{playerName} disconnected.", OWML.Common.MessageType.Info);
PlayerRegistry.RemovePlayer(message.AboutId);
2020-08-18 22:37:27 +02:00
message.NetIds.ToList().ForEach(netId => QSBNetworkManager.Instance.CleanupNetworkBehaviour(netId));
2020-08-09 12:19:51 +01:00
}
}
}