ScrollViewScript.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class ScrollViewScript : MonoBehaviour, IDropHandler {
  8. public GameObject prefab;
  9. public Transform contentPanel;
  10. public Transform newQuestionsPanel;
  11. NewQuestion nq;
  12. private int newQuestionSiblingIndex;
  13. private bool answeredCorrectly;
  14. private List<KeyValuePair<string, int>> players;
  15. private int gameId;
  16. StatsScript statsScript;
  17. Database db;
  18. GameManagerScript gameManagerScript;
  19. TimerScript ts;
  20. InformationPanelScript ips;
  21. string currentPlayer;
  22. // Start is called before the first frame update
  23. void Start() {
  24. statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();
  25. db = GameObject.Find("GameManager").GetComponent<Database>();
  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 = db.GetCurrentPlayer(gameId);
  32. ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  33. ips.SetCurrentPlayer(currentPlayer);
  34. statsScript.MakeBold(currentPlayer);
  35. List<QuestionCard> answerlineQuestions = db.GetPlayerQuestions(gameId, currentPlayer);
  36. SetQuestionsInAnswerLine(answerlineQuestions);
  37. statsScript.SetQuestionsLost(db.GetQuestionsLost(gameId, currentPlayer));
  38. SetNewQuestion(db.GetNewQuestion());
  39. ResetNewQuestionPosition();
  40. ts.ResetTimer();
  41. ts.StartTimer();
  42. }
  43. private void SetQuestionsInAnswerLine(List<QuestionCard> questions) {
  44. foreach (QuestionCard q in questions) {
  45. q.transform.SetParent(contentPanel);
  46. q.SetQuestionSafe();
  47. }
  48. SetAllQuestionsLocked(false);
  49. }
  50. public int GetUnlockedQuestionCount() {
  51. int unlockedQuestionCount = 0;
  52. for (int i = 0; i < contentPanel.childCount; i++) {
  53. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  54. if (!qc.IsQuestionSafe()) {
  55. unlockedQuestionCount++;
  56. }
  57. }
  58. return unlockedQuestionCount;
  59. }
  60. public void SetAllQuestionsLocked(bool needsSave) {
  61. for (int i = 0; i < contentPanel.childCount; i++) {
  62. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  63. q.SetQuestionSafe();
  64. if (needsSave) {
  65. Database db = GameObject.Find("GameManager").GetComponent<Database>();
  66. db.SavePlayersQuestion(q.idString, currentPlayer, gameId);
  67. }
  68. }
  69. }
  70. public void SetNewQuestion(NewQuestion q) {
  71. nq = q;
  72. nq.SetQuestionText(q.questionString);
  73. nq.SetAnswerText("???? - ????");
  74. nq.GetComponent<CanvasGroup>().alpha = 1f;
  75. q.transform.SetParent(newQuestionsPanel.transform);
  76. ResetNewQuestionPosition();
  77. }
  78. private void ResetNewQuestionPosition() {
  79. nq.transform.position = nq.originalPos;
  80. nq.SetAnswerText("???? - ????");
  81. nq.transform.localPosition = new Vector3(0, 0, 0);
  82. nq.GetComponent<CanvasGroup>().alpha = 1f;
  83. nq.transform.SetParent(newQuestionsPanel);
  84. }
  85. public void OnDrop(PointerEventData eventData) {
  86. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  87. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  88. return;
  89. }
  90. nq = d.GetComponent<NewQuestion>();
  91. if ("???? - ????".Equals(nq.GetAnswerText().text)) {
  92. ResetNewQuestionPosition();
  93. return;
  94. }
  95. newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
  96. GenericDialog dialog = GenericDialog.Instance();
  97. dialog.SetTitle("Är du säker?");
  98. dialog.SetMessage("Vill du svara att " + nq.GetQuestionText().text + " hände " + nq.GetAnswerText().text + "?");
  99. dialog.SetOnAccept("Ja", () => {
  100. YesFunction();
  101. dialog.Hide();
  102. if (answeredCorrectly) {
  103. GameObject.Find("NewQuestionButtonPanel").GetComponent<RoundButtonsScript>().ShowPanel();
  104. nq.GetComponent<CanvasGroup>().alpha = 0;
  105. CheckWin();
  106. ts.StopTimer();
  107. ts.ResetTimer();
  108. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.childCount);
  109. } else {
  110. ts.StopTimer();
  111. ts.ResetTimer();
  112. dialog.SetTitle("Tyvärr fel svar, korrekt svar var " + nq.answerString);
  113. dialog.SetMessage("Du förlorade " + GetUnlockedQuestionCount() + ", bättre lycka nästa gång");
  114. RemoveUnlockedQuestions();
  115. dialog.SetOnAccept("Ok", () => {
  116. dialog.Hide();
  117. NextPlayer();
  118. });
  119. dialog.SetOnDecline("", () => dialog.Hide());
  120. ResetNewQuestionPosition();
  121. dialog.Show();
  122. }
  123. });
  124. dialog.SetOnDecline("Nej", () => { dialog.Hide(); ResetNewQuestionPosition(); });
  125. dialog.Show();
  126. }
  127. private void CheckWin() { // TODO
  128. if (db.GetWinCondition(gameId) <= contentPanel.childCount) {
  129. GenericDialog dialog = GenericDialog.Instance();
  130. dialog.title.text = "You won!";
  131. dialog.message.text = "You reached the goal of " + db.GetWinCondition(gameId) + " first, " +
  132. "you lost " + statsScript.GetQuestionsLost() + " questions from your answer to unlocked questions. " +
  133. "It took " + db.GetRoundValue(gameId) + " rounds";
  134. dialog.SetOnAccept("YEAY!", () => { dialog.Hide(); db.SetFinishedDate(gameId, DateTime.Today.ToShortDateString()); });
  135. dialog.Show();
  136. }
  137. }
  138. public void RemoveAllQuestionsFromAnswerline() {
  139. QuestionCard[] questions = contentPanel.GetComponentsInChildren<QuestionCard>();
  140. foreach (QuestionCard q in questions) {
  141. Destroy(q);
  142. Destroy(q.gameObject);
  143. }
  144. }
  145. private void RemoveUnlockedQuestions() {
  146. int lostQuestions = 0;
  147. for (int i = 0; i < contentPanel.childCount; i++) {
  148. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  149. if (!q.IsQuestionSafe()) {
  150. lostQuestions++;
  151. Destroy(q);
  152. Destroy(q.gameObject);
  153. }
  154. }
  155. gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
  156. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.childCount - lostQuestions);
  157. }
  158. void YesFunction() {
  159. int correctAnswer = Int32.Parse(nq.answerString);
  160. int currentChildCount = contentPanel.childCount;
  161. Transform questionBefore = null;
  162. Transform questionAfter = null;
  163. if (newQuestionSiblingIndex - 1 >= 0) {
  164. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex - 1);
  165. }
  166. if (newQuestionSiblingIndex < currentChildCount) {
  167. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex);
  168. }
  169. int answerBefore = -1;
  170. int answerAfter = 999999;
  171. if (questionBefore != null) {
  172. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  173. }
  174. if (questionAfter != null) {
  175. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  176. }
  177. if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
  178. // korrect svar, spara frågan i tidslinjen och prompta ny modal för "Vill du ha en fråga till?" med meddelande om vad som står på spel (olåsta frågor)
  179. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  180. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  181. questionCard.SetAnswerText(nq.answerString);
  182. questionCard.SetQuestionText(nq.questionString);
  183. questionCard.questionString = nq.questionString;
  184. questionCard.answerString = nq.answerString;
  185. questionCard.categoryString = nq.categoryString;
  186. questionCard.idString = nq.idString;
  187. questionCard.transform.SetParent(contentPanel);
  188. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  189. answeredCorrectly = true;
  190. } else {
  191. answeredCorrectly = false;
  192. }
  193. }
  194. void TimerRunOutEvent() {
  195. GenericDialog dialog = GenericDialog.Instance();
  196. dialog.SetTitle("Tiden tog slut!");
  197. dialog.SetMessage("Tiden tog slut och du förlorade " + GetUnlockedQuestionCount() + " frågor");
  198. dialog.SetOnAccept("Ok", () => {
  199. if (db.GetGameMode(gameId).Equals("Local")) {
  200. RemoveUnlockedQuestions();
  201. ts.ResetTimer();
  202. dialog.Hide();
  203. NextPlayer();
  204. } else {
  205. // online
  206. }
  207. });
  208. dialog.SetOnDecline("", () => dialog.Hide());
  209. dialog.Show();
  210. }
  211. public void NextPlayer() {
  212. for (int i = 0; i < players.Count; i++) {
  213. if (players[i].Key.Equals(currentPlayer)) {
  214. if (i + 1 < players.Count) {
  215. currentPlayer = players[i + 1].Key;
  216. break;
  217. } else {
  218. currentPlayer = players[0].Key;
  219. statsScript.IncreaseRoundValue();
  220. break;
  221. }
  222. }
  223. }
  224. GenericDialog dialog = GenericDialog.Instance();
  225. dialog.SetTitle("Nästa spelare");
  226. dialog.SetMessage("Dags för spelare " + currentPlayer);
  227. dialog.SetOnAccept("Ok", () => {
  228. RemoveAllQuestionsFromAnswerline();
  229. List<QuestionCard> questions = db.GetPlayerQuestions(gameId, currentPlayer);
  230. SetQuestionsInAnswerLine(questions);
  231. statsScript.SetQuestionsLost(db.GetQuestionsLost(gameManagerScript.GameId, currentPlayer));
  232. statsScript.MakeBold(currentPlayer);
  233. db.SetCurrentPlayer(gameId, currentPlayer);
  234. ips.SetCurrentPlayer(currentPlayer);
  235. ResetNewQuestionPosition();
  236. SetNewQuestion(db.GetNewQuestion());
  237. ts.StartTimer();
  238. dialog.Hide();
  239. });
  240. dialog.SetOnDecline("", () => dialog.Hide());
  241. dialog.Show();
  242. }
  243. }