using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using System.Linq; public class ScrollViewScript : MonoBehaviour { private const string newQuestionAnswerLineDefault = "???? - ????"; public GameObject answerlineQuestionPrefab; public Transform contentPanel; public Transform newQuestionsPanel; private int newQuestionSiblingIndex; private bool answeredCorrectly; private List> players; private int gameId; private string gameMode; StatsScript statsScript; GameManagerScript gameManagerScript; TimerScript ts; InformationPanelScript ips; public string currentPlayer; // Start is called before the first frame update void Start() { statsScript = GameObject.Find("StatsPanel").GetComponent(); gameManagerScript = GameObject.Find("GameManager").GetComponent(); ts = GameObject.Find("TimerCircle").GetComponent(); players = gameManagerScript.GetPlayers(); EventManager.StartListening("TimerEvent", TimerRunOutEvent); gameId = gameManagerScript.GameId; currentPlayer = Database.Instance.GetCurrentPlayer(gameId, GetGameMode()); statsScript.MakeBold(currentPlayer); List answerlineQuestions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode()); if (Database.Instance.GetRoundValue(gameId, GetGameMode()) > 1) { SetQuestionsInAnswerLine(answerlineQuestions); } statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameId, currentPlayer, GetGameMode())); } public string GetGameMode() { if (gameMode == null) { gameMode = PlayerPrefs.GetString("GameMode"); } return gameMode; } [SerializeField] GameObject leftDropZone; [SerializeField] GameObject rightDropZone; [SerializeField] GameObject middleDropZone; public void SetQuestionsInAnswerLine(List questions) { int i = 0; GameObject ldz = Instantiate(leftDropZone,Vector2.zero, Quaternion.identity); GameObject rdz = Instantiate(rightDropZone,Vector2.zero, Quaternion.identity); ldz.transform.SetParent(contentPanel); ldz.transform.SetSiblingIndex(i++); ldz.transform.localScale = new Vector3(1,1,1); KeyValuePair signedInUser = Database.Instance.GetSignedInUser(); foreach (NewQuestionData q in questions) { GameObject answerLineQuestion = Instantiate(answerlineQuestionPrefab, Vector3.zero, Quaternion.identity); answerLineQuestion.transform.SetParent(contentPanel, false); answerLineQuestion.transform.SetSiblingIndex(i++); answerLineQuestion.transform.localScale = new Vector3(1,1,1); QuestionCard answerLineQuestionCard = answerLineQuestion.GetComponent(); answerLineQuestionCard.SetAnswerText(q.Answer); answerLineQuestionCard.SetQuestionText(q.Question); answerLineQuestionCard.SetId(q.Id); answerLineQuestionCard.SetQuestionSafe(); if (gameMode.Equals("Online")) { if (signedInUser.Value.Equals(GameManagerScript.GetCurrentPlayer(), StringComparison.InvariantCultureIgnoreCase)) { answerLineQuestionCard.SetFrostingActive(false); } else { answerLineQuestionCard.SetFrostingActive(true); } } GameObject mdz = Instantiate(middleDropZone, Vector3.zero, Quaternion.identity); mdz.transform.SetParent(contentPanel); mdz.transform.SetSiblingIndex(i++); mdz.transform.localScale = new Vector3(1,1,1); } NewDropZoneScript newDropZoneScript = contentPanel.GetChild(i-1).GetComponent(); Destroy(newDropZoneScript.gameObject); rdz.transform.SetParent(contentPanel); rdz.transform.SetSiblingIndex(i); rdz.transform.localScale = new Vector3(1,1,1); SetAllQuestionsLocked(false); } public int GetUnlockedQuestionCount() { /*int unlockedQuestionCount = 0; for (int i = 0; i < contentPanel.childCount; i++) { QuestionCard qc = contentPanel.GetChild(i).GetComponent(); if (!qc.IsQuestionSafe()) { unlockedQuestionCount++; } } */ return contentPanel.GetComponentsInChildren().Where(q => !q.IsQuestionSafe()).ToArray().Length;; } public void SetAllQuestionsLocked(bool needsSave) { List saveQuestions = new List(); AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren(); foreach (var questionCard in answerLineQuestionCards) { if (needsSave && !questionCard.IsQuestionSafe()) { saveQuestions.Add(questionCard.GetId()); questionCard.SetQuestionSafe(); } } if (saveQuestions.Count > 0) { Database.Instance.SavePlayersQuestion(saveQuestions, GameManagerScript.GetCurrentPlayer(), gameId, GetGameMode()); } } public List GetQuestionIdsInAnswerLine() { List questionIds = new List(); foreach (AnswerLineQuestionCard q in contentPanel.GetComponentsInChildren()) { questionIds.Add(q.GetId()); } return questionIds; } public void RemoveEverythingFromAnswerline() { foreach (Transform child in contentPanel.transform) { Destroy(child.gameObject); } } public void RemoveUnlockedQuestions() { int lostQuestions = 0; for (int i = 0; i < contentPanel.childCount; i++) { AnswerLineQuestionCard q = contentPanel.GetChild(i).GetComponent(); if (q is AnswerLineQuestionCard && !q.IsQuestionSafe()) { lostQuestions++; Destroy(q); Destroy(q.gameObject); } } gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer); statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.GetComponents().Length); } void TimerRunOutEvent() { // Should be moved to some gameController GenericDialog dialog = GenericDialog.Instance(); string message = LocalizationManager.Instance.GetText("TIMER_DIALOG_MESSAGE"); dialog.SetTitle(LocalizationManager.Instance.GetText("TIMER_DIALOG_TITLE")); dialog.SetMessage(String.Format(message, GetUnlockedQuestionCount())); dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => { RemoveUnlockedQuestions(); ts.ResetTimer(); dialog.Hide(); NextPlayer(); }); dialog.SetOnDecline("", () => dialog.Hide()); dialog.Show(); } public void NextPlayer() { for (int i = 0; i < players.Count; i++) { if (players[i].Key.Equals(currentPlayer, StringComparison.InvariantCultureIgnoreCase)) { if (i + 1 < players.Count) { currentPlayer = players[i + 1].Key; break; } else { currentPlayer = players[0].Key; statsScript.IncreaseRoundValue(); break; } } } GenericDialog dialog = GenericDialog.Instance(); dialog.SetTitle(LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_TITLE")); string message = LocalizationManager.Instance.GetText("NEXT_PLAYER_DIALOG_MESSAGE"); dialog.SetMessage(String.Format(message, currentPlayer)); dialog.SetOnAccept(LocalizationManager.Instance.GetText("OK"), () => { RemoveEverythingFromAnswerline(); List questions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode()); SetQuestionsInAnswerLine(questions); statsScript.SetQuestionsLost(Database.Instance.GetQuestionsLost(gameManagerScript.GameId, currentPlayer, GetGameMode())); statsScript.MakeBold(currentPlayer); Database.Instance.SetCurrentPlayer(gameId, currentPlayer, GetGameMode()); dialog.Hide(); }); dialog.SetOnDecline("", () => dialog.Hide()); dialog.Show(); } }