StatusController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UIElements;
  8. /* 这个controller 是用于控制 Status UI菜单的
  9. * 包含显示狗的最新状态
  10. * 添加狗,把狗送给朋友,删除狗
  11. */
  12. public class StatusController : MonoBehaviour
  13. {
  14. private Button backButton, cancelButton;
  15. private Label nameLabel, genderLabel, breedLabel, hostLabel, hostnameLabel, ageLabel, ageValueLabel,commandLabel, commandValueLabel, statusLabel, statusValueLabel, resetVoice;
  16. private Label leftArrow, rightArrow;
  17. private Label addLabel, transfer, remove, callback;
  18. private Label transferMsgLabel;
  19. private string transferMsgBasic;
  20. private DateTime transferCountStartTime = new DateTime();
  21. DogProperty puppy;
  22. private VisualElement selectElement, statusElement, transferElement;
  23. private StausUIPage currentPage = StausUIPage.Status;
  24. // Start is called before the first frame update
  25. void OnEnable()
  26. {
  27. var root = GetComponent<UIDocument>().rootVisualElement;
  28. statusElement = root.Q("statusElement");
  29. transferElement = root.Q("transferElement");
  30. backButton = root.Q<Button>("back");
  31. cancelButton = root.Q<Button>("cancel");
  32. nameLabel = root.Q<Label>("name");
  33. genderLabel = root.Q<Label>("gender");
  34. breedLabel = root.Q<Label>("breed");
  35. hostLabel = root.Q<Label>("host");
  36. hostnameLabel = root.Q<Label>("hostname");
  37. ageLabel = root.Q<Label>("age");
  38. ageValueLabel = root.Q<Label>("ageValue");
  39. commandLabel = root.Q<Label>("command");
  40. commandValueLabel = root.Q<Label>("commandValue");
  41. resetVoice = root.Q<Label>("resetVoice");
  42. statusLabel = root.Q<Label>("status");
  43. statusValueLabel = root.Q<Label>("statusValue");
  44. selectElement = root.Q("selectElement");
  45. leftArrow = root.Q<Label>("leftArrow");
  46. rightArrow = root.Q<Label>("rightArrow");
  47. addLabel = root.Q<Label>("add");
  48. transfer = root.Q<Label>("transfer");
  49. remove = root.Q<Label>("remove");
  50. callback = root.Q<Label>("callback");
  51. transferMsgLabel = root.Q<Label>("transferMsg");
  52. // 绑定事件
  53. backButton.clicked += BackBtnClick;
  54. cancelButton.clicked += CancelBtnClick;
  55. leftArrow.RegisterCallback<ClickEvent>(e => LeftArrowClicked(e));
  56. rightArrow.RegisterCallback<ClickEvent>(e => RightArrowClicked(e));
  57. addLabel.RegisterCallback<ClickEvent>(e => AddClick(e));
  58. remove.RegisterCallback<ClickEvent>(e => RemoveClick(e));
  59. transfer.RegisterCallback<ClickEvent>(e => TransferClick(e));
  60. resetVoice.RegisterCallback<ClickEvent>(e => ResetVoiceClick(e));
  61. // 箭头是否显示
  62. if (UserProperty.dogs.Count > 1)
  63. {
  64. leftArrow.visible = true;
  65. rightArrow.visible = true;
  66. }
  67. else
  68. {
  69. leftArrow.visible = false;
  70. rightArrow.visible = false;
  71. }
  72. // 根据用户名下狗的数量判断是否显示transfer按键
  73. if (UserProperty.dogs.Count > 1)
  74. {
  75. transfer.style.display = DisplayStyle.Flex;
  76. }
  77. else
  78. {
  79. transfer.style.display = DisplayStyle.None;
  80. }
  81. // 如果用户狗的数量达到系统规定上限,则隐藏add按键
  82. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  83. {
  84. addLabel.style.display = DisplayStyle.None;
  85. }
  86. else
  87. {
  88. addLabel.style.display = DisplayStyle.Flex;
  89. }
  90. }
  91. //private void Start()
  92. //{
  93. //}
  94. // Update is called once per frame
  95. void Update()
  96. {
  97. // 刷新狗的数据
  98. // puppy = UserProperty.dogs[GameData.focusDog];
  99. // StatusPageUpdate();
  100. // LabelLanguageSetting();
  101. // StatusSummary();
  102. if (currentPage == StausUIPage.Transfer)
  103. {
  104. TimeSpan ts = DateTime.Now - transferCountStartTime;
  105. if (ts.TotalSeconds > 120)
  106. {
  107. // todo 超过120秒,关闭二维码
  108. transferElement.style.display = DisplayStyle.None;
  109. statusElement.style.display = DisplayStyle.Flex;
  110. currentPage = StausUIPage.Status;
  111. }
  112. }
  113. if (currentPage == StausUIPage.Status)
  114. {
  115. // 刷新狗的数据
  116. puppy = UserProperty.dogs[GameData.focusDog];
  117. StatusPageUpdate();
  118. LabelLanguageSetting();
  119. StatusSummary();
  120. CommandSummary();
  121. }
  122. }
  123. //void BackPressed()
  124. //{
  125. // if (backButton != null)
  126. // {
  127. // var root = GetComponent<UIDocument>();
  128. // Destroy(GameObject.Find(root.GetComponentInParent<Canvas>().name));
  129. // }
  130. //}
  131. void StatusPageUpdate()
  132. {
  133. //backButton.clicked += BackPressed;
  134. nameLabel.text = puppy.dog_name;
  135. if (puppy.sex == 1)
  136. {
  137. genderLabel.text = "♂";
  138. }
  139. else
  140. {
  141. genderLabel.text = "♀";
  142. }
  143. breedLabel.text = puppy.breed;
  144. foreach (var breed in EnviromentSetting.dogBreeds)
  145. {
  146. if (breed.breed == puppy.breed)
  147. {
  148. breedLabel.text = breed.name[EnviromentSetting.languageCode];
  149. break;
  150. }
  151. }
  152. hostnameLabel.text = UserProperty.name;
  153. // 计算狗生日和现在时间差
  154. TimeSpan ts = System.DateTime.Now - puppy.brithday;
  155. ageValueLabel.text = ts.Days.ToString();
  156. //根据狗的数量添加选择球
  157. //int puppyQty = UserProperty.dogs.Count;
  158. //VisualTreeAsset selectBall = Resources.Load<VisualTreeAsset>("Status/SelectBall");
  159. //// 这里将VisualTreeAsset转换成VisualElement类型
  160. //VisualElement uiDocument = selectBall.CloneTree();
  161. //VisualElement[] selectBalls = new VisualElement[puppyQty];
  162. //for (int i = 0; i<puppyQty; i++)
  163. //{
  164. // selectBalls[i] = uiDocument;
  165. // selectElement.Add(selectBalls[i]);
  166. //}
  167. // todo 设置selectBall颜色对应选择
  168. }
  169. void LabelLanguageSetting()
  170. {
  171. // 设置status UI界面里面label和按键的语言显示
  172. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  173. backButton.text = textValue;
  174. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
  175. cancelButton.text = textValue;
  176. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  177. hostLabel.text = textValue;
  178. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  179. ageLabel.text = textValue;
  180. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  181. statusLabel.text = textValue;
  182. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "command", EnviromentSetting.languageCode });
  183. commandLabel.text = textValue;
  184. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "resetVoice", EnviromentSetting.languageCode });
  185. resetVoice.text = textValue;
  186. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
  187. transferMsgBasic = textValue;
  188. }
  189. void CommandSummary()
  190. {
  191. // 先清空状态文字,应对切换多只狗
  192. commandValueLabel.text = null;
  193. // 汇总宠物状态
  194. string summary = String.Empty;
  195. if (puppy.commandSit)
  196. {
  197. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Sit", EnviromentSetting.languageCode });
  198. summary += ", ";
  199. }
  200. if (puppy.commandStand)
  201. {
  202. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Stand", EnviromentSetting.languageCode });
  203. summary += ", ";
  204. }
  205. if (puppy.commandBark)
  206. {
  207. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Bark", EnviromentSetting.languageCode });
  208. summary += ", ";
  209. }
  210. if (puppy.commandShake)
  211. {
  212. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Rotate", EnviromentSetting.languageCode });
  213. summary += ", ";
  214. }
  215. if (puppy.commandLieDown)
  216. {
  217. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "LieDown", EnviromentSetting.languageCode });
  218. summary += ", ";
  219. }
  220. if (puppy.commandTouch)
  221. {
  222. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Hug", EnviromentSetting.languageCode });
  223. summary += ", ";
  224. }
  225. if (puppy.commandDeath)
  226. {
  227. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Death", EnviromentSetting.languageCode });
  228. summary += ", ";
  229. }
  230. if (puppy.commandTurnL)
  231. {
  232. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnL", EnviromentSetting.languageCode });
  233. summary += ", ";
  234. }
  235. if (puppy.commandTurnR)
  236. {
  237. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnR", EnviromentSetting.languageCode });
  238. summary += ", ";
  239. }
  240. if (summary == String.Empty)
  241. {
  242. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "none", EnviromentSetting.languageCode });
  243. }
  244. else
  245. {
  246. // 去掉最后一个逗号
  247. summary = summary.Substring(0, summary.Length - 2);
  248. commandValueLabel.text = summary;
  249. }
  250. }
  251. void StatusSummary()
  252. {
  253. // 先清空状态文字,应对切换多只狗
  254. statusValueLabel.text = null;
  255. // 汇总宠物状态
  256. string summary = "";
  257. if (puppy.satiety < 30)
  258. {
  259. if (puppy.satiety < 10)
  260. {
  261. // 小于10,达到L2警告
  262. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  263. }
  264. else
  265. {
  266. // 不小于10,触发L1警告
  267. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  268. }
  269. summary += "<br>";
  270. }
  271. if (puppy.stamina < 30)
  272. {
  273. if (puppy.stamina < 10)
  274. {
  275. // 小于10,达到L2警告
  276. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  277. }
  278. else
  279. {
  280. // 不小于10,触发L1警告
  281. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  282. }
  283. summary += "<br>";
  284. }
  285. if (puppy.thirsty < 30)
  286. {
  287. if (puppy.thirsty < 10)
  288. {
  289. // 小于10,达到L2警告
  290. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  291. }
  292. else
  293. {
  294. // 不小于10,触发L1警告
  295. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  296. }
  297. summary += "<br>";
  298. }
  299. if (puppy.healthy < 30)
  300. {
  301. if (puppy.healthy < 10)
  302. {
  303. // 小于10,达到L2警告
  304. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  305. }
  306. else
  307. {
  308. // 不小于10,触发L1警告
  309. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  310. }
  311. summary += "<br>";
  312. }
  313. if (puppy.clean < 30)
  314. {
  315. if (puppy.clean < 10)
  316. {
  317. // 小于10,达到L2警告
  318. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  319. }
  320. else
  321. {
  322. // 不小于10,触发L1警告
  323. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  324. }
  325. summary += "<br>";
  326. }
  327. if (puppy.obesity > 70)
  328. {
  329. if (puppy.obesity > 90)
  330. {
  331. // 小于10,达到L2警告
  332. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  333. }
  334. else
  335. {
  336. // 不小于10,触发L1警告
  337. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  338. }
  339. summary += "<br>";
  340. }
  341. // 如果没有任何异常,返回正常的状态
  342. if (summary.Length == 0)
  343. {
  344. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  345. }
  346. statusValueLabel.text = summary;
  347. }
  348. void RightArrowClicked(ClickEvent e)
  349. {
  350. GameData.focusDog++;
  351. if (GameData.focusDog == UserProperty.dogs.Count)
  352. {
  353. GameData.focusDog = 0;
  354. }
  355. }
  356. void LeftArrowClicked(ClickEvent e)
  357. {
  358. GameData.focusDog--;
  359. if (GameData.focusDog == -1)
  360. {
  361. GameData.focusDog = UserProperty.dogs.Count - 1;
  362. }
  363. }
  364. void ResetVoiceClick(ClickEvent e)
  365. {
  366. // todo 重置狗的语音训练数据提示
  367. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "reset_voice_reminder", EnviromentSetting.languageCode });
  368. msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  369. MessageBoxController.YorN_Message(msg, ResetVoiceRequest);
  370. }
  371. void ResetVoiceRequest()
  372. {
  373. // todo 重置狗的声音提交
  374. string url = "/api/voice/reset/";
  375. Dictionary<string, string> formData = new();
  376. WWWForm form = new();
  377. form.AddField("user_id", UserProperty.userId);
  378. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  379. form.AddField("type", "all");
  380. StartCoroutine(WebController.PostRequest(url, form, callback: ResetVoiceCallback));
  381. }
  382. void ResetVoiceCallback(string json)
  383. {
  384. // todo 重置狗的声音提交成功
  385. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  386. if (data != null && data["status"].ToString() == "success"){
  387. // 跳转login场景,重新登录
  388. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  389. }else{
  390. Debug.Log(data["message"]);
  391. }
  392. }
  393. // 点击返回按钮
  394. void BackBtnClick()
  395. {
  396. var uiPlaceholder = GameObject.Find("UI Placeholder");
  397. if (uiPlaceholder != null)
  398. {
  399. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  400. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  401. shoppingUI.SetActive(false);
  402. vamUI.SetActive(true);
  403. }
  404. }
  405. void CancelBtnClick()
  406. {
  407. statusElement.style.display = DisplayStyle.Flex;
  408. transferElement.style.display = DisplayStyle.None;
  409. currentPage = StausUIPage.Status;
  410. }
  411. void AddClick(ClickEvent e)
  412. {
  413. // if (UserProperty.level == "basic")
  414. // {
  415. // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
  416. // MessageBoxController.ShowMessage(msg);
  417. // }
  418. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  419. {
  420. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
  421. MessageBoxController.ShowMessage(msg);
  422. }
  423. else
  424. {
  425. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
  426. MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
  427. }
  428. }
  429. // 跳转login 场景添加新的狗
  430. void SwitchAddNewDog()
  431. {
  432. GameData.subScene = "Login_InitDog";
  433. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  434. }
  435. void RemoveClick(ClickEvent e)
  436. {
  437. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
  438. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  439. MessageBoxController.YorN_Message(msg, FosterDog);
  440. }
  441. // 点击确认remove后调用寄养的程序
  442. void FosterDog()
  443. {
  444. // 检测用户是否有200金币
  445. if (UserProperty.coin >= 200)
  446. {
  447. // todo 寄养某一只狗,并扣除金币
  448. }
  449. }
  450. // 找回寄养的狗
  451. void CallbackDogClick(ClickEvent e)
  452. {
  453. }
  454. void TransferClick(ClickEvent e)
  455. {
  456. // TODO 添加代码增加面对面赠送狗的功能
  457. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
  458. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  459. MessageBoxController.YorN_Message(msg, TransferDogRequest);
  460. }
  461. void TransferDogRequest()
  462. {
  463. string url = "/api/transfer_dog/receive/";
  464. Dictionary<string, string> formData = new();
  465. WWWForm form = new();
  466. form.AddField("user_id", UserProperty.userId);
  467. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  468. StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
  469. }
  470. void TransferDogCallback(string json)
  471. {
  472. // TODO 生成二维码,和120秒倒计时
  473. transferCountStartTime = DateTime.Now;
  474. currentPage = StausUIPage.Transfer;
  475. }
  476. }
  477. enum StausUIPage
  478. {
  479. Status,
  480. Transfer,
  481. }