StatsScript.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Text[] statsTextNames;
  11. private Text[] statsTextValues;
  12. private List<StatsLine> statLines = new List<StatsLine>();
  13. GameManagerScript gms;
  14. private void Start() {
  15. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  16. CreateStandardStats();
  17. AddPlayersToStats(gms.GetPlayers());
  18. }
  19. private void CreateStandardStats() {
  20. StatsLine round = CreateStatLine();
  21. round.SetStatName("Round");
  22. int roundValue = gms.GetDatabase().GetRoundValue(gms.GameId);
  23. round.SetStatValue(roundValue.ToString());
  24. round.name = "roundStat";
  25. StatsLine lostQuestions = CreateStatLine();
  26. lostQuestions.SetStatName("Questions lost");
  27. lostQuestions.SetStatValue("0");
  28. lostQuestions.name = "questionsLost";
  29. StatsLine playerTitle = CreateStatLine();
  30. playerTitle.SetStatName("Players");
  31. playerTitle.SetStatValue("");
  32. playerTitle.MakeBold();
  33. statLines.Add(round);
  34. statLines.Add(lostQuestions);
  35. statLines.Add(playerTitle);
  36. }
  37. private StatsLine CreateStatLine() {
  38. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  39. StatsLine statLine = slp.GetComponent<StatsLine>();
  40. statLine.transform.SetParent(this.transform);
  41. return statLine;
  42. }
  43. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  44. foreach (KeyValuePair<string, int> player in players) {
  45. StatsLine p = CreateStatLine();
  46. p.SetStatName(player.Key);
  47. p.SetStatValue(player.Value.ToString());
  48. statLines.Add(p);
  49. }
  50. }
  51. public void IncreaseRoundValue() {
  52. foreach (StatsLine sl in statLines) {
  53. if (sl.GetName().Equals("Round")) {
  54. Int32.TryParse(sl.GetValue(), out int round);
  55. round++;
  56. sl.SetStatValue(round.ToString());
  57. break;
  58. }
  59. }
  60. }
  61. public void MakeBold(string playerName) {
  62. foreach (StatsLine sl in statLines) {
  63. if (sl.GetName().Equals(playerName)) {
  64. sl.MakeBold();
  65. } else {
  66. sl.UnBold();
  67. }
  68. }
  69. }
  70. public void SetQuestionsInAnswerLine(string playerName, int count) {
  71. foreach (StatsLine sl in statLines) {
  72. if (sl.GetName().Equals(playerName)) {
  73. sl.SetStatValue(count.ToString()) ;
  74. break;
  75. }
  76. }
  77. return;
  78. }
  79. }