using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; using System.Threading.Tasks; using UnityEditor.PackageManager; using JetBrains.Annotations; using System.Data; public class auth_login { public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e"; public string grantType = "password"; } //public class TemplateList//可空 //{ // public string templateId=""; // public string templateName; // public string sceneId; // public string subjectId; // public string queryType; //} public class createTemplate { public string templateName; public string sceneId; public string subjectId; public string isTemplate; public string mode; } //============================================================================================ public class login : MonoBehaviour { public string token;//登录返回的token public Global Global; auth_login auth_Login = new auth_login(); public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e"; // 创建请求头,使用最新的 token public Dictionary CreateHeaders() { if (string.IsNullOrEmpty(token)) { Debug.LogWarning("尝试创建请求头时,token 未设置。"); return new Dictionary(); } return new Dictionary { { "Authorization","Bearer "+token }, {"clientId", clientId } }; } //================================================================================================= public async void Start() { await loging(); SubmitTemplate(); BindNPC(); BindMaterial(); BindPlayer(); QueryDrillSubject(); TemplateList(); createTemplate(); QueryScene(); queryRoleList(); } //登录 public async Task loging() { auth_login auth_Login = new auth_login(); Debug.Log(JsonUtility.ToJson(auth_Login)); string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(auth_Login)); Debug.Log("登录" + response); // 解析服务器返回的数据 server serverData = JsonConvert.DeserializeObject(response); token = serverData.data.access_token; Debug.Log("===========" + serverData.data.isCreater); } //获取模板列表 public async void TemplateList() { string response = await web.SendRequest(web.URL + "/game/gameTemplate/list", "GET", "", CreateHeaders()); Debug.Log("获取模板列表" + response); //解析服务器返回的数据 TemplateListData templateListData = JsonConvert.DeserializeObject(response); Debug.Log("================" + templateListData.msg); } //新建模板 public async void createTemplate() { string body = "{\"templateName\": \"11111\",\"sceneId\": \"11111\",\"subjectId\": \"111\",\"isTemplate\": \"111\",\"mode\": \"111\",\"playerList\": [{\"userId\": \"111\",\"roleId\": \"1111\",\"birthAreaId\": \"111\",\"chargeAreaId\": \"111\"}],\"npcList\": [{\"npcId\": \"111\",\"areaId\": \"111\",\"npcNum\": 0}],\"materialList\": [{\"materialId\": \"111\",\"num\": 0}]}"; string response = await web.SendRequest(web.URL + "/game/gameTemplate/add", "POST", body, CreateHeaders()); Debug.Log("新建模板列表" + response); // 解析服务器返回的数据 newTemplateData newTemplateData = JsonConvert.DeserializeObject(response); Debug.Log(newTemplateData.data); } //查询场景信息列表 public async void QueryScene() { // 发送请求并获取返回数据 string response = await web.SendRequest(web.URL + "/game/scene/list", "GET", "", CreateHeaders()); Debug.Log("查询场景信息列表" + response); // 使用Newtonsoft.Json来反序列化JSON数据到QuerySceneData对象 QuerySceneData serverData = JsonConvert.DeserializeObject(response); // 打印整体响应数据的某些属性 if (serverData != null && serverData.data != null) { foreach (var scene in serverData.data) { Debug.Log("场景ID: " + scene.id); Debug.Log("场景名称: " + scene.name); Debug.Log("描述: " + scene.description); Debug.Log("价格: " + scene.price); Debug.Log("行业: " + scene.industryName); if (scene.fileList != null) { foreach (var file in scene.fileList) { Debug.Log("文件名称: " + file.name); Debug.Log("文件URL: " + file.url); } } } } else { Debug.Log("解析服务器数据失败"); } } //查询演练科目信息列表 public async void QueryDrillSubject() { string response = await web.SendRequest(web.URL + "/game/subject/list", "GET", "", CreateHeaders()); Debug.Log("查询演练科目信息列表: " + response); // 使用 Newtonsoft.Json 进行反序列化 DrillSubject drillSubject = JsonConvert.DeserializeObject(response); // 检查反序列化结果 if (drillSubject == null) { Debug.LogError("Failed to deserialize JSON. DrillSubject is null."); return; } if (drillSubject.data == null || drillSubject.data.Count == 0) { Debug.LogWarning("No subjects found in the returned data."); return; } // 遍历反序列化后的 data 列表 foreach (var subject in drillSubject.data) { Debug.Log("ID: " + subject.id); Debug.Log("Scene Names: " + subject.sceneNames); Debug.Log("Name: " + subject.name); Debug.Log("Suit Version: " + subject.suitVersion); Debug.Log("Type: " + subject.type); Debug.Log("Description: " + subject.description); Debug.Log("Price: " + subject.price); Debug.Log("Company ID: " + subject.companyId); Debug.Log("Status: " + subject.status); Debug.Log("Delete Flag: " + subject.delFlag); Debug.Log("Remark: " + subject.remark); Debug.Log("Game Name: " + subject.gameName); Debug.Log("OSS ID: " + subject.ossId); Debug.Log("File List: " + subject.fileList); // 如果需要进一步处理字段,可以在这里进行 // 比如,将价格转换为数字计算,或根据特定类型执行逻辑 } } //角色列表 public async void queryRoleList() { string body = "{\"sceneId\": \"1845704588547301377\"}"; string response = await web.SendRequest(web.URL + "/game/role/list", "GET", body, CreateHeaders()); Debug.Log("查询角色列表: " + response); RoleList serverData = JsonConvert.DeserializeObject(response); // 检查反序列化结果 if (serverData == null) { Debug.LogError("Failed to deserialize JSON. RoleList is null."); return; } if (serverData.data == null || serverData.data.Count == 0) { Debug.LogWarning("No role data found in the returned data."); return; } // 遍历反序列化后的角色列表 foreach (var role in serverData.data) { Debug.Log("Role ID: " + role.id); Debug.Log("Role Name: " + role.roleName); Debug.Log("Scene ID: " + role.sceneId); Debug.Log("Game Name: " + role.gameName); Debug.Log("Role Attributions: " + role.roleAttributions); } } //模板玩家绑定 public async void BindPlayer() { string templateId = "1111"; string userId = "1"; string roleId = "1"; string birthAreaId = "11"; string chargeAreaId = "2"; string body = $@" {{ ""templateId"": ""{templateId}"", ""playerList"": [ {{ ""userId"": ""{userId}"", ""roleId"": ""{roleId}"", ""birthAreaId"": ""{birthAreaId}"", ""chargeAreaId"": ""{chargeAreaId}"" }} ] }}"; string response = await web.SendRequest(web.URL + "/game/gameTemplate/player", "POST", body, CreateHeaders()); Debug.Log("模板玩家绑定: " + response); BindPlayer serverData = JsonConvert.DeserializeObject(response); } //模板NPC绑定 public async void BindNPC() { string templateId = "1111"; string npcId = "1"; string areaId = "1"; int npcNum = 1; //string chargeAreaId = "2"; string body = $@" {{ ""templateId"": ""{templateId}"", ""npcList"": [ {{ ""npcId"": ""{npcId}"", ""areaId"": ""{areaId}"", ""npcNum"": ""{npcNum}"" }} ] }}"; string response = await web.SendRequest(web.URL + "/game/gameTemplate/npc", "POST", body, CreateHeaders()); Debug.Log("模板NPC绑定: " + response); BindNPC serverData = JsonConvert.DeserializeObject(response); } //模板设备绑定 public async void BindMaterial() { string templateId = "1111"; string materialId = "1"; int num = 0; //string chargeAreaId = "2"; string body = $@" {{ ""templateId"": ""{templateId}"", ""materialList"": [ {{ ""materialId"": ""{materialId}"", ""num"": ""{num}"" }} ] }}"; string response = await web.SendRequest(web.URL + "/game/gameTemplate/material", "POST", body, CreateHeaders()); Debug.Log("模板设备绑定: " + response); BindMaterial serverData = JsonConvert.DeserializeObject(response); if (serverData.data == null) { Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); } Debug.Log(serverData.data); } //提交预定演练模板 public async void SubmitTemplate() { string templateId = "1111"; string reserveDate = "2024-11-22"; // int num = 0; //string chargeAreaId = "2"; string body = $@" {{ ""templateId"": ""{templateId}"", ""reserveDate"":""{reserveDate}"" }}"; string response = await web.SendRequest(web.URL + "/game/gameRoom/reserve", "POST", body, CreateHeaders()); Debug.Log("提交预定演练模板: " + response); BindMaterial serverData = JsonConvert.DeserializeObject(response); if (serverData.data == null) { Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); } Debug.Log(serverData.data); } }