AnswerLineInfoScript.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class AnswerLineInfoScript : MonoBehaviour
  8. {
  9. [SerializeField] Button nextPlayerButton;
  10. [SerializeField] Button prevPlayerButton;
  11. [SerializeField] Text lockedQuestionsText;
  12. [SerializeField] Text unlockedQuestionsText;
  13. [SerializeField] GameObject scrollView;
  14. [SerializeField] GameObject answerLine;
  15. [SerializeField] GameObject newQuestion;
  16. [SerializeField] GameObject gameManager;
  17. private List<KeyValuePair<string, int>> players;
  18. private string currentPlayerShown;
  19. private void Start() {
  20. nextPlayerButton.onClick.AddListener(ShowNextPlayerAnswerLine);
  21. prevPlayerButton.onClick.AddListener(ShowPrevPlayerAnswerLine);
  22. currentPlayerShown = GameManagerScript.GetCurrentPlayer();
  23. }
  24. private void Update() {
  25. UpdateLockedQuestionsText();
  26. UpdateUnlockedQuestionsText();
  27. UpdateButtonsText();
  28. SetQuestionClickable();
  29. }
  30. private void SetQuestionClickable() {
  31. if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown)) {
  32. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
  33. } else {
  34. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
  35. }
  36. }
  37. public void UpdateButtonsText() {
  38. if (players == null) {
  39. players = gameManager.GetComponent<GameManagerScript>().GetPlayers();
  40. }
  41. if (players.Count <= 1) {
  42. nextPlayerButton.gameObject.SetActive(false);
  43. prevPlayerButton.gameObject.SetActive(false);
  44. } else {
  45. nextPlayerButton.gameObject.SetActive(true);
  46. prevPlayerButton.gameObject.SetActive(true);
  47. if (currentPlayerShown == null) {
  48. currentPlayerShown = GameManagerScript.GetCurrentPlayer();
  49. }
  50. string playerBaseText = LocalizationManager.Instance.GetText("ANSWERLINE_OTHER_PLAYER");
  51. for (int i = 0; i < players.Count; i++) {
  52. if (players[i].Key.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  53. if (i + 1 < players.Count) {
  54. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i+1].Key;
  55. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText ,players[i + 1].Key);
  56. if (i - 1 > 0) {
  57. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i-1].Key;
  58. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText ,players[i - 1].Key);
  59. } else {
  60. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[players.Count-1].Key;
  61. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText ,players[players.Count - 1].Key);
  62. }
  63. break;
  64. } else {
  65. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[0].Key;
  66. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText ,players[0].Key);
  67. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i-1].Key;
  68. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText ,players[i - 1].Key);
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. private void UpdateUnlockedQuestionsText() {
  76. int unlockedQuestionCount = scrollView.GetComponent<ScrollViewScript>().GetUnlockedQuestionCount();
  77. if (unlockedQuestionCount <= 0) {
  78. unlockedQuestionsText.gameObject.SetActive(false);
  79. } else {
  80. unlockedQuestionsText.gameObject.SetActive(true);
  81. unlockedQuestionsText.text = String.Format(
  82. LocalizationManager.Instance.GetText(
  83. unlockedQuestionsText.GetComponent<TextLocalization>().key),
  84. unlockedQuestionCount);
  85. }
  86. }
  87. private void UpdateLockedQuestionsText() {
  88. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  89. int lockedQuestionsCount = scrollViewScript.GetQuestionIdsInAnswerLine().Count - scrollViewScript.GetUnlockedQuestionCount();
  90. lockedQuestionsText.text = String.Format(
  91. LocalizationManager.Instance.GetText(
  92. lockedQuestionsText.GetComponent<TextLocalization>().key),
  93. lockedQuestionsCount);
  94. }
  95. private void ShowNextPlayerAnswerLine()
  96. {
  97. GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  98. List<NewQuestionData> playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName, gameManagerScript.GameMode);
  99. currentPlayerShown = nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  100. setQuestionFrosting(playerQuestions);
  101. UpdateButtonsText();
  102. }
  103. private void ShowPrevPlayerAnswerLine()
  104. {
  105. GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  106. List<NewQuestionData> playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName, gameManagerScript.GameMode);
  107. currentPlayerShown = prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  108. setQuestionFrosting(playerQuestions);
  109. UpdateButtonsText();
  110. }
  111. private void setQuestionFrosting(List<NewQuestionData> questions) {
  112. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  113. scrollViewScript.RemoveEverythingFromAnswerline();
  114. scrollViewScript.SetQuestionsInAnswerLine(questions);
  115. if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown)) {
  116. scrollViewScript.SetQuestionsFrosted(false);
  117. } else {
  118. scrollViewScript.SetQuestionsFrosted(true);
  119. }
  120. }
  121. }