StatsScript.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 List<StatsLine> statLines = new List<StatsLine>();
  11. private string roundText;
  12. private string questionsLostText;
  13. private 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. {
  33. UpdatePlayerAnswer();
  34. }
  35. private void UpdatePlayerAnswer() {
  36. String currentPlayerShown = answerLineInfoScript.getCurrentPlayerShown();
  37. int answerCount = svc.GetQuestionIdsInAnswerLine().Count;
  38. SetQuestionsInAnswerLine(currentPlayerShown, answerCount);
  39. }
  40. private void CreateStandardStats() {
  41. StatsLine round = CreateStatLine();
  42. round.SetStatName(roundText);
  43. int roundValue = Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode);
  44. round.SetLocalizationKey("ROUND");
  45. round.SetStatValue(roundValue);
  46. round.name = "roundStat";
  47. StatsLine lostQuestions = CreateStatLine();
  48. lostQuestions.SetStatName(questionsLostText);
  49. lostQuestions.SetStatValue(0);
  50. lostQuestions.name = "questionsLost";
  51. lostQuestions.SetLocalizationKey("QUESTIONS_LOST");
  52. StatsLine playerTitle = CreateStatLine();
  53. playerTitle.SetStatName(LocalizationManager.Instance.GetText("PLAYERS"));
  54. playerTitle.SetStatValue("");
  55. playerTitle.MakeBold();
  56. playerTitle.GetComponent<RectTransform>().sizeDelta = new Vector2(playerTitle.GetComponent<RectTransform>().rect.width,50f);
  57. playerTitle.SetLocalizationKey("PLAYERS");
  58. statLines.Add(round);
  59. statLines.Add(lostQuestions);
  60. statLines.Add(playerTitle);
  61. }
  62. private StatsLine CreateStatLine() {
  63. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  64. StatsLine statLine = slp.GetComponent<StatsLine>();
  65. statLine.transform.SetParent(this.transform, false);
  66. return statLine;
  67. }
  68. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  69. foreach (KeyValuePair<string, int> player in players) {
  70. StatsLine p = CreateStatLine();
  71. p.RemoveLocalization();
  72. p.SetStatName(player.Key);
  73. p.SetStatValue(player.Value);
  74. statLines.Add(p);
  75. }
  76. }
  77. public void IncreaseRoundValue() {
  78. foreach (StatsLine sl in statLines) {
  79. if (sl.GetName().Equals(roundText)) {
  80. Int32.TryParse(sl.GetValue(), out int round);
  81. round++;
  82. sl.SetStatValue(round);
  83. Database.Instance.SetRoundValue(gameManagerScript.GameId, round, gameManagerScript.GameMode);
  84. break;
  85. }
  86. }
  87. }
  88. public void MakeBold(string playerName) {
  89. foreach (StatsLine sl in statLines) {
  90. if (sl.GetName().Equals(playerName)) {
  91. sl.MakeBold();
  92. } else {
  93. sl.UnBold();
  94. }
  95. }
  96. }
  97. public void SetQuestionsInAnswerLine(string playerName, int count) {
  98. foreach (StatsLine sl in statLines) {
  99. if (sl.GetName().Equals(playerName)) {
  100. sl.SetStatValue(count) ;
  101. break;
  102. }
  103. }
  104. }
  105. public void SetQuestionsLost(int value) {
  106. StatsLine sl = statLines.Find(s => s.GetName().Equals(questionsLostText));
  107. sl.SetStatValue(sl.GetIntValue() + value);
  108. }
  109. }