StatsScript.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class StatsScript : MonoBehaviour {
  7. public GameObject statsNames;
  8. public GameObject statsValues;
  9. public GameObject statsLinePrefab;
  10. private readonly List<StatsLine> statLines = new List<StatsLine>();
  11. private string roundText;
  12. private string questionsLostText;
  13. private readonly string gameMode;
  14. private string currenPlayer;
  15. [SerializeField] GameObject gameManager;
  16. [SerializeField] GameObject answerLine;
  17. [SerializeField] GameObject answerLineInfo;
  18. GameManagerScript gameManagerScript;
  19. AnswerLineInfoScript answerLineInfoScript;
  20. ScrollViewScript svc;
  21. private void Start() {
  22. gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  23. answerLineInfoScript = answerLineInfo.GetComponent<AnswerLineInfoScript>();
  24. svc = answerLine.transform.parent.parent.GetComponent<ScrollViewScript>();
  25. roundText = LocalizationManager.Instance.GetText("ROUND");
  26. questionsLostText = LocalizationManager.Instance.GetText("QUESTIONS_LOST");
  27. currenPlayer = GameManagerScript.GetCurrentPlayer();
  28. CreateStandardStats();
  29. AddPlayersToStats(Database.Instance.GetPlayersForGame(gameManagerScript.GameId, gameManagerScript.GameMode));
  30. }
  31. void Update() {
  32. UpdatePlayerAnswer();
  33. }
  34. private void UpdatePlayerAnswer() {
  35. String currentPlayerShown = answerLineInfoScript.GetCurrentPlayerShown();
  36. int answerCount = svc.GetQuestionIdsInAnswerLine().Count;
  37. SetQuestionsInAnswerLine(currentPlayerShown, answerCount);
  38. }
  39. private void CreateStandardStats() {
  40. StatsLine round = CreateStatLine();
  41. round.SetStatName(roundText);
  42. int roundValue = Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode);
  43. round.SetLocalizationKey("ROUND");
  44. round.SetStatValue(roundValue);
  45. round.name = "roundStat";
  46. StatsLine lostQuestions = CreateStatLine();
  47. lostQuestions.SetStatName(questionsLostText);
  48. lostQuestions.SetStatValue(0);
  49. lostQuestions.name = "questionsLost";
  50. lostQuestions.SetLocalizationKey("QUESTIONS_LOST");
  51. StatsLine playerTitle = CreateStatLine();
  52. playerTitle.SetStatName(LocalizationManager.Instance.GetText("PLAYERS"));
  53. playerTitle.SetStatValue("");
  54. playerTitle.MakeBold();
  55. playerTitle.GetComponent<RectTransform>().sizeDelta = new Vector2(playerTitle.GetComponent<RectTransform>().rect.width, 50f);
  56. playerTitle.SetLocalizationKey("PLAYERS");
  57. statLines.Add(round);
  58. statLines.Add(lostQuestions);
  59. statLines.Add(playerTitle);
  60. }
  61. private StatsLine CreateStatLine() {
  62. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  63. StatsLine statLine = slp.GetComponent<StatsLine>();
  64. statLine.transform.SetParent(this.transform, false);
  65. return statLine;
  66. }
  67. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  68. foreach (KeyValuePair<string, int> player in players) {
  69. StatsLine p = CreateStatLine();
  70. p.RemoveLocalization();
  71. p.SetStatName(player.Key);
  72. p.SetStatValue(player.Value);
  73. statLines.Add(p);
  74. }
  75. }
  76. public void IncreaseRoundValue() {
  77. foreach (StatsLine sl in statLines) {
  78. if (sl.GetName().Equals(roundText)) {
  79. Int32.TryParse(sl.GetValue(), out int round);
  80. round++;
  81. sl.SetStatValue(round);
  82. Database.Instance.SetRoundValue(gameManagerScript.GameId, round, gameManagerScript.GameMode);
  83. break;
  84. }
  85. }
  86. }
  87. public void MakeBold(string playerName) {
  88. foreach (StatsLine sl in statLines) {
  89. if (sl.GetName().Equals(playerName)) {
  90. sl.MakeBold();
  91. } else {
  92. sl.UnBold();
  93. }
  94. }
  95. }
  96. public void SetQuestionsInAnswerLine(string playerName, int count) {
  97. foreach (StatsLine sl in statLines) {
  98. if (sl.GetName().Equals(playerName)) {
  99. sl.SetStatValue(count);
  100. break;
  101. }
  102. }
  103. }
  104. public void SetQuestionsLost(int value) {
  105. StatsLine sl = statLines.Find(s => s.GetName().Equals(questionsLostText));
  106. sl.SetStatValue(sl.GetIntValue() + value);
  107. }
  108. }