diff --git a/TheStrongestSnail/Assets/Scripts/Battle_Royale/Test.cs b/TheStrongestSnail/Assets/Scripts/Battle_Royale/Test.cs index 93fad17..e2c888f 100644 --- a/TheStrongestSnail/Assets/Scripts/Battle_Royale/Test.cs +++ b/TheStrongestSnail/Assets/Scripts/Battle_Royale/Test.cs @@ -1,15 +1,31 @@ +using JetBrains.Annotations; +using Newtonsoft.Json; +using System; using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting.Antlr3.Runtime; using UnityEngine; public class Test : MonoBehaviour { - + public string token; + + // 当接收到 token 时触发此方法,仅保存 token + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + // 首次调用加载初始数据 + //LoadGameEscapeData(); + // Debug.Log("接收到新的 token: " + token); + } + public UpdateUserInfo updateUserInfo; public int sec; // Start is called before the first frame update void Start() { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; updateUserInfo = new UpdateUserInfo(); } @@ -17,20 +33,78 @@ public class Test : MonoBehaviour void Update() { TestButton(); - - } public async void TestButton() { if(Input.GetKey(KeyCode.X)) - { - sec = selectGameEscape512.instance.carrySeconds; - Debug.Log("nnnnnnnnnnnnnnnn"+sec); - - - await updateUserInfo.UpdateUserInformation(); - Debug.Log(updateUserInfo.UpdateUserInformation()); - + { + sjdlkjf(); + //Debug.Log("nnnnnnnnnnnnnnnn"+sec); } - } + } + + + public async void sjdlkjf() + { + // 用来给请求头赋值 + 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/gameEscape/queryLatest", "POST", "{}", head51); + Debug.Log("5.1查询最近一场大屠杀(独立)" + response51); // 查询最近一场大逃亡游戏详情: + + // 解析JSON数据 + string json = response51; + Response response = JsonConvert.DeserializeObject(json); + + int gameEscapeId = response.data.gameEscapeModel.id; + Debug.Log(gameEscapeId); + //Debug.Log("解析成功,id为: " + gameEscapeId); + + int carrySeconds = response.data.carrySeconds; + Debug.Log("====================================================================================="+carrySeconds); + + } + } + + + + + + [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; // 数据对象 + } \ No newline at end of file diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/QueryEscapeRoom.cs b/TheStrongestSnail/Assets/Scripts/Login/11/QueryEscapeRoom.cs index 87d9230..eb319b0 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/QueryEscapeRoom.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/QueryEscapeRoom.cs @@ -4,7 +4,8 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class QueryEscapeRoom : MonoBehaviour +using UnityEngine.UI; +/*public class QueryEscapeRoom : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -103,4 +104,159 @@ public class QueryEscapeRoom : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + + +public class QueryEscapeRoom : 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 + selectGameEscape512.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/gameEscape/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; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听================================================================================================ + if (queryEscapeRoomButton != null) + { + queryEscapeRoomButton.onClick.RemoveListener(OnQueryEscapeRoomButtonClicked); + } + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/UpdateUserInfo.cs b/TheStrongestSnail/Assets/Scripts/Login/11/UpdateUserInfo.cs index d73aaba..f04a210 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/UpdateUserInfo.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/UpdateUserInfo.cs @@ -6,7 +6,8 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class UpdateUserInfo : MonoBehaviour +using UnityEngine.UI; +/*public class UpdateUserInfo : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -108,4 +109,148 @@ public class UpdateUserInfo : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + +public class UpdateUserInfo : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastUpdateUserInfoResponse = null; // 保存最新的用户信息更新响应 + + // 假设有一个手动更新用户信息的按钮========================================================================================= + public Button updateUserInfoButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnUpdateUserInfoButtonClicked================================================================== + if (updateUserInfoButton != null) + { + updateUserInfoButton.onClick.AddListener(OnUpdateUserInfoButtonClicked); + } + } + + // 当接收到 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 更新时自动调用更新用户信息 + lastUpdateUserInfoResponse = await UpdateUserInformation(); + Debug.Log("HandleGameEscapeIdUpdated 处理的用户信息更新响应: " + lastUpdateUserInfoResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用更新用户信息方法 + lastUpdateUserInfoResponse = await UpdateUserInformation(); + Debug.Log("初始加载的用户信息更新响应: " + lastUpdateUserInfoResponse); + } + + // 按钮点击后触发用户信息更新操作 + public async void OnUpdateUserInfoButtonClicked() + { + // 检查 token 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法更新用户信息,token 未设置。"); + return; + } + + // 调用更新用户信息方法 + lastUpdateUserInfoResponse = await UpdateUserInformation(); + Debug.Log("用户按钮触发的用户信息更新响应: " + lastUpdateUserInfoResponse); + + // 根据响应做进一步的处理 + HandleUpdateUserInfoResponse(lastUpdateUserInfoResponse); + } + + // 更新用户信息 + public async Task UpdateUserInformation() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法更新用户信息,token 未设置。"); + return null; + } + + var headers = CreateHeaders(); + string body = @" + { + ""nickName"": ""wulongxiao"", + ""headImg"": ""https://fantasymonster-app.oss-cn-hangzhou.aliyuncs.com/upload/imgs/127e4e42d7c0405aab53359c1b278a9c.png"", + ""gender"": 1, + ""birthday"": ""2023-12-12 12:12:12"" + }"; + + Debug.Log("正在更新用户信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/update", "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 } + }; + } + + // 处理 UpdateUserInformation 方法的响应 + private void HandleUpdateUserInfoResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("更新用户信息成功!"); + } + else + { + Debug.LogWarning("更新用户信息失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听=============================================================================================== + if (updateUserInfoButton != null) + { + updateUserInfoButton.onClick.RemoveListener(OnUpdateUserInfoButtonClicked); + } + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/UserBetResult.cs b/TheStrongestSnail/Assets/Scripts/Login/11/UserBetResult.cs index 07102ff..85e789e 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/UserBetResult.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/UserBetResult.cs @@ -4,7 +4,8 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class UserBetResult : MonoBehaviour +using UnityEngine.UI; +/*public class UserBetResult : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -103,4 +104,159 @@ public class UserBetResult : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + + + +public class UserBetResult : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastUserBetResultResponse = null; // 保存最新的用户下注结果响应 + + // 假设有一个按钮可以手动查询用户下注结果============================================================================= + //public Button queryUserBetButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + //// 假设按钮被点击时调用 OnQueryUserBetButtonClicked===================================================================== + //if (queryUserBetButton != null) + //{ + // queryUserBetButton.onClick.AddListener(OnQueryUserBetButtonClicked); + //} + } + + // 当接收到 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); + + // 调用查询用户下注结果方法 + lastUserBetResultResponse = await QueryUserBetResult(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询结果响应: " + lastUserBetResultResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询用户下注结果方法 + lastUserBetResultResponse = await QueryUserBetResult(); + Debug.Log("初始加载的查询用户下注结果响应: " + lastUserBetResultResponse); + } + + // 按钮点击后触发用户下注结果查询操作 + public async void OnQueryUserBetButtonClicked() + { + // 检查 token 和 escapeId 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询下注结果,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法查询下注结果,escapeId 未设置。"); + return; + } + + // 调用查询用户下注结果方法 + lastUserBetResultResponse = await QueryUserBetResult(); + Debug.Log("用户按钮触发的查询结果响应: " + lastUserBetResultResponse); + + // 根据响应做进一步的处理 + HandleUserBetResultResponse(lastUserBetResultResponse); + } + + // 查询用户下注结果 + public async Task QueryUserBetResult() + { + 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} + }}"; + + string response = await web.SendRequest("http://121.40.42.41:8080/snail/gameEscape/queryUserBetResult", "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 } + }; + } + + // 处理 QueryUserBetResult 方法的响应 + private void HandleUserBetResultResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("查询下注结果成功!"); + } + else + { + Debug.LogWarning("查询下注结果失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + //// 取消按钮的点击监听================================================================================================ + //if (queryUserBetButton != null) + //{ + // queryUserBetButton.onClick.RemoveListener(OnQueryUserBetButtonClicked); + //} + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/gamber.cs b/TheStrongestSnail/Assets/Scripts/Login/11/gamber.cs index 18e8955..accae73 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/gamber.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/gamber.cs @@ -4,7 +4,8 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class gamber : MonoBehaviour +using UnityEngine.UI; +/*public class gamber : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -32,7 +33,7 @@ public class gamber : MonoBehaviour public async void HandleGameEscapeIdUpdated(int newGameEscapeId) { escapeId = newGameEscapeId; // 保存最新的 escapeId - //await UserBet();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器 + await UserBet();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器 //Debug.Log("接收到新的 GameEscapeId: " + escapeId); } @@ -105,4 +106,157 @@ public class gamber : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + +public class Gamber : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastUserBetResponse = null; // 保存最新的用户下注响应 + + // 假设有一个下注按钮==================================================================================================== + public Button userBetButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法 + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设下注按钮被点击时调用 OnUserBetButtonClicked=================================================================== + if (userBetButton != null) + { + userBetButton.onClick.AddListener(OnUserBetButtonClicked); + } + } + + // 处理接收到 token 时的事件 + public void HandleTokenReceived(string receivedToken) + { + token = receivedToken; // 保存最新的 token + Debug.Log("接收到的 token: " + token); + // 可以根据需要调用 LoadInitialData(),比如在 token 更新时自动加载初始数据 + LoadInitialData(); + } + + // 处理游戏逃亡 ID 更新的事件 + public async void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + escapeId = newGameEscapeId; // 保存最新的 escapeId + Debug.Log("接收到新的 GameEscapeId: " + escapeId); + + // 在这里可以选择手动调用 UserBet(),例如 + lastUserBetResponse = await UserBet(); + Debug.Log("HandleGameEscapeIdUpdated 处理的 UserBet 响应: " + lastUserBetResponse); + } + + // 按钮点击后触发下注操作 + public async void OnUserBetButtonClicked() + { + // 当按钮被点击时调用 UserBet() + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法进行下注,token 未设置。"); + return; + } + + if (escapeId == -1) + { + Debug.LogWarning("无法进行下注,escapeId 未设置。"); + return; + } + + // 调用 UserBet 并保存响应//========================================================================================== + lastUserBetResponse = await UserBet(); + Debug.Log("用户按钮触发的 UserBet 响应: " + lastUserBetResponse); + + // 可以根据响应做进一步的处理 + HandleUserBetResponse(lastUserBetResponse); + } + + // 用户下注方法 + public async Task UserBet() + { + 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}, + ""bet"": 100, + ""roomNo"": 1 + }}"; + + string response = await web.SendRequest("http://121.40.42.41:8080/snail/gameEscape/userBet", "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 } + }; + } + + // 处理 UserBet 方法的响应 + private void HandleUserBetResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("下注成功!"); + } + else + { + Debug.LogWarning("下注失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听================================================================================================ + if (userBetButton != null) + { + userBetButton.onClick.RemoveListener(OnUserBetButtonClicked); + } + } + + // 加载初始数据 + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + lastUserBetResponse = await UserBet(); // 调用 UserBet 并保存响应 + Debug.Log("初始加载的 UserBet 响应: " + lastUserBetResponse); + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/loadMall.cs b/TheStrongestSnail/Assets/Scripts/Login/11/loadMall.cs index bc2d8d2..0c7e20f 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/loadMall.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/loadMall.cs @@ -4,7 +4,8 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class loadMall : MonoBehaviour +using UnityEngine.UI; +/*public class loadMall : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -140,4 +141,187 @@ public class loadMall : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + + + +public class LoadMall : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastMallResponse = null; // 保存最新的商城操作响应 + + // 假设有一个手动加载商城数据的按钮======================================================================================== + public Button loadMallButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnLoadMallButtonClicked======================================================================== + if (loadMallButton != null) + { + loadMallButton.onClick.AddListener(OnLoadMallButtonClicked); + } + } + + // 当接收到 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 更新时自动调用加载商城数据 + lastMallResponse = await LoadMallData(); + Debug.Log("HandleGameEscapeIdUpdated 处理的商城数据响应: " + lastMallResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用加载商城数据方法 + lastMallResponse = await LoadMallData(); + Debug.Log("初始加载的商城数据响应: " + lastMallResponse); + } + + // 按钮点击后触发商城数据加载操作 + public async void OnLoadMallButtonClicked() + { + // 检查 token 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载商城数据,token 未设置。"); + return; + } + + // 调用加载商城数据方法 + lastMallResponse = await LoadMallData(); + Debug.Log("用户按钮触发的商城数据加载响应: " + lastMallResponse); + + // 根据响应做进一步的处理 + HandleMallResponse(lastMallResponse); + } + + // 加载商城数据 + public async Task LoadMallData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载商城数据,token 未设置。"); + return null; + } + + var headers = CreateHeaders(); + + // 查询商城虚拟商品列表 + await QueryMallList(0); + // 查询商城实体商品列表 + await QueryMallList(1); + // 获取商品详情 + await GetMallProductDetails(1); + // 购买商品 + await BuyMallProduct(106, 1); + + Debug.Log("商城数据加载完成"); + return "商城数据加载完成"; + } + + // 查询商城列表 + public async Task QueryMallList(int productType) + { + var headers = CreateHeaders(); + Mall_List mallList = new Mall_List + { + productType = productType + }; + string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/page", "POST", JsonUtility.ToJson(mallList), headers); + Debug.Log($"商城列表 (productType: {productType}) 响应: " + response); + } + + // 获取商城商品详情 + public async Task GetMallProductDetails(int productId) + { + var headers = CreateHeaders(); + Product_Details productDetails = new Product_Details + { + productId = productId + }; + string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/info", "POST", JsonUtility.ToJson(productDetails), headers); + Debug.Log("获取商品详情响应: " + response); + } + + // 购买商品 + public async Task BuyMallProduct(int userId, int productId) + { + var headers = CreateHeaders(); + Mall_buy mallBuy = new Mall_buy + { + userId = userId, + productId = productId + }; + string response = await web.SendRequest("http://121.40.42.41:8080/snail/product/buy", "POST", JsonUtility.ToJson(mallBuy), headers); + Debug.Log("购买商品响应: " + response); + } + + // 创建请求头,使用最新的 token + public Dictionary CreateHeaders() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("尝试创建请求头时,token 未设置。"); + return new Dictionary(); + } + + return new Dictionary + { + { "Authorization", token } + }; + } + + // 处理商城数据的响应 + private void HandleMallResponse(string response) + { + if (response.Contains("完成")) + { + Debug.Log("商城数据加载成功!"); + } + else + { + Debug.LogWarning("商城数据加载失败,响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听============================================================================================== + if (loadMallButton != null) + { + loadMallButton.onClick.RemoveListener(OnLoadMallButtonClicked); + } + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/selectQueryKill.cs b/TheStrongestSnail/Assets/Scripts/Login/11/selectQueryKill.cs index 143cafc..475e35e 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/selectQueryKill.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/selectQueryKill.cs @@ -6,11 +6,13 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class selectQueryKill : MonoBehaviour +using UnityEngine.UI; +/*public class selectQueryKill : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + void Start() { // 注册监听事件,当收到 token 时,触发 HandleTokenReceived @@ -105,4 +107,159 @@ public class selectQueryKill : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + + +public class SelectQueryKill : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastQueryKillResponse = null; // 保存最新的击杀信息查询响应 + + // 假设有一个手动查询击杀信息的按钮======================================================================================= + public Button queryKillButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.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"": 106, + ""escapeId"": {escapeId} + }}"; + + Debug.Log("正在查询击杀信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/gameEscape/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; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听================================================================================================= + if (queryKillButton != null) + { + queryKillButton.onClick.RemoveListener(OnQueryKillButtonClicked); + } + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/11/selectUserInfo.cs b/TheStrongestSnail/Assets/Scripts/Login/11/selectUserInfo.cs index aef1c51..c3d205c 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/11/selectUserInfo.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/11/selectUserInfo.cs @@ -4,7 +4,9 @@ using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using Newtonsoft.Json; -public class selsecUserInfo : MonoBehaviour +using UnityEngine.UI; +using Newtonsoft.Json.Linq; +/*public class selsecUserInfo : MonoBehaviour { public string token = null; // 保存最新的 token,初始为 null public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 @@ -101,4 +103,319 @@ public class selsecUserInfo : MonoBehaviour LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; } -} \ No newline at end of file +}*/ + + + + +/*public class SelectUserInfo : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastSelectUserInfoResponse = null; // 保存最新的用户详情查询响应 + + // 假设有一个手动查询用户详情的按钮 + public Button selectUserInfoButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnSelectUserInfoButtonClicked + if (selectUserInfoButton != null) + { + selectUserInfoButton.onClick.AddListener(OnSelectUserInfoButtonClicked); + } + } + + // 当接收到 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 更新时自动调用查询用户详情 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询用户详情响应: " + lastSelectUserInfoResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询用户详情方法 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("初始加载的查询用户详情响应: " + lastSelectUserInfoResponse); + } + + // 按钮点击后触发用户详情查询操作 + public async void OnSelectUserInfoButtonClicked() + { + // 检查 token 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询用户详情,token 未设置。"); + return; + } + + // 调用查询用户详情方法 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("用户按钮触发的查询用户详情响应: " + lastSelectUserInfoResponse); + + // 根据响应做进一步的处理 + HandleSelectUserResponse(lastSelectUserInfoResponse); + } + + // 查询用户详情 + public async Task SelectUser() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询用户详情,token 未设置。"); + return null; + } + + var headers = CreateHeaders(); + Debug.Log("正在查询用户详细信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/queryUserInfo", "POST", "{}", 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 } + }; + } + + // 处理 SelectUser 方法的响应 + private void HandleSelectUserResponse(string response) + { + if (response.Contains("success")) + { + Debug.Log("查询用户详情成功!"); + } + else + { + Debug.LogWarning("查询用户详情失败,服务器响应: " + response); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听 + if (selectUserInfoButton != null) + { + selectUserInfoButton.onClick.RemoveListener(OnSelectUserInfoButtonClicked); + } + } +}*/ + + + +public class SelectUserInfo : MonoBehaviour +{ + public string token = null; // 保存最新的 token,初始为 null + public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置 + private string lastSelectUserInfoResponse = null; // 保存最新的用户详情查询响应 + + // 假设有一个手动查询用户详情的按钮======================================================================================= + public Button selectUserInfoButton; + + void Start() + { + // 注册监听事件,当收到 token 时,触发 HandleTokenReceived + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + + // 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + + // 假设按钮被点击时调用 OnSelectUserInfoButtonClicked=================================================================== + if (selectUserInfoButton != null) + { + selectUserInfoButton.onClick.AddListener(OnSelectUserInfoButtonClicked); + } + } + + // 当接收到 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 更新时自动调用查询用户详情 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("HandleGameEscapeIdUpdated 处理的查询用户详情响应: " + lastSelectUserInfoResponse); + + // 处理响应内容 + HandleSelectUserResponse(lastSelectUserInfoResponse); + } + + // 加载初始数据,使用最新的 token + public async void LoadInitialData() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法加载初始数据,token 未设置。"); + return; + } + + // 调用查询用户详情方法 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("初始加载的查询用户详情响应: " + lastSelectUserInfoResponse); + + // 处理响应内容 + HandleSelectUserResponse(lastSelectUserInfoResponse); + } + + // 按钮点击后触发用户详情查询操作 + public async void OnSelectUserInfoButtonClicked() + { + // 检查 token 是否已正确设置 + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询用户详情,token 未设置。"); + return; + } + + // 调用查询用户详情方法 + lastSelectUserInfoResponse = await SelectUser(); + Debug.Log("用户按钮触发的查询用户详情响应: " + lastSelectUserInfoResponse); + + // 处理响应内容 + HandleSelectUserResponse(lastSelectUserInfoResponse); + } + + // 查询用户详情 + public async Task SelectUser() + { + if (string.IsNullOrEmpty(token)) + { + Debug.LogWarning("无法查询用户详情,token 未设置。"); + return null; + } + + var headers = CreateHeaders(); + Debug.Log("正在查询用户详细信息..."); + string response = await web.SendRequest("http://121.40.42.41:8080/snail/user/queryUserInfo", "POST", "{}", 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 } + }; + } + + // 处理 SelectUser 方法的响应,直接从字符串中获取字段 + private void HandleSelectUserResponse(string response) + { + try + { + // 使用 JObject 解析 JSON 字符串 + JObject jsonObject = JObject.Parse(response); + + // 获取 code 和 message + int code = jsonObject.Value("code"); + string message = jsonObject.Value("message"); + + if (code == 200) + { + JObject data = jsonObject.Value("data"); + if (data != null)//用户信息 + { + int userId = data.Value("userId");//用户ID + string userName = data.Value("userName");//用户名 + string nickName = data.Value("nickName");//呢称 + string headImg = data.Value("headImg");//头像 + int gender = data.Value("gender");//性别 + string birthday = data.Value("birthday");//生日 + int voluteCoin = data.Value("voluteCoin");//蜗壳 + int beansCoin = data.Value("beansCoin");//蜗牛蛋 + int ichorCoin = data.Value("ichorCoin");//灵液 + int cuteNo = data.Value("cuteNo");//靓号 + + // 打印输出获取的字段 + Debug.Log($"查询用户信息成功!用户 ID: {userId}, 用户名: {userName}, 昵称: {nickName}, 性别: {(gender == 1 ? "男" : "女")}, 蜗壳: {voluteCoin}, 蜗蛋: {beansCoin}, 灵液: {ichorCoin}, 靓号: {cuteNo}"); + } + else + { + Debug.LogWarning("数据对象为空,无法获取用户详细信息"); + } + } + else + { + Debug.LogWarning($"查询用户信息失败,消息: {message}"); + } + } + catch (Exception ex) + { + Debug.LogError($"解析响应时出现异常: {ex.Message}"); + } + } + + void OnDestroy() + { + // 注销监听事件,避免内存泄漏 + LoginAndGetToken.OnTokenReceived -= HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated; + + // 取消按钮的点击监听-============================================================================================== + if (selectUserInfoButton != null) + { + selectUserInfoButton.onClick.RemoveListener(OnSelectUserInfoButtonClicked); + } + } +} diff --git a/TheStrongestSnail/Assets/Scripts/Login/WebConnact.cs b/TheStrongestSnail/Assets/Scripts/Login/WebConnact.cs index 1e8c66d..eada41c 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/WebConnact.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/WebConnact.cs @@ -999,8 +999,8 @@ public class WebConnact : MonoBehaviour { token = receivedToken; // 保存最新的 token // 首次调用加载初始数据 - //LoadInitialData(); - //LoadGameEscapeData(); + LoadInitialData(); + LoadGameEscapeData(); Debug.Log("接收到新的 token: " + token); } @@ -1008,7 +1008,7 @@ public class WebConnact : MonoBehaviour public void HandleGameEscapeIdUpdated(int newGameEscapeId) { escapeId = newGameEscapeId; // 保存最新的 escapeId - //LoadGameEscapeData(); + LoadGameEscapeData(); //Debug.Log("接收到新的 GameEscapeId: " + escapeId); } diff --git a/TheStrongestSnail/Assets/Scripts/Login/selectGameEscape512.cs b/TheStrongestSnail/Assets/Scripts/Login/selectGameEscape512.cs index 11c72ff..ab04bc6 100644 --- a/TheStrongestSnail/Assets/Scripts/Login/selectGameEscape512.cs +++ b/TheStrongestSnail/Assets/Scripts/Login/selectGameEscape512.cs @@ -5,7 +5,7 @@ using UnityEngine; using Newtonsoft.Json; using System.Threading.Tasks; -public class selectGameEscape512 : MonoBehaviour +/*public class selectGameEscape512 : MonoBehaviour { public static selectGameEscape512 instance; public int gameEscapeId; @@ -50,6 +50,7 @@ public class selectGameEscape512 : MonoBehaviour yield return new WaitForSeconds(2f); // 每2秒暂停一次 yield return gameEscape(token); // 调用gameEscape方法 } + yield return null; } public async Task gameEscape(string token) @@ -128,5 +129,155 @@ public class selectGameEscape512 : MonoBehaviour public Data data; // 数据对象 } +}*/ + + + +public class selectGameEscape512 : MonoBehaviour +{ + public static selectGameEscape512 instance; + public int gameEscapeId; + public int carrySeconds; + + // 定义一个事件,当 gameEscapeId 被更新时触发 + public static event Action OnGameEscapeIdUpdated; + + private string token; // 用于保存收到的 token + + void Start() + { + instance = this; + + // 监听登录获取 token + LoginAndGetToken.OnTokenReceived += HandleTokenReceived; + selectGameEscape512.OnGameEscapeIdUpdated += HandleGameEscapeIdUpdated; + } + + void HandleTokenReceived(string token) + { + this.token = token; + Debug.Log("HandleTokenReceived监听:" + token); + StartCoroutine(GameEscapeRoutine(token)); // 启动协程,每2秒调用一次gameEscape + } + + void HandleGameEscapeIdUpdated(int newGameEscapeId) + { + Debug.Log("GameEscapeId 已更新为: " + newGameEscapeId); + } + + IEnumerator GameEscapeRoutine(string token) + { + while (true) + { + yield return new WaitForSeconds(2f); // 每2秒暂停一次 + yield return gameEscape(token); // 调用gameEscape方法 + } + yield return null; + } + + 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/gameEscape/queryLatest", "POST", "{}", head51); + Debug.Log("5.1查询最近一场大屠杀(独立)" + response51); + + // 解析JSON数据为 Response 对象 + Response response = JsonConvert.DeserializeObject(response51); + + 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; // 数据对象 + } } +