ScrollViewScript.cs 11 KB

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