quantum-space-buddies/QSB/Player/Events/PlayerJoinEvent.cs

78 lines
2.8 KiB
C#
Raw Normal View History

2020-08-22 17:08:25 +00:00
using OWML.Common;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2020-08-08 11:23:23 +00:00
using QSB.Utility;
2020-11-03 21:33:48 +00:00
namespace QSB.Player.Events
2020-08-08 11:23:23 +00:00
{
2020-12-02 21:23:01 +00:00
public class PlayerJoinEvent : QSBEvent<PlayerJoinMessage>
{
public override EventType Type => EventType.PlayerJoin;
2020-08-08 11:23:23 +00:00
2020-12-02 21:23:01 +00:00
public override void SetupListener() => GlobalMessenger<string>.AddListener(EventNames.QSBPlayerJoin, Handler);
public override void CloseListener() => GlobalMessenger<string>.RemoveListener(EventNames.QSBPlayerJoin, Handler);
2020-08-15 19:32:58 +00:00
2020-12-02 21:23:01 +00:00
private void Handler(string name) => SendEvent(CreateMessage(name));
2020-08-08 11:23:23 +00:00
2020-12-02 21:23:01 +00:00
private PlayerJoinMessage CreateMessage(string name) => new PlayerJoinMessage
{
AboutId = LocalPlayerId,
2021-03-09 16:43:41 +00:00
PlayerName = name,
2021-08-10 13:54:23 +00:00
QSBVersion = QSBCore.QSBVersion,
2021-10-21 16:21:54 +00:00
GameVersion = QSBCore.GameVersion,
Platform = QSBCore.Platform
2020-12-02 21:23:01 +00:00
};
2020-08-10 16:17:54 +00:00
2020-12-11 22:42:21 +00:00
public override void OnReceiveRemote(bool server, PlayerJoinMessage message)
2020-12-02 21:23:01 +00:00
{
2021-08-10 13:54:23 +00:00
if (message.QSBVersion != QSBCore.QSBVersion)
2021-03-09 16:43:41 +00:00
{
2021-08-10 13:54:23 +00:00
if (server)
{
DebugLog.ToConsole($"Error - Client {message.PlayerName} connecting with wrong QSB version. (Client:{message.QSBVersion}, Server:{QSBCore.QSBVersion})", MessageType.Error);
QSBEventManager.FireEvent(EventNames.QSBPlayerKick, message.AboutId, KickReason.QSBVersionNotMatching);
}
2021-08-22 16:17:46 +00:00
2021-08-10 13:54:23 +00:00
return;
}
if (message.GameVersion != QSBCore.GameVersion)
{
if (server)
{
DebugLog.ToConsole($"Error - Client {message.PlayerName} connecting with wrong game version. (Client:{message.GameVersion}, Server:{QSBCore.GameVersion})", MessageType.Error);
QSBEventManager.FireEvent(EventNames.QSBPlayerKick, message.AboutId, KickReason.GameVersionNotMatching);
}
2021-08-22 16:17:46 +00:00
2021-03-09 16:43:41 +00:00
return;
}
2021-06-18 21:38:32 +00:00
2021-10-21 16:21:54 +00:00
if (message.Platform != QSBCore.Platform)
{
if (server)
{
DebugLog.ToConsole($"Error - Client {message.PlayerName} connecting with wrong game platform. (Client:{message.Platform}, Server:{QSBCore.Platform})", MessageType.Error);
QSBEventManager.FireEvent(EventNames.QSBPlayerKick, message.AboutId, KickReason.GamePlatformNotMatching);
}
}
2020-12-02 21:23:01 +00:00
var player = QSBPlayerManager.GetPlayer(message.AboutId);
player.Name = message.PlayerName;
2020-12-19 18:44:27 +00:00
DebugLog.ToAll($"{player.Name} joined!", MessageType.Info);
2021-10-21 16:21:54 +00:00
DebugLog.DebugWrite($"{player.Name} joined. id:{player.PlayerId}, qsbVersion:{message.QSBVersion}, gameVersion:{message.GameVersion}, platform:{message.Platform}", MessageType.Info);
2020-12-02 21:23:01 +00:00
}
2020-08-08 11:23:23 +00:00
2020-12-11 22:42:21 +00:00
public override void OnReceiveLocal(bool server, PlayerJoinMessage message)
2020-12-02 21:23:01 +00:00
{
var player = QSBPlayerManager.GetPlayer(QSBPlayerManager.LocalPlayerId);
player.Name = message.PlayerName;
var text = $"Connected to server as {player.Name}.";
DebugLog.ToAll(text, MessageType.Info);
2021-07-12 15:04:02 +00:00
if (QSBSceneManager.IsInUniverse)
{
QSBPlayerManager.LocalPlayer.PlayerStates.IsReady = true;
QSBEventManager.FireEvent(EventNames.QSBPlayerReady, true);
}
2020-12-02 21:23:01 +00:00
}
}
2020-12-03 08:28:05 +00:00
}