Resolve WES-132-Common-Abstract-Minigame

This commit is contained in:
Jerome Coudron
2023-04-23 21:20:12 +00:00
committed by Jelle De Geest
parent 4fdb8f95cb
commit 966475455a
30 changed files with 1046 additions and 1078 deletions

View File

@@ -1,8 +1,6 @@
using DigitalRuby.Tween;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
@@ -11,7 +9,7 @@ using Random = UnityEngine.Random;
/// <summary>
/// Contains all game logic for the JustSign game
/// </summary>
public class JustSignController : AbstractFeedback
public class JustSignController : AbstractMinigameController
{
/// <summary>
/// All of the words that can be used in this session
@@ -33,17 +31,6 @@ public class JustSignController : AbstractFeedback
/// </summary>
public TMP_Text scoreDisplay;
/// <summary>
/// Reference to the minigame ScriptableObject
/// </summary>
public Minigame minigame;
/// <summary>
/// We keep the minigamelist as well so that the minigame-index doesn't get reset
/// DO NOT REMOVE
/// </summary>
public MinigameList minigamelist;
/// </summary>
/// Reference to the list of available songs
/// </summary>
@@ -69,11 +56,6 @@ public class JustSignController : AbstractFeedback
/// </summary>
public RectTransform hitZoneMeh;
/// <summary>
/// Reference to the webcam
/// </summary>
public RawImage webcamScreen;
/// <summary>
/// Score obtained when getting a perfect hit
/// </summary>
@@ -124,11 +106,6 @@ public class JustSignController : AbstractFeedback
/// </summary>
private List<GameObject> activeSymbols = new List<GameObject>();
/// <summary>
/// Have the symbols started spawning or not
/// </summary>
private bool gameIsActive = false;
/// <summary>
/// Controls movement speed of symbols (higher -> faster)
/// </summary>
@@ -184,26 +161,6 @@ public class JustSignController : AbstractFeedback
/// </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 current user
/// </summary>
private User user;
/// <summary>
/// LPM
/// </summary>
@@ -239,21 +196,11 @@ public class JustSignController : AbstractFeedback
/// </summary>
public TMP_Text scoreText;
/// <summary>
/// Reference to the gameEnded panel, so we can update its display
/// </summary>
public GameObject gameEndedPanel;
/// <summary>
/// Reference to the feedback field
/// </summary>
public TMP_Text feedbackText;
/// <summary>
/// Reference to the progress bar
/// </summary>
public Slider feedbackProgressBar;
/// <summary>
/// Reference to the progress bar image, so we can add fancy colors
/// </summary>
@@ -299,53 +246,9 @@ public class JustSignController : AbstractFeedback
/// </summary>
public GameObject userFeedback;
/// <summary>
/// Start is called before the first frame update
/// </summary>
public void Start()
protected override Theme signPredictorTheme
{
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
signPredictor.SetModel(currentTheme.modelIndex);
signPredictor.SwapScreen(webcamScreen);
AddSelfAsListener();
StartController();
}
/// <summary>
/// Holds the game-specific logic to start the controller
/// </summary>
public void StartController()
{
userFeedback.SetActive(currentTheme.modelIndex != ModelIndex.NONE);
previewMessage.SetActive(currentTheme.modelIndex == ModelIndex.NONE);
perfectSigns = 0;
goodSigns = 0;
mehSigns = 0;
terribleSigns = 0;
incorrectSigns = 0;
timingFeedback.text = "";
imageFeedback.sprite = minigame.thumbnail;
gameEndedPanel.SetActive(false);
// Create entry in current user for keeping track of progress
user = UserList.GetCurrentUser();
var progress = user.GetMinigameProgress(minigame.index);
if (progress == null)
{
progress = new PersistentDataController.SavedMinigameProgress();
progress.minigameIndex = minigame.index;
user.AddMinigameProgress(progress);
}
UserList.Save();
scoreDisplay.text = $"Score: {CalculateScore()}";
words.AddRange(currentTheme.learnables);
currentSong = songList.songs[songList.currentSongIndex];
AudioSource.PlayClipAtPoint(currentSong.song, Vector3.zero, 1.0f);
beginTime = Time.time;
lastSymbolTime = beginTime + currentSong.duration - 1920.0f / moveSpeed;
StartCoroutine(WaitThenStart(currentSong.firstSymbolTime));
get { return currentTheme; }
}
/// <summary>
@@ -388,7 +291,8 @@ public class JustSignController : AbstractFeedback
// Check if the song has ended and activate scorescreen if it has
if (currentTime - beginTime > currentSong.duration)
{
ActivateEnd();
// The boolean that is passed is irrelevant for this game
ActivateEnd(true);
}
// Move all active symbols to the right
@@ -406,28 +310,11 @@ public class JustSignController : AbstractFeedback
/// Calculate the score
/// </summary>
/// <returns>The calculated score</returns>
public int CalculateScore()
public override int CalculateScore()
{
return goodSigns * goodScore + perfectSigns * perfectScore + mehScore * mehSigns + terribleScore * terribleSigns + incorrectSigns * offscreenScore;
}
/// <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>
@@ -474,282 +361,160 @@ public class JustSignController : AbstractFeedback
}
/// <summary>
/// Update and save the scores
/// The logic to process the signs sent by the signPredictor
/// </summary>
public void SaveScores()
/// <param name="accuracy">The accuracy of the passed sign</param>
/// <param name="predictedSign">The name of the passed sign</param>
protected override void ProcessMostProbableSign(float accuracy, string predictedSign)
{
// Calculate new score
int newScore = CalculateScore();
Learnable predSign = currentTheme.learnables.Find(l => l.name.ToUpper() == predictedSign);
// 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
var progress = user.GetMinigameProgress(minigame.index);
// Get the current list of scores
List<Score> latestScores = progress.latestScores;
List<Score> highestScores = progress.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.latestScores = latestScores.Take(10).ToList();
progress.highestScores = highestScores.Take(10).ToList();
PersistentDataController.GetInstance().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: {CalculateScore()}";
}
/// <summary>
/// Sets the scoreboard
/// </summary>
private void SetScoreBoard()
{
// Clean the previous scoreboard entries
for (int i = 0; i < scoreboardEntries.Count; i++)
// If there is a feedback-object, we wil change its appearance
if (feedbackText != null && feedbackProgressImage != null)
{
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
var progress = user.GetMinigameProgress(minigame.index);
if (progress != null)
Color col;
if (accuracy > predSign.thresholdPercentage)
{
// Add scores to dictionary
List<Score> scores = progress.highestScores;
foreach (Score score in scores)
{
allScores.Add(new Tuple<string, Score>(user.GetUsername(), score));
}
feedbackText.text = $"Herkent '{predictedSign}'";
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
}
// 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).GetAvatar();
// 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)
else if (accuracy > 0.9 * predSign.thresholdPercentage)
{
formatted = $"{diff.Days}d ";
}
else if (diff.Hours > 0)
{
formatted = $"{diff.Hours}h ";
}
else if (diff.Minutes > 0)
{
formatted = $"{diff.Minutes}m ";
feedbackText.text = $"Lijkt op '{predictedSign}'";
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
formatted = "now";
feedbackText.text = "Detecteren...";
col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
entry.transform.Find("Ago").GetComponent<TMP_Text>().text = formatted;
feedbackText.color = col;
feedbackProgressImage.color = col;
// Alternating colors looks nice
if (rank % 2 == 0)
float oldValue = feedbackProgress.value;
// use an exponential scale
float newValue = Mathf.Exp(4 * (Mathf.Clamp(accuracy / predSign.thresholdPercentage, 0.0f, 1.0f) - 1.0f));
feedbackProgress.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
Image image = entry.transform.GetComponent<Image>();
image.color = new Color(image.color.r, image.color.g, image.color.b, 0f);
}
if (feedbackProgress != null)
{
feedbackProgress.value = t.CurrentValue;
}
});
}
// Make new score stand out
if (diff.TotalSeconds < 1)
// The logic for the internal workings of the game
if (accuracy > predSign.thresholdPercentage)
{
int matchedSymbolIndex = activeWords.IndexOf(predictedSign.ToUpper());
// Destroy the oldest symbol if the current input matches it
if (0 <= matchedSymbolIndex)
{
Image image = entry.transform.GetComponent<Image>();
image.color = new Color(0, 229, 255, 233);
}
float x = activeSymbols[matchedSymbolIndex].transform.localPosition.x;
rank++;
// parameters to define the Perfect hit zone
float perfectRange = hitZonePerfect.sizeDelta.x;
float perfectCenter = hitZonePerfect.localPosition.x;
// parameters to define the Good hit zone
float goodRange = hitZoneGood.sizeDelta.x;
float goodCenter = hitZoneGood.localPosition.x;
// parameters to define the Meh hit zone
float mehRange = hitZoneMeh.sizeDelta.x;
float mehCenter = hitZoneMeh.localPosition.x;
if (perfectCenter - perfectRange / 2 <= x && x <= perfectCenter + perfectRange / 2)
{
timingFeedback.text = $"Perfect! \n +{perfectScore}";
imageFeedback.sprite = perfectSprite;
perfectSigns++;
timingFeedback.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (goodCenter - goodRange / 2 <= x && x <= goodCenter + goodRange / 2)
{
timingFeedback.text = $"Goed \n +{goodScore}";
imageFeedback.sprite = goodSprite;
goodSigns++;
timingFeedback.color = new Color(0xf7 / 255.0f, 0xad / 255.0f, 0x19 / 255.0f);
}
else if (mehCenter - mehRange / 2 <= x && x <= mehCenter + mehRange / 2)
{
timingFeedback.text = $"Bijna... \n +{mehScore}";
imageFeedback.sprite = mehSprite;
mehSigns++;
timingFeedback.color = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
timingFeedback.text = $"Te vroeg! \n {terribleScore}";
imageFeedback.sprite = terribleSprite;
terribleSigns++;
timingFeedback.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
DestroySymbolAt(matchedSymbolIndex);
}
}
}
/// <summary>
/// The updateFunction that is called when new probabilities become available
/// The logic to set the scoreboard of justsign
/// </summary>
/// <returns></returns>
protected override IEnumerator UpdateFeedback()
/// <param name="victory">Shows whether or not the player won, is not relevant for JustSIgn</param>
protected override void SetScoreBoard(bool victory)
{
// Get the predicted sign
if (signPredictor != null && signPredictor.learnableProbabilities != null && gameIsActive)
gameEndedPanel.GetComponent<JustSignGameEndedPanel>().GenerateContent(
perfectSigns: perfectSigns,
goodSigns: goodSigns,
mehSigns: mehSigns,
terribleSigns: terribleSigns,
incorrectSigns: incorrectSigns,
duration: currentSong.duration,
score: CalculateScore()
);
}
/// <summary>
/// The justsign-specific logic that needs to be called at the start of the game
/// </summary>
protected override void StartGameLogic()
{
// Set the current theme so that it can be passed along
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
userFeedback.SetActive(currentTheme.modelIndex != ModelIndex.NONE);
previewMessage.SetActive(currentTheme.modelIndex == ModelIndex.NONE);
perfectSigns = 0;
goodSigns = 0;
mehSigns = 0;
terribleSigns = 0;
incorrectSigns = 0;
timingFeedback.text = "";
imageFeedback.sprite = minigame.thumbnail;
gameEndedPanel.SetActive(false);
scoreDisplay.text = $"Score: {CalculateScore()}";
words.AddRange(currentTheme.learnables);
currentSong = songList.songs[songList.currentSongIndex];
AudioSource.PlayClipAtPoint(currentSong.song, Vector3.zero, 1.0f);
beginTime = Time.time;
lastSymbolTime = beginTime + currentSong.duration - 1920.0f / moveSpeed;
StartCoroutine(WaitThenStart(currentSong.firstSymbolTime));
}
/// <summary>
/// The justsign-specific logic that needs to be called at the end of a game
/// </summary>
/// <param name="victory"></param>
protected override void EndGameLogic(bool victory)
{
gameIsActive = false;
while (activeSymbols.Count > 0)
{
// Get highest predicted sign
string predictedSign = signPredictor.learnableProbabilities.Aggregate((a, b) => a.Value > b.Value ? a : b).Key;
float accuracy = signPredictor.learnableProbabilities[predictedSign];
// vvv TEMPORARY STUFF vvv
if (predictedSign == "J" && accuracy <= 0.97f)
{
predictedSign = signPredictor.learnableProbabilities.Aggregate((x, y) => x.Value > y.Value && x.Key != "J" ? x : y).Key;
}
accuracy = signPredictor.learnableProbabilities[predictedSign];
// ^^^ TEMPORARY STUFF ^^^
Learnable predSign = currentTheme.learnables.Find(l => l.name.ToUpper().Replace(" ", "-") == predictedSign);
if (feedbackText != null && feedbackProgressImage != null)
{
Color col;
if (accuracy > predSign.thresholdPercentage)
{
feedbackText.text = $"Herkent '{predictedSign}'";
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (accuracy > 0.9 * predSign.thresholdPercentage)
{
feedbackText.text = $"Lijkt op '{predictedSign}'";
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
feedbackText.text = "Detecteren...";
col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
feedbackText.color = col;
feedbackProgressImage.color = col;
float oldValue = feedbackProgressBar.value;
// use an exponential scale
float newValue = Mathf.Exp(4 * (Mathf.Clamp(accuracy / predSign.thresholdPercentage, 0.0f, 1.0f) - 1.0f));
feedbackProgressBar.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
if (feedbackProgressBar != null)
{
feedbackProgressBar.value = t.CurrentValue;
}
});
}
if (accuracy > predSign.thresholdPercentage)
{
int matchedSymbolIndex = activeWords.IndexOf(predictedSign.ToUpper());
// Destroy the oldest symbol if the current input matches it
if (0 <= matchedSymbolIndex)
{
float x = activeSymbols[matchedSymbolIndex].transform.localPosition.x;
// parameters to define the Perfect hit zone
float perfectRange = hitZonePerfect.sizeDelta.x;
float perfectCenter = hitZonePerfect.localPosition.x;
// parameters to define the Good hit zone
float goodRange = hitZoneGood.sizeDelta.x;
float goodCenter = hitZoneGood.localPosition.x;
// parameters to define the Meh hit zone
float mehRange = hitZoneMeh.sizeDelta.x;
float mehCenter = hitZoneMeh.localPosition.x;
if (perfectCenter - perfectRange / 2 <= x && x <= perfectCenter + perfectRange / 2)
{
timingFeedback.text = $"Perfect! \n +{perfectScore}";
imageFeedback.sprite = perfectSprite;
perfectSigns++;
timingFeedback.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (goodCenter - goodRange / 2 <= x && x <= goodCenter + goodRange / 2)
{
timingFeedback.text = $"Goed \n +{goodScore}";
imageFeedback.sprite = goodSprite;
goodSigns++;
timingFeedback.color = new Color(0xf7 / 255.0f, 0xad / 255.0f, 0x19 / 255.0f);
}
else if (mehCenter - mehRange / 2 <= x && x <= mehCenter + mehRange / 2)
{
timingFeedback.text = $"Bijna... \n +{mehScore}";
imageFeedback.sprite = mehSprite;
mehSigns++;
timingFeedback.color = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
timingFeedback.text = $"Te vroeg! \n {terribleScore}";
imageFeedback.sprite = terribleSprite;
terribleSigns++;
timingFeedback.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
DestroySymbolAt(matchedSymbolIndex);
}
}
DestroySymbolAt(0);
}
else if (feedbackProgressBar != null)
{
feedbackProgressBar.value = 0.0f;
}
yield return null;
}
}