ScrollViewScript.cs 9.8 KB

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