| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 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<KeyValuePair<string, int>> 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<StatsScript>();
- gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
- ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
- players = gameManagerScript.GetPlayers();
- EventManager.StartListening("TimerEvent", TimerRunOutEvent);
- gameId = gameManagerScript.GameId;
- currentPlayer = Database.Instance.GetCurrentPlayer(gameId, GetGameMode());
- ips = GameObject.Find("InformationPanel").GetComponent<InformationPanelScript>();
- ips.SetCurrentPlayer(currentPlayer);
- statsScript.MakeBold(currentPlayer);
- List<NewQuestionData> answerlineQuestions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, GetGameMode());
- //if (answerlineQuestions.Count > 0) {
- 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<NewQuestionData> 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<int, string> 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<QuestionCard>();
- answerLineQuestionCard.SetAnswerText(q.Answer);
- answerLineQuestionCard.SetQuestionText(q.Question);
- answerLineQuestionCard.SetId(q.Id);
- answerLineQuestionCard.SetQuestionSafe();
- if (gameMode.Equals("Online")) {
- if (signedInUser.Value.Equals(currentPlayer)) {
- 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<NewDropZoneScript>();
- 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<QuestionCard>();
- if (!qc.IsQuestionSafe()) {
- unlockedQuestionCount++;
- }
- }
- return unlockedQuestionCount;
- }
- public void SetAllQuestionsLocked(bool needsSave) {
- List<int> saveQuestions = new List<int>();
- AnswerLineQuestionCard[] answerLineQuestionCards = contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>();
- 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<int> GetQuestionIdsInAnswerLine() {
- List<int> questionIds = new List<int>();
- foreach (AnswerLineQuestionCard q in contentPanel.GetComponentsInChildren<AnswerLineQuestionCard>()) {
- 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<AnswerLineQuestionCard>();
- if (q is AnswerLineQuestionCard && !q.IsQuestionSafe()) {
- lostQuestions++;
- Destroy(q);
- Destroy(q.gameObject);
- }
- }
- gameManagerScript.UpdateQuestiosLost(lostQuestions, currentPlayer);
- statsScript.SetQuestionsInAnswerLine(currentPlayer, contentPanel.childCount - lostQuestions);
- }
-
- 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"), () => {
- if (PlayerPrefs.GetString("GameMode").Equals("Local")) {
- RemoveUnlockedQuestions();
- ts.ResetTimer();
- dialog.Hide();
- NextPlayer();
- } else {
- // TODO online
- }
- });
- 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<NewQuestionData> 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());
- ips.SetCurrentPlayer(currentPlayer);
- dialog.Hide();
- });
- dialog.SetOnDecline("", () => dialog.Hide());
- dialog.Show();
- }
- }
|