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

60 lines
1.2 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;
2022-03-02 19:46:33 -08:00
namespace QSB.Player.Messages;
/// <summary>
/// always sent by host
/// </summary>
2022-03-18 02:58:01 -07:00
internal class PlayerKickMessage : QSBMessage<string>
2021-12-22 16:44:03 -08:00
{
2022-03-02 19:46:33 -08:00
private uint PlayerId;
2022-03-18 02:58:01 -07:00
public PlayerKickMessage(uint playerId, string reason) : base(reason) =>
2022-03-02 19:46:33 -08:00
PlayerId = playerId;
2021-12-22 17:13:54 -08:00
2022-03-02 19:46:33 -08:00
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(PlayerId);
}
2021-12-22 17:13:54 -08:00
2022-03-02 19:46:33 -08:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
PlayerId = reader.Read<uint>();
}
2021-12-22 17:13:54 -08:00
2022-03-02 19:46:33 -08:00
public override void OnReceiveLocal()
{
if (!QSBCore.IsHost)
2021-12-22 17:13:54 -08:00
{
2022-03-02 19:46:33 -08:00
return;
2021-12-22 17:13:54 -08:00
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
Delay.RunFramesLater(10, KickPlayer);
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
private void KickPlayer()
=> PlayerId.GetNetworkConnection().Disconnect();
2022-03-02 19:46:33 -08:00
public override void OnReceiveRemote()
{
if (PlayerId != QSBPlayerManager.LocalPlayerId)
2021-12-22 16:44:03 -08:00
{
2022-03-02 19:46:33 -08:00
if (QSBPlayerManager.PlayerExists(PlayerId))
2021-12-22 16:44:03 -08:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToAll($"{QSBPlayerManager.GetPlayer(PlayerId).Name} was kicked.");
2021-12-22 16:44:03 -08:00
return;
}
2022-03-02 19:46:33 -08:00
DebugLog.ToAll($"Player id:{PlayerId} was kicked.");
return;
2021-12-22 16:44:03 -08:00
}
2022-03-02 19:46:33 -08:00
DebugLog.ToAll($"Kicked from server. Reason : {Data}");
MenuManager.Instance.OnKicked(Data);
2021-12-22 16:44:03 -08:00
}
}