ScrollViewScript.cs 8.7 KB

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