AnswerLineInfoScript.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 String signedInName;
  20. private void Start() {
  21. nextPlayerButton.onClick.AddListener(ShowNextPlayerAnswerLine);
  22. prevPlayerButton.onClick.AddListener(ShowPrevPlayerAnswerLine);
  23. currentPlayerShown = GameManagerScript.GetCurrentPlayer();
  24. signedInName = Database.Instance.GetSignedInUser().Value;
  25. lockedQuestionsText.resizeTextMaxSize = unlockedQuestionsText.resizeTextMaxSize;
  26. }
  27. private void Update() {
  28. UpdateLockedQuestionsText();
  29. UpdateUnlockedQuestionsText();
  30. UpdateButtonsText();
  31. SetQuestionClickable();
  32. }
  33. private void SetQuestionClickable() {
  34. if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  35. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
  36. } else {
  37. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
  38. }
  39. }
  40. public void UpdateButtonsText() {
  41. if (players == null) {
  42. players = gameManager.GetComponent<GameManagerScript>().GetPlayers();
  43. }
  44. if (players.Count <= 1) {
  45. nextPlayerButton.gameObject.SetActive(false);
  46. prevPlayerButton.gameObject.SetActive(false);
  47. } else {
  48. nextPlayerButton.gameObject.SetActive(true);
  49. prevPlayerButton.gameObject.SetActive(true);
  50. if (currentPlayerShown == null) {
  51. currentPlayerShown = signedInName;
  52. }
  53. string playerBaseText = LocalizationManager.Instance.GetText("ANSWERLINE_OTHER_PLAYER");
  54. for (int i = 0; i < players.Count; i++) {
  55. if (players[i].Key.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  56. if (i + 1 < players.Count) {
  57. if (players[i+1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  58. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  59. } else {
  60. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i + 1].Key + "s";
  61. }
  62. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  63. if (i - 1 >= 0) {
  64. if (players[i-1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  65. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  66. } else {
  67. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i - 1].Key + "s";
  68. }
  69. } else {
  70. if (players[players.Count - 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  71. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  72. } else {
  73. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[players.Count-1].Key + "s";
  74. }
  75. }
  76. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  77. break;
  78. } else {
  79. if (players[0].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  80. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  81. } else {
  82. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[0].Key + "s";
  83. }
  84. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  85. if (players[i-1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  86. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  87. } else {
  88. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i-1].Key;
  89. }
  90. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. private void UpdateUnlockedQuestionsText() {
  98. int unlockedQuestionCount = scrollView.GetComponent<ScrollViewScript>().GetUnlockedQuestionCount();
  99. if (unlockedQuestionCount <= 0) {
  100. unlockedQuestionsText.gameObject.SetActive(false);
  101. } else {
  102. unlockedQuestionsText.gameObject.SetActive(true);
  103. unlockedQuestionsText.text = String.Format(
  104. LocalizationManager.Instance.GetText(
  105. unlockedQuestionsText.GetComponent<TextLocalization>().key),
  106. unlockedQuestionCount);
  107. }
  108. }
  109. private void UpdateLockedQuestionsText() {
  110. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  111. int lockedQuestionsCount = scrollViewScript.GetQuestionIdsInAnswerLine().Count - scrollViewScript.GetUnlockedQuestionCount();
  112. String playerText;
  113. if (currentPlayerShown == null || currentPlayerShown.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  114. playerText = LocalizationManager.Instance.GetText("YOU");
  115. } else {
  116. playerText = currentPlayerShown;
  117. }
  118. if (lockedQuestionsText != null){
  119. lockedQuestionsText.text = String.Format(
  120. LocalizationManager.Instance.GetText(
  121. lockedQuestionsText.GetComponent<TextLocalization>().key),
  122. playerText,
  123. lockedQuestionsCount);
  124. }
  125. }
  126. private void ShowNextPlayerAnswerLine()
  127. {
  128. GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  129. if (nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
  130. currentPlayerShown = signedInName;
  131. } else {
  132. currentPlayerShown = nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  133. currentPlayerShown = currentPlayerShown.Substring(0,currentPlayerShown.Length - 1);
  134. }
  135. List<NewQuestionData> playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode);
  136. setQuestionFrosting(playerQuestions);
  137. UpdateButtonsText();
  138. }
  139. private void ShowPrevPlayerAnswerLine()
  140. {
  141. GameManagerScript gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  142. if (prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
  143. currentPlayerShown = signedInName;
  144. } else {
  145. currentPlayerShown = prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  146. currentPlayerShown = currentPlayerShown.Substring(0, currentPlayerShown.Length - 1);
  147. }
  148. List<NewQuestionData> playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode);
  149. setQuestionFrosting(playerQuestions);
  150. UpdateButtonsText();
  151. }
  152. private void setQuestionFrosting(List<NewQuestionData> questions) {
  153. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  154. scrollViewScript.RemoveEverythingFromAnswerline();
  155. scrollViewScript.SetQuestionsInAnswerLine(questions);
  156. //if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown)) {
  157. if (Database.Instance.GetSignedInUser().Value.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  158. scrollViewScript.SetQuestionsFrosted(false);
  159. } else {
  160. scrollViewScript.SetQuestionsFrosted(true);
  161. }
  162. }
  163. }