WalkDogsScoreController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 OnEnable()
  19. {
  20. var root = GetComponent<UIDocument>().rootVisualElement;
  21. root.style.backgroundColor = new Color(255, 255, 255, 0.25f); // Set background color to white with 50% opacity
  22. scoreLabel = root.Q<Label>("scoreLabel");
  23. maxComboLabel = root.Q<Label>("maxComboLabel");
  24. perfectLabel = root.Q<Label>("perfectLabel");
  25. goodLabel = root.Q<Label>("goodLabel");
  26. poorLabel = root.Q<Label>("poorLabel");
  27. missLabel = root.Q<Label>("missLabel");
  28. coinLabel = root.Q<Label>("coinLabel");
  29. confirmButton = root.Q<Button>("confirm");
  30. scoreValueLabel = root.Q<Label>("scoreValue");
  31. maxComboValueLabel = root.Q<Label>("maxComboValue");
  32. perfectValueLabel = root.Q<Label>("perfectValue");
  33. goodValueLabel = root.Q<Label>("goodValue");
  34. poorValueLabel = root.Q<Label>("poorValue");
  35. missValueLabel = root.Q<Label>("missValue");
  36. coinValueLabel = root.Q<Label>("coinValue");
  37. confirmButton = root.Q<Button>("confirm");
  38. // confirmButton.clicked += ScoreSubmitRequest;
  39. confirmButton.clicked += ConfirmButtonClick;
  40. LabelLanguageLoading();
  41. }
  42. // Update is called once per frame
  43. void Update()
  44. {
  45. // ScoreDisplay();
  46. }
  47. // 读取显示正确的语言
  48. void LabelLanguageLoading()
  49. {
  50. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "score", EnviromentSetting.languageCode });
  51. scoreLabel.text = textValue;
  52. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "maxCombo", EnviromentSetting.languageCode });
  53. maxComboLabel.text = textValue;
  54. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "perfect", EnviromentSetting.languageCode });
  55. perfectLabel.text = textValue;
  56. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "good", EnviromentSetting.languageCode });
  57. goodLabel.text = textValue;
  58. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "poor", EnviromentSetting.languageCode });
  59. poorLabel.text = textValue;
  60. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "miss", EnviromentSetting.languageCode });
  61. missLabel.text = textValue;
  62. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "label", "coin", EnviromentSetting.languageCode });
  63. coinLabel.text = textValue;
  64. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "WalkDogsScore", "button", "confirm", EnviromentSetting.languageCode });
  65. confirmButton.text = textValue;
  66. }
  67. public void ShowScore()
  68. {
  69. coin = Mathf.FloorToInt(score / 1000f);
  70. // Display the score and other statistics in the UI
  71. scoreValueLabel.text = score.ToString();
  72. maxComboValueLabel.text = maxCombo.ToString();
  73. perfectValueLabel.text = perfect.ToString();
  74. goodValueLabel.text = good.ToString();
  75. poorValueLabel.text = poor.ToString();
  76. missValueLabel.text = miss.ToString();
  77. coinValueLabel.text = coin.ToString();
  78. // Optionally, you can also log the score to the console for debugging
  79. Debug.Log($"Score: {score}, Max Combo: {maxCombo}, Perfect: {perfect}, Good: {good}, Poor: {poor}, Miss: {miss}, Coin: {coin}");
  80. }
  81. // This method should be called when the score is ready to be displayed
  82. private void ConfirmButtonClick()
  83. {
  84. MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
  85. }
  86. private void ScoreSubmitRequest()
  87. {
  88. Debug.Log("GetUserData request");
  89. // 提交POST
  90. string url = "/api/walkdogs/score/";
  91. WWWForm form = new();
  92. form.AddField("user_id", UserProperty.userId);
  93. form.AddField("score", score);
  94. form.AddField("max_combo", maxCombo);
  95. form.AddField("perfect", perfect);
  96. form.AddField("good", good);
  97. form.AddField("poor", poor);
  98. form.AddField("miss", miss);
  99. form.AddField("coin", coin);
  100. StartCoroutine(WebController.PostRequest(url, form, callback: ScoreSubmitCallback));
  101. }
  102. private void ScoreSubmitCallback(string response)
  103. {
  104. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
  105. if (data != null && data["status"] == "success")
  106. {
  107. string user_info = data["user_info"].ToString();
  108. UserProperty.RefreshUserInfo(user_info);
  109. string dogs = data["dogs"].ToString();
  110. UserProperty.RefreshDogInfo(dogs);
  111. MaskTransitions.TransitionManager.Instance.LoadLevel("Home");
  112. }
  113. }
  114. }