调接口

This commit is contained in:
GL 2024-11-15 15:07:55 +08:00
parent 4c85b4fd5d
commit 7f9ea5388e
5 changed files with 229 additions and 151 deletions

View File

@ -12317,13 +12317,6 @@ GameObject:
- component: {fileID: 2127674358}
- component: {fileID: 2127674361}
- component: {fileID: 2127674362}
- component: {fileID: 2127674369}
- component: {fileID: 2127674368}
- component: {fileID: 2127674367}
- component: {fileID: 2127674366}
- component: {fileID: 2127674365}
- component: {fileID: 2127674364}
- component: {fileID: 2127674363}
m_Layer: 5
m_Name: InfoObject
m_TagString: Untagged
@ -12413,139 +12406,6 @@ MonoBehaviour:
HouseLu: {fileID: 2143530050}
HouseHuang: {fileID: 20259221}
HouseFen: {fileID: 715869104}
--- !u!114 &2127674363
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e57021e815a075842afcb8cea3c745d1, type: 3}
m_Name:
m_EditorClassIdentifier:
token:
escapeId: -1
userId: 0
code: 0
message:
roomNoKill: 0
roomNoRemain: 0
--- !u!114 &2127674364
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3e317c8c24b13004bb49858be0928128, type: 3}
m_Name:
m_EditorClassIdentifier:
token:
escapeId: -1
code: 0
message:
data: 0
updateUserInfoButton: {fileID: 0}
--- !u!114 &2127674365
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 81202125ae1fe2f43a0ccf3e996186bf, type: 3}
m_Name:
m_EditorClassIdentifier:
token:
escapeId: -1
code: 0
message:
userId: 0
userName:
nickName:
headImg:
gender: 0
birthday:
voluteCoin: 0
beansCoin: 0
ichorCoin: 0
cuteNo: 0
selectUserInfoButton: {fileID: 0}
--- !u!114 &2127674366
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c230883956c0b3546a885c300ad34498, type: 3}
m_Name:
m_EditorClassIdentifier:
token:
escapeId: -1
userId: 0
latestCode: 0
latestMessage:
latestRoomNoKill: 0
latestRoomNoRemain: 0
queryKillButton: {fileID: 0}
--- !u!114 &2127674367
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b27d2d49f51751e42b31653d0b0b8d2d, type: 3}
m_Name:
m_EditorClassIdentifier:
latestEscapeId: 0
latestRoomNo: 0
latestRoomBeansCoin: 0
token:
escapeId: -1
userId: 0
lastEscapeRoomResponse:
--- !u!114 &2127674368
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e8d2e3fe1964bd34f8d61e48e533c393, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &2127674369
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2127674355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 265365642b7b8644081cd2ce79416fb3, type: 3}
m_Name:
m_EditorClassIdentifier:
token:
escapeId: -1
userId: 0
code: 0
message:
userBetButton: {fileID: 0}
--- !u!1 &2135586305
GameObject:
m_ObjectHideFlags: 0

View File

@ -32,25 +32,30 @@ public class HegemonTime : MonoBehaviour
while (timeNum >=0)
{
timeText.text = (timeNum-carrySeconds).ToString(); // 显示整数秒数
timeText.text = (timeNum).ToString(); // 显示整数秒数
yield return new WaitForSeconds(1f); // 等待1秒
//timeNum -= 1;
timeNum -= 1;
if (timeNum==20)
if (timeNum<= int.Parse(InfoObject.GetComponent<selectLatest511>().betTime)&&InfoObject.GetComponent<selectLatest511>().status==1)//停止下注,关门
{
allHouseContro.ControAllDoorClose();
MaskContro.instance.SetMask(true);
}
if (timeNum<20- int.Parse(InfoObject.GetComponent<selectLatest511>().countTime))//boos出现
{
Debug.Log("Boos来了");
BossContro.instance.BossMove();
}
}
Debug.Log("Boos来了");
BossContro.instance.BossMove();
//timeNum = 60;
}
public void HadInfAndStartGame()//启动倒计时
{
timeNum=60-InfoObject.GetComponent<selectLatest511>().carrySeconds;
timeNum =int.Parse(InfoObject.GetComponent<selectLatest511>().startTime);
StartCoroutine(StartGame());
}
}

View File

@ -700,7 +700,7 @@ using UnityEngine.UI;
}*/
//解析+高频+单例
//解析+单例
public class QueryRoomdetails512 : MonoBehaviour
/*public class QueryRoomdetails512 : MonoBehaviour
{
// 单例实例
public static QueryRoomdetails512 Instance { get; private set; }
@ -712,7 +712,8 @@ public class QueryRoomdetails512 : MonoBehaviour
public string token = null; // 保存最新的 token初始为 null
public int escapeId = -1; // 保存最新的 escapeId初始为 -1 表示未设置
public int userId;
public int userId=106;
public int latestCarrySeconds;//时间
public string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应
private Coroutine queryCoroutine; // 用于保存协程引用
@ -839,6 +840,7 @@ public class QueryRoomdetails512 : MonoBehaviour
latestEscapeId = room.escapeId;
latestRoomNo = room.roomNo;
latestRoomBeansCoin = room.roomBeansCoin;
foreach (var roomDetail in serverResponse.data.gameEscapeRoomResponseVoList)
{
@ -852,6 +854,213 @@ public class QueryRoomdetails512 : MonoBehaviour
}
}
// 每两秒查询一次逃亡房间详情的协程
private IEnumerator QueryEscapeRoomRoutine()
{
while (true)
{
// 每两秒检查并查询逃亡房间详情
if (!string.IsNullOrEmpty(token) && escapeId != -1)
{
yield return QueryEscapeRoomDetails();
// Debug.Log("carryTime"+latestCarrySeconds);
}
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; // 房间下注欢乐豆
public int carrySeconds;//时间
}
[Serializable]
public class Data
{
public List<GameEscapeRoomResponseVo> 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 int latestCarrySeconds; // 最新的携带秒数
public string token = null; // 保存最新的 token初始为 null
public int escapeId = -1; // 保存最新的 escapeId初始为 -1 表示未设置
public int userId = 106;
public string lastEscapeRoomResponse = null; // 保存最新的逃亡房间查询响应
private Coroutine queryCoroutine; // 用于保存协程引用
public List<GameEscapeRoomResponseVo> gameEscapeRoomResponseVoList=new List<GameEscapeRoomResponseVo>();
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
// 在接收到 escapeId 更新时自动调用查询逃亡房间详情
lastEscapeRoomResponse = await QueryEscapeRoomDetails();
}
// 加载初始数据,使用最新的 token
public async void LoadInitialData()
{
if (string.IsNullOrEmpty(token))
{
Debug.LogWarning("无法加载初始数据token 未设置。");
return;
}
// 调用查询逃亡房间详情方法
lastEscapeRoomResponse = await QueryEscapeRoomDetails();
}
// 查询逃亡房间详情
public async Task<string> 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}
}}";
string response = await web.SendRequest("http://121.40.42.41:8080/snail/gameEscape/queryEscapeRoomList", "POST", body, headers);
Debug.Log("5.1.2查询逃亡房间详情响应: " + response);
// 反序列化 JSON 数据为 ServerResponse 对象并处理响应
HandleEscapeRoomResponse(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 }
};
}
// 处理 QueryEscapeRoomDetails 方法的响应
private void HandleEscapeRoomResponse(string response)
{
ServerResponse serverResponse = JsonConvert.DeserializeObject<ServerResponse>(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;
latestCarrySeconds = serverResponse.data.carrySeconds;
foreach (var roomDetail in serverResponse.data.gameEscapeRoomResponseVoList)
{
gameEscapeRoomResponseVoList.Add(roomDetail);
Debug.Log($"Escape ID: {roomDetail.escapeId}, Room No: {roomDetail.roomNo}, Room Beans Coin: {roomDetail.roomBeansCoin}");
}
}
}
else
{
Debug.LogWarning("查询逃亡房间详情失败,服务器响应: " + response);
}
}
// 每两秒查询一次逃亡房间详情的协程
private IEnumerator QueryEscapeRoomRoutine()
{
@ -892,7 +1101,10 @@ public class QueryRoomdetails512 : MonoBehaviour
[Serializable]
public class Data
{
public int carrySeconds; // 携带时间秒数
public List<GameEscapeRoomResponseVo> gameEscapeRoomResponseVoList; // 房间列表
public object gameEscapeModel; // 游戏逃亡模型 (目前未处理)
public object gameEscapeUserModel; // 游戏逃亡用户模型 (目前未处理)
}
[Serializable]
@ -903,6 +1115,7 @@ public class QueryRoomdetails512 : MonoBehaviour
public Data data; // 数据对象
}
}
//单例使用示例
//public class AnotherClass : MonoBehaviour
//{

View File

@ -18,7 +18,7 @@ public class LoginAndGetToken : MonoBehaviour
Login();
}
public async void Login()
public async void Login()
{
// ... 되쩌쭉서 ...
loginbody body = new loginbody();

View File

@ -189,7 +189,7 @@ public class selectLatest511 : MonoBehaviour
{
while (true)
{
yield return new WaitForSeconds(1f);//=============================================================================================
yield return new WaitForSeconds(30f);//=============================================================================================
yield return LatestGame511(token);
}
}