using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class AnswerLineInfoScript : MonoBehaviour { [SerializeField] Button nextPlayerButton; [SerializeField] Button prevPlayerButton; [SerializeField] Text lockedQuestionsText; [SerializeField] Text unlockedQuestionsText; [SerializeField] GameObject scrollView; [SerializeField] GameObject answerLine; [SerializeField] GameObject newQuestion; [SerializeField] GameObject gameManager; private List> players; private string currentPlayerShown; private String signedInName; private void Start() { nextPlayerButton.onClick.AddListener(ShowNextPlayerAnswerLine); prevPlayerButton.onClick.AddListener(ShowPrevPlayerAnswerLine); currentPlayerShown = GameManagerScript.GetCurrentPlayer(); signedInName = Database.Instance.GetSignedInUser().Value; lockedQuestionsText.resizeTextMaxSize = unlockedQuestionsText.resizeTextMaxSize; } private void Update() { UpdateLockedQuestionsText(); UpdateUnlockedQuestionsText(); UpdateButtonsText(); SetQuestionClickable(); } private void SetQuestionClickable() { if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) { newQuestion.GetComponent().BackClickable = true; } else { newQuestion.GetComponent().BackClickable = false; } } public void UpdateButtonsText() { if (players == null) { players = gameManager.GetComponent().GetPlayers(); } if (players.Count <= 1) { nextPlayerButton.gameObject.SetActive(false); prevPlayerButton.gameObject.SetActive(false); } else { nextPlayerButton.gameObject.SetActive(true); prevPlayerButton.gameObject.SetActive(true); if (currentPlayerShown == null) { currentPlayerShown = signedInName; } string playerBaseText = LocalizationManager.Instance.GetText("ANSWERLINE_OTHER_PLAYER"); for (int i = 0; i < players.Count; i++) { if (players[i].Key.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) { if (i + 1 < players.Count) { if (players[i+1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { nextPlayerButton.GetComponent().PlayerName = LocalizationManager.Instance.GetText("YOUR"); } else { nextPlayerButton.GetComponent().PlayerName = players[i + 1].Key + "s"; } nextPlayerButton.GetComponentInChildren().text = String.Format(playerBaseText, nextPlayerButton.GetComponent().PlayerName); if (i - 1 >= 0) { if (players[i-1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { prevPlayerButton.GetComponent().PlayerName = LocalizationManager.Instance.GetText("YOUR"); } else { prevPlayerButton.GetComponent().PlayerName = players[i - 1].Key + "s"; } } else { if (players[players.Count - 1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { prevPlayerButton.GetComponent().PlayerName = LocalizationManager.Instance.GetText("YOUR"); } else { prevPlayerButton.GetComponent().PlayerName = players[players.Count-1].Key + "s"; } } prevPlayerButton.GetComponentInChildren().text = String.Format(playerBaseText, prevPlayerButton.GetComponent().PlayerName); break; } else { if (players[0].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { nextPlayerButton.GetComponent().PlayerName = LocalizationManager.Instance.GetText("YOUR"); } else { nextPlayerButton.GetComponent().PlayerName = players[0].Key + "s"; } nextPlayerButton.GetComponentInChildren().text = String.Format(playerBaseText, nextPlayerButton.GetComponent().PlayerName); if (players[i-1].Key.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { prevPlayerButton.GetComponent().PlayerName = LocalizationManager.Instance.GetText("YOUR"); } else { prevPlayerButton.GetComponent().PlayerName = players[i-1].Key; } prevPlayerButton.GetComponentInChildren().text = String.Format(playerBaseText, prevPlayerButton.GetComponent().PlayerName); break; } } } } } private void UpdateUnlockedQuestionsText() { int unlockedQuestionCount = scrollView.GetComponent().GetUnlockedQuestionCount(); if (unlockedQuestionCount <= 0) { unlockedQuestionsText.gameObject.SetActive(false); } else { unlockedQuestionsText.gameObject.SetActive(true); unlockedQuestionsText.text = String.Format( LocalizationManager.Instance.GetText( unlockedQuestionsText.GetComponent().key), unlockedQuestionCount); } } private void UpdateLockedQuestionsText() { ScrollViewScript scrollViewScript = scrollView.GetComponent(); int lockedQuestionsCount = scrollViewScript.GetQuestionIdsInAnswerLine().Count - scrollViewScript.GetUnlockedQuestionCount(); String playerText; if (currentPlayerShown == null || currentPlayerShown.Equals(signedInName, StringComparison.InvariantCultureIgnoreCase)) { playerText = LocalizationManager.Instance.GetText("YOU"); } else { playerText = currentPlayerShown; } if (lockedQuestionsText != null){ lockedQuestionsText.text = String.Format( LocalizationManager.Instance.GetText( lockedQuestionsText.GetComponent().key), playerText, lockedQuestionsCount); } } private void ShowNextPlayerAnswerLine() { GameManagerScript gameManagerScript = gameManager.GetComponent(); if (nextPlayerButton.GetComponent().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) { currentPlayerShown = signedInName; } else { currentPlayerShown = nextPlayerButton.GetComponent().PlayerName; currentPlayerShown = currentPlayerShown.Substring(0,currentPlayerShown.Length - 1); } List playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode); setQuestionFrosting(playerQuestions); UpdateButtonsText(); } private void ShowPrevPlayerAnswerLine() { GameManagerScript gameManagerScript = gameManager.GetComponent(); if (prevPlayerButton.GetComponent().PlayerName.Equals(LocalizationManager.Instance.GetText("YOUR"))) { currentPlayerShown = signedInName; } else { currentPlayerShown = prevPlayerButton.GetComponent().PlayerName; currentPlayerShown = currentPlayerShown.Substring(0, currentPlayerShown.Length - 1); } List playerQuestions = Database.Instance.GetPlayerQuestions(gameManagerScript.GameId, currentPlayerShown, gameManagerScript.GameMode); setQuestionFrosting(playerQuestions); UpdateButtonsText(); } private void setQuestionFrosting(List questions) { ScrollViewScript scrollViewScript = scrollView.GetComponent(); scrollViewScript.RemoveEverythingFromAnswerline(); scrollViewScript.SetQuestionsInAnswerLine(questions); //if (GameManagerScript.GetCurrentPlayer().Equals(currentPlayerShown)) { if (Database.Instance.GetSignedInUser().Value.Equals(currentPlayerShown, StringComparison.InvariantCultureIgnoreCase)) { scrollViewScript.SetQuestionsFrosted(false); } else { scrollViewScript.SetQuestionsFrosted(true); } } }