StatsScript.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.SetLocalizationKey("PLAYERS");
  45. statLines.Add(round);
  46. statLines.Add(lostQuestions);
  47. statLines.Add(playerTitle);
  48. }
  49. private StatsLine CreateStatLine() {
  50. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  51. StatsLine statLine = slp.GetComponent<StatsLine>();
  52. statLine.transform.SetParent(this.transform, false);
  53. return statLine;
  54. }
  55. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  56. foreach (KeyValuePair<string, int> player in players) {
  57. StatsLine p = CreateStatLine();
  58. p.RemoveLocalization();
  59. p.SetStatName(player.Key);
  60. p.SetStatValue(player.Value);
  61. statLines.Add(p);
  62. }
  63. }
  64. public void IncreaseRoundValue() {
  65. foreach (StatsLine sl in statLines) {
  66. if (sl.GetName().Equals(roundText)) {
  67. Int32.TryParse(sl.GetValue(), out int round);
  68. round++;
  69. sl.SetStatValue(round);
  70. Database.Instance.SetRoundValue(gms.GameId, round, GetGameMode());
  71. break;
  72. }
  73. }
  74. }
  75. public int GetRound() {
  76. return statLines.Find(s => s.GetName().Equals(roundText)).GetIntValue();
  77. }
  78. public void MakeBold(string playerName) {
  79. foreach (StatsLine sl in statLines) {
  80. if (sl.GetName().Equals(playerName)) {
  81. sl.MakeBold();
  82. } else {
  83. sl.UnBold();
  84. }
  85. }
  86. }
  87. public void SetQuestionsInAnswerLine(string playerName, int count) {
  88. foreach (StatsLine sl in statLines) {
  89. if (sl.GetName().Equals(playerName)) {
  90. sl.SetStatValue(count) ;
  91. break;
  92. }
  93. }
  94. }
  95. public void SetQuestionsLost(int value) {
  96. StatsLine sl = statLines.Find(s => s.GetName().Equals(questionsLostText));
  97. sl.SetStatValue(value);
  98. }
  99. public int GetQuestionsLost() {
  100. return statLines.Find(s => s.GetName().Equals(questionsLostText)).GetIntValue();
  101. }
  102. }