ScrollViewScript.cs 15 KB

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