ScrollViewScript.cs 11 KB

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