ScrollViewScript.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. private void UpdateAnswerlineCardSize() {
  107. RectTransform newQuestionRect = GameObject.Find("NewQuestion").GetComponent<RectTransform>();
  108. List<QuestionCard> questions = contentPanel.GetComponentsInChildren<QuestionCard>().ToList();
  109. foreach (QuestionCard q in questions) {
  110. q.GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  111. q.GetComponent<LayoutElement>().preferredHeight = newQuestionRect.rect.height;
  112. }
  113. if (contentPanel.childCount > 0) {
  114. contentPanel.GetChild(0).GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  115. contentPanel.GetChild(contentPanel.childCount -1).GetComponent<LayoutElement>().preferredWidth = newQuestionRect.rect.width;
  116. }
  117. if (questions.Count > 0 && questions[0].GetComponent<LayoutElement>().preferredWidth >= 0) {
  118. sizeUpdated = true;
  119. }
  120. }
  121. public int GetUnlockedQuestionCount() {
  122. return contentPanel.GetComponentsInChildren<QuestionCard>().Where(q => !q.IsQuestionSafe()).ToArray().Length;;
  123. }
  124. public void SetAllQuestionsLocked(bool needsSave) {
  125. List<int> saveQuestions = new List<int>();
  126. AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
  127. foreach (var questionCard in answerLineQuestionCards)
  128. {
  129. if (needsSave && !questionCard.IsQuestionSafe()) {
  130. saveQuestions.Add(questionCard.GetId());
  131. questionCard.SetQuestionSafe();
  132. }
  133. }
  134. if (saveQuestions.Count > 0) {
  135. Database.Instance.SavePlayersQuestion(saveQuestions, GameManagerScript.GetCurrentPlayer(), gameId, GetGameMode());
  136. }
  137. }
  138. public List<int> GetQuestionIdsInAnswerLine() {
  139. List<int> questionIds = new List<int>();
  140. foreach (AnswerLineQuestionCard q in contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>()) {
  141. questionIds.Add(q.GetId());
  142. }
  143. return questionIds;
  144. }
  145. public void RemoveEverythingFromAnswerline() {
  146. foreach (Transform child in contentPanel.transform) {
  147. Destroy(child.gameObject);
  148. }
  149. }
  150. public void RemoveUnlockedQuestions() {
  151. int lostQuestions = 0;
  152. AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
  153. foreach (AnswerLineQuestionCard aq in answerLineQuestionCards)
  154. {
  155. if (!aq.IsQuestionSafe()){
  156. lostQuestions++;
  157. aq.transform.SetParent(null);
  158. Destroy(aq);
  159. Destroy(aq.gameObject);
  160. }
  161. }
  162. gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
  163. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.GetComponents<AnswerLineQuestionCard>().Length);
  164. }
  165. void TimerRunOutEvent() { // Should be moved to some gameController
  166. ts.StopTimer();
  167. GenericDialog dialog = GenericDialog.Instance();
  168. string message = LocalizationManager.Instance.GetText("TIMER_DIALOG_MESSAGE");
  169. dialog.SetTitle(LocalizationManager.Instance.GetText("TIMER_DIALOG_TITLE"));
  170. dialog.SetMessage(String.Format(message, GetUnlockedQuestionCount()));
  171. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  172. RemoveUnlockedQuestions();
  173. ts.ResetTimer();
  174. dialog.Hide();
  175. NextPlayer();
  176. });
  177. dialog.SetOnDecline("", () => dialog.Hide());
  178. dialog.Show();
  179. }
  180. public void NextPlayer() {
  181. for (int i = 0; i < players.Count; i++) {
  182. if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) {
  183. if (i + 1 < players.Count) {
  184. currentPlayer = players[i + 1].Key;
  185. break;
  186. } else {
  187. currentPlayer = players[0].Key;
  188. statsScript.IncreaseRoundValue();
  189. break;
  190. }
  191. }
  192. }
  193. GenericDialog dialog = GenericDialog.Instance();
  194. dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  195. string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  196. dialog.SetMessage(String.Format(message, currentPlayer));
  197. dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => {
  198. RemoveEverythingFromAnswerline();
  199. List<NewQuestionData> questions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
  200. SetQuestionsInAnswerLine(questions);
  201. statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameManagerScript.GameId, currentPlayer, GetGameMode()));
  202. statsScript.MakeBold(currentPlayer);
  203. Database.Instance.SetCurrentPlayer(gameId, currentPlayer, GetGameMode());
  204. dialog.Hide();
  205. });
  206. dialog.SetOnDecline("", () => dialog.Hide());
  207. dialog.Show();
  208. }
  209. }