ScrollViewScript.cs 10 KB

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