TH1/Unity/Assets/Scripts/TH1_Logic/Steam/GameNetSender.cs
2025-09-18 16:42:32 +08:00

126 lines
4.1 KiB
C#

/*
* @Author: 白哉
* @Description:
* @Date: 2025年09月08日 星期一 17:09:18
* @Modify:
*/
using System;
using Logic.Action;
using Logic.AI;
using RuntimeData;
using Steamworks;
using TH1_Logic.Core;
using TH1_Logic.Net;
using UnityEngine;
namespace TH1_Logic.Steam
{
public class GameNetSender
{
public static GameNetSender Instance { get; } = new GameNetSender();
// 发送消息给房主
public void SendMessage<T>(T message) where T : BaseMessage
{
if (LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.SendMessageToPeer(LobbyManager.Instance.Lobby.GetLobbyOwnerId(), messageBytes);
}
// 发送消息给指定人
public void SendMessageToPlayer<T>(ulong memberId, T message) where T : BaseMessage
{
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.SendMessageToPeer(memberId, messageBytes);
}
// 房主广播消息给所有成员
public void BroadcastMessage<T>(T message) where T : BaseMessage
{
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.BroadcastMessage(messageBytes);
}
// 游戏开始
public void GameStart()
{
var data = new GameStartMessage();
data.MapData = Main.MapData;
BroadcastMessage(data);
}
// 请求行为 (成员 => 房主)
public void ActionConfirm(CommonActionId id, CommonActionParams param)
{
var actionData = new ActionNetData();
actionData.Version = Main.MapData.Net.GetActionVersion();
actionData.MapHash = NetData.GetMapDataHash(Main.MapData);
actionData.Param = param;
actionData.ActionId = id;
var data = new ActionConfirmMessage();
data.ActionData = actionData;
SendMessage(data);
}
// 行为执行 (房主 => 所有成员)
public void ActionExcute(CommonActionId id, CommonActionParams param)
{
var actionData = new ActionNetData();
actionData.Version = Main.MapData.Net.GetActionVersion();
actionData.MapHash = NetData.GetMapDataHash(Main.MapData);
actionData.Param = param;
actionData.ActionId = id;
var data = new ActionExcuteMessage();
data.ActionData = actionData;
BroadcastMessage(data);
}
// 请求回合结束 (成员 => 房主)
public void TurnEndConfirm()
{
var data = new TurnEndMessage();
data.PlayerId = Main.MapData.PlayerMap.SelfPlayerId;
SendMessage(data);
}
// 回合切换广播 (房主 => 所有成员)
public void TurnChange(uint playerId)
{
var data = new TurnChangeMessage();
data.PlayerId = playerId;
BroadcastMessage(data);
}
// 出错申请强制更新 (成员 => 房主)
public void ForceUpdateConfirm()
{
var data = new ForceUpdateConfirmMessage();
data.PlayerId = Main.MapData.PlayerMap.SelfPlayerId;
SendMessage(data);
}
// 强制更新 (房主 => 单成员)
public void ForceUpdate(uint playerId)
{
var steamId = Main.MapData.Net.GetSteamId(playerId);
if (steamId == 0) return;
var data = new ForceUpdateMessage();
data.MapData = Main.MapData;
SendMessageToPlayer(steamId, data);
}
// 强制更新 (房主 => 所有成员)
public void BroadcastForceUpdate()
{
var data = new ForceUpdateMessage();
data.MapData = Main.MapData;
BroadcastMessage(data);
}
}
}