Resolve WES-117 "Persistent data handling"

This commit is contained in:
Dries Van Schuylenbergh
2023-04-04 17:00:47 +00:00
parent 3499e61bb0
commit 5f4408063f
82 changed files with 1963 additions and 1190 deletions

View File

@@ -10,7 +10,7 @@ using UnityEngine.UI;
/// Contains all game logic for the JustSign game
/// </summary>
public class JustSignController : MonoBehaviour
{
{
/// <summary>
/// All of the words that can be used in this session
/// </summary>
@@ -107,7 +107,7 @@ public class JustSignController : MonoBehaviour
/// Controls movement speed of symbols (higher -> faster)
/// </summary>
private int moveSpeed = 200;
/// <summary>
/// Starting X-coordinate of a symbol = (-1920 - symbolsize) / 2
/// </summary>
@@ -213,11 +213,6 @@ public class JustSignController : MonoBehaviour
/// </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>
@@ -276,18 +271,15 @@ public class JustSignController : MonoBehaviour
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);
user = UserList.GetCurrentUser();
var 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);
progress = new PersistentDataController.SavedMinigameProgress();
progress.minigameIndex = minigame.index;
user.AddMinigameProgress(progress);
}
userList.Save();
PersistentDataController.GetInstance().Save();
scoreDisplay.text = "Score: " + score.ToString();
currentTheme = minigame.themeList.themes[minigame.themeList.currentThemeIndex];
@@ -314,31 +306,42 @@ public class JustSignController : MonoBehaviour
/// Update is called once per frame
/// </summary>
void Update()
{
if (gameIsActive) {
{
if (gameIsActive)
{
int matchedSymbolIndex = -1;
for (int i = 0; i < activeWords.Count; i++) {
if (activeWords[i].ToLower() == answerField.text.ToLower()) {
for (int i = 0; i < activeWords.Count; i++)
{
if (activeWords[i].ToLower() == answerField.text.ToLower())
{
matchedSymbolIndex = i;
}
}
// Destroy the oldest symbol if the current input matches it
if (matchedSymbolIndex >= 0) {
int difference = Math.Abs((int) (activeSymbols[matchedSymbolIndex].transform.position.x - hitZone.transform.position.x));
if (difference < perfectBoundary) {
if (matchedSymbolIndex >= 0)
{
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) {
}
else if (difference < goodBoundary)
{
feedBack.text = "Good!";
goodSigns++;
score += goodScore;
} else if (difference < mehBoundary) {
}
else if (difference < mehBoundary)
{
feedBack.text = "Meh...";
mehSigns++;
score += mehScore;
} else {
}
else
{
feedBack.text = "Terrible!";
terribleSigns++;
score += terribleScore;
@@ -349,8 +352,10 @@ public class JustSignController : MonoBehaviour
}
// Destroy the oldest symbol if it leaves the screen
if (activeSymbols.Count > 0) {
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX) {
if (activeSymbols.Count > 0)
{
if (activeSymbols[0].GetComponent<RectTransform>().localPosition.x > -trackX)
{
DestroySymbolAt(0);
incorrectSigns++;
feedBack.text = "Te laat!";
@@ -360,18 +365,21 @@ public class JustSignController : MonoBehaviour
// Spawn new symbol every spawn period
float currentTime = Time.time;
if (currentTime - lastSpawn > currentSong.spawnPeriod && lastSymbolTime > currentTime) {
if (currentTime - lastSpawn > currentSong.spawnPeriod && lastSymbolTime > currentTime)
{
lastSpawn = currentTime;
SpawnNewSymbol();
}
// Check if the song has ended and activate scorescreen if it has
if (currentTime - beginTime > currentSong.duration) {
if (currentTime - beginTime > currentSong.duration)
{
ActivateEnd();
}
// Move all active symbols to the right
foreach (GameObject symbol in activeSymbols) {
foreach (GameObject symbol in activeSymbols)
{
RectTransform rectTransform = symbol.GetComponent<RectTransform>();
rectTransform.localPosition = new Vector3(rectTransform.localPosition.x + Time.deltaTime * moveSpeed, trackY, 0);
}
@@ -401,7 +409,8 @@ public class JustSignController : MonoBehaviour
/// Destroy the symbol at the given index
/// </summary>
/// <param name="index">The index of the symbol to destroy</param>
void DestroySymbolAt(int index) {
void DestroySymbolAt(int index)
{
activeWords.RemoveAt(index);
GameObject symbol = activeSymbols[index];
activeSymbols.RemoveAt(index);
@@ -411,12 +420,15 @@ public class JustSignController : MonoBehaviour
/// <summary>
/// Create a new symbol at the start of the track
/// </summary>
void SpawnNewSymbol() {
void SpawnNewSymbol()
{
// Pick a word that isn't in use yet
List<int> unusedWordIndices = new List<int>();
for (int i = 0; i < words.Count; i++) {
if (!activeWords.Contains(words[i].name)) {
for (int i = 0; i < words.Count; i++)
{
if (!activeWords.Contains(words[i].name))
{
unusedWordIndices.Add(i);
}
}
@@ -455,12 +467,11 @@ public class JustSignController : MonoBehaviour
score.time = DateTime.Now.ToString();
// Save the new score
user = userList.GetCurrentUser();
Progress progress = user.GetMinigameProgress(minigame.index);
var 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");
List<Score> latestScores = progress.latestScores;
List<Score> highestScores = progress.highestScores;
// Add the new score
latestScores.Add(score);
@@ -470,10 +481,10 @@ public class JustSignController : MonoBehaviour
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());
progress.latestScores = latestScores.Take(10).ToList();
progress.highestScores = highestScores.Take(10).ToList();
userList.Save();
PersistentDataController.GetInstance().Save();
}
/// <summary>
@@ -520,17 +531,17 @@ public class JustSignController : MonoBehaviour
// 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())
foreach (User user in UserList.GetUsers())
{
// Get user's progress for this minigame
Progress progress = user.GetMinigameProgress(minigame.index);
var progress = user.GetMinigameProgress(minigame.index);
if (progress != null)
{
// Add scores to dictionary
List<Score> scores = progress.Get<List<Score>>("highestScores");
List<Score> scores = progress.highestScores;
foreach (Score score in scores)
{
allScores.Add(new Tuple<string, Score>(user.username, score));
allScores.Add(new Tuple<string, Score>(user.GetUsername(), score));
}
}
}
@@ -549,7 +560,7 @@ public class JustSignController : MonoBehaviour
scoreboardEntries.Add(entry);
// Set the player icon
entry.transform.Find("Image").GetComponent<Image>().sprite = userList.GetUserByUsername(username).avatar;
entry.transform.Find("Image").GetComponent<Image>().sprite = UserList.GetUserByUsername(username).GetAvatar();
// Set the player name
entry.transform.Find("PlayerName").GetComponent<TMP_Text>().text = username;

View File

@@ -0,0 +1,19 @@
{
"name": "JustSignScripts",
"rootNamespace": "",
"references": [
"GUID:e83ddf9a537a96b4a804a16bb7872ec1",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:1631ed2680c61245b8211d943c1639a8",
"GUID:7f2d0ee6dd21e1d4eb25b71b7a749d25"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 643048e31db889c4c8ab5a04e9a70473
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: