_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Login/selectLatest511.cs
wulongxiao 2c61dc24b5 jiekou
2024-11-14 18:25:21 +08:00

286 lines
8.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<int> 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<string, string> head51 = new Dictionary<string, string>
{
{ "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<Response>(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<GameEscapeRoomResponseVo> 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<int> 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<string, string> head511 = new Dictionary<string, string>
{
{ "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<Response>(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<GameEscapeRoomResponseVo> gameEscapeRoomResponseVoList; // 房间信息列表
public object gameEscapeUserModel; // 用户模型,可以保持为空或根据需要进行定义
}
[Serializable]
public class Response
{
public int code; // 状态码
public string message; // 返回信息
public Data data; // 数据对象
}
}