ScrollViewScript.cs 13 KB

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