ScrollViewScript.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. StatsScript statsScript;
  19. GameManagerScript gameManagerScript;
  20. TimerScript ts;
  21. InformationPanelScript ips;
  22. public string currentPlayer;
  23. // Start is called before the first frame update
  24. void Start() {
  25. statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();
  26. gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  27. ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
  28. players = gameManagerScript.GetPlayers();
  29. EventManager.StartListening("TimerEvent", TimerRunOutEvent);
  30. gameId = gameManagerScript.GameId;
  31. currentPlayer = Database.Instance.GetCurrentPlayer(gameId, GetGameMode());
  32. statsScript.MakeBold(currentPlayer);
  33. List<NewQuestionData> answerlineQuestions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
  34. if (Database.Instance.GetRoundValue(gameId, GetGameMode()) > 1) {
  35. SetQuestionsInAnswerLine(answerlineQuestions);
  36. }
  37. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameId, currentPlayer, GetGameMode()));
  38. }
  39. public string GetGameMode() {
  40. if (gameMode == null) {
  41. gameMode = PlayerPrefs.GetString("GameMode");
  42. }
  43. return gameMode;
  44. }
  45. [SerializeField] GameObject leftDropZone;
  46. [SerializeField] GameObject rightDropZone;
  47. [SerializeField] GameObject middleDropZone;
  48. public void SetQuestionsInAnswerLine(List<NewQuestionData> questions) {
  49. int i = 0;
  50. GameObject ldz = Instantiate(leftDropZone,Vector2.zero, Quaternion.identity);
  51. GameObject rdz = Instantiate(rightDropZone,Vector2.zero, Quaternion.identity);
  52. ldz.transform.SetParent(contentPanel);
  53. ldz.transform.SetSiblingIndex(i++);
  54. ldz.transform.localScale = new Vector3(1,1,1);
  55. KeyValuePair<int, string> signedInUser = Database.Instance.GetSignedInUser();
  56. foreach (NewQuestionData q in questions) {
  57. GameObject answerLineQuestion = Instantiate(answerlineQuestionPrefab, Vector3.zero, Quaternion.identity);
  58. answerLineQuestion.transform.SetParent(contentPanel, false);
  59. answerLineQuestion.transform.SetSiblingIndex(i++);
  60. answerLineQuestion.transform.localScale = new Vector3(1,1,1);
  61. QuestionCard answerLineQuestionCard = answerLineQuestion.GetComponent<QuestionCard>();
  62. answerLineQuestionCard.SetAnswerText(q.Answer);
  63. answerLineQuestionCard.SetQuestionText(q.Question);
  64. answerLineQuestionCard.SetId(q.Id);
  65. answerLineQuestionCard.SetQuestionSafe();
  66. if (gameMode.Equals("Online")) {
  67. if (signedInUser.Value.Equals(GameManagerScript.GetCurrentPlayer(), StringComparison.InvariantCultureIgnoreCase)) {
  68. answerLineQuestionCard.SetFrostingActive(false);
  69. } else {
  70. answerLineQuestionCard.SetFrostingActive(true);
  71. }
  72. }
  73. GameObject mdz = Instantiate(middleDropZone, Vector3.zero, Quaternion.identity);
  74. mdz.transform.SetParent(contentPanel);
  75. mdz.transform.SetSiblingIndex(i++);
  76. mdz.transform.localScale = new Vector3(1,1,1);
  77. }
  78. NewDropZoneScript newDropZoneScript = contentPanel.GetChild(i-1).GetComponent<NewDropZoneScript>();
  79. Destroy(newDropZoneScript.gameObject);
  80. rdz.transform.SetParent(contentPanel);
  81. rdz.transform.SetSiblingIndex(i);
  82. rdz.transform.localScale = new Vector3(1,1,1);
  83. SetAllQuestionsLocked(false);
  84. }
  85. public int GetUnlockedQuestionCount() {
  86. /*int unlockedQuestionCount = 0;
  87. for (int i = 0; i < contentPanel.childCount; i++) {
  88. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  89. if (!qc.IsQuestionSafe()) {
  90. unlockedQuestionCount++;
  91. }
  92. }
  93. */
  94. return contentPanel.GetComponentsInChildren<QuestionCard>().Where(q => !q.IsQuestionSafe()).ToArray().Length;;
  95. }
  96. public void SetAllQuestionsLocked(bool needsSave) {
  97. List<int> saveQuestions = new List<int>();
  98. AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
  99. foreach (var questionCard in answerLineQuestionCards)
  100. {
  101. if (needsSave && !questionCard.IsQuestionSafe()) {
  102. saveQuestions.Add(questionCard.GetId());
  103. questionCard.SetQuestionSafe();
  104. }
  105. }
  106. if (saveQuestions.Count > 0) {
  107. Database.Instance.SavePlayersQuestion(saveQuestions, GameManagerScript.GetCurrentPlayer(), gameId, GetGameMode());
  108. }
  109. }
  110. public List<int> GetQuestionIdsInAnswerLine() {
  111. List<int> questionIds = new List<int>();
  112. foreach (AnswerLineQuestionCard q in contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>()) {
  113. questionIds.Add(q.GetId());
  114. }
  115. return questionIds;
  116. }
  117. public void RemoveEverythingFromAnswerline() {
  118. foreach (Transform child in contentPanel.transform) {
  119. Destroy(child.gameObject);
  120. }
  121. }
  122. public void RemoveUnlockedQuestions() {
  123. int lostQuestions = 0;
  124. for (int i = 0; i < contentPanel.childCount; i++) {
  125. AnswerLineQuestionCard q = contentPanel.GetChild(i).GetComponent<AnswerLineQuestionCard>();
  126. if (q is AnswerLineQuestionCard && !q.IsQuestionSafe()) {
  127. lostQuestions++;
  128. Destroy(q);
  129. Destroy(q.gameObject);
  130. }
  131. }
  132. gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
  133. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.GetComponents<AnswerLineQuestionCard>().Length);
  134. }
  135. void TimerRunOutEvent() { // Should be moved to some gameController
  136. GenericDialog dialog = GenericDialog.Instance();
  137. string message = LocalizationManager.Instance.GetText("TIMER_DIALOG_MESSAGE");
  138. dialog.SetTitle(LocalizationManager.Instance.GetText("TIMER_DIALOG_TITLE"));
  139. dialog.SetMessage(String.Format(message, GetUnlockedQuestionCount()));
  140. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  141. RemoveUnlockedQuestions();
  142. ts.ResetTimer();
  143. dialog.Hide();
  144. NextPlayer();
  145. });
  146. dialog.SetOnDecline("", () => dialog.Hide());
  147. dialog.Show();
  148. }
  149. public void NextPlayer() {
  150. for (int i = 0; i < players.Count; i++) {
  151. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  152. if (i + 1 < players.Count) {
  153. currentPlayer = players[i + 1].Key;
  154. break;
  155. } else {
  156. currentPlayer = players[0].Key;
  157. statsScript.IncreaseRoundValue();
  158. break;
  159. }
  160. }
  161. }
  162. GenericDialog dialog = GenericDialog.Instance();
  163. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  164. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  165. dialog.SetMessage(String.Format(message, currentPlayer));
  166. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  167. RemoveEverythingFromAnswerline();
  168. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
  169. SetQuestionsInAnswerLine(questions);
  170. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameManagerScript.GameId, currentPlayer, GetGameMode()));
  171. statsScript.MakeBold(currentPlayer);
  172. Database.Instance.SetCurrentPlayer(gameId, currentPlayer, GetGameMode());
  173. dialog.Hide();
  174. });
  175. dialog.SetOnDecline("", () => dialog.Hide());
  176. dialog.Show();
  177. }
  178. }