263 lines
8.7 KiB
C#
263 lines
8.7 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Threading.Tasks;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine.UI;
|
||
/*public class UserBetResult : MonoBehaviour
|
||
{
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法
|
||
selectGameEscape512.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 QueryUserBetResult();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器
|
||
Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
}
|
||
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
await QueryUserBetResult();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要
|
||
}
|
||
|
||
// 加载游戏逃亡数据,使用最新的 escapeId 和 token
|
||
public async void selectQueryKill1()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
if (escapeId == -1)
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。");
|
||
return;
|
||
}
|
||
|
||
await QueryUserBetResult();//==========================================================================================================================================================================
|
||
|
||
}
|
||
|
||
//===============================================================================================================================================================================================================================
|
||
// 查询用户下注结果
|
||
public async Task QueryUserBetResult()
|
||
{
|
||
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);
|
||
}
|
||
//===============================================================================================================================================================================================================================
|
||
|
||
// 创建请求头,使用最新的 token
|
||
public Dictionary<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectGameEscape512.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
}
|
||
}*/
|
||
|
||
|
||
|
||
|
||
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<string> 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<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "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);
|
||
//}
|
||
}
|
||
}
|