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,210 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GameEndedPanel : MonoBehaviour
{
/// <summary>
/// "VERLOREN" or "GEWONNEN"
/// </summary>
public TMP_Text endText;
/// <summary>
/// LPM
/// </summary>
public TMP_Text lpmText;
/// <summary>
/// Letters ( right | wrong )
/// </summary>
public TMP_Text lettersRightText;
public TMP_Text lettersWrongText;
/// <summary>
/// Letters
/// </summary>
public TMP_Text lettersTotalText;
/// <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>
/// 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>
/// Generate the content of the GameEnded panel
/// </summary>
/// <param name="startTime">Time of starting the minigame</param>
/// <param name="totalWords">Total number of words</param>
/// <param name="correctLetters">Total number of correctly spelled letters</param>
/// <param name="incorrectLetters">Total number of incorrectly spelled letters</param>
/// <param name="result">"VERLOREN" or "GEWONNEN"</param>
/// <param name="score">Final score</param>
public void GenerateContent(DateTime startTime, int totalWords, int correctLetters, int incorrectLetters, string result, int score)
{
// Final result
endText.text = result;
// 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();
lettersTotalText.text = (correctLetters + incorrectLetters).ToString();
// Accuracy
if (correctLetters + incorrectLetters > 0)
{
accuracyText.text = ((correctLetters) * 100f / (correctLetters + incorrectLetters)).ToString("#.##") + "%";
}
else
{
accuracyText.text = "-";
}
// Words
wordsText.text = $"{totalWords}";
// Time
timeText.text = duration.ToString(@"mm\:ss");
// Score
scoreText.text = $"Score: {score}";
SetScoreBoard();
}
/// <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
var progress = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE);
if (progress != null)
{
// Add scores to dictionary
List<Score> scores = progress.highestScores;
foreach (Score score in scores)
{
allScores.Add(new Tuple<string, Score>(user.GetUsername(), 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).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)
{
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

@@ -7,7 +7,7 @@ using TMPro;
using UnityEngine;
using UnityEngine.UI;
public partial class SpellingBeeController : AbstractFeedback
public partial class SpellingBeeController : AbstractMinigameController
{
/// <summary>
/// All of the words that can be used in this session
@@ -45,11 +45,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
private float timerValue;
/// <summary>
/// Indicates if the game is still going
/// </summary>
private bool gameEnded;
/// <summary>
/// List of learnables to get the threshold for the letters
/// </summary>
@@ -81,22 +76,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
private DateTime startTime;
/// <summary>
/// Reference to the current user
/// </summary>
private User user;
/// <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>
/// Letter prefab
/// </summary>
@@ -137,21 +116,11 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
public Transform Scoreboard;
/// <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 feedbackProgress;
/// <summary>
/// Reference to the progress bar image, so we can add fancy colors
/// </summary>
@@ -172,11 +141,6 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
protected string previousIncorrectSign = null;
/// <summary>
/// Reference used to set the webcam for the SignPredictor
/// </summary>
public RawImage webcamScreen;
/// <summary>
/// Reference to display the score
/// </summary>
@@ -198,57 +162,11 @@ public partial class SpellingBeeController : AbstractFeedback
private int incorrectLettersScore = -5;
/// <summary>
/// Start is called before the first frame update
/// Set the AbstractMinigameController variable to inform it of the theme for the signPredictor
/// </summary>
public void Start()
protected override Theme signPredictorTheme
{
signPredictor.SetModel(ModelIndex.FINGERSPELLING);
signPredictor.SwapScreen(webcamScreen);
AddSelfAsListener();
StartController();
}
/// <summary>
/// Is called at the start of the scene AND when the game is replayed
/// </summary>
public void StartController()
{
correctLetters = 0;
incorrectLetters = 0;
words.Clear();
// We use -1 instead of 0 so SetNextWord can simply increment it each time
spelledWords = -1;
wordIndex = 0;
gameEnded = false;
timerValue = 30.0f;
bonusActiveRemaining = 0.0f;
startTime = DateTime.Now;
gameEndedPanel.SetActive(false);
bonusTimeText.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 = MinigameIndex.SPELLING_BEE;
user.AddMinigameProgress(progress);
}
UserList.Save();
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
//feedback.signPredictor.SetModel(currentTheme.modelIndex);
words.AddRange(currentTheme.learnables);
ShuffleWords();
NextWord();
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = "";
get { return fingerspelling; }
}
/// <summary>
@@ -256,7 +174,7 @@ public partial class SpellingBeeController : AbstractFeedback
/// </summary>
public void Update()
{
if (!gameEnded)
if (gameIsActive)
{
timerValue -= Time.deltaTime;
if (bonusActiveRemaining <= 0.0 && bonusTimeText.activeSelf)
@@ -272,7 +190,8 @@ public partial class SpellingBeeController : AbstractFeedback
if (timerValue <= 0.0f)
{
timerValue = 0.0f;
ActivateGameOver();
//ActivateGameOver();
ActivateEnd(false);
}
int minutes = Mathf.FloorToInt(timerValue / 60.0f);
@@ -302,89 +221,11 @@ public partial class SpellingBeeController : AbstractFeedback
/// Calculate the score
/// </summary>
/// <returns>The calculated score</returns>
public int CalculateScore()
public override int CalculateScore()
{
return correctLetters * correctLettersScore + incorrectLetters * incorrectLettersScore;
}
/// <summary>
/// Displays the game over panel and score values
/// </summary>
public void ActivateGameOver()
{
gameEnded = true;
DeleteWord();
// Save the scores and show the scoreboard
SaveScores();
gameEndedPanel.GetComponent<GameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: "VERLOREN",
score: CalculateScore()
);
gameEndedPanel.SetActive(true);
}
/// <summary>
/// Display win screen
/// </summary>
public void ActivateWin()
{
gameEnded = true;
DeleteWord();
// Save the scores and show the scoreboard
SaveScores();
gameEndedPanel.GetComponent<GameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: "GEWONNEN",
score: CalculateScore()
);
gameEndedPanel.SetActive(true);
}
/// <summary>
/// Update and save the scores
/// </summary>
public void SaveScores()
{
// Calculate new score
int newScore = CalculateScore();
// 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();
UserList.Save();
}
/// <summary>
/// Delete all letter objects
/// </summary>
@@ -414,7 +255,7 @@ public partial class SpellingBeeController : AbstractFeedback
/// <param name="successful">true if the letter was correctly signed, false otherwise</param>
public void NextLetter(bool successful)
{
if (gameEnded) { return; }
if (!gameIsActive) { return; }
// Change color of current letter (skip spaces)
if (successful)
@@ -469,7 +310,8 @@ public partial class SpellingBeeController : AbstractFeedback
}
else
{
ActivateWin();
//ActivateWin();
ActivateEnd(true);
}
}
@@ -514,7 +356,7 @@ public partial class SpellingBeeController : AbstractFeedback
Learnable letter = fingerspelling.learnables.Find(l => l.name == sign);
return letter.thresholdPercentage;
}
/*
/// <summary>
/// The updateFunction that is called when new probabilities become available
/// </summary>
@@ -525,7 +367,7 @@ public partial class SpellingBeeController : AbstractFeedback
string currentSign = GetSign();
// Get the predicted sign
if (signPredictor != null && signPredictor.learnableProbabilities != null &&
currentSign != null && signPredictor.learnableProbabilities.ContainsKey(currentSign))
currentSign != null && signPredictor.learnableProbabilities.ContainsKey(currentSign) && gameIsActive)
{
float accCurrentSign = signPredictor.learnableProbabilities[currentSign];
float thresholdCurrentSign = GetTresholdPercentage(currentSign);
@@ -613,6 +455,7 @@ public partial class SpellingBeeController : AbstractFeedback
}
yield return null;
}
*/
/// <summary>
/// Function to get the current letter that needs to be signed
/// </summary>
@@ -638,4 +481,157 @@ public partial class SpellingBeeController : AbstractFeedback
}
NextLetter(successful);
}
/// <summary>
/// The logic to process the signs sent by the signPredictor
/// </summary>
/// <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)
{
string currentSign = GetSign();
float accPredictSign = accuracy;
float accCurrentSign = signPredictor.learnableProbabilities[currentSign];
float thresholdCurrentSign = GetTresholdPercentage(currentSign);
float thresholdPredictedSign = GetTresholdPercentage(predictedSign);
// If there is a feedback-object, we wil change its appearance
if (feedbackText != null && feedbackProgressImage != null)
{
Color col;
if (accPredictSign > thresholdCurrentSign)
{
feedbackText.text = "Goed";
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (accCurrentSign > 0.9 * thresholdCurrentSign)
{
feedbackText.text = "Bijna...";
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else if (accPredictSign > thresholdPredictedSign)
{
feedbackText.text = $"Verkeerde gebaar: '{predictedSign}'";
col = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
accCurrentSign = 0.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 = feedbackProgress.value;
// use an exponential scale
float newValue = Mathf.Exp(4 * (Mathf.Clamp(accCurrentSign / thresholdCurrentSign, 0.0f, 1.0f) - 1.0f));
feedbackProgress.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
if (feedbackProgress != null)
{
feedbackProgress.value = t.CurrentValue;
}
});
}
// The logic for the internal workings of the game
if (accPredictSign > thresholdPredictedSign)
{
// Correct sign, instantly pass it along
if (predictedSign == currentSign)
{
PredictSign(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
// Incorrect sign, wait a bit before passing it along
else
{
if (previousIncorrectSign != predictedSign)
{
timer = DateTime.Now;
previousIncorrectSign = predictedSign;
}
else if (DateTime.Now - timer > TimeSpan.FromSeconds(2.0f))
{
PredictSign(predictedSign);
timer = DateTime.Now;
predictedSign = null;
previousIncorrectSign = null;
}
}
}
}
/// <summary>
/// The logic to set the scoreboard of spellingbee
/// </summary>
/// <param name="victory">SHows whether or not the player won</param>
protected override void SetScoreBoard(bool victory)
{
string resultTxt;
if (victory)
{
resultTxt = "GEWONNEN";
}
else
{
resultTxt = "VERLOREN";
}
// Save the scores and show the scoreboard
gameEndedPanel.GetComponent<SpellingBeeGameEndedPanel>().GenerateContent(
startTime: startTime,
totalWords: spelledWords,
correctLetters: correctLetters,
incorrectLetters: incorrectLetters,
result: resultTxt,
score: CalculateScore()
);
}
/// <summary>
/// The spellinbee-specific logic that needs to be called at the start of the game
/// </summary>
protected override void StartGameLogic()
{
correctLetters = 0;
incorrectLetters = 0;
words.Clear();
// We use -1 instead of 0 so SetNextWord can simply increment it each time
spelledWords = -1;
wordIndex = 0;
gameIsActive = true;
timerValue = 30.0f;
bonusActiveRemaining = 0.0f;
startTime = DateTime.Now;
gameEndedPanel.SetActive(false);
bonusTimeText.SetActive(false);
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
//feedback.signPredictor.SetModel(currentTheme.modelIndex);
words.AddRange(currentTheme.learnables);
ShuffleWords();
NextWord();
scoreDisplay.text = $"Score: {CalculateScore()}";
scoreBonus.text = "";
}
/// <summary>
/// The spellingbee-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;
DeleteWord();
}
}

View File

@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class SpellingBeeGameEndedPanel : AbstractGameEndedPanel
{
/// <summary>
/// Tell the scoreboard that the scoreboard is for SpellingBee
/// </summary>
protected override MinigameIndex minigameIndex
{
get { return MinigameIndex.SPELLING_BEE; }
}
/// <summary>
/// "VERLOREN" or "GEWONNEN"
/// </summary>
public TMP_Text endText;
/// <summary>
/// LPM
/// </summary>
public TMP_Text lpmText;
/// <summary>
/// Letters ( right | wrong )
/// </summary>
public TMP_Text lettersRightText;
public TMP_Text lettersWrongText;
/// <summary>
/// Letters
/// </summary>
public TMP_Text lettersTotalText;
/// <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>
/// Generate the content of the GameEnded panel
/// </summary>
/// <param name="startTime">Time of starting the minigame</param>
/// <param name="totalWords">Total number of words</param>
/// <param name="correctLetters">Total number of correctly spelled letters</param>
/// <param name="incorrectLetters">Total number of incorrectly spelled letters</param>
/// <param name="result">"VERLOREN" or "GEWONNEN"</param>
/// <param name="score">Final score</param>
public void GenerateContent(DateTime startTime, int totalWords, int correctLetters, int incorrectLetters, string result, int score)
{
// Final result
endText.text = result;
// 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();
lettersTotalText.text = (correctLetters + incorrectLetters).ToString();
// Accuracy
if (correctLetters + incorrectLetters > 0)
{
accuracyText.text = ((correctLetters) * 100f / (correctLetters + incorrectLetters)).ToString("#.##") + "%";
}
else
{
accuracyText.text = "-";
}
// Words
wordsText.text = $"{totalWords}";
// Time
timeText.text = duration.ToString(@"mm\:ss");
// Score
scoreText.text = $"Score: {score}";
SetScoreBoard();
}
}

View File

@@ -8,7 +8,8 @@
"GUID:d0b6b39a21908f94fbbd9f2c196a9725",
"GUID:58e104b97fb3752438ada2902a36dcbf",
"GUID:e83ddf9a537a96b4a804a16bb7872ec1",
"GUID:7f2d0ee6dd21e1d4eb25b71b7a749d25"
"GUID:7f2d0ee6dd21e1d4eb25b71b7a749d25",
"GUID:403dd94a93598934eb522dc36df43d7b"
],
"includePlatforms": [],
"excludePlatforms": [],