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

40 lines
1.3 KiB
C#
Raw Normal View History

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