StatsScript.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. GameManagerScript gms;
  14. private string gameMode;
  15. private void Start() {
  16. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  17. roundText = LocalizationManager.Instance.GetText("ROUND");
  18. questionsLostText = LocalizationManager.Instance.GetText("QUESTIONS_LOST");
  19. CreateStandardStats();
  20. AddPlayersToStats(Database.Instance.GetPlayersForGame(gms.GameId, GetGameMode()));
  21. }
  22. private string GetGameMode() {
  23. if (gameMode == null) {
  24. gameMode = PlayerPrefs.GetString("GameMode");
  25. }
  26. return gameMode;
  27. }
  28. private void CreateStandardStats() {
  29. StatsLine round = CreateStatLine();
  30. round.SetStatName(roundText);
  31. int roundValue = Database.Instance.GetRoundValue(gms.GameId, GetGameMode());
  32. round.SetLocalizationKey("ROUND");
  33. round.SetStatValue(roundValue);
  34. round.name = "roundStat";
  35. StatsLine lostQuestions = CreateStatLine();
  36. lostQuestions.SetStatName(questionsLostText);
  37. lostQuestions.SetStatValue(0);
  38. lostQuestions.name = "questionsLost";
  39. lostQuestions.SetLocalizationKey("QUESTIONS_LOST");
  40. StatsLine playerTitle = CreateStatLine();
  41. playerTitle.SetStatName(LocalizationManager.Instance.GetText("PLAYERS"));
  42. playerTitle.SetStatValue("");
  43. playerTitle.MakeBold();
  44. playerTitle.GetComponent<RectTransform>().sizeDelta = new Vector2(playerTitle.GetComponent<RectTransform>().rect.width,50f);
  45. playerTitle.SetLocalizationKey("PLAYERS");
  46. statLines.Add(round);
  47. statLines.Add(lostQuestions);
  48. statLines.Add(playerTitle);
  49. }
  50. private StatsLine CreateStatLine() {
  51. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  52. StatsLine statLine = slp.GetComponent<StatsLine>();
  53. statLine.transform.SetParent(this.transform, false);
  54. return statLine;
  55. }
  56. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  57. foreach (KeyValuePair<string, int> player in players) {
  58. StatsLine p = CreateStatLine();
  59. p.RemoveLocalization();
  60. p.SetStatName(player.Key);
  61. p.SetStatValue(player.Value);
  62. statLines.Add(p);
  63. }
  64. }
  65. public void IncreaseRoundValue() {
  66. foreach (StatsLine sl in statLines) {
  67. if (sl.GetName().Equals(roundText)) {
  68. Int32.TryParse(sl.GetValue(), out int round);
  69. round++;
  70. sl.SetStatValue(round);
  71. Database.Instance.SetRoundValue(gms.GameId, round, GetGameMode());
  72. break;
  73. }
  74. }
  75. }
  76. public int GetRound() {
  77. return statLines.Find(s => s.GetName().Equals(roundText)).GetIntValue();
  78. }
  79. public void MakeBold(string playerName) {
  80. foreach (StatsLine sl in statLines) {
  81. if (sl.GetName().Equals(playerName)) {
  82. sl.MakeBold();
  83. } else {
  84. sl.UnBold();
  85. }
  86. }
  87. }
  88. public void SetQuestionsInAnswerLine(string playerName, int count) {
  89. foreach (StatsLine sl in statLines) {
  90. if (sl.GetName().Equals(playerName)) {
  91. sl.SetStatValue(count) ;
  92. break;
  93. }
  94. }
  95. }
  96. public void SetQuestionsLost(int value) {
  97. StatsLine sl = statLines.Find(s => s.GetName().Equals(questionsLostText));
  98. sl.SetStatValue(sl.GetIntValue() + value);
  99. }
  100. public int GetQuestionsLost() {
  101. return statLines.Find(s => s.GetName().Equals(questionsLostText)).GetIntValue();
  102. }
  103. }