StatusController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. void LabelLanguageSetting()
  158. {
  159. // 设置status UI界面里面label和按键的语言显示
  160. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "back", EnviromentSetting.languageCode });
  161. backButton.text = textValue;
  162. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "button", "cancel", EnviromentSetting.languageCode });
  163. cancelButton.text = textValue;
  164. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "host", EnviromentSetting.languageCode });
  165. hostLabel.text = textValue;
  166. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "days", EnviromentSetting.languageCode });
  167. ageLabel.text = textValue;
  168. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "status", EnviromentSetting.languageCode });
  169. statusLabel.text = textValue;
  170. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "command", EnviromentSetting.languageCode });
  171. commandLabel.text = textValue;
  172. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "label", "resetVoice", EnviromentSetting.languageCode });
  173. resetVoice.text = textValue;
  174. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "message", "scan_QRcode", EnviromentSetting.languageCode });
  175. transferMsgBasic = textValue;
  176. }
  177. void CommandSummary()
  178. {
  179. // 先清空状态文字,应对切换多只狗
  180. commandValueLabel.text = null;
  181. // 汇总宠物状态
  182. string summary = String.Empty;
  183. if (puppy.commandSit)
  184. {
  185. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Sit", EnviromentSetting.languageCode });
  186. summary += ", ";
  187. }
  188. if (puppy.commandStand)
  189. {
  190. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Stand", EnviromentSetting.languageCode });
  191. summary += ", ";
  192. }
  193. if (puppy.commandBark)
  194. {
  195. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Bark", EnviromentSetting.languageCode });
  196. summary += ", ";
  197. }
  198. if (puppy.commandShake)
  199. {
  200. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Shake", EnviromentSetting.languageCode });
  201. summary += ", ";
  202. }
  203. if (puppy.commandLieDown)
  204. {
  205. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "LieDown", EnviromentSetting.languageCode });
  206. summary += ", ";
  207. }
  208. if (puppy.commandTouch)
  209. {
  210. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Touch", EnviromentSetting.languageCode });
  211. summary += ", ";
  212. }
  213. if (puppy.commandDeath)
  214. {
  215. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "Death", EnviromentSetting.languageCode });
  216. summary += ", ";
  217. }
  218. if (puppy.commandTurnL)
  219. {
  220. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnL", EnviromentSetting.languageCode });
  221. summary += ", ";
  222. }
  223. if (puppy.commandTurnR)
  224. {
  225. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "TurnR", EnviromentSetting.languageCode });
  226. summary += ", ";
  227. }
  228. if (summary == String.Empty)
  229. {
  230. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "command", "none", EnviromentSetting.languageCode });
  231. }
  232. else
  233. {
  234. // 去掉最后一个逗号
  235. summary = summary.Substring(0, summary.Length - 2);
  236. commandValueLabel.text = summary;
  237. }
  238. }
  239. void StatusSummary()
  240. {
  241. // 先清空状态文字,应对切换多只狗
  242. statusValueLabel.text = null;
  243. // 汇总宠物状态
  244. string summary = "";
  245. if (puppy.satiety < 30)
  246. {
  247. if (puppy.satiety < 10)
  248. {
  249. // 小于10,达到L2警告
  250. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_2", EnviromentSetting.languageCode });
  251. }
  252. else
  253. {
  254. // 不小于10,触发L1警告
  255. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "satiety_1", EnviromentSetting.languageCode });
  256. }
  257. summary += "<br>";
  258. }
  259. if (puppy.stamina < 30)
  260. {
  261. if (puppy.stamina < 10)
  262. {
  263. // 小于10,达到L2警告
  264. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_2", EnviromentSetting.languageCode });
  265. }
  266. else
  267. {
  268. // 不小于10,触发L1警告
  269. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "stamina_1", EnviromentSetting.languageCode });
  270. }
  271. summary += "<br>";
  272. }
  273. if (puppy.thirsty < 30)
  274. {
  275. if (puppy.thirsty < 10)
  276. {
  277. // 小于10,达到L2警告
  278. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_2", EnviromentSetting.languageCode });
  279. }
  280. else
  281. {
  282. // 不小于10,触发L1警告
  283. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "thirsty_1", EnviromentSetting.languageCode });
  284. }
  285. summary += "<br>";
  286. }
  287. if (puppy.healthy < 30)
  288. {
  289. if (puppy.healthy < 10)
  290. {
  291. // 小于10,达到L2警告
  292. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_2", EnviromentSetting.languageCode });
  293. }
  294. else
  295. {
  296. // 不小于10,触发L1警告
  297. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "healthy_1", EnviromentSetting.languageCode });
  298. }
  299. summary += "<br>";
  300. }
  301. if (puppy.clean < 30)
  302. {
  303. if (puppy.clean < 10)
  304. {
  305. // 小于10,达到L2警告
  306. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_2", EnviromentSetting.languageCode });
  307. }
  308. else
  309. {
  310. // 不小于10,触发L1警告
  311. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "clean_1", EnviromentSetting.languageCode });
  312. }
  313. summary += "<br>";
  314. }
  315. if (puppy.obesity > 70)
  316. {
  317. if (puppy.obesity > 90)
  318. {
  319. // 小于10,达到L2警告
  320. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_2", EnviromentSetting.languageCode });
  321. }
  322. else
  323. {
  324. // 不小于10,触发L1警告
  325. summary += GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "obesity_1", EnviromentSetting.languageCode });
  326. }
  327. summary += "<br>";
  328. }
  329. // 如果没有任何异常,返回正常的状态
  330. if (summary.Length == 0)
  331. {
  332. summary = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "statusUI", "status", "normal", EnviromentSetting.languageCode });
  333. }
  334. statusValueLabel.text = summary;
  335. }
  336. void RightArrowClicked(ClickEvent e)
  337. {
  338. GameData.focusDog++;
  339. if (GameData.focusDog == UserProperty.dogs.Count)
  340. {
  341. GameData.focusDog = 0;
  342. }
  343. }
  344. void LeftArrowClicked(ClickEvent e)
  345. {
  346. GameData.focusDog--;
  347. if (GameData.focusDog == -1)
  348. {
  349. GameData.focusDog = UserProperty.dogs.Count - 1;
  350. }
  351. }
  352. void ResetVoiceClick(ClickEvent e)
  353. {
  354. // todo 重置狗的语音训练数据提示
  355. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "reset_voice_reminder", EnviromentSetting.languageCode });
  356. msg = msg.Replace("<<dog_name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  357. MessageBoxController.YorN_Message(msg, ResetVoiceRequest);
  358. }
  359. void ResetVoiceRequest()
  360. {
  361. // todo 重置狗的声音提交
  362. string url = "/api/voice/reset/";
  363. Dictionary<string, string> formData = new();
  364. WWWForm form = new();
  365. form.AddField("user_id", UserProperty.userId);
  366. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  367. form.AddField("type", "all");
  368. StartCoroutine(WebController.PostRequest(url, form, callback: ResetVoiceCallback));
  369. }
  370. void ResetVoiceCallback(string json)
  371. {
  372. // todo 重置狗的声音提交成功
  373. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  374. if (data != null && data["status"].ToString() == "success"){
  375. // 跳转login场景,重新登录
  376. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  377. }else{
  378. Debug.Log(data["message"]);
  379. }
  380. }
  381. // 点击返回按钮
  382. void BackBtnClick()
  383. {
  384. var uiPlaceholder = GameObject.Find("UI Placeholder");
  385. if (uiPlaceholder != null)
  386. {
  387. var shoppingUI = uiPlaceholder.transform.Find("Status").gameObject;
  388. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  389. shoppingUI.SetActive(false);
  390. vamUI.SetActive(true);
  391. }
  392. }
  393. void CancelBtnClick()
  394. {
  395. statusElement.style.display = DisplayStyle.Flex;
  396. transferElement.style.display = DisplayStyle.None;
  397. currentPage = StausUIPage.Status;
  398. }
  399. void AddClick(ClickEvent e)
  400. {
  401. // if (UserProperty.level == "basic")
  402. // {
  403. // string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_account_level", EnviromentSetting.languageCode });
  404. // MessageBoxController.ShowMessage(msg);
  405. // }
  406. if (UserProperty.dogs.Count >= EnviromentSetting.maxDogQty)
  407. {
  408. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_fail_no_more", EnviromentSetting.languageCode });
  409. MessageBoxController.ShowMessage(msg);
  410. }
  411. else
  412. {
  413. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "add_dog_prompt", EnviromentSetting.languageCode });
  414. MessageBoxController.YorN_Message(msg, SwitchAddNewDog);
  415. }
  416. }
  417. // 跳转login 场景添加新的狗
  418. void SwitchAddNewDog()
  419. {
  420. GameData.subScene = "Login_InitDog";
  421. MaskTransitions.TransitionManager.Instance.LoadLevel("Login");
  422. }
  423. void RemoveClick(ClickEvent e)
  424. {
  425. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "foster_dog_prompt", EnviromentSetting.languageCode });
  426. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  427. MessageBoxController.YorN_Message(msg, FosterDog);
  428. }
  429. // 点击确认remove后调用寄养的程序
  430. void FosterDog()
  431. {
  432. // 检测用户是否有200金币
  433. if (UserProperty.coin >= 200)
  434. {
  435. // todo 寄养某一只狗,并扣除金币
  436. }
  437. }
  438. // 找回寄养的狗
  439. void CallbackDogClick(ClickEvent e)
  440. {
  441. }
  442. void TransferClick(ClickEvent e)
  443. {
  444. // TODO 添加代码增加面对面赠送狗的功能
  445. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "transfer_dog_prompt", EnviromentSetting.languageCode });
  446. msg = msg.Replace("<<dog name>>", UserProperty.dogs[GameData.focusDog].dog_name);
  447. MessageBoxController.YorN_Message(msg, TransferDogRequest);
  448. }
  449. void TransferDogRequest()
  450. {
  451. string url = "/api/transfer_dog/receive/";
  452. Dictionary<string, string> formData = new();
  453. WWWForm form = new();
  454. form.AddField("user_id", UserProperty.userId);
  455. form.AddField("dog_id", UserProperty.GetDogIdByIndex(GameData.focusDog));
  456. StartCoroutine(WebController.PostRequest(url, form, callback: TransferDogCallback));
  457. }
  458. void TransferDogCallback(string json)
  459. {
  460. // TODO 生成二维码,和120秒倒计时
  461. transferCountStartTime = DateTime.Now;
  462. currentPage = StausUIPage.Transfer;
  463. }
  464. }
  465. enum StausUIPage
  466. {
  467. Status,
  468. Transfer,
  469. }