TH1/Unity/Assets/Scripts/TH1_Logic/Steam/GameNetSender.cs
2025-10-15 17:35:54 +08:00

161 lines
5.3 KiB
C#

/*
* @Author: 白哉
* @Description:
* @Date: 2025年09月08日 星期一 17:09:18
* @Modify:
*/
using System;
using Logic.Action;
using Logic.AI;
using Logic.CrashSight;
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(BaseMessage message)
{
if (LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.SendMessageToPeer(LobbyManager.Instance.Lobby.GetLobbyOwnerId(), messageBytes);
}
// 发送消息给指定人
public void SendMessageToPlayer(ulong memberId, BaseMessage message)
{
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.SendMessageToPeer(memberId, messageBytes);
}
// 房主广播消息给所有成员
public void BroadcastMessage(BaseMessage message)
{
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.BroadcastMessage(messageBytes);
}
// 房主广播消息给所有成员
public void SendMessageToAllPlayer(BaseMessage message)
{
byte[] messageBytes = MemoryPack.MemoryPackSerializer.Serialize(message);
LobbyManager.Instance.Lobby.BroadcastMessage(messageBytes);
}
// 广播字符串
public void BroadcastString(string str)
{
var data = new StringMessage();
data.Content = str;
SendMessageToAllPlayer(data);
}
// 游戏开始
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);
}
// map 校验 (成员 => 房主)
public void MapConfirm()
{
var data = new MapConfirmMessage();
data.PlayerId = Main.MapData.PlayerMap.SelfPlayerId;
data.Index = Main.MapData.Net.Actions.Count;
if (data.Index > 0) data.ActionData = Main.MapData.Net.Actions[^1];
SendMessage(data);
}
// 强制更新 (房主 => 单成员)
public void ForceUpdate(uint playerId)
{
var memberId = Main.MapData.Net.GetmemberIdId(playerId);
if (memberId == 0) return;
var data = new ForceUpdateMessage();
data.MapData = Main.MapData;
SendMessageToPlayer(memberId, data);
}
// 强制更新 (房主 => 所有成员)
public void BroadcastForceUpdate()
{
var data = new ForceUpdateMessage();
data.MapData = Main.MapData;
BroadcastMessage(data);
}
// 修改阵营 (成员 => 房主)
public void ChangeCiv(MapConfig config)
{
var selfMemberId = LobbyManager.Instance.Lobby.GetSelfMemberId();
var memberCiv = config.GetMemberCiv(selfMemberId);
if (memberCiv == null)
{
LogSystem.LogError($"Get Self MemberCiv Error ");
return;
}
var data = new ChangeCivMessage();
data.Civ = memberCiv;
SendMessage(data);
}
// 更新房间配置 (房主 => 所有成员)
public void UpdateLobbyData(MapConfig config)
{
if (!LobbyManager.Instance.Lobby.IsLobbyOwner()) return;
var data = new UpdateLobbyDataMessage();
data.Config = config;
BroadcastMessage(data);
}
}
}