/* * @Author: 白哉 * @Description: * @Date: 2025年09月08日 星期一 17:09:18 * @Modify: */ using Logic.Action; using Logic.AI; using Logic.CrashSight; using RuntimeData; using TH1_Logic.Core; using TH1_Logic.Net; 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; // data.Map = Main.MapData; BroadcastMessage(data); } // 请求回合结束 (成员 => 房主) public void TurnEndConfirm() { var data = new TurnEndMessage(); data.PlayerId = Main.MapData.PlayerMap.SelfPlayerId; SendMessage(data); } // 心跳校验 (成员 => 房主) public void MapConfirm() { var data = new MapConfirmMessage(); data.MemberId = LobbyManager.Instance.Lobby.GetSelfMemberId(); 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(ulong memberId) { 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(MemberCiv memberCiv) { 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); } // 请求更新房间配置 (单成员 => 房主) public void RequestLobbyData() { var data = new RequestLobbyDataMessage(); data.MemberId = LobbyManager.Instance.Lobby.GetSelfMemberId(); SendMessage(data); } // 更新房间配置 (房主 => 单成员) public void SendLobbyData(MapConfig config, ulong memberId) { if (!LobbyManager.Instance.Lobby.IsLobbyOwner() || memberId == 0) return; var data = new UpdateLobbyDataMessage(); data.Config = config; SendMessageToPlayer(memberId, data); } // 心跳 (单成员 => 房主) public void SendHeartbeat() { var data = new HeartbeatMessage(); data.MemberId = LobbyManager.Instance.Lobby.GetSelfMemberId(); SendMessage(data); } } }