ScrollViewScript.cs 13 KB

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