WalkDogsScoreController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using Newtonsoft.Json;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using ZXing;
  6. /* * WalkDogsScoreController.cs
  7. * This script is responsible for managing the score in the Walk Dogs game.
  8. * It will handle score updates, display, and any related functionality.
  9. */
  10. public class WalkDogsScoreController : MonoBehaviour
  11. {
  12. // Start is called once before the first execution of Update after the MonoBehaviour is created
  13. public int score, maxCombo, perfect, good, poor, miss;
  14. private int coin;
  15. private Label scoreLabel, maxComboLabel, perfectLabel, goodLabel, poorLabel, missLabel, coinLabel;
  16. private Label scoreValueLabel, maxComboValueLabel, perfectValueLabel, goodValueLabel, poorValueLabel, missValueLabel, coinValueLabel;
  17. private Button confirmButton;
  18. void Start()
  19. {
  20. var root = GetComponent<UIDocument>().rootVisualElement;
  21. scoreLabel = root.Q<Label>("scoreLabel");
  22. maxComboLabel = root.Q<Label>("maxComboLabel");
  23. perfectLabel = root.Q<Label>("perfectLabel");
  24. goodLabel = root.Q<Label>("goodLabel");
  25. poorLabel = root.Q<Label>("poorLabel");
  26. missLabel = root.Q<Label>("missLabel");
  27. coinLabel = root.Q<Label>("coinLabel");
  28. confirmButton = root.Q<Button>("confirm");
  29. scoreValueLabel = root.Q<Label>("scoreValue");
  30. maxComboValueLabel = root.Q<Label>("maxComboValue");
  31. perfectValueLabel = root.Q<Label>("perfectValue");
  32. goodValueLabel = root.Q<Label>("goodValue");
  33. poorValueLabel = root.Q<Label>("poorValue");
  34. missValueLabel = root.Q<Label>("missValue");
  35. coinValueLabel = root.Q<Label>("coinValue");
  36. confirmButton = root.Q<Button>("confirm");
  37. // confirmButton.clicked += ScoreSubmitRequest;
  38. confirmButton.clicked += ConfirmButtonClick;
  39. LabelLanguageLoading();
  40. }
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. ScoreDisplay();
  45. }
  46. // 读取显示正确的语言
  47. void LabelLanguageLoading()
  48. {
  49. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "score", EnviromentSetting.languageCode });
  50. scoreLabel.text = textValue;
  51. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "maxCombo", EnviromentSetting.languageCode });
  52. maxComboLabel.text = textValue;
  53. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "perfect", EnviromentSetting.languageCode });
  54. perfectLabel.text = textValue;
  55. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "good", EnviromentSetting.languageCode });
  56. goodLabel.text = textValue;
  57. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "poor", EnviromentSetting.languageCode });
  58. poorLabel.text = textValue;
  59. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "miss", EnviromentSetting.languageCode });
  60. missLabel.text = textValue;
  61. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "coin", EnviromentSetting.languageCode });
  62. coinLabel.text = textValue;
  63. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "button", "confirm", EnviromentSetting.languageCode });
  64. confirmButton.text = textValue;
  65. }
  66. void ScoreDisplay()
  67. {
  68. coin = Mathf.FloorToInt(score / 1000f);
  69. // Display the score and other statistics in the UI
  70. scoreValueLabel.text = score.ToString();
  71. maxComboValueLabel.text = maxCombo.ToString();
  72. perfectValueLabel.text = perfect.ToString();
  73. goodValueLabel.text = good.ToString();
  74. poorValueLabel.text = poor.ToString();
  75. missValueLabel.text = miss.ToString();
  76. coinValueLabel.text = coin.ToString();
  77. // Optionally, you can also log the score to the console for debugging
  78. Debug.Log($"Score: {score}, Max Combo: {maxCombo}, Perfect: {perfect}, Good: {good}, Poor: {poor}, Miss: {miss}, Coin: {coin}");
  79. }
  80. // This method should be called when the score is ready to be displayed
  81. private void ConfirmButtonClick()
  82. {
  83. MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
  84. }
  85. private void ScoreSubmitRequest()
  86. {
  87. Debug.Log("GetUserData request");
  88. // 提交POST
  89. string url = "/api/walkdogs/score/";
  90. WWWForm form = new();
  91. form.AddField("user_id", UserProperty.userId);
  92. form.AddField("score", score);
  93. form.AddField("max_combo", maxCombo);
  94. form.AddField("perfect", perfect);
  95. form.AddField("good", good);
  96. form.AddField("poor", poor);
  97. form.AddField("miss", miss);
  98. form.AddField("coin", coin);
  99. StartCoroutine(WebController.PostRequest(url, form, callback: ScoreSubmitCallback));
  100. }
  101. private void ScoreSubmitCallback(string response)
  102. {
  103. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
  104. if (data != null && data["status"] == "success")
  105. {
  106. string user_info = data["user_info"].ToString();
  107. UserProperty.RefreshUserInfo(user_info);
  108. string dogs = data["dogs"].ToString();
  109. UserProperty.RefreshDogInfo(dogs);
  110. MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
  111. }
  112. }
  113. }