StatsScript.cs 3.6 KB

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