ScrollViewScript.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class ScrollViewScript : MonoBehaviour {
  9. public GameObject answerlineQuestionPrefab;
  10. public Transform contentPanel;
  11. public Transform newQuestionsPanel;
  12. private List<KeyValuePair<string, int>> players;
  13. private int gameId;
  14. private string gameMode;
  15. private bool sizeUpdated = false;
  16. StatsScript statsScript;
  17. GameManagerScript gameManagerScript;
  18. TimerScript ts;
  19. readonly InformationPanelScript ips;
  20. public string currentPlayer;
  21. // Start is called before the first frame update
  22. void Start() {
  23. statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();
  24. gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  25. ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
  26. players = gameManagerScript.GetPlayers();
  27. EventManager.StartListening("TimerEvent", TimerRunOutEvent);
  28. gameId = gameManagerScript.GameId;
  29. currentPlayer = Database.Instance.GetCurrentPlayer(gameId, GetGameMode());
  30. statsScript.MakeBold(currentPlayer);
  31. KeyValuePair<int, string> signedInUser = Database.Instance.GetSignedInUser();
  32. List<NewQuestionData> answerlineQuestions;
  33. if (gameMode.Equals(Constants.ONLINE)) {
  34. answerlineQuestions = Database.Instance.GetPlayerQuestions(gameId, signedInUser.Value, GetGameMode());
  35. } else {
  36. answerlineQuestions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
  37. }
  38. if (Database.Instance.GetRoundValue(gameId, GetGameMode()) > 1) {
  39. SetQuestionsInAnswerLine(answerlineQuestions);
  40. }
  41. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameId, currentPlayer, GetGameMode()));
  42. }
  43. private void Update() {
  44. if (!sizeUpdated) {
  45. UpdateAnswerlineCardSize();
  46. }
  47. }
  48. public string GetGameMode() {
  49. if (gameMode == null) {
  50. gameMode = PlayerPrefs.GetString(Constants.GAME_MODE);
  51. }
  52. return gameMode;
  53. }
  54. [SerializeField] GameObject leftDropZone;
  55. [SerializeField] GameObject rightDropZone;
  56. [SerializeField] GameObject middleDropZone;
  57. public void SetQuestionsInAnswerLine(List<NewQuestionData> questions) {
  58. int i = 0;
  59. RectTransform newQuestionRect = GameObject.Find("NewQuestion").GetComponent<RectTransform>();
  60. GameObject ldz = Instantiate(leftDropZone, Vector2.zero, Quaternion.identity);
  61. GameObject rdz = Instantiate(rightDropZone, Vector2.zero, Quaternion.identity);
  62. ldz.transform.SetParent(contentPanel);
  63. ldz.transform.SetSiblingIndex(i++);
  64. ldz.transform.localScale = new Vector3(1, 1, 1);
  65. if (newQuestionRect.rect.width < 50f) {
  66. ldz.GetComponent<LayoutElement>().preferredWidth = 50f;
  67. } else {
  68. ldz.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  69. }
  70. KeyValuePair<int, string> signedInUser = Database.Instance.GetSignedInUser();
  71. foreach (NewQuestionData q in questions) {
  72. GameObject answerLineQuestion = Instantiate(answerlineQuestionPrefab, Vector3.zero, Quaternion.identity);
  73. answerLineQuestion.transform.SetParent(contentPanel, false);
  74. answerLineQuestion.transform.SetSiblingIndex(i++);
  75. answerLineQuestion.transform.localScale = new Vector3(1, 1, 1);
  76. answerLineQuestion.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  77. answerLineQuestion.GetComponent<LayoutElement>().preferredHeight = newQuestionRect.rect.height;
  78. QuestionCard answerLineQuestionCard = answerLineQuestion.GetComponent<QuestionCard>();
  79. answerLineQuestionCard.SetAnswerText(q.Answer);
  80. answerLineQuestionCard.SetQuestionText(q.Question);
  81. answerLineQuestionCard.SetId(q.Id);
  82. answerLineQuestionCard.SetQuestionSafe(q.QuestionSafe);
  83. answerLineQuestionCard.SetBackCategoryText(q.CategoryName);
  84. answerLineQuestionCard.SetQuestionCategoryColor(q.CategoryColor);
  85. if (gameMode.Equals(Constants.ONLINE)) {
  86. if (signedInUser.Value.Equals(Database.Instance.GetSignedInUser().Value, StringComparison.InvariantCultureIgnoreCase)) {
  87. answerLineQuestionCard.SetFrostingActive(false);
  88. } else {
  89. answerLineQuestionCard.SetFrostingActive(true);
  90. }
  91. }
  92. GameObject mdz = Instantiate(middleDropZone, Vector3.zero, Quaternion.identity);
  93. mdz.transform.SetParent(contentPanel);
  94. mdz.transform.SetSiblingIndex(i++);
  95. mdz.transform.localScale = new Vector3(1, 1, 1);
  96. }
  97. NewDropZoneScript newDropZoneScript = contentPanel.GetChild(i - 1).GetComponent<NewDropZoneScript>();
  98. Destroy(newDropZoneScript.gameObject);
  99. rdz.transform.SetParent(contentPanel);
  100. rdz.transform.SetSiblingIndex(i);
  101. rdz.transform.localScale = new Vector3(1, 1, 1);
  102. if (newQuestionRect.rect.width < 50f) {
  103. rdz.GetComponent<LayoutElement>().preferredWidth = 50f;
  104. } else {
  105. rdz.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  106. }
  107. SetAllQuestionsLocked(false);
  108. }
  109. internal void SetQuestionsFrosted(bool frost) {
  110. List<QuestionCard> questions = contentPanel.GetComponentsInChildren<QuestionCard>().ToList();
  111. questions.ForEach(q => q.SetFrostingActive(frost));
  112. }
  113. private void UpdateAnswerlineCardSize() {
  114. RectTransform newQuestionRect = GameObject.Find("NewQuestion").GetComponent<RectTransform>();
  115. List<QuestionCard> questions = contentPanel.GetComponentsInChildren<QuestionCard>().ToList();
  116. foreach (QuestionCard q in questions) {
  117. q.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  118. q.GetComponent<LayoutElement>().preferredHeight = newQuestionRect.rect.height;
  119. }
  120. if (contentPanel.childCount > 0) {
  121. contentPanel.GetChild(0).GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  122. contentPanel.GetChild(contentPanel.childCount - 1).GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  123. }
  124. if (questions.Count > 0 && questions[0].GetComponent<LayoutElement>().preferredWidth >= 0) {
  125. sizeUpdated = true;
  126. }
  127. }
  128. public int GetUnlockedQuestionCount() {
  129. return contentPanel.GetComponentsInChildren<QuestionCard>().Where(q => !q.IsQuestionSafe()).ToArray().Length; ;
  130. }
  131. public void SetAllQuestionsLocked(bool needsSave) {
  132. List<int> saveQuestions = new List<int>();
  133. AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
  134. foreach (var questionCard in answerLineQuestionCards) {
  135. if (needsSave && !questionCard.IsQuestionSafe()) {
  136. saveQuestions.Add(questionCard.QuestionId);
  137. questionCard.SetQuestionSafe();
  138. }
  139. }
  140. if (saveQuestions.Count > 0) {
  141. Database.Instance.SavePlayersQuestion(saveQuestions, GameManagerScript.GetCurrentPlayer(), gameId, GetGameMode());
  142. }
  143. }
  144. public List<int> GetQuestionIdsInAnswerLine() {
  145. List<int> questionIds = new List<int>();
  146. foreach (AnswerLineQuestionCard q in contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>()) {
  147. questionIds.Add(q.QuestionId);
  148. }
  149. return questionIds;
  150. }
  151. public void RemoveEverythingFromAnswerline() {
  152. foreach (Transform child in contentPanel.transform) {
  153. Destroy(child.gameObject);
  154. }
  155. }
  156. public void RemoveUnlockedQuestions() {
  157. int lostQuestions = 0;
  158. AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
  159. foreach (AnswerLineQuestionCard aq in answerLineQuestionCards) {
  160. if (!aq.IsQuestionSafe()) {
  161. lostQuestions++;
  162. aq.transform.SetParent(null);
  163. Destroy(aq);
  164. Destroy(aq.gameObject);
  165. }
  166. }
  167. gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
  168. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>().Length);
  169. }
  170. void TimerRunOutEvent() { // Should be moved to some gameController
  171. ts.StopTimer();
  172. GenericDialog dialog = GenericDialog.Instance();
  173. string message = LocalizationManager.Instance.GetText("TIMER_DIALOG_MESSAGE");
  174. dialog.SetTitle(LocalizationManager.Instance.GetText("TIMER_DIALOG_TITLE"));
  175. dialog.SetMessage(String.Format(message, GetUnlockedQuestionCount()));
  176. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  177. RemoveUnlockedQuestions();
  178. ts.ResetTimer();
  179. dialog.Hide();
  180. NextPlayer();
  181. });
  182. dialog.SetOnDecline("", () => dialog.Hide());
  183. dialog.Show();
  184. }
  185. public void NextPlayer() {
  186. for (int i = 0; i < players.Count; i++) {
  187. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  188. if (i + 1 < players.Count) {
  189. currentPlayer = players[i + 1].Key;
  190. break;
  191. } else {
  192. currentPlayer = players[0].Key;
  193. statsScript.IncreaseRoundValue();
  194. break;
  195. }
  196. }
  197. }
  198. GenericDialog dialog = GenericDialog.Instance();
  199. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  200. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  201. dialog.SetMessage(String.Format(message, currentPlayer));
  202. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  203. RemoveEverythingFromAnswerline();
  204. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
  205. SetQuestionsInAnswerLine(questions);
  206. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameManagerScript.GameId, currentPlayer, GetGameMode()));
  207. statsScript.MakeBold(currentPlayer);
  208. Database.Instance.SetCurrentPlayer(gameId, currentPlayer, GetGameMode());
  209. dialog.Hide();
  210. });
  211. dialog.SetOnDecline("", () => dialog.Hide());
  212. dialog.Show();
  213. }
  214. }