AnswerLineInfoScript.cs 10 KB

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