Merge branch 'WES-88-Scoreboard' into 'development'

Resolve WES-88 "Scoreboard"

See merge request wesign/unity-application!66
This commit was merged in pull request #66.
This commit is contained in:
Jelle De Geest
2023-03-26 18:01:52 +00:00
4 changed files with 4126 additions and 13 deletions

View File

@@ -15,6 +15,7 @@ MonoBehaviour:
title: Groenten en Fruit
description: Van kers tot pompoen
index: 3
model: {fileID: 0}
learnables:
- name: Aardappel
image: {fileID: 21300000, guid: 2610cdbc24a125f43ada7fed67d8f51b, type: 3}
@@ -79,6 +80,6 @@ MonoBehaviour:
- name: Tomaat
image: {fileID: 21300000, guid: e16943962d961d948a0a6dc73ebf2b0b, type: 3}
clip: {fileID: 32900000, guid: 7ca3dbd42b3033341b3c5c51059643f6, type: 3}
- name: Wrotel
- name: Wortel
image: {fileID: 21300000, guid: 8962e03659a079546810c3ad9c8a211c, type: 3}
clip: {fileID: 32900000, guid: ef9554901c0c6784e9102d5c1fd28a9a, type: 3}

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -72,11 +73,6 @@ public class JustSignController : MonoBehaviour
/// </summary>
public Transform symbolContainer;
/// <summary>
/// All of the available themes
/// </summary>
private ThemeList themeList;
/// <summary>
/// The theme we are currently using
/// </summary>
@@ -177,11 +173,122 @@ public class JustSignController : MonoBehaviour
/// </summary>
private float lastSymbolTime;
/// <summary>
/// Counter that keeps track of how many signs get you score "perfect"
/// </summary>
private int perfectSigns;
/// <summary>
/// Counter that keeps track of how many signs get you score "good"
/// </summary>
private int goodSigns;
/// <summary>
/// Counter that keeps track of how many signs get you score "meh"
/// </summary>
private int mehSigns;
/// <summary>
/// Counter that keeps track of how many signs get you score "perfect"
/// </summary>
private int terribleSigns;
/// <summary>
/// Counter that keeps track of how many signs done incorrectly
/// </summary>
private int incorrectSigns;
/// <summary>
/// Reference to the scoreboard entries container
/// </summary>
public Transform scoreboardEntriesContainer;
/// <summary>
/// The GameObjects representing the letters
/// </summary>
private List<GameObject> scoreboardEntries = new List<GameObject>();
/// <summary>
/// Reference to the ScoreboardEntry prefab
/// </summary>
public GameObject scoreboardEntry;
/// <summary>
/// Reference to the user list to access the current user
/// </summary>
public UserList userList;
/// <summary>
/// Reference to the current user
/// </summary>
private User user;
/// <summary>
/// LPM
/// </summary>
public TMP_Text lpmText;
/// <summary>
/// Perfect Signs Score
/// </summary>
public TMP_Text perfectSignsText;
/// <summary>
/// Good Signs Score
/// </summary>
public TMP_Text goodSignsText;
/// <summary>
/// Meh Signs Score
/// </summary>
public TMP_Text mehSignsText;
/// <summary>
/// Perfect Signs Score
/// </summary>
public TMP_Text terribleSignsText;
/// <summary>
/// Signs that were not found
/// </summary>
public TMP_Text notFoundSignsText;
/// <summary>
/// Score
/// </summary>
public TMP_Text scoreText;
/// <summary>
/// Reference to the gameEnded panel, so we can update its display
/// </summary>
public GameObject gameEndedPanel;
/// <summary>
/// Start is called before the first frame update
/// </summary>
void Start()
public void Start()
{
perfectSigns = 0;
goodSigns = 0;
mehSigns = 0;
terribleSigns = 0;
incorrectSigns = 0;
score = 0;
gameEndedPanel.SetActive(false);
// Create entry in current user for keeping track of progress
userList.Load();
user = userList.GetCurrentUser();
Progress progress = user.GetMinigameProgress(minigame.index);
if (progress == null)
{
progress = new Progress();
progress.AddOrUpdate<MinigameIndex>("minigameIndex", minigame.index);
progress.AddOrUpdate<List<Score>>("highestScores", new List<Score>());
progress.AddOrUpdate<List<Score>>("latestScores", new List<Score>());
user.minigames.Add(progress);
}
userList.Save();
scoreDisplay.text = "Score: " + score.ToString();
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
words.AddRange(currentTheme.learnables);
@@ -221,15 +328,19 @@ public class JustSignController : MonoBehaviour
int difference = Math.Abs((int) (activeSymbols[matchedSymbolIndex].transform.position.x - hitZone.transform.position.x));
if (difference < perfectBoundary) {
feedBack.text = "Perfect!";
perfectSigns++;
score += perfectScore;
} else if (difference < goodBoundary) {
feedBack.text = "Good!";
goodSigns++;
score += goodScore;
} else if (difference < mehBoundary) {
feedBack.text = "Meh...";
mehSigns++;
score += mehScore;
} else {
feedBack.text = "Terrible!";
terribleSigns++;
score += terribleScore;
}
@@ -241,7 +352,8 @@ public class JustSignController : MonoBehaviour
if (activeSymbols.Count > 0) {
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
DestroySymbolAt(0);
feedBack.text = "Terrible!";
incorrectSigns++;
feedBack.text = "Te laat!";
score += offscreenScore;
}
}
@@ -255,11 +367,7 @@ public class JustSignController : MonoBehaviour
// Check if the song has ended and activate scorescreen if it has
if (currentTime - beginTime > currentSong.duration) {
gameIsActive = false;
while (activeSymbols.Count > 0) {
DestroySymbolAt(0);
}
// TODO: Scoreboard
ActivateEnd();
}
// Move all active symbols to the right
@@ -272,6 +380,23 @@ public class JustSignController : MonoBehaviour
}
}
/// <summary>
/// Display Scoreboard + Metrics
/// </summary>
public void ActivateEnd()
{
gameIsActive = false;
while (activeSymbols.Count > 0)
{
DestroySymbolAt(0);
}
// TODO: Scoreboard
SaveScores();
SetScoreMetrics();
SetScoreBoard();
gameEndedPanel.SetActive(true);
}
/// <summary>
/// Destroy the symbol at the given index
/// </summary>
@@ -315,4 +440,169 @@ public class JustSignController : MonoBehaviour
activeWords.Add(nextSymbol);
activeSymbols.Add(newSymbolObject);
}
/// <summary>
/// Update and save the scores
/// </summary>
public void SaveScores()
{
// Calculate new score
int newScore = this.score;
// Save the score as a tuple: < int score, string time ago>
Score score = new Score();
score.scoreValue = newScore;
score.time = DateTime.Now.ToString();
// Save the new score
user = userList.GetCurrentUser();
Progress progress = user.GetMinigameProgress(minigame.index);
// Get the current list of scores
List<Score> latestScores = progress.Get<List<Score>>("latestScores");
List<Score> highestScores = progress.Get<List<Score>>("highestScores");
// Add the new score
latestScores.Add(score);
highestScores.Add(score);
// Sort the scores
highestScores.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>>("latestScores", latestScores.Take(10).ToList());
progress.AddOrUpdate<List<Score>>("highestScores", highestScores.Take(10).ToList());
userList.Save();
}
/// <summary>
/// Set score metrics
/// </summary>
private void SetScoreMetrics()
{
// In de zone
perfectSignsText.text = perfectSigns.ToString();
// Aanvaardbaar
goodSignsText.text = goodSigns.ToString();
// Nipt
mehSignsText.text = mehSigns.ToString();
// Slechte timing
terribleSignsText.text = terribleSigns.ToString();
// Niet Geraden
notFoundSignsText.text = incorrectSigns.ToString();
// LPM
int duration = songList.songs[songList.currentSongIndex].duration;
int correctSigns = goodSigns + perfectSigns + mehSigns + terribleSigns;
lpmText.text = (60f * correctSigns / duration).ToString("#") + " GPM";
// Score
scoreText.text = "Score: " + score.ToString();
}
/// <summary>
/// Sets the scoreboard
/// </summary>
private void SetScoreBoard()
{
// Clean the previous scoreboard entries
for (int i = 0; i < scoreboardEntries.Count; i++)
{
Destroy(scoreboardEntries[i]);
}
scoreboardEntries.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 progress = user.GetMinigameProgress(minigame.index);
if (progress != null)
{
// Add scores to dictionary
List<Score> scores = progress.Get<List<Score>>("highestScores");
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, scoreboardEntriesContainer);
scoreboardEntries.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++;
}
}
}

View File

@@ -370,6 +370,8 @@ public partial class GameController : MonoBehaviour
/// <param name="successful">true if the letter was correctly signed, false otherwise</param>
public void NextLetter(bool successful)
{
if (gameEnded) { return; }
// Change color of current letter (skip spaces)
if (successful)
{