ScrollViewScript.cs 14 KB

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