TH1/Unity/Assets/Scripts/TH1_Logic/Steam/GameNetReceiver.cs
2025-09-17 10:52:48 +08:00

143 lines
5.7 KiB
C#

using Logic.Action;
using Logic.CrashSight;
using RuntimeData;
using Steamworks;
using TH1_Logic.Core;
namespace TH1_Logic.Steam
{
public class GameNetReceiver
{
public static GameNetReceiver Instance { get; } = new GameNetReceiver();
public void OnMessageReceived(CSteamID cSteamID, byte[] data)
{
P2PMsgType msgType = GetMessageType(data);
if (msgType == P2PMsgType.GameStart) OnReceivedGameStart(data);
if (msgType == P2PMsgType.ActionConfirm) OnReceivedActionConfirm(data);
if (msgType == P2PMsgType.ActionExcute) OnReceivedActionExcute(data);
if (msgType == P2PMsgType.TurnEnd) OnReceivedTurnEnd(data);
if (msgType == P2PMsgType.TurnChange) OnReceivedTurnChange(data);
}
// 只有玩家会收到
private void OnReceivedGameStart(byte[] data)
{
var message = MemoryPack.MemoryPackSerializer.Deserialize<GameStartMessage>(data);
if (message == null)
{
LogSystem.LogError($"消息解析失败: OnReceivedGameStart");
return;
}
if (message.MapData == null) return;
if (SteamLobbyManager.Instance.IsLobbyOwner) return;
Main.Instance.NetStartGame(message.MapData);
}
// 只有房主会收到
private void OnReceivedActionConfirm(byte[] data)
{
var message = MemoryPack.MemoryPackSerializer.Deserialize<ActionConfirmMessage>(data);
if (message == null)
{
LogSystem.LogError($"消息解析失败: OnReceivedActionConfirm");
return;
}
if (!SteamLobbyManager.Instance.IsLobbyOwner) return;
if (message.ActionData == null) return;
if (message.ActionData.MapHash != Main.MapData.Net.MapHash)
{
LogSystem.LogError($"OnReceivedActionConfirm MapHash不一致");
return;
}
if (message.ActionData.Version != Main.MapData.Net.GetActionVersion())
{
LogSystem.LogError($"OnReceivedActionConfirm Version 不一致");
return;
}
message.ActionData.Param.MapData = Main.MapData;
message.ActionData.Param.RefreshParams();
var action = ActionLogicFactory.GetActionLogic(message.ActionData.ActionId);
if (action == null)
{
LogSystem.LogError($"OnReceivedActionConfirm 找不到对应 Action");
return;
}
action.CompleteExecute(message.ActionData.Param, false);
GameNetSender.Instance.ActionExcute(message.ActionData.ActionId, message.ActionData.Param);
}
private void OnReceivedActionExcute(byte[] data)
{
var message = MemoryPack.MemoryPackSerializer.Deserialize<ActionExcuteMessage>(data);
if (message == null)
{
LogSystem.LogError($"消息解析失败: OnReceivedActionExcute");
return;
}
if (SteamLobbyManager.Instance.IsLobbyOwner) return;
if (message.ActionData == null) return;
if (message.ActionData.MapHash != Main.MapData.Net.MapHash)
{
LogSystem.LogError($"OnReceivedActionConfirm MapHash不一致");
return;
}
if (message.ActionData.Version != Main.MapData.Net.GetActionVersion())
{
LogSystem.LogError($"OnReceivedActionConfirm Version 不一致");
return;
}
message.ActionData.Param.MapData = Main.MapData;
message.ActionData.Param.RefreshParams();
var action = ActionLogicFactory.GetActionLogic(message.ActionData.ActionId);
if (action == null)
{
LogSystem.LogError($"OnReceivedActionConfirm 找不到对应 Action");
return;
}
action.CompleteExecute(message.ActionData.Param, false);
}
// 只有房主会收到
private void OnReceivedTurnEnd(byte[] data)
{
var message = MemoryPack.MemoryPackSerializer.Deserialize<TurnEndMessage>(data);
if (message == null)
{
LogSystem.LogError($"消息解析失败: OnReceivedTurnEnd");
return;
}
if (!SteamLobbyManager.Instance.IsLobbyOwner) return;
if (Main.Instance.GameLogic.CurPlayer.Id != message.PlayerId) return;
Main.Instance.GameLogic.ChangeToNextPlayerRound();
}
// 只有普通玩家会收到
private void OnReceivedTurnChange(byte[] data)
{
var message = MemoryPack.MemoryPackSerializer.Deserialize<TurnChangeMessage>(data);
if (message == null)
{
LogSystem.LogError($"消息解析失败: OnReceivedTurnChange");
return;
}
if (SteamLobbyManager.Instance.IsLobbyOwner) return;
Main.Instance.GameLogic.ChangeToNextPlayerRound();
if (Main.Instance.GameLogic.CurPlayer.Id != message.PlayerId)
{
LogSystem.LogError($"OnReceivedTurnChange Error: CurPlayer id is {Main.Instance.GameLogic.CurPlayer.Id}, " +
$"but message.PlayerId is {message.PlayerId}");
}
}
private P2PMsgType GetMessageType(byte[] data)
{
// 临时反序列化只为获取消息类型
var tempMessage = MemoryPack.MemoryPackSerializer.Deserialize<BaseMessage>(data);
return tempMessage.MessageType;
}
}
}