Resolve WES-47 "Final result view"
This commit is contained in:
@@ -1,105 +1,221 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GameController : MonoBehaviour
|
||||
{
|
||||
// All of the words that can be used in this session
|
||||
/// <summary>
|
||||
/// All of the words that can be used in this session
|
||||
/// </summary>
|
||||
private string[] words;
|
||||
|
||||
// Where we currently are in the word
|
||||
/// <summary>
|
||||
/// Where we currently are in the word
|
||||
/// </summary>
|
||||
private int letterIndex;
|
||||
|
||||
// Where we currently are in the word list
|
||||
/// <summary>
|
||||
/// Where we currently are in the word list
|
||||
/// </summary>
|
||||
private int wordIndex;
|
||||
|
||||
// The word that is currently being spelled
|
||||
/// <summary>
|
||||
/// The word that is currently being spelled
|
||||
/// </summary>
|
||||
private string currentWord;
|
||||
|
||||
// All of the available themes
|
||||
/// <summary>
|
||||
/// All of the available themes
|
||||
/// </summary>
|
||||
private ThemeList themeList;
|
||||
|
||||
// The theme we are currently using
|
||||
/// <summary>
|
||||
/// The theme we are currently using
|
||||
/// </summary>
|
||||
private Theme currentTheme;
|
||||
|
||||
// The input field for testing purposes
|
||||
public TMP_InputField input;
|
||||
|
||||
// Current value of timer in seconds
|
||||
/// <summary>
|
||||
/// Current value of timer in seconds
|
||||
/// </summary>
|
||||
private float timerValue;
|
||||
|
||||
// "Game over" or "You win!"
|
||||
/// <summary>
|
||||
/// "Game over" or "You win!"
|
||||
/// </summary>
|
||||
public TMP_Text endText;
|
||||
|
||||
// First score display
|
||||
public TMP_Text correctWordsText;
|
||||
/// <summary>
|
||||
/// LPM
|
||||
/// </summary>
|
||||
public TMP_Text lpmText;
|
||||
|
||||
// Second score display
|
||||
public TMP_Text correctLettersText;
|
||||
/// <summary>
|
||||
/// Letters ( right | wrong )
|
||||
/// </summary>
|
||||
public TMP_Text lettersRightText;
|
||||
public TMP_Text lettersWrongText;
|
||||
|
||||
// The game over panel
|
||||
/// <summary>
|
||||
/// Letters
|
||||
/// </summary>
|
||||
public TMP_Text lettersText;
|
||||
|
||||
/// <summary>
|
||||
/// Accuracy
|
||||
/// </summary>
|
||||
public TMP_Text accuracyText;
|
||||
|
||||
/// <summary>
|
||||
/// Words
|
||||
/// </summary>
|
||||
public TMP_Text wordsText;
|
||||
|
||||
/// <summary>
|
||||
/// Time
|
||||
/// </summary>
|
||||
public TMP_Text timeText;
|
||||
|
||||
/// <summary>
|
||||
/// Score
|
||||
/// </summary>
|
||||
public TMP_Text scoreText;
|
||||
|
||||
/// <summary>
|
||||
/// The game over panel
|
||||
/// </summary>
|
||||
public GameObject gameEndedPanel;
|
||||
|
||||
// Button for restarting the game
|
||||
/// <summary>
|
||||
/// Button for restarting the game
|
||||
/// </summary>
|
||||
public Button replayButton;
|
||||
|
||||
// Indicates if the game is still going
|
||||
/// <summary>
|
||||
/// Indicates if the game is still going
|
||||
/// </summary>
|
||||
private bool gameEnded;
|
||||
|
||||
// Amount of seconds user gets per letter of the current word
|
||||
// Set to 1 for testing; should be increased later
|
||||
/// <summary>
|
||||
/// Amount of seconds user gets per letter of the current word
|
||||
/// Set to 1 for testing; should be increased later
|
||||
/// </summary>
|
||||
private int secondsPerLetter = 1;
|
||||
|
||||
// Counter that keeps track of how many words have been spelled correctly
|
||||
private int spelledLetters;
|
||||
/// <summary>
|
||||
/// Counter that keeps track of how many letters have been spelled correctly
|
||||
/// </summary>
|
||||
private int correctLetters;
|
||||
|
||||
// Counter that keeps track of how many letters have been spelled correctly
|
||||
/// <summary>
|
||||
/// Counter that keeps track of how many letters have been spelled incorrectly
|
||||
/// </summary>
|
||||
private int incorrectLetters;
|
||||
|
||||
/// <summary>
|
||||
/// Counter that keeps track of how many words have been spelled correctly
|
||||
/// </summary>
|
||||
private int spelledWords;
|
||||
|
||||
/// <summary>
|
||||
/// Timer that keeps track of when the game was started
|
||||
/// </summary>
|
||||
private DateTime startTime;
|
||||
|
||||
|
||||
[Header("User")]
|
||||
// Reference to the user list to access the current user
|
||||
/// <summary>
|
||||
/// Reference to the user list to access the current user
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
// Reference to the current user
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the current user
|
||||
/// </summary>
|
||||
private User user;
|
||||
// Reference to the minigame progress of the current user
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the minigame progress of the current user
|
||||
/// </summary>
|
||||
private Progress progress = null;
|
||||
|
||||
[Header("Minigame")]
|
||||
// Reference to the minigame ScriptableObject
|
||||
/// <summary>
|
||||
/// Reference to the minigame ScriptableObject
|
||||
/// </summary>
|
||||
public Minigame minigame;
|
||||
|
||||
[Header("Letter prefab")]
|
||||
// Letter prefab
|
||||
|
||||
/// <summary>
|
||||
/// Letter prefab
|
||||
/// </summary>
|
||||
public GameObject letterPrefab;
|
||||
|
||||
[Header("UI References")]
|
||||
// Reference to letter prefab
|
||||
/// <summary>
|
||||
/// Reference to letter prefab
|
||||
/// </summary>
|
||||
public Transform letterContainer;
|
||||
// The Image component for displaying the appropriate sprite
|
||||
|
||||
/// <summary>
|
||||
/// The Image component for displaying the appropriate sprite
|
||||
/// </summary>
|
||||
public Image wordImage;
|
||||
// Timer display
|
||||
|
||||
/// <summary>
|
||||
/// Timer display
|
||||
/// </summary>
|
||||
public TMP_Text timerText;
|
||||
|
||||
[Header("Private Variables")]
|
||||
// The GameObjects representing the letters
|
||||
/// <summary>
|
||||
/// The GameObjects representing the letters
|
||||
/// </summary>
|
||||
private List<GameObject> letters = new List<GameObject>();
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the scoreboard
|
||||
/// </summary>
|
||||
public Transform Scoreboard;
|
||||
|
||||
// Start is called before the first frame update
|
||||
/// <summary>
|
||||
/// Reference to the entries grid
|
||||
/// </summary>
|
||||
public Transform EntriesGrid;
|
||||
|
||||
/// <summary>
|
||||
/// The GameObjects representing the letters
|
||||
/// </summary>
|
||||
private List<GameObject> entries = new List<GameObject>();
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the ScoreboardEntry prefab
|
||||
/// </summary>
|
||||
public GameObject scoreboardEntry;
|
||||
|
||||
/// <summary>
|
||||
/// Score class TODO: Move to separate file
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class Score
|
||||
{
|
||||
public int scoreValue;
|
||||
public string time;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Start is called before the first frame update
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
spelledLetters = 0;
|
||||
correctLetters = 0;
|
||||
incorrectLetters = 0;
|
||||
// We use -1 instead of 0 so SetNextWord can simply increment it each time
|
||||
spelledWords = -1;
|
||||
gameEnded = false;
|
||||
wordIndex = 0;
|
||||
input.text = "";
|
||||
timerValue = 0.0f;
|
||||
startTime = DateTime.Now;
|
||||
gameEndedPanel.SetActive(false);
|
||||
replayButton.onClick.AddListener(Start);
|
||||
|
||||
@@ -110,12 +226,12 @@ public class GameController : MonoBehaviour
|
||||
{
|
||||
progress = new Progress();
|
||||
progress.AddOrUpdate<MinigameIndex>("minigameIndex", MinigameIndex.SPELLING_BEE);
|
||||
// TODO: add progress we want to keep track off
|
||||
// (for example 'highscore')
|
||||
progress.AddOrUpdate<int>("highscore", 0);
|
||||
progress.AddOrUpdate<List<Score>>("scores", new List<Score>());
|
||||
user.minigames.Add(progress);
|
||||
}
|
||||
userList.Save();
|
||||
|
||||
DeleteWord();
|
||||
|
||||
// TODO: change to ScriptableObject
|
||||
@@ -126,14 +242,36 @@ public class GameController : MonoBehaviour
|
||||
SetNextWord();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
/// <summary>
|
||||
/// Update is called once per frame
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
if (!gameEnded)
|
||||
{
|
||||
if (input.text.Length > 0)
|
||||
// Get keyboard input
|
||||
// Check if the correct char has been given as input
|
||||
foreach (char c in Input.inputString)
|
||||
{
|
||||
CheckChar(input.text[0]);
|
||||
if (Char.ToUpper(c) == Char.ToUpper(currentWord[letterIndex]))
|
||||
{
|
||||
// correct letter
|
||||
letters[letterIndex].GetComponent<Image>().color = Color.green;
|
||||
correctLetters++;
|
||||
letterIndex++;
|
||||
|
||||
if (letterIndex >= currentWord.Length)
|
||||
{
|
||||
DeleteWord();
|
||||
StartCoroutine(Wait());
|
||||
SetNextWord();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// incorrect letter
|
||||
incorrectLetters++;
|
||||
}
|
||||
}
|
||||
|
||||
timerValue -= Time.deltaTime;
|
||||
@@ -151,7 +289,9 @@ public class GameController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Randomly shuffle the list of words
|
||||
/// <summary>
|
||||
/// Randomly shuffle the list of words
|
||||
/// </summary>
|
||||
private void ShuffleWords()
|
||||
{
|
||||
for (int i = words.Length - 1; i > 0; i--)
|
||||
@@ -164,88 +304,232 @@ public class GameController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Displays the game over panel and score values
|
||||
/// <summary>
|
||||
/// Calculate the score
|
||||
/// </summary>
|
||||
/// <returns>The calculated score</returns>
|
||||
private int CalculateScore()
|
||||
{
|
||||
return spelledWords * 5 + correctLetters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set score metrics
|
||||
/// </summary>
|
||||
private void SetScoreMetrics()
|
||||
{
|
||||
// LPM
|
||||
TimeSpan duration = DateTime.Now.Subtract(startTime);
|
||||
lpmText.text = (60f * correctLetters / duration.TotalSeconds).ToString("#") + " LPM";
|
||||
|
||||
// Letters ( right | wrong ) total
|
||||
lettersRightText.text = correctLetters.ToString();
|
||||
lettersWrongText.text = incorrectLetters.ToString();
|
||||
lettersText.text = (correctLetters + incorrectLetters).ToString();
|
||||
|
||||
// Accuracy
|
||||
if (correctLetters + incorrectLetters > 0)
|
||||
{
|
||||
accuracyText.text = ((correctLetters) * 100f / (correctLetters + incorrectLetters)).ToString("#.##") + "%";
|
||||
} else
|
||||
{
|
||||
accuracyText.text = "-";
|
||||
}
|
||||
|
||||
|
||||
// Words
|
||||
wordsText.text = spelledWords.ToString();
|
||||
|
||||
// Time
|
||||
timeText.text = duration.ToString(@"mm\:ss");
|
||||
|
||||
// Score
|
||||
scoreText.text = "Score: " + CalculateScore().ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the game over panel and score values
|
||||
/// </summary>
|
||||
private void ActivateGameOver()
|
||||
{
|
||||
DeleteWord();
|
||||
endText.text = "GAME OVER";
|
||||
correctLettersText.text = "Correctly spelled letters: " + spelledLetters.ToString();
|
||||
correctWordsText.text = "Correctly spelled words: " + spelledWords.ToString();
|
||||
|
||||
SetScoreMetrics();
|
||||
|
||||
gameEndedPanel.SetActive(true);
|
||||
gameEndedPanel.transform.SetAsLastSibling();
|
||||
|
||||
gameEnded = true;
|
||||
|
||||
// Save the scores and show the scoreboard
|
||||
SaveScores();
|
||||
SetScoreBoard();
|
||||
}
|
||||
|
||||
// Display win screen
|
||||
/// <summary>
|
||||
/// Display win screen
|
||||
/// </summary>
|
||||
private void ActivateWin()
|
||||
{
|
||||
int score = words.Length;
|
||||
|
||||
// Update progress
|
||||
// TODO: update all tracked progress
|
||||
int highscore = progress.Get<int>("highscore");
|
||||
if (score < highscore)
|
||||
{
|
||||
progress.AddOrUpdate<int>("highsscore", score);
|
||||
userList.Save();
|
||||
}
|
||||
|
||||
// @lukas stuff
|
||||
DeleteWord();
|
||||
endText.text = "YOU WIN!";
|
||||
correctLettersText.text = "Your time: " + spelledLetters.ToString();
|
||||
int totalWordsDuration = 0;
|
||||
|
||||
foreach (string word in words)
|
||||
{
|
||||
totalWordsDuration += word.Length * secondsPerLetter + 1;
|
||||
}
|
||||
|
||||
// How much time was spent by the player
|
||||
int spentTime = totalWordsDuration - (int)timerValue;
|
||||
|
||||
int seconds = spentTime % 60;
|
||||
int minutes = spentTime / 60;
|
||||
|
||||
if (minutes == 0)
|
||||
{
|
||||
correctLettersText.text = "Your time: " + seconds + " seconds";
|
||||
}
|
||||
else
|
||||
{
|
||||
correctLettersText.text = "Your time: " + minutes + " minutes and " + seconds + " seconds";
|
||||
}
|
||||
|
||||
correctWordsText.text = "";
|
||||
SetScoreMetrics();
|
||||
|
||||
gameEndedPanel.SetActive(true);
|
||||
gameEndedPanel.transform.SetAsLastSibling();
|
||||
|
||||
gameEnded = true;
|
||||
|
||||
// Save the scores and show the scoreboard
|
||||
SaveScores();
|
||||
SetScoreBoard();
|
||||
}
|
||||
|
||||
// Check if the correct char has been given as input
|
||||
private void CheckChar(char letter)
|
||||
/// <summary>
|
||||
/// Update and save the scores
|
||||
/// </summary>
|
||||
private void SaveScores()
|
||||
{
|
||||
if (Char.ToUpper(letter) == Char.ToUpper(currentWord[letterIndex]) && input.text.Length == 1)
|
||||
{
|
||||
letters[letterIndex].GetComponent<Image>().color = Color.green;
|
||||
input.text = "";
|
||||
spelledLetters++;
|
||||
letterIndex++;
|
||||
// Calculate new score
|
||||
int newScore = spelledWords * 5 + correctLetters;
|
||||
// Save the score as a tuple: < int score, string time ago>
|
||||
Score score = new Score();
|
||||
score.scoreValue = newScore;
|
||||
score.time = DateTime.Now.ToString();
|
||||
|
||||
if (letterIndex >= currentWord.Length)
|
||||
// Save the new score
|
||||
user = userList.GetCurrentUser();
|
||||
progress = user.minigames.Find((p) => p != null && p.Get<MinigameIndex>("minigameIndex") == minigame.index);
|
||||
if (progress != null)
|
||||
{
|
||||
// Get the current list of scores
|
||||
List<Score> scores = progress.Get<List<Score>>("scores");
|
||||
|
||||
// Add the new score
|
||||
scores.Add(score);
|
||||
|
||||
// Sort the scores
|
||||
scores.Sort((a, b) => b.scoreValue.CompareTo(a.scoreValue));
|
||||
|
||||
// Only save the top 10 scores, so this list doesn't keep growing endlessly
|
||||
progress.AddOrUpdate<List<Score>>("scores", scores.Take(10).ToList());
|
||||
}
|
||||
|
||||
// Update the highscore
|
||||
int highscore = progress.Get<int>("highscore");
|
||||
if (score.scoreValue < highscore)
|
||||
{
|
||||
progress.AddOrUpdate<int>("highscore", score.scoreValue);
|
||||
}
|
||||
|
||||
userList.Save();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the scoreboard
|
||||
/// </summary>
|
||||
private void SetScoreBoard()
|
||||
{
|
||||
// Clean the previous scoreboard entries
|
||||
for (int i = 0; i < entries.Count; i++)
|
||||
{
|
||||
Destroy(entries[i]);
|
||||
}
|
||||
entries.Clear();
|
||||
|
||||
// Instantiate new entries
|
||||
// Get all scores from all users
|
||||
List<Tuple<string, Score>> allScores = new List<Tuple<string, Score>>();
|
||||
foreach (User user in userList.GetUsers())
|
||||
{
|
||||
// Get user's progress for this minigame
|
||||
progress = user.minigames.Find((p) => p != null && p.Get<MinigameIndex>("minigameIndex") == minigame.index);
|
||||
if (progress != null)
|
||||
{
|
||||
DeleteWord();
|
||||
StartCoroutine(Wait());
|
||||
SetNextWord();
|
||||
// Add scores to dictionary
|
||||
List<Score> scores = progress.Get<List<Score>>("scores");
|
||||
foreach (Score score in scores)
|
||||
{
|
||||
allScores.Add(new Tuple<string, Score>(user.username, score));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort allScores based on Score.scoreValue
|
||||
allScores.Sort((a, b) => b.Item2.scoreValue.CompareTo(a.Item2.scoreValue));
|
||||
|
||||
// Instantiate scoreboard entries
|
||||
int rank = 1;
|
||||
foreach (Tuple<string, Score> tup in allScores.Take(10))
|
||||
{
|
||||
string username = tup.Item1;
|
||||
Score score = tup.Item2;
|
||||
|
||||
GameObject entry = Instantiate(scoreboardEntry, EntriesGrid);
|
||||
entries.Add(entry);
|
||||
// Set the player icon
|
||||
entry.transform.Find("Image").GetComponent<Image>().sprite = userList.GetUserByUsername(username).avatar;
|
||||
|
||||
// Set the player name
|
||||
entry.transform.Find("PlayerName").GetComponent<TMP_Text>().text = username;
|
||||
|
||||
// Set the score
|
||||
entry.transform.Find("Score").GetComponent<TMP_Text>().text = score.scoreValue.ToString();
|
||||
|
||||
// Set the rank
|
||||
entry.transform.Find("Rank").GetComponent<TMP_Text>().text = rank.ToString();
|
||||
|
||||
// Set the ago
|
||||
// Convert the score.time to Datetime
|
||||
DateTime time = DateTime.Parse(score.time);
|
||||
DateTime currentTime = DateTime.Now;
|
||||
TimeSpan diff = currentTime.Subtract(time);
|
||||
|
||||
string formatted;
|
||||
if (diff.Days > 0)
|
||||
{
|
||||
formatted = $"{diff.Days}d ";
|
||||
}
|
||||
else if (diff.Hours > 0)
|
||||
{
|
||||
formatted = $"{diff.Hours}h ";
|
||||
}
|
||||
else if (diff.Minutes > 0)
|
||||
{
|
||||
formatted = $"{diff.Minutes}m ";
|
||||
}
|
||||
else
|
||||
{
|
||||
formatted = "now";
|
||||
}
|
||||
|
||||
entry.transform.Find("Ago").GetComponent<TMP_Text>().text = formatted;
|
||||
|
||||
|
||||
// Alternating colors looks nice
|
||||
if (rank % 2 == 0)
|
||||
{
|
||||
Image image = entry.transform.GetComponent<Image>();
|
||||
image.color = new Color(image.color.r, image.color.g, image.color.b, 0f);
|
||||
}
|
||||
|
||||
// Make new score stand out
|
||||
if (diff.TotalSeconds < 1)
|
||||
{
|
||||
Image image = entry.transform.GetComponent<Image>();
|
||||
image.color = new Color(0, 229, 255, 233);
|
||||
}
|
||||
|
||||
rank++;
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all letter objects
|
||||
/// <summary>
|
||||
/// Delete all letter objects
|
||||
/// </summary>
|
||||
private void DeleteWord()
|
||||
{
|
||||
for (int i = 0; i < letters.Count; i++)
|
||||
@@ -255,13 +539,20 @@ public class GameController : MonoBehaviour
|
||||
letters.Clear();
|
||||
}
|
||||
|
||||
// Adds seconds to timer
|
||||
/// <summary>
|
||||
/// Adds seconds to timer
|
||||
/// </summary>
|
||||
/// <param name="seconds"></param>
|
||||
private void AddSeconds(int seconds)
|
||||
{
|
||||
timerValue += (float)seconds;
|
||||
}
|
||||
|
||||
// Find the chosen theme by its name
|
||||
/// <summary>
|
||||
/// Find the chosen theme by its name
|
||||
/// </summary>
|
||||
/// <param name="themeName">The name of the theme to find</param>
|
||||
/// <returns>The requested theme</returns>
|
||||
private Theme FindThemeByName(string themeName)
|
||||
{
|
||||
int themeIndex = 0;
|
||||
@@ -280,7 +571,9 @@ public class GameController : MonoBehaviour
|
||||
return null;
|
||||
}
|
||||
|
||||
// Display next word in the series
|
||||
/// <summary>
|
||||
/// Display next word in the series
|
||||
/// </summary>
|
||||
private void SetNextWord()
|
||||
{
|
||||
spelledWords++;
|
||||
@@ -302,7 +595,10 @@ public class GameController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Displays the word that needs to be spelled
|
||||
/// <summary>
|
||||
/// Displays the word that needs to be spelled
|
||||
/// </summary>
|
||||
/// <param name="word">The word to display</param>
|
||||
private void DisplayWord(string word)
|
||||
{
|
||||
for (int i = 0; i < word.Length; i++)
|
||||
@@ -319,7 +615,10 @@ public class GameController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Change the image that is being displayed
|
||||
/// <summary>
|
||||
/// Change the image that is being displayed
|
||||
/// </summary>
|
||||
/// <param name="spriteName">Name of the new sprite</param>
|
||||
private void ChangeSprite(string spriteName)
|
||||
{
|
||||
// Load the new sprite from the Resources folder
|
||||
@@ -329,6 +628,10 @@ public class GameController : MonoBehaviour
|
||||
wordImage.sprite = sprite;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// wait for 2 seconds
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IEnumerator Wait()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(2);
|
||||
|
||||
Reference in New Issue
Block a user