AnswerLineInfoScript.cs 12 KB

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