ScrollViewScript.cs 11 KB

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