| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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<StatsLine> statLines = new List<StatsLine>();
- 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<GameManagerScript>();
- answerLineInfoScript = answerLineInfo.GetComponent<AnswerLineInfoScript>();
- svc = answerLine.transform.parent.parent.GetComponent<ScrollViewScript>();
- 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<RectTransform>().sizeDelta = new Vector2(playerTitle.GetComponent<RectTransform>().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<StatsLine>();
- statLine.transform.SetParent(this.transform, false);
- return statLine;
- }
- public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
- foreach (KeyValuePair<string, int> 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);
- }
- }
|