ScrollViewScript.cs 8.3 KB

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