ScrollViewScript.cs 13 KB

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