ScrollViewScript.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. private const string newQuestionAnswerLineDefault = "???? - ????";
  9. public GameObject prefab;
  10. public Transform contentPanel;
  11. public Transform newQuestionsPanel;
  12. NewQuestion nq;
  13. private int newQuestionSiblingIndex;
  14. private bool answeredCorrectly;
  15. private List<KeyValuePair<string, int>> players;
  16. private int gameId;
  17. StatsScript statsScript;
  18. Database db;
  19. GameManagerScript gameManagerScript;
  20. TimerScript ts;
  21. private LocalizationManager LANG = LocalizationManager.Instance;
  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. db = GameObject.Find("GameManager").GetComponent<Database>();
  28. gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  29. ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
  30. players = gameManagerScript.GetPlayers();
  31. EventManager.StartListening("TimerEvent", TimerRunOutEvent);
  32. gameId = gameManagerScript.GameId;
  33. currentPlayer = db.GetCurrentPlayer(gameId);
  34. ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
  35. ips.SetCurrentPlayer(currentPlayer);
  36. statsScript.MakeBold(currentPlayer);
  37. List<QuestionCard> answerlineQuestions = db.GetPlayerQuestions(gameId, currentPlayer);
  38. SetQuestionsInAnswerLine(answerlineQuestions);
  39. statsScript.SetQuestionsLost(db.GetQuestionsLost(gameId, currentPlayer));
  40. ShowRoundButtons(false);
  41. }
  42. private void ShowRoundButtons(bool ShowNextPlayer) {
  43. RoundButtonsScript rbs = GameObject.Find("NewQuestionButtonPanel").GetComponent<RoundButtonsScript>();
  44. if (ShowNextPlayer) {
  45. rbs.ActivateNextPlayer();
  46. } else {
  47. rbs.DeactivateNextPlayer();
  48. }
  49. rbs.ShowPanel();
  50. if (nq == null) {
  51. nq = gameManagerScript.db.GetNewQuestion(GetQuestionIdsInAnswerLine(), currentPlayer);
  52. }
  53. nq.GetComponent<CanvasGroup>().alpha = 0;
  54. nq.GetComponent<CanvasGroup>().interactable = false;
  55. nq.GetComponent<CanvasGroup>().blocksRaycasts = false;
  56. }
  57. public void HideRoundButtons() {
  58. GameObject.Find("NewQuestionButtonPanel").GetComponent<RoundButtonsScript>().HidePanel();
  59. nq.GetComponent<CanvasGroup>().alpha = 1;
  60. nq.GetComponent<CanvasGroup>().interactable = true;
  61. nq.GetComponent<CanvasGroup>().blocksRaycasts = true;
  62. }
  63. private void SetQuestionsInAnswerLine(List<QuestionCard> questions) {
  64. foreach (QuestionCard q in questions) {
  65. q.transform.SetParent(contentPanel, false);
  66. q.SetQuestionSafe();
  67. }
  68. SetAllQuestionsLocked(false);
  69. }
  70. public int GetUnlockedQuestionCount() {
  71. int unlockedQuestionCount = 0;
  72. for (int i = 0; i < contentPanel.childCount; i++) {
  73. QuestionCard qc = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  74. if (!qc.IsQuestionSafe()) {
  75. unlockedQuestionCount++;
  76. }
  77. }
  78. return unlockedQuestionCount;
  79. }
  80. public void SetAllQuestionsLocked(bool needsSave) {
  81. for (int i = 0; i < contentPanel.childCount; i++) {
  82. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  83. q.SetQuestionSafe();
  84. if (needsSave) {
  85. Database db = GameObject.Find("GameManager").GetComponent<Database>();
  86. db.SavePlayersQuestion(q.idString, currentPlayer, gameId);
  87. }
  88. }
  89. }
  90. public void SetNewQuestion(NewQuestion q) {
  91. nq = q;
  92. nq.SetQuestionText(q.questionString);
  93. nq.SetAnswerText(newQuestionAnswerLineDefault);
  94. q.transform.SetParent(newQuestionsPanel.transform);
  95. ResetNewQuestionPosition();
  96. }
  97. private void ResetNewQuestionPosition() {
  98. nq.transform.position = nq.originalPos;
  99. nq.SetAnswerText(newQuestionAnswerLineDefault);
  100. nq.transform.SetParent(newQuestionsPanel);
  101. nq.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
  102. }
  103. public void OnDrop(PointerEventData eventData) {
  104. Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
  105. if (d == null || !d.gameObject.name.Contains("NewQuestion")) {
  106. return;
  107. }
  108. nq = d.GetComponent<NewQuestion>();
  109. if (newQuestionAnswerLineDefault.Equals(nq.GetAnswerText().text)) {
  110. ResetNewQuestionPosition();
  111. return;
  112. }
  113. newQuestionSiblingIndex = d.placeholder.transform.GetSiblingIndex();
  114. GenericDialog dialog = GenericDialog.Instance();
  115. LANG = LocalizationManager.Instance;
  116. dialog.SetTitle(LANG.GetText("ARE_YOU_SURE_TITLE"));
  117. string message = LANG.GetText("DROPPED_QUESTION_DIALOG_MESSAGE_TEXT");
  118. message = String.Format(message, nq.GetQuestionText().text, nq.GetAnswerText().text);
  119. dialog.SetMessage(message);
  120. dialog.SetOnAccept(LANG.GetText("YES"), () => {
  121. YesFunction();
  122. dialog.Hide();
  123. if (ts == null) {
  124. ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
  125. }
  126. if (answeredCorrectly) {
  127. ShowRoundButtons(true);
  128. CheckWin();
  129. ts.StopTimer();
  130. ts.ResetTimer();
  131. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.childCount);
  132. } else {
  133. ts.StopTimer();
  134. ts.ResetTimer();
  135. string title = LANG.GetText("DROPPED_QUESTION_WRONG_ANSWER_TITLE");
  136. dialog.SetTitle(String.Format(title, nq.answerString));
  137. message = LANG.GetText("DROPPED_QUESTION_WRONG_ANSWER_MESSAGE");
  138. dialog.SetMessage(String.Format(message,GetUnlockedQuestionCount()));
  139. RemoveUnlockedQuestions();
  140. dialog.SetOnAccept(LANG.GetText("OK"), () => {
  141. dialog.Hide();
  142. NextPlayer();
  143. });
  144. dialog.SetOnDecline("", () => dialog.Hide());
  145. ResetNewQuestionPosition();
  146. dialog.Show();
  147. }
  148. });
  149. dialog.SetOnDecline(LANG.GetText("NO"), () => { dialog.Hide(); ResetNewQuestionPosition(); });
  150. dialog.Show();
  151. }
  152. private void CheckWin() {
  153. if (db.GetWinCondition(gameId) <= contentPanel.childCount) {
  154. GenericDialog dialog = GenericDialog.Instance();
  155. dialog.title.text = LANG.GetText("WINNER_DIALOG_TITLE");
  156. string message = LANG.GetText("WINNER_DIALOG_MESSAGE");
  157. dialog.message.text = String.Format(message, db.GetWinCondition(gameId), statsScript.GetQuestionsLost(), statsScript.GetRound());
  158. dialog.SetOnAccept(LANG.GetText("WINNER_DIALOG_BUTTON"), () => {
  159. dialog.Hide();
  160. SetAllQuestionsLocked(true);
  161. db.SetFinishedDate(gameId, DateTime.Today.ToShortDateString());
  162. GameObject.Find("NewQuestionButtonPanel").GetComponent<RoundButtonsScript>().SetGameOver();
  163. });
  164. dialog.SetOnDecline("", () => { });
  165. dialog.Show();
  166. }
  167. }
  168. public List<int> GetQuestionIdsInAnswerLine() {
  169. List<int> questionIds = new List<int>();
  170. foreach (QuestionCard q in contentPanel.GetComponentsInChildren<QuestionCard>()) {
  171. questionIds.Add(q.GetId());
  172. }
  173. return questionIds;
  174. }
  175. public void RemoveAllQuestionsFromAnswerline() {
  176. QuestionCard[] questions = contentPanel.GetComponentsInChildren<QuestionCard>();
  177. foreach (QuestionCard q in questions) {
  178. Destroy(q);
  179. Destroy(q.gameObject);
  180. }
  181. }
  182. private void RemoveUnlockedQuestions() {
  183. int lostQuestions = 0;
  184. for (int i = 0; i < contentPanel.childCount; i++) {
  185. QuestionCard q = contentPanel.GetChild(i).GetComponent<QuestionCard>();
  186. if (!q.IsQuestionSafe()) {
  187. lostQuestions++;
  188. Destroy(q);
  189. Destroy(q.gameObject);
  190. }
  191. }
  192. gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
  193. statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.childCount - lostQuestions);
  194. }
  195. void YesFunction() {
  196. int correctAnswer = Int32.Parse(nq.answerString);
  197. int currentChildCount = contentPanel.childCount;
  198. Transform questionBefore = null;
  199. Transform questionAfter = null;
  200. if (newQuestionSiblingIndex - 1 >= 0) {
  201. questionBefore = contentPanel.GetChild(newQuestionSiblingIndex - 1);
  202. }
  203. if (newQuestionSiblingIndex < currentChildCount) {
  204. questionAfter = contentPanel.GetChild(newQuestionSiblingIndex);
  205. }
  206. int answerBefore = -1;
  207. int answerAfter = 999999;
  208. if (questionBefore != null) {
  209. answerBefore = Int16.Parse(questionBefore.GetComponent<QuestionCard>().GetAnswerText().text);
  210. }
  211. if (questionAfter != null) {
  212. answerAfter = Int16.Parse(questionAfter.GetComponent<QuestionCard>().GetAnswerText().text);
  213. }
  214. if (answerBefore <= correctAnswer && answerAfter >= correctAnswer) {
  215. // 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)
  216. GameObject question = Instantiate(prefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  217. QuestionCard questionCard = question.GetComponent<QuestionCard>();
  218. questionCard.SetAnswerText(nq.answerString);
  219. questionCard.SetQuestionText(nq.questionString);
  220. questionCard.questionString = nq.questionString;
  221. questionCard.answerString = nq.answerString;
  222. questionCard.categoryString = nq.categoryString;
  223. questionCard.idString = nq.idString;
  224. questionCard.transform.SetParent(contentPanel, false);
  225. questionCard.transform.SetSiblingIndex(newQuestionSiblingIndex);
  226. questionCard.SetQuestionUnSafe();
  227. CategoryPanel cp = GameObject.Find("Categories").GetComponent<CategoryPanel>();
  228. CategoryPanel.Category cat = cp.GetCategoryById(questionCard.GetCategoryId());
  229. questionCard.SetQuestionCategoryColor(cat.color);
  230. answeredCorrectly = true;
  231. } else {
  232. answeredCorrectly = false;
  233. }
  234. }
  235. void TimerRunOutEvent() {
  236. GenericDialog dialog = GenericDialog.Instance();
  237. string message = LANG.GetText("TIMER_DIALOG_MESSAGE");
  238. dialog.SetTitle(LANG.GetText("TIMER_DIALOG_TITLE"));
  239. dialog.SetMessage(String.Format(message, GetUnlockedQuestionCount()));
  240. dialog.SetOnAccept(LANG.GetText("OK"), () => {
  241. if (db.GetGameMode(gameId).Equals("Local")) {
  242. RemoveUnlockedQuestions();
  243. ts.ResetTimer();
  244. dialog.Hide();
  245. NextPlayer();
  246. } else {
  247. // online
  248. }
  249. });
  250. dialog.SetOnDecline("", () => dialog.Hide());
  251. dialog.Show();
  252. }
  253. public void NextPlayer() {
  254. for (int i = 0; i < players.Count; i++) {
  255. if (players[i].Key.Equals(currentPlayer)) {
  256. if (i + 1 < players.Count) {
  257. currentPlayer = players[i + 1].Key;
  258. break;
  259. } else {
  260. currentPlayer = players[0].Key;
  261. statsScript.IncreaseRoundValue();
  262. break;
  263. }
  264. }
  265. }
  266. GenericDialog dialog = GenericDialog.Instance();
  267. dialog.SetTitle(LANG.GetText("NEXT_PLAYER_DIALOG_TITLE"));
  268. string message = LANG.GetText("NEXT_PLAYER_DIALOG_MESSAGE");
  269. dialog.SetMessage(String.Format(message, currentPlayer));
  270. dialog.SetOnAccept(LANG.GetText("OK"), () => {
  271. RemoveAllQuestionsFromAnswerline();
  272. List<QuestionCard> questions = db.GetPlayerQuestions(gameId, currentPlayer);
  273. SetQuestionsInAnswerLine(questions);
  274. statsScript.SetQuestionsLost(db.GetQuestionsLost(gameManagerScript.GameId, currentPlayer));
  275. statsScript.MakeBold(currentPlayer);
  276. db.SetCurrentPlayer(gameId, currentPlayer);
  277. ips.SetCurrentPlayer(currentPlayer);
  278. ResetNewQuestionPosition();
  279. ShowRoundButtons(false);
  280. dialog.Hide();
  281. });
  282. dialog.SetOnDecline("", () => dialog.Hide());
  283. dialog.Show();
  284. }
  285. }