ScrollViewScript.cs 10 KB

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