ScrollViewScript.cs 11 KB

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