AnswerLineInfoScript.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class AnswerLineInfoScript : MonoBehaviour {
  8. [SerializeField] Button nextPlayerButton;
  9. [SerializeField] Button prevPlayerButton;
  10. [SerializeField] Text lockedQuestionsText;
  11. [SerializeField] Text unlockedQuestionsText;
  12. [SerializeField] GameObject scrollView;
  13. [SerializeField] GameObject answerLine;
  14. [SerializeField] GameObject newQuestion;
  15. [SerializeField] GameObject gameManager;
  16. private List<KeyValuePair<string, int>> players;
  17. private string currentPlayerShown;
  18. private String signedInName;
  19. private GameManagerScript gameManagerScript;
  20. private List<AnswerLineQuestionCard> signedInPlayerQuestions = new List<AnswerLineQuestionCard>();
  21. private void Start() {
  22. nextPlayerButton.onClick.AddListener(ShowNextPlayerAnswerLine);
  23. prevPlayerButton.onClick.AddListener(ShowPrevPlayerAnswerLine);
  24. signedInName = Database.Instance.GetSignedInUser().Value;
  25. lockedQuestionsText.resizeTextMaxSize = unlockedQuestionsText.resizeTextMaxSize;
  26. gameManagerScript = gameManager.GetComponent<GameManagerScript>();
  27. }
  28. private void Update() {
  29. UpdateLockedQuestionsText();
  30. UpdateUnlockedQuestionsText();
  31. UpdateButtonsText();
  32. SetQuestionClickable();
  33. if (signedInName.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase) &&
  34. signedInPlayerQuestions != answerLine.GetComponentsInChildren<AnswerLineQuestionCard>().ToList()) {
  35. signedInPlayerQuestions = answerLine.GetComponentsInChildren<AnswerLineQuestionCard>().ToList();
  36. }
  37. }
  38. private void SetQuestionClickable() {
  39. List<KeyValuePair<string, int>> players = gameManagerScript.GetPlayers();
  40. if (gameManagerScript.GameStatus == null || !gameManagerScript.GameStatus.Equals("FINISHED")) {
  41. if (gameManagerScript.GameMode.Equals(Constants.ONLINE)) {
  42. if (players.Count > 1 && currentPlayerShown != null) {
  43. if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  44. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
  45. } else {
  46. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
  47. }
  48. }
  49. } else {
  50. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = true;
  51. }
  52. } else {
  53. newQuestion.GetComponent<NewQuestionCardController>().BackClickable = false;
  54. }
  55. }
  56. public void UpdateButtonsText() {
  57. if (players == null) {
  58. players = gameManagerScript.GetPlayers();
  59. }
  60. if (players.Count <= 1) {
  61. nextPlayerButton.gameObject.SetActive(false);
  62. prevPlayerButton.gameObject.SetActive(false);
  63. } else {
  64. nextPlayerButton.gameObject.SetActive(true);
  65. prevPlayerButton.gameObject.SetActive(true);
  66. if (gameManagerScript.GameMode.Equals(Constants.ONLINE) && currentPlayerShown == null) { // fungerar enbart vid online
  67. currentPlayerShown = signedInName;
  68. } else if (gameManagerScript.GameMode.Equals(Constants.LOCAL) && currentPlayerShown == null) {
  69. currentPlayerShown = Database.Instance.GetCurrentPlayer(gameManagerScript.GameId, gameManagerScript.GameMode);
  70. }
  71. string playerBaseText = LocalizationManager.Instance.GetText("ANSWERLINE_OTHER_PLAYER");
  72. for (int i = 0; i < players.Count; i++) {
  73. if (players[i].Key.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  74. if (i + 1 < players.Count) {
  75. if (players[i + 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  76. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  77. } else {
  78. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i + 1].Key + "s";
  79. }
  80. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  81. if (i - 1 >= 0) {
  82. if (players[i - 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  83. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  84. } else {
  85. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i - 1].Key + "s";
  86. }
  87. } else {
  88. if (players[players.Count - 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  89. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  90. } else {
  91. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[players.Count - 1].Key + "s";
  92. }
  93. }
  94. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  95. break;
  96. } else {
  97. if (players[0].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  98. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  99. } else {
  100. nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[0].Key + "s";
  101. }
  102. nextPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  103. if (players[i - 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  104. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = LocalizationManager.Instance.GetText("YOUR");
  105. } else {
  106. prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName = players[i - 1].Key + "s";
  107. }
  108. prevPlayerButton.GetComponentInChildren<Text>().text = String.Format(playerBaseText, prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName);
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. private void UpdateUnlockedQuestionsText() {
  116. int unlockedQuestionCount = scrollView.GetComponent<ScrollViewScript>().GetUnlockedQuestionCount();
  117. if (unlockedQuestionCount <= 0) {
  118. unlockedQuestionsText.gameObject.SetActive(false);
  119. } else {
  120. unlockedQuestionsText.gameObject.SetActive(true);
  121. unlockedQuestionsText.text = String.Format(
  122. LocalizationManager.Instance.GetText(
  123. unlockedQuestionsText.GetComponent<TextLocalization>().key),
  124. unlockedQuestionCount);
  125. }
  126. }
  127. private void UpdateLockedQuestionsText() {
  128. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  129. int lockedQuestionsCount = scrollViewScript.GetQuestionIdsInAnswerLine().Count - scrollViewScript.GetUnlockedQuestionCount();
  130. String playerText;
  131. if (currentPlayerShown == null || currentPlayerShown.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  132. playerText = LocalizationManager.Instance.GetText("YOU");
  133. } else {
  134. playerText = currentPlayerShown;
  135. }
  136. if (lockedQuestionsText != null) {
  137. lockedQuestionsText.text = String.Format(
  138. LocalizationManager.Instance.GetText(
  139. lockedQuestionsText.GetComponent<TextLocalization>().key),
  140. playerText,
  141. lockedQuestionsCount);
  142. }
  143. }
  144. private void ShowNextPlayerAnswerLine() {
  145. if (nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
  146. currentPlayerShown = signedInName;
  147. } else {
  148. currentPlayerShown = nextPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  149. currentPlayerShown = currentPlayerShown.Substring(0, currentPlayerShown.Length - 1);
  150. }
  151. if (currentPlayerShown.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  152. List<NewQuestionData> questions = new List<NewQuestionData>();
  153. foreach (AnswerLineQuestionCard aq in signedInPlayerQuestions) {
  154. questions.Add(new NewQuestionData(aq.GetAnswerText(), aq.GetQuestionText(), aq.CategoryText, aq.CategoryId, aq.QuestionId, aq.CategoryColor, aq.QuestionSafe));
  155. }
  156. SetQuestionFrosting(questions);
  157. } else {
  158. List<NewQuestionData> showPlayerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode);
  159. SetQuestionFrosting(showPlayerQuestions);
  160. }
  161. UpdateButtonsText();
  162. }
  163. private void ShowPrevPlayerAnswerLine() {
  164. if (prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) {
  165. currentPlayerShown = signedInName;
  166. } else {
  167. currentPlayerShown = prevPlayerButton.GetComponent<AnswerLineInfoPlayerButton>().PlayerName;
  168. currentPlayerShown = currentPlayerShown.Substring(0, currentPlayerShown.Length - 1);
  169. }
  170. if (currentPlayerShown.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) {
  171. List<NewQuestionData> questions = new List<NewQuestionData>();
  172. foreach (AnswerLineQuestionCard aq in signedInPlayerQuestions) {
  173. questions.Add(new NewQuestionData(aq.GetAnswerText(), aq.GetQuestionText(), aq.CategoryText, aq.CategoryId, aq.QuestionId, aq.CategoryColor, aq.QuestionSafe));
  174. }
  175. SetQuestionFrosting(questions);
  176. } else {
  177. List<NewQuestionData> showPlayerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode);
  178. SetQuestionFrosting(showPlayerQuestions);
  179. }
  180. UpdateButtonsText();
  181. }
  182. private void SetQuestionFrosting(List<NewQuestionData> questions) {
  183. ScrollViewScript scrollViewScript = scrollView.GetComponent<ScrollViewScript>();
  184. scrollViewScript.RemoveEverythingFromAnswerline();
  185. scrollViewScript.SetQuestionsInAnswerLine(questions);
  186. if (gameManagerScript.GameMode.Equals(Constants.ONLINE)) {
  187. if (Database.Instance.GetSignedInUser().Value.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) {
  188. scrollViewScript.SetQuestionsFrosted(false);
  189. } else {
  190. scrollViewScript.SetQuestionsFrosted(true);
  191. }
  192. } else {
  193. if (currentPlayerShown.Equals(Database.Instance.GetCurrentPlayer(gameManagerScript.GameId, gameManagerScript.GameMode), StringComparison.InvariantCultureIgnoreCase)) {
  194. scrollViewScript.SetQuestionsFrosted(false);
  195. } else {
  196. scrollViewScript.SetQuestionsFrosted(true);
  197. }
  198. }
  199. }
  200. public String GetCurrentPlayerShown() {
  201. return currentPlayerShown;
  202. }
  203. }