quantum-space-buddies/QSB/Player/Messages/PlayerKickMessage.cs

66 lines
1.3 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Menus;
2021-12-22 16:44:03 -08:00
using QSB.Messaging;
using QSB.Utility;
2021-12-23 17:07:29 -08:00
using System.Linq;
2021-12-22 16:44:03 -08:00
2021-12-23 13:49:47 -08:00
namespace QSB.Player.Messages
2021-12-22 16:44:03 -08:00
{
/// <summary>
/// always sent by host
/// </summary>
2022-01-26 20:35:43 -08:00
internal class PlayerKickMessage : QSBMessage<KickReason>
2021-12-22 16:44:03 -08:00
{
2021-12-22 17:13:54 -08:00
private uint PlayerId;
public PlayerKickMessage(uint playerId, KickReason reason)
{
PlayerId = playerId;
Value = reason;
}
public override void Serialize(NetworkWriter writer)
2021-12-22 17:13:54 -08:00
{
base.Serialize(writer);
writer.Write(PlayerId);
}
public override void Deserialize(NetworkReader reader)
2021-12-22 17:13:54 -08:00
{
base.Deserialize(reader);
PlayerId = reader.Read<uint>();
2021-12-22 17:13:54 -08:00
}
2021-12-22 16:44:03 -08:00
public override void OnReceiveLocal()
{
if (!QSBCore.IsHost)
{
return;
}
2022-01-29 01:29:02 -08:00
Delay.RunFramesLater(10, KickPlayer);
2021-12-22 16:44:03 -08:00
}
private void KickPlayer()
=> NetworkServer.connections.Values.First(x => PlayerId == x.GetPlayerId()).Disconnect();
2021-12-22 16:44:03 -08:00
public override void OnReceiveRemote()
{
if (PlayerId != QSBPlayerManager.LocalPlayerId)
{
if (QSBPlayerManager.PlayerExists(PlayerId))
{
DebugLog.ToAll($"{QSBPlayerManager.GetPlayer(PlayerId).Name} was kicked.");
return;
}
DebugLog.ToAll($"Player id:{PlayerId} was kicked.");
return;
}
DebugLog.ToAll($"Kicked from server. Reason : {Value}");
MenuManager.Instance.OnKicked(Value);
}
}
}