AnswerLineInfoScript.cs 12 KB

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