using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class StatsScript : MonoBehaviour { public GameObject statsNames; public GameObject statsValues; public GameObject statsLinePrefab; private readonly List statLines = new List(); private string roundText; private string questionsLostText; private readonly string gameMode; private string currenPlayer; [SerializeField] GameObject gameManager; [SerializeField] GameObject answerLine; [SerializeField] GameObject answerLineInfo; GameManagerScript gameManagerScript; AnswerLineInfoScript answerLineInfoScript; ScrollViewScript svc; private void Start() { gameManagerScript = gameManager.GetComponent(); answerLineInfoScript = answerLineInfo.GetComponent(); svc = answerLine.transform.parent.parent.GetComponent(); roundText = LocalizationManager.Instance.GetText("ROUND"); questionsLostText = LocalizationManager.Instance.GetText("QUESTIONS_LOST"); currenPlayer = GameManagerScript.GetCurrentPlayer(); CreateStandardStats(); AddPlayersToStats(Database.Instance.GetPlayersForGame(gameManagerScript.GameId, gameManagerScript.GameMode)); } void Update() { UpdatePlayerAnswer(); } private void UpdatePlayerAnswer() { String currentPlayerShown = answerLineInfoScript.GetCurrentPlayerShown(); int answerCount = svc.GetQuestionIdsInAnswerLine().Count; SetQuestionsInAnswerLine(currentPlayerShown, answerCount); } private void CreateStandardStats() { StatsLine round = CreateStatLine(); round.SetStatName(roundText); int roundValue = Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode); round.SetLocalizationKey("ROUND"); round.SetStatValue(roundValue); round.name = "roundStat"; StatsLine lostQuestions = CreateStatLine(); lostQuestions.SetStatName(questionsLostText); lostQuestions.SetStatValue(0); lostQuestions.name = "questionsLost"; lostQuestions.SetLocalizationKey("QUESTIONS_LOST"); StatsLine playerTitle = CreateStatLine(); playerTitle.SetStatName(LocalizationManager.Instance.GetText("PLAYERS")); playerTitle.SetStatValue(""); playerTitle.MakeBold(); playerTitle.GetComponent().sizeDelta = new Vector2(playerTitle.GetComponent().rect.width, 50f); playerTitle.SetLocalizationKey("PLAYERS"); statLines.Add(round); statLines.Add(lostQuestions); statLines.Add(playerTitle); } private StatsLine CreateStatLine() { GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject; StatsLine statLine = slp.GetComponent(); statLine.transform.SetParent(this.transform, false); return statLine; } public void AddPlayersToStats(List> players) { foreach (KeyValuePair player in players) { StatsLine p = CreateStatLine(); p.RemoveLocalization(); p.SetStatName(player.Key); p.SetStatValue(player.Value); statLines.Add(p); } } public void IncreaseRoundValue() { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals(roundText)) { Int32.TryParse(sl.GetValue(), out int round); round++; sl.SetStatValue(round); Database.Instance.SetRoundValue(gameManagerScript.GameId, round, gameManagerScript.GameMode); break; } } } public void MakeBold(string playerName) { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals(playerName)) { sl.MakeBold(); } else { sl.UnBold(); } } } public void SetQuestionsInAnswerLine(string playerName, int count) { foreach (StatsLine sl in statLines) { if (sl.GetName().Equals(playerName)) { sl.SetStatValue(count); break; } } } public void SetQuestionsLost(int value) { StatsLine sl = statLines.Find(s => s.GetName().Equals(questionsLostText)); sl.SetStatValue(sl.GetIntValue() + value); } }