323 lines
9.8 KiB
C#
323 lines
9.8 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 gamber : 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 UserBet();//=====================================================================放在这里仅因为懒得写触发条件,可以放在任何地方,比如input.GetKeyDown.....必须改掉,吃服务器
|
||
//Debug.Log("接收到新的 GameEscapeId: " + escapeId);
|
||
}
|
||
|
||
|
||
// 加载初始数据,使用最新的 token
|
||
public async void LoadInitialData()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载初始数据,token 未设置。");
|
||
return;
|
||
}
|
||
await UserBet();//=====================================================================放在此处只为解除一个黄色报错,看着闹心。但可以不要
|
||
}
|
||
|
||
// 加载游戏逃亡数据,使用最新的 escapeId 和 token
|
||
public async void selectQueryKill1()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,token 未设置。");
|
||
return;
|
||
}
|
||
|
||
if (escapeId == -1)
|
||
{
|
||
Debug.LogWarning("无法加载游戏逃亡数据,escapeId 未设置。");
|
||
return;
|
||
}
|
||
|
||
await UserBet();//==========================================================================================================================================================================
|
||
|
||
}
|
||
|
||
//===============================================================================================================================================================================================================================
|
||
// 用户下注
|
||
public async Task UserBet()
|
||
{
|
||
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/LatestGame511/userBet", "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;
|
||
selectLatest511.OnGameEscapeIdUpdated -= HandleGameEscapeIdUpdated;
|
||
}
|
||
}*/
|
||
|
||
//解析+单例
|
||
public class Gamber513 : MonoBehaviour
|
||
{
|
||
// 单例模式实现
|
||
public static Gamber513 Instance { get; private set; }
|
||
|
||
// 全局变量,用于存储服务器返回的具体字段
|
||
public string token = null; // 保存最新的 token,初始为 null
|
||
public int escapeId = -1; // 保存最新的 escapeId,初始为 -1 表示未设置
|
||
public int userId; // 用户 ID
|
||
private string lastUserBetResponse = null; // 保存最新的用户下注响应
|
||
|
||
// 服务器返回的字段,作为全局变量
|
||
public int code; // 返回状态码,例如 200 表示成功
|
||
public string message; // 提示信息
|
||
public object data; // 数据,可能为空
|
||
|
||
// 假设有一个下注按钮
|
||
public Button userBetButton;
|
||
|
||
void Awake()
|
||
{
|
||
// 实现单例模式
|
||
if (Instance == null)
|
||
{
|
||
Instance = this;
|
||
DontDestroyOnLoad(gameObject); // 保证此实例在场景切换时不会被销毁
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
// 注册监听事件,当收到 token 时,触发 HandleTokenReceived
|
||
LoginAndGetToken.OnTokenReceived += HandleTokenReceived;
|
||
|
||
// 注册监听,当 escapeId 更新时调用 HandleGameEscapeIdUpdated,但不执行其他方法
|
||
selectLatest511.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);
|
||
|
||
// 解析响应
|
||
ParseUserBetResponse(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);
|
||
|
||
// 解析响应
|
||
ParseUserBetResponse(lastUserBetResponse);
|
||
}
|
||
|
||
// 用户下注方法
|
||
public async Task<string> UserBet()
|
||
{
|
||
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},
|
||
""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<string, string> CreateHeaders()
|
||
{
|
||
if (string.IsNullOrEmpty(token))
|
||
{
|
||
Debug.LogWarning("尝试创建请求头时,token 未设置。");
|
||
return new Dictionary<string, string>();
|
||
}
|
||
|
||
return new Dictionary<string, string>
|
||
{
|
||
{ "Authorization", token }
|
||
};
|
||
}
|
||
|
||
// 处理 UserBet 方法的响应,并将解析后的字段保存到全局变量中
|
||
private void ParseUserBetResponse(string response)
|
||
{
|
||
if (string.IsNullOrEmpty(response))
|
||
{
|
||
Debug.LogWarning("无法解析用户下注响应,响应为空。");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
// 使用 Newtonsoft.Json 解析响应
|
||
UserBetResponse betResponse = JsonConvert.DeserializeObject<UserBetResponse>(response);
|
||
|
||
// 将解析后的数据赋值给全局变量
|
||
code = betResponse.code;
|
||
message = betResponse.message;
|
||
data = betResponse.data;
|
||
|
||
// 输出结果
|
||
if (code == 200)
|
||
{
|
||
Debug.Log($"下注成功!消息: {message}");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"下注失败,消息: {message}");
|
||
}
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
Debug.LogWarning("解析用户下注响应时出错: " + e.Message);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
// 注销监听事件,避免内存泄漏
|
||
LoginAndGetToken.OnTokenReceived -= HandleTokenReceived;
|
||
selectLatest511.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);
|
||
|
||
// 解析响应
|
||
ParseUserBetResponse(lastUserBetResponse);
|
||
}
|
||
}
|
||
|
||
// 用户下注响应数据类,用于存储解析后的响应数据
|
||
public class UserBetResponse
|
||
{
|
||
public int code { get; set; } // 返回状态码,例如 200 表示成功
|
||
public string message { get; set; } // 提示信息
|
||
public object data { get; set; } // 数据,可能为空
|
||
}
|