diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs b/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs new file mode 100644 index 0000000..5b0affb --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs @@ -0,0 +1,917 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.Threading.Tasks; +using Newtonsoft.Json; +using UnityEngine.UI; +/*public class QueryRoomdetails512 : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法 + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + // 首次调用加载初始数据 + LoadInitialData(); + //LoadGameEscapeData(); + //Debug.Log("接收到新的 token: " + token); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + await QueryEscapeRoomDetails();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器 + //Debug.Log("接收到新的 GameEscapeId: " + escapeId); + } + + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + await QueryEscapeRoomDetails();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要 + } + + // 加载游戏逃亡数据,使用最新的 escapeId 和 token + public async void selectQueryKill1() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。"); + return; + } + + await QueryEscapeRoomDetails();//========================================================================================================================================================================== + + } + + //=============================================================================================================================================================================================================================== + // 查询逃亡房间详情 + public async Task QueryEscapeRoomDetails() + { + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": 106, + ""escapeId"": {escapeId} + }}"; + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryEscapeRoomList", "POST", body, headers); + Debug.Log("逃亡房间详情响应: " + response); + } + //=============================================================================================================================================================================================================================== + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + } +}*/ + + +//字符串版 +/*public class QueryRoomdetails512 : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应 + + // 假设有一个手动查询逃亡房间详情的按钮=================================================================================== + public Button queryEscapeRoomButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnQueryEscapeRoomButtonClicked================================================================ + if (queryEscapeRoomButton != null) + { + queryEscapeRoomButton.onClick.AddListener(OnQueryEscapeRoomButtonClicked); + } + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询逃亡房间详情 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询逃亡房间响应: " + lastEscapeRoomResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("初始加载的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + } + + // 按钮点击后触发逃亡房间详情查询操作 + public async void OnQueryEscapeRoomButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("用户按钮触发的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + + // 根据响应做进一步的处理 + HandleEscapeRoomResponse(lastEscapeRoomResponse); + } + + // 查询逃亡房间详情 + public async Task QueryEscapeRoomDetails() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": 106, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询逃亡房间详情..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryEscapeRoomList", "POST", body, headers); + Debug.Log("查询逃亡房间详情响应: " + response); + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryEscapeRoomDetails 方法的响应 + private void HandleEscapeRoomResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("查询逃亡房间详情成功!"); + } + else + { + Debug.LogWarning("查询逃亡房间详情失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听================================================================================================ + if (queryEscapeRoomButton != null) + { + queryEscapeRoomButton.onClick.RemoveListener(OnQueryEscapeRoomButtonClicked); + } + } +}*/ +//解析版 +/*public class QueryRoomdetails512 : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应 + + // 假设有一个手动查询逃亡房间详情的按钮 + public Button queryEscapeRoomButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnQueryEscapeRoomButtonClicked + if (queryEscapeRoomButton != null) + { + queryEscapeRoomButton.onClick.AddListener(OnQueryEscapeRoomButtonClicked); + } + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询逃亡房间详情 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询逃亡房间响应: " + lastEscapeRoomResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("初始加载的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + } + + // 按钮点击后触发逃亡房间详情查询操作 + public async void OnQueryEscapeRoomButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("用户按钮触发的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + + // 根据响应做进一步的处理 + HandleEscapeRoomResponse(lastEscapeRoomResponse); + } + + // 查询逃亡房间详情 + public async Task QueryEscapeRoomDetails() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": 106, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询逃亡房间详情..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryEscapeRoomList", "POST", body, headers); + Debug.Log("查询逃亡房间详情响应: " + response); + + // 反序列化 JSON 数据为 ServerResponse 对象 + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + // 保存反序列化后的房间详情数据 + if (serverResponse.data != null && serverResponse.data.gameEscapeRoomResponseVoList != null) + { + foreach (var room in serverResponse.data.gameEscapeRoomResponseVoList) + { + Debug.Log($"Escape ID: {room.escapeId}, Room No: {room.roomNo}, Room Beans Coin: {room.roomBeansCoin}"); + } + } + } + else + { + Debug.LogWarning("解析失败或响应错误:" + response); + } + + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryEscapeRoomDetails 方法的响应 + private void HandleEscapeRoomResponse(string response) + { + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + Debug.Log("查询逃亡房间详情成功!"); + if (serverResponse.data != null && serverResponse.data.gameEscapeRoomResponseVoList != null) + { + foreach (var room in serverResponse.data.gameEscapeRoomResponseVoList) + { + Debug.Log($"Escape ID: {room.escapeId}, Room No: {room.roomNo}, Room Beans Coin: {room.roomBeansCoin}"); + } + } + } + else + { + Debug.LogWarning("查询逃亡房间详情失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听 + if (queryEscapeRoomButton != null) + { + queryEscapeRoomButton.onClick.RemoveListener(OnQueryEscapeRoomButtonClicked); + } + } + + // 数据类 + [Serializable] + public class GameEscapeRoomResponseVo + { + public int escapeId; // 游戏的ID + public int roomNo; // 房间编号 + public float roomBeansCoin; // 房间下注欢乐豆 + } + + [Serializable] + public class Data + { + public List gameEscapeRoomResponseVoList; // 房间列表 + } + + [Serializable] + public class ServerResponse + { + public int code; // 响应状态码 + public string message; // 提示语 + public Data data; // 数据对象 + } +}*/ +//解析+高频版 +/*public class QueryRoomdetails512 : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + public int userId; + public string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应 + + // 假设有一个手动查询逃亡房间详情的按钮 + //public Button queryEscapeRoomButton; + + private Coroutine queryCoroutine; // 用于保存协程引用 + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + //// 假设按钮被点击时调用 OnQueryEscapeRoomButtonClicked + //if (queryEscapeRoomButton != null) + //{ + // queryEscapeRoomButton.onClick.AddListener(OnQueryEscapeRoomButtonClicked); + //} + + // 启动协程,每两秒查询一次 + queryCoroutine = StartCoroutine(QueryEscapeRoomRoutine()); + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询逃亡房间详情 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询逃亡房间响应: " + lastEscapeRoomResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("初始加载的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + } + + // 按钮点击后触发逃亡房间详情查询操作 + public async void OnQueryEscapeRoomButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + Debug.Log("用户按钮触发的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + + // 根据响应做进一步的处理 + HandleEscapeRoomResponse(lastEscapeRoomResponse); + } + + // 查询逃亡房间详情 + public async Task QueryEscapeRoomDetails() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": {userId}, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询逃亡房间详情..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryEscapeRoomList", "POST", body, headers); + Debug.Log("查询逃亡房间详情响应: " + response); + + // 反序列化 JSON 数据为 ServerResponse 对象 + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + // 保存反序列化后的房间详情数据 + if (serverResponse.data != null && serverResponse.data.gameEscapeRoomResponseVoList != null) + { + foreach (var room in serverResponse.data.gameEscapeRoomResponseVoList) + { + Debug.Log($"Escape ID: {room.escapeId}, Room No: {room.roomNo}, Room Beans Coin: {room.roomBeansCoin}"); + } + } + } + else + { + Debug.LogWarning("解析失败或响应错误:" + response); + } + + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryEscapeRoomDetails 方法的响应 + private void HandleEscapeRoomResponse(string response) + { + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + Debug.Log("查询逃亡房间详情成功!"); + if (serverResponse.data != null && serverResponse.data.gameEscapeRoomResponseVoList != null) + { + foreach (var room in serverResponse.data.gameEscapeRoomResponseVoList) + { + Debug.Log($"Escape ID: {room.escapeId}, Room No: {room.roomNo}, Room Beans Coin: {room.roomBeansCoin}"); + } + } + } + else + { + Debug.LogWarning("查询逃亡房间详情失败,服务器响应: " + response); + } + } + + // 每两秒查询一次逃亡房间详情的协程 + private IEnumerator QueryEscapeRoomRoutine() + { + while (true) + { + // 每两秒检查并查询逃亡房间详情 + if (!string.IsNullOrEmpty(token) && escapeId != -1) + { + yield return QueryEscapeRoomDetails(); + } + + yield return new WaitForSeconds(2f); // 每两秒执行一次 + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + //// 取消按钮的点击监听 + //if (queryEscapeRoomButton != null) + //{ + // queryEscapeRoomButton.onClick.RemoveListener(OnQueryEscapeRoomButtonClicked); + //} + + // 停止查询协程,防止内存泄漏 + if (queryCoroutine != null) + { + StopCoroutine(queryCoroutine); + } + } + + // 数据类 + [Serializable] + public class GameEscapeRoomResponseVo + { + public int escapeId; // 游戏的ID + public int roomNo; // 房间编号 + public float roomBeansCoin; // 房间下注欢乐豆 + } + + [Serializable] + public class Data + { + public List gameEscapeRoomResponseVoList; // 房间列表 + } + + [Serializable] + public class ServerResponse + { + public int code; // 响应状态码 + public string message; // 提示语 + public Data data; // 数据对象 + } +}*/ +//解析+高频+单例 +public class QueryRoomdetails512 : MonoBehaviour +{ + // 单例实例 + public static QueryRoomdetails512 Instance { get; private set; } + + // 逃亡房间详情变量 + public int latestEscapeId; // 最新的 escapeId + public int latestRoomNo; // 最新的房间编号 + public float latestRoomBeansCoin; // 最新的房间下注欢乐豆 + + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + public int userId; + public string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应 + + private Coroutine queryCoroutine; // 用于保存协程引用 + + void Awake() + { + // 实现单例模式 + if (Instance == null) + { + Instance = this; + DontDestroyOnLoad(gameObject); // 保证场景切换时不销毁这个对象 + } + else + { + Destroy(gameObject); // 确保只有一个实例 + } + } + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 启动协程,每两秒查询一次 + queryCoroutine = StartCoroutine(QueryEscapeRoomRoutine()); + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + // Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询逃亡房间详情 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + // Debug.Log("HandleGameEscapeIdUpdated 处理的查询逃亡房间响应: " + lastEscapeRoomResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询逃亡房间详情方法 + lastEscapeRoomResponse = await QueryEscapeRoomDetails(); + // Debug.Log("初始加载的查询逃亡房间详情响应: " + lastEscapeRoomResponse); + } + + // 查询逃亡房间详情 + public async Task QueryEscapeRoomDetails() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询逃亡房间详情,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询逃亡房间详情,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": {userId}, + ""escapeId"": {escapeId} + }}"; + + // Debug.Log("正在查询逃亡房间详情..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryEscapeRoomList", "POST", body, headers); + Debug.Log("5.1.2查询逃亡房间详情响应: " + response); + + // 反序列化 JSON 数据为 ServerResponse 对象并处理响应 + HandleEscapeRoomResponse(response); + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryEscapeRoomDetails 方法的响应 + private void HandleEscapeRoomResponse(string response) + { + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + Debug.Log("查询逃亡房间详情成功!"); + if (serverResponse.data != null && serverResponse.data.gameEscapeRoomResponseVoList != null) + { + // 保存第一个房间的详情==============================================================================仅第一=========================================================== + var room = serverResponse.data.gameEscapeRoomResponseVoList[0]; + latestEscapeId = room.escapeId; + latestRoomNo = room.roomNo; + latestRoomBeansCoin = room.roomBeansCoin; + + foreach (var roomDetail in serverResponse.data.gameEscapeRoomResponseVoList) + { + Debug.Log($"Escape ID: {roomDetail.escapeId}, Room No: {roomDetail.roomNo}, Room Beans Coin: {roomDetail.roomBeansCoin}"); + } + } + } + else + { + Debug.LogWarning("查询逃亡房间详情失败,服务器响应: " + response); + } + } + + // 每两秒查询一次逃亡房间详情的协程 + private IEnumerator QueryEscapeRoomRoutine() + { + while (true) + { + // 每两秒检查并查询逃亡房间详情 + if (!string.IsNullOrEmpty(token) && escapeId != -1) + { + yield return QueryEscapeRoomDetails(); + } + + yield return new WaitForSeconds(2f); // 每两秒执行一次 + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 停止查询协程,防止内存泄漏 + if (queryCoroutine != null) + { + StopCoroutine(queryCoroutine); + } + } + + // 数据类 + [Serializable] + public class GameEscapeRoomResponseVo + { + public int escapeId; // 游戏的ID + public int roomNo; // 房间编号 + public float roomBeansCoin; // 房间下注欢乐豆 + } + + [Serializable] + public class Data + { + public List gameEscapeRoomResponseVoList; // 房间列表 + } + + [Serializable] + public class ServerResponse + { + public int code; // 响应状态码 + public string message; // 提示语 + public Data data; // 数据对象 + } +} +//单例使用示例 +//public class AnotherClass : MonoBehaviour +//{ +// void Start() +// { +// // 通过 Singleton 实例访问数据 +// int escapeId = QueryRoomdetails512.Instance.latestEscapeId; +// int roomNo = QueryRoomdetails512.Instance.latestRoomNo; +// float roomBeansCoin = QueryRoomdetails512.Instance.latestRoomBeansCoin; + +// Debug.Log($"Latest Escape ID: {escapeId}, Room No: {roomNo}, Room Beans Coin: {roomBeansCoin}"); +// } +//} \ No newline at end of file diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs.meta b/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs.meta new file mode 100644 index 0000000..69aa20a --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/QueryRoomdetails512.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b27d2d49f51751e42b31653d0b0b8d2d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs b/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs new file mode 100644 index 0000000..aa9d8a6 --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs @@ -0,0 +1,501 @@ + + +using System; + +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.Threading.Tasks; +using Newtonsoft.Json; +using UnityEngine.UI; +/*public class selectQueryKill : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法 + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + // 首次调用加载初始数据 + LoadInitialData(); + //LoadGameEscapeData(); + //Debug.Log("接收到新的 token: " + token); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + await QueryKill();//==================================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改 + // Debug.Log("接收到新的 GameEscapeId: " + escapeId); + } + + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + await QueryKill(); + } + + // 加载游戏逃亡数据,使用最新的 escapeId 和 token + public async void selectQueryKill1() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。"); + return; + } + // 查询击杀数据 + await QueryKill();//================================================================================================================================= + + } + +//=============================================================================================================================================================================================================================== + // 查询击杀信息 + public async Task QueryKill() + { + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": 106, + ""escapeId"": {escapeId} + }}"; + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryKill", "POST", body, headers); + Debug.Log("查询击杀信息响应: " + response); + } +//=============================================================================================================================================================================================================================== + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + } +}*/ + + +//可改id版 +/*public class SelectQueryKill514 : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + public int userId;//用户Id; + private string lastQueryKillResponse = null; // 保存最新的击杀信息查询响应 + + // 假设有一个手动查询击杀信息的按钮======================================================================================= + public Button queryKillButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnQueryKillButtonClicked====================================================================== + if (queryKillButton != null) + { + queryKillButton.onClick.AddListener(OnQueryKillButtonClicked); + } + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询击杀信息 + lastQueryKillResponse = await QueryKill(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询击杀信息响应: " + lastQueryKillResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询击杀信息方法 + lastQueryKillResponse = await QueryKill(); + Debug.Log("初始加载的查询击杀信息响应: " + lastQueryKillResponse); + } + + // 按钮点击后触发击杀信息查询操作 + public async void OnQueryKillButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询击杀信息,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询击杀信息,escapeId 未设置。"); + return; + } + + // 调用查询击杀信息方法 + lastQueryKillResponse = await QueryKill(); + Debug.Log("用户按钮触发的查询击杀信息响应: " + lastQueryKillResponse); + + // 根据响应做进一步的处理 + HandleQueryKillResponse(lastQueryKillResponse); + } + + // 查询击杀信息 + public async Task QueryKill() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询击杀信息,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询击杀信息,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": {userId}, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询击杀信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryKill", "POST", body, headers); + Debug.Log("查询击杀信息响应: " + response); + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryKill 方法的响应 + private void HandleQueryKillResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("查询击杀信息成功!"); + } + else + { + Debug.LogWarning("查询击杀信息失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听================================================================================================= + if (queryKillButton != null) + { + queryKillButton.onClick.RemoveListener(OnQueryKillButtonClicked); + } + } +}*/ +//解析+单例 + +public class SelectQueryKill514 : MonoBehaviour +{ + // 单例模式实例 + public static SelectQueryKill514 Instance { get; private set; } + + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + public int userId; // 用户 Id + + // 解析的响应字段作为全局变量 + public int latestCode; // 保存最新的 code + public string latestMessage; // 保存最新的提示语 + public int latestRoomNoKill; // 被击杀的房间编号 + public int latestRoomNoRemain; // 留存的房间编号 + + // 保存 JSON 响应为字符串 + private string lastQueryKillResponse = null; + + public Button queryKillButton; // 查询按钮 + + void Awake() + { + // 实现单例模式 + if (Instance == null) + { + Instance = this; + DontDestroyOnLoad(gameObject); // 保证场景切换时不销毁这个对象 + } + else + { + Destroy(gameObject); // 确保只有一个实例 + } + } + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnQueryKillButtonClicked + if (queryKillButton != null) + { + queryKillButton.onClick.AddListener(OnQueryKillButtonClicked); + } + } + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到新的 token: " + token); + + // 首次调用加载初始数据 + LoadInitialData(); + } + + // 当游戏逃亡 ID 更新时触发此方法,仅保存最新的 escapeId + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在接收到 escapeId 更新时自动调用查询击杀信息 + lastQueryKillResponse = await QueryKill(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询击杀信息响应: " + lastQueryKillResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询击杀信息方法 + lastQueryKillResponse = await QueryKill(); + Debug.Log("初始加载的查询击杀信息响应: " + lastQueryKillResponse); + } + + // 按钮点击后触发击杀信息查询操作 + public async void OnQueryKillButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询击杀信息,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询击杀信息,escapeId 未设置。"); + return; + } + + // 调用查询击杀信息方法 + lastQueryKillResponse = await QueryKill(); + Debug.Log("用户按钮触发的查询击杀信息响应: " + lastQueryKillResponse); + + // 根据响应做进一步的处理 + HandleQueryKillResponse(lastQueryKillResponse); + } + + // 查询击杀信息 + public async Task QueryKill() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询击杀信息,token 未设置。"); + return null; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询击杀信息,escapeId 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = $@" + {{ + ""userId"": {userId}, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询击杀信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryKill", "POST", body, headers); + Debug.Log("查询击杀信息响应: " + response); + HandleQueryKillResponse(response); // 处理响应数据 + return response; // 返回响应 + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理 QueryKill 方法的响应并解析字段 + private void HandleQueryKillResponse(string response) + { + // 保存原始响应字符串 + lastQueryKillResponse = response; + + try + { + ServerResponse serverResponse = JsonConvert.DeserializeObject(response); + + if (serverResponse != null && serverResponse.code == 200) + { + Debug.Log("查询击杀信息成功!"); + // 更新全局变量保存每个字段 + latestCode = serverResponse.code; + latestMessage = serverResponse.message; + + if (serverResponse.data != null) + { + latestRoomNoKill = serverResponse.data.roomNoKill; + latestRoomNoRemain = serverResponse.data.roomNoRemain; + + Debug.Log($"Room No Kill: {latestRoomNoKill}, Room No Remain: {latestRoomNoRemain}"); + } + } + else + { + Debug.LogWarning("查询击杀信息失败,服务器响应: " + response); + } + } + catch (Exception e) + { + Debug.LogWarning("解析击杀信息响应失败: " + e.Message); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听 + if (queryKillButton != null) + { + queryKillButton.onClick.RemoveListener(OnQueryKillButtonClicked); + } + } + + // 数据类,用于 JSON 解析 + [Serializable] + public class Data + { + public int roomNoKill; // 被击杀的房间编号 + public int roomNoRemain; // 留存的房间编号 + } + + [Serializable] + public class ServerResponse + { + public int code; // 响应状态码 + public string message; // 提示语 + public Data data; // 数据对象 + } +} +//使用示例 +//public class AnotherClass : MonoBehaviour +//{ +// void Start() +// { +// int code = SelectQueryKill514.Instance.latestCode; +// string message = SelectQueryKill514.Instance.latestMessage; +// int roomNoKill = SelectQueryKill514.Instance.latestRoomNoKill; +// int roomNoRemain = SelectQueryKill514.Instance.latestRoomNoRemain; +// string jsonResponse = SelectQueryKill514.Instance.lastQueryKillResponse; + +// Debug.Log($"Code: {code}, Message: {message}, Room No Kill: {roomNoKill}, Room No Remain: {roomNoRemain}"); +// Debug.Log($"Original JSON Response: {jsonResponse}"); +// } +//} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs.meta b/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs.meta new file mode 100644 index 0000000..a376f4f --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/SelectQueryKill514.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c230883956c0b3546a885c300ad34498 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs b/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs new file mode 100644 index 0000000..f1cd2a2 --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.Threading.Tasks; + + + +public class motherFucker511 : MonoBehaviour +{ + public int gameEscapeId; + public int carrySeconds; + + // 定义一个事件,当 gameEscapeId 被更新时触发 + public static event Action OnGameEscapeIdUpdated; + + private string token; // 用于保存收到的 token + + void Start() + { + + + // 监听登录获取 token + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + } + void HandleTokenReceived(string token) + { + this.token = token; + Debug.Log("HandleTokenReceived监听=============================================================================================================:" + token); + gameEscape(token); + + } + public async Task gameEscape(string token) + { + // 用来给请求头赋值 + string Authorization = token; + + // 设置请求头 + Dictionary head51 = new Dictionary + { + { "Authorization", Authorization } + }; + + // 发送请求并接收响应 + string response51 = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryLatest", "POST", "{}", head51); + Debug.Log("5.1查询最近一场大屠杀" + response51); + + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs.meta b/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs.meta new file mode 100644 index 0000000..e1b0a63 --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/motherFucker511.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5a03d3578440c8a4abd9e8c39581a11d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs b/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs new file mode 100644 index 0000000..7cd06bb --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class query10Recird : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs.meta b/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs.meta new file mode 100644 index 0000000..814573c --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/11/queryRecord516.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e8d2e3fe1964bd34f8d61e48e533c393 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs b/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs new file mode 100644 index 0000000..9f100da --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Newtonsoft.Json; +using System.Threading.Tasks; + +/*public class selectLatest511 : MonoBehaviour +{ + public static selectLatest511 instance; + public int gameEscapeId; + public int carrySeconds; + // 定义一个事件,当 gameEscapeId 被更新时触发 + public static event Action OnGameEscapeIdUpdated; + + + + void Start() + { + instance=this; + + // 由于除登录注册外的其他方法,都需要登录后返回的token + // 因此登录不在此发送请求(同时) + // 采用监听和事件回调的方法,只有监听到登录和token返回时,才运行其他方法 + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + // 订阅事件,当 gameEscapeId 更新时调用 HandleGameEscapeIdUpdated + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + } + + private string token; // 用于保存收到的 token + + void HandleTokenReceived(string token) + { + // 使用 token + this.token = token; + Debug.Log("HandleTokenReceived监听:" + token); + StartCoroutine(GameEscapeRoutine(token)); // 启动协程,每2秒调用一次gameEscape + } + void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + Debug.Log("GameEscapeId 已更新为: " + newGameEscapeId); + // 在这里添加你想要执行的逻辑 + } + + // 创建一个IEnumerator的协程 + IEnumerator GameEscapeRoutine(string token) + { + while (true) // 无限循环来实现每2秒发送一次请求 + { + yield return new WaitForSeconds(2f); // 每2秒暂停一次 + yield return LatestGame511(token); // 调用gameEscape方法 + } + yield return null; + } + + public async Task LatestGame511(string token) + { + // 用来给请求头赋值 + string Authorization = token; + //Debug.Log("Loding(Authorization)请求头赋值" + Authorization); + + // 5.1查询最近一场大屠杀 + Dictionary head51 = new Dictionary + { + { "Authorization", Authorization }, // 设置授权头 + }; + + string response51 = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryLatest", "POST", "{}", head51); + Debug.Log("5.1查询最近一场大屠杀(独立)" + response51); // 查询最近一场大逃亡游戏详情: + + // 解析JSON数据 + string json = response51; + Response response = JsonConvert.DeserializeObject(json); + + if (response != null && response.code == 200 && response.data != null) + { + gameEscapeId = response.data.gameEscapeModel.id; + //Debug.Log("解析成功,id为: " + gameEscapeId); + carrySeconds= response.data.carrySeconds; + // 触发事件,通知所有订阅者 + OnGameEscapeIdUpdated?.Invoke(gameEscapeId); + } + else + { + Debug.LogError("解析失败或响应错误"); + } + } + + void OnDestroy() + { + // 取消监听,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + } + + // Update is called once per frame + void Update() + { + + } + + [Serializable] + public class GameEscapeModel + { + public int id; // 解析游戏ID + public string gameNo; // 包括游戏编号 + } + + [Serializable] + public class GameEscapeRoomResponseVo + { + public int escapeId; // 逃脱游戏的ID + public int roomNo; // 房间号 + } + + [Serializable] + public class Data + { + public int carrySeconds; // 持续秒数 + public GameEscapeModel gameEscapeModel; // 嵌套的GameEscapeModel对象 + public List gameEscapeRoomResponseVoList; // 房间信息列表 + public object gameEscapeUserModel; // 用户模型,可以保持为空或根据需要进行定义 + } + + [Serializable] + public class Response + { + public int code; // 状态码 + public string message; // 返回信息 + public Data data; // 数据对象 + } + +}*/ + + + + +public class selectLatest511 : MonoBehaviour +{ + public int gameEscapeId; + public int carrySeconds; + + // 定义一个事件,当 gameEscapeId 被更新时触发 + public static event Action OnGameEscapeIdUpdated; + + private string token; // 用于保存收到的 token + + void Start() + { + // 监听登录获取 token + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + selectLatest511.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + } + + void HandleTokenReceived(string token) + { + this.token = token; + Debug.Log("HandleTokenReceived监听:" + token); + + // 一开始获取 token 时立即运行一次 LatestGame511 + LatestGame511(token).Wait(); + + // 然后启动协程,每30秒调用一次 LatestGame511 + StartCoroutine(GameEscapeRoutine(token)); + } + + void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + Debug.Log("GameEscapeId 已更新为: " + newGameEscapeId); + } + + IEnumerator GameEscapeRoutine(string token) + { + while (true) + { + yield return new WaitForSeconds(30f); // 每30秒暂停一次 + yield return LatestGame511(token); // 调用 LatestGame511 方法 + } + } + + public async Task LatestGame511(string token) + { + // 用来给请求头赋值 + string Authorization = token; + + // 设置请求头 + Dictionary head511 = new Dictionary + { + { "Authorization", Authorization } + }; + + // 发送请求并接收响应 + string response511 = await web.SendRequest("http://121.40.42.41:8080/snail/LatestGame511/queryLatest", "POST", "{}", head511); + Debug.Log("5.1.1查询最近一场大屠杀" + response511); + + // 解析JSON数据为 Response 对象 + Response response = JsonConvert.DeserializeObject(response511); + + if (response != null && response.code == 200 && response.data != null) + { + // 保存解析后的字段 + gameEscapeId = response.data.gameEscapeModel.id; + carrySeconds = response.data.carrySeconds; + + // 使用其他字段 + string gameNo = response.data.gameEscapeModel.gameNo; + float beansCoinAll = response.data.gameEscapeModel.beansCoinAll; + + Debug.Log($"Game Escape ID: {gameEscapeId}"); + Debug.Log($"Carry Seconds: {carrySeconds}"); + Debug.Log($"Game No: {gameNo}"); + Debug.Log($"Beans Coin All: {beansCoinAll}"); + + // 如果你想要处理房间列表 + foreach (var room in response.data.gameEscapeRoomResponseVoList) + { + int roomNo = room.roomNo; + float roomBeansCoin = room.roomBeansCoin; + Debug.Log($"Room No: {roomNo}, Room Beans Coin: {roomBeansCoin}"); + } + + // 触发事件,通知所有订阅者 + OnGameEscapeIdUpdated?.Invoke(gameEscapeId); + } + else + { + Debug.LogError("解析失败或响应错误"); + } + } + + void OnDestroy() + { + // 取消监听,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + } + + // 数据类 + [Serializable] + public class GameEscapeModel + { + public int id; // 游戏 ID + public string gameNo; // 游戏编号 + public int demonMode; + public string startTime; + public string betTime; + public string countTime; + public string settleTime; + public int status; + public string roomNoKill; + public string roomNoRemain; + public float beansCoinAll; + public float beansCoinKill; + public float beansCoinRemain; + public float beansCoinFee; + public float beansCoinRank; + public float beansCoinDivide; + } + + [Serializable] + public class GameEscapeRoomResponseVo + { + public int escapeId; // 逃脱游戏的ID + public int roomNo; // 房间号 + public float roomBeansCoin; // 房间中豆币数量 + } + + [Serializable] + public class Data + { + public int carrySeconds; // 持续秒数 + public GameEscapeModel gameEscapeModel; // 嵌套的GameEscapeModel对象 + public List gameEscapeRoomResponseVoList; // 房间信息列表 + public object gameEscapeUserModel; // 用户模型,可以保持为空或根据需要进行定义 + } + + [Serializable] + public class Response + { + public int code; // 状态码 + public string message; // 返回信息 + public Data data; // 数据对象 + } +} + + + diff --git a/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs.meta b/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs.meta new file mode 100644 index 0000000..5c04e63 --- /dev/null +++ b/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e861ed692e9579d44968fc5f4e3a7b37 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: