Resolve WES-117 "Persistent data handling"
This commit is contained in:
42
Assets/Accounts/ScriptableObjects/UserAvatarList.asset
Normal file
42
Assets/Accounts/ScriptableObjects/UserAvatarList.asset
Normal file
@@ -0,0 +1,42 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 81765ea55baf15d45b01b02187b02429, type: 3}
|
||||
m_Name: UserAvatarList
|
||||
m_EditorClassIdentifier:
|
||||
avatars:
|
||||
- {fileID: 21300000, guid: 6feaaea4b64633f49b4538bfd4b3755c, type: 3}
|
||||
- {fileID: 21300000, guid: 399cc241fc79bd74db314f146e7fb6b9, type: 3}
|
||||
- {fileID: 21300000, guid: 4654657a2ebea444898fa6eeaefcd18d, type: 3}
|
||||
- {fileID: 21300000, guid: 0cc198f1cde659246a199b30459720b1, type: 3}
|
||||
- {fileID: 21300000, guid: 7f105a09a3d164547925ee1bdfa595ca, type: 3}
|
||||
- {fileID: 21300000, guid: 313c22e5834595645989d609fe9d8853, type: 3}
|
||||
- {fileID: 21300000, guid: e5f3d99cdf5298d4f86d42dd019a6bad, type: 3}
|
||||
- {fileID: 21300000, guid: b67b99d98d1ceb1489743bcd78b9ab70, type: 3}
|
||||
- {fileID: 21300000, guid: e4af402f37099cd4195d7d2654519744, type: 3}
|
||||
- {fileID: 21300000, guid: 73f4e028d9efb644aa23538a749667c5, type: 3}
|
||||
- {fileID: 21300000, guid: 61e56570163833e448037bc353ada0ac, type: 3}
|
||||
- {fileID: 21300000, guid: 8e69dfd19237abb4f98eb11e74153109, type: 3}
|
||||
- {fileID: 21300000, guid: bc11ff9d6cc35de45ab58f39f13d7142, type: 3}
|
||||
- {fileID: 21300000, guid: f989719a5c45c7a4183b2bc12c1c4905, type: 3}
|
||||
- {fileID: 21300000, guid: 1392922567cd59d4fb0beceea3f5917a, type: 3}
|
||||
- {fileID: 21300000, guid: 3c314ae9e10eafb49b57c98c9e779bd8, type: 3}
|
||||
- {fileID: 21300000, guid: 33509e453b5093e43a148a668a961c56, type: 3}
|
||||
- {fileID: 21300000, guid: 39ac3b86729423846be1fbd56b8d92d6, type: 3}
|
||||
- {fileID: 21300000, guid: 1118d67f50622cc4ab02aa23c6dc1fd4, type: 3}
|
||||
- {fileID: 21300000, guid: e519f0f7b00e6834187963a0131cae52, type: 3}
|
||||
- {fileID: 21300000, guid: eefb367ff44256d43a14cde2c3924321, type: 3}
|
||||
- {fileID: 21300000, guid: 83d6525e4efa4954091e08095349f45a, type: 3}
|
||||
- {fileID: 21300000, guid: db31bca6363270441ab54421f55c1263, type: 3}
|
||||
- {fileID: 21300000, guid: 9d5771baa6ea6e041b066135d6798e1c, type: 3}
|
||||
- {fileID: 21300000, guid: 2b01165a5836ab14593d7a5862bd6793, type: 3}
|
||||
- {fileID: 21300000, guid: 8c304fe460423214ea0bb6ebc235ed2d, type: 3}
|
||||
- {fileID: 21300000, guid: b203b4e5f8568ff46b2277ce6d61017a, type: 3}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 017ec1af3b6cc4d4ab2b506911a4edad
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,6 +3,7 @@
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||
"GUID:e83ddf9a537a96b4a804a16bb7872ec1",
|
||||
"GUID:7f2d0ee6dd21e1d4eb25b71b7a749d25"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
|
||||
@@ -22,11 +22,6 @@ public class ChangeUserScreen : MonoBehaviour
|
||||
/// </summary>
|
||||
public GameObject error;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the user list
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current selected user in the UserList
|
||||
/// </summary>
|
||||
@@ -42,7 +37,7 @@ public class ChangeUserScreen : MonoBehaviour
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
userList.Load();
|
||||
PersistentDataController.GetInstance().Load();
|
||||
error.SetActive(false);
|
||||
DisplayUsers();
|
||||
}
|
||||
@@ -57,8 +52,8 @@ public class ChangeUserScreen : MonoBehaviour
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
List<User> users = userList.GetUsers();
|
||||
currentUserIndex = userList.GetCurrentUserIndex();
|
||||
List<User> users = UserList.GetUsers();
|
||||
int currentUserIndex = UserList.IndexOf(UserList.GetCurrentUser().GetUsername());
|
||||
for (int i = 0; i < users.Count; i++)
|
||||
{
|
||||
User user = users[i];
|
||||
@@ -99,8 +94,7 @@ public class ChangeUserScreen : MonoBehaviour
|
||||
/// </summary>
|
||||
public void IChooseYou()
|
||||
{
|
||||
userList.ChangeCurrentUser(currentUserIndex);
|
||||
userList.Save();
|
||||
UserList.ChangeCurrentUser(currentUserIndex);
|
||||
SystemController.GetInstance().BackToPreviousScene();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class CourseProgressCard : MonoBehaviour
|
||||
/// <summary>
|
||||
/// Reference to the progress so we can display a progress bar
|
||||
/// </summary>
|
||||
public Progress courseProgress;
|
||||
public PersistentDataController.SavedCourseProgress courseProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the list of courses so we can query the correct course
|
||||
@@ -48,11 +48,11 @@ public class CourseProgressCard : MonoBehaviour
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
Course course = courseList.GetCourseByIndex(courseProgress.Get<CourseIndex>("courseIndex"));
|
||||
Course course = courseList.GetCourseByIndex(courseProgress.courseIndex);
|
||||
|
||||
thumbnail.sprite = course.thumbnail;
|
||||
title.text = course.title;
|
||||
progressBar.value = courseProgress.Get<float>("courseProgress");
|
||||
progressBar.value = courseProgress.progress;
|
||||
button.onClick.AddListener(selectActivity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class MinigameProgressCard : MonoBehaviour
|
||||
/// <summary>
|
||||
/// Reference to the minigame progress
|
||||
/// </summary>
|
||||
public Progress minigameProgress;
|
||||
public PersistentDataController.SavedMinigameProgress minigameProgress;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the minigame list
|
||||
@@ -49,11 +49,11 @@ public class MinigameProgressCard : MonoBehaviour
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
Minigame minigame = minigameList.GetMinigameByIndex(minigameProgress.Get<MinigameIndex>("minigameIndex"));
|
||||
Minigame minigame = minigameList.GetMinigameByIndex(minigameProgress.minigameIndex);
|
||||
|
||||
thumbnail.sprite = minigame.thumbnail;
|
||||
title.text = minigame.title;
|
||||
List<Score> highscores = minigameProgress.Get<List<Score>>("highestScores");
|
||||
List<Score> highscores = minigameProgress.highestScores;
|
||||
int score = highscores.Count > 0 ? highscores[0].scoreValue : 0;
|
||||
highscore.text = $"Topscore: {score}";
|
||||
button.onClick.AddListener(selectActivity);
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// A class for holding all progress belonging to a user
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class Progress
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper class for handling the stored progress
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
protected class DataEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The key, used to reference the data object
|
||||
/// </summary>
|
||||
public string key;
|
||||
|
||||
/// <summary>
|
||||
/// The object, representated as a list of byte (which can be serialized)
|
||||
/// </summary>
|
||||
public List<byte> bytes = new List<byte>();
|
||||
|
||||
public DataEntry(string key, byte[] data)
|
||||
{
|
||||
this.key = key;
|
||||
this.bytes = new List<byte>(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entries in the <c>Progress</c> object
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private List<DataEntry> entries = new List<DataEntry>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Update the value of a certain key,
|
||||
/// or add a new value if the key was not present
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data to be added/updated</typeparam>
|
||||
/// <param name="key">The key, used for referencing the data</param>
|
||||
/// <param name="data">The object of type <typeparamref name="T"/></param>
|
||||
/// <returns><c>true</c> if successful, <c>false</c> otherwise</returns>
|
||||
public bool AddOrUpdate<T>(string key, T data)
|
||||
{
|
||||
if (data == null)
|
||||
return false;
|
||||
|
||||
DataEntry entry = entries.Find(x => x.key == key);
|
||||
|
||||
// Hacky serialization stuff
|
||||
BinaryFormatter bf = new BinaryFormatter();
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
bf.Serialize(ms, data);
|
||||
if (entry != null)
|
||||
{
|
||||
entry.bytes.Clear();
|
||||
entry.bytes.AddRange(ms.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
entries.Add(new DataEntry(key, ms.ToArray()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the data object of a certain key
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data object</typeparam>
|
||||
/// <param name="key">The key referencing the data object</param>
|
||||
/// <returns>The data, cast to a type <typeparamref name="T"/></returns>
|
||||
/// <exception cref="KeyNotFoundException"></exception>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
BinaryFormatter bf = new BinaryFormatter();
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
// Find the correct key
|
||||
foreach (DataEntry entry in entries)
|
||||
{
|
||||
if (entry.key == key)
|
||||
{
|
||||
// Hacky serialization stuff
|
||||
byte[] data = entry.bytes.ToArray();
|
||||
ms.Write(data, 0, data.Length);
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
return (T)bf.Deserialize(ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Raise an exception when key is not found
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check whether a key is present
|
||||
/// </summary>
|
||||
/// <param name="key">The key to check</param>
|
||||
/// <returns>true if a item can be found with the specified key</returns>
|
||||
public bool Has(string key)
|
||||
{
|
||||
return entries.Find(x => x.key == key) != null;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d887bc641cc7a8f4abf9d4eb34d26923
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,104 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
/// <summary>
|
||||
/// SystemController singleton
|
||||
/// </summary>
|
||||
public class SystemController
|
||||
{
|
||||
/// <summary>
|
||||
/// The instance controlling the singleton
|
||||
/// </summary>
|
||||
private static SystemController instance = null;
|
||||
|
||||
/// <summary>
|
||||
/// Stack of the loaded scenes, used to easily go back to previous scenes
|
||||
/// </summary>
|
||||
private Stack<int> sceneStack = new Stack<int>();
|
||||
|
||||
/// <summary>
|
||||
/// Get the instance loaded by the singleton
|
||||
/// </summary>
|
||||
/// <returns>SystemController instance</returns>
|
||||
public static SystemController GetInstance()
|
||||
{
|
||||
// Create a new instance if non exists
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new SystemController();
|
||||
instance.sceneStack.Push(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the scene and push on the stack
|
||||
/// </summary>
|
||||
/// <param name="scenePath">Path of the scene</param>
|
||||
public void LoadNextScene(string scenePath)
|
||||
{
|
||||
LoadNextScene(SceneUtility.GetBuildIndexByScenePath(scenePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the scene and push on the stack
|
||||
/// </summary>
|
||||
/// <param name="sceneIndex">Buildindex of the scene</param>
|
||||
public void LoadNextScene(int sceneIndex)
|
||||
{
|
||||
sceneStack.Push(sceneIndex);
|
||||
SceneManager.LoadScene(sceneIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swap the current scene with the new scene on the stack
|
||||
/// </summary>
|
||||
/// <param name="scenePath">Path of the scene</param>
|
||||
public void SwapScene(string scenePath)
|
||||
{
|
||||
SwapScene(SceneUtility.GetBuildIndexByScenePath(scenePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Swap the current scene with the new scene on the stack
|
||||
/// </summary>
|
||||
/// <param name="sceneIndex">Buildindex of the scene</param>
|
||||
public void SwapScene(int sceneIndex)
|
||||
{
|
||||
sceneStack.Pop();
|
||||
LoadNextScene(sceneIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Go back to the previous scene and unload the current scene
|
||||
/// </summary>
|
||||
public void BackToPreviousScene()
|
||||
{
|
||||
sceneStack.Pop();
|
||||
|
||||
if (sceneStack.Count > 0) SceneManager.LoadScene(sceneStack.Peek());
|
||||
else Application.Quit();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Go back to a specific scene, unloading all the scenes on the way
|
||||
/// </summary>
|
||||
/// <param name="scenePath">Path of the scene</param>
|
||||
public void BackToScene(string scenePath)
|
||||
{
|
||||
BackToScene(SceneUtility.GetBuildIndexByScenePath(scenePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Go back to a specific scene, unloading all the scene on the way
|
||||
/// </summary>
|
||||
/// <param name="sceneIndex">Buildindex of the scene</param>
|
||||
public void BackToScene(int sceneIndex)
|
||||
{
|
||||
while (0 < sceneStack.Count && sceneStack.Peek() != sceneIndex) sceneStack.Pop();
|
||||
|
||||
if (sceneStack.Count > 0) SceneManager.LoadScene(sceneStack.Peek());
|
||||
else Application.Quit();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf5ea73aa43049e45a0ad926db15f315
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,40 +2,45 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using static PersistentDataController;
|
||||
|
||||
/// <summary>
|
||||
/// A class holding all information of a user
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class User
|
||||
{
|
||||
/// <summary>
|
||||
/// User nickname
|
||||
/// Reference to the user stored data record
|
||||
/// </summary>
|
||||
public string username;
|
||||
private SavedUserData storedUserData;
|
||||
|
||||
/// <summary>
|
||||
/// The avatar of the user
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public Sprite avatar;
|
||||
/// <param name="data">Reference to the user stored data record</param>
|
||||
public User(SavedUserData data)
|
||||
{
|
||||
this.storedUserData = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The total playtime of the user
|
||||
/// Get the username
|
||||
/// </summary>
|
||||
/// <remarks>TODO: needs to be implemented</remarks>
|
||||
public double playtime;
|
||||
/// <returns></returns>
|
||||
public string GetUsername() { return storedUserData.username; }
|
||||
|
||||
/// <summary>
|
||||
/// List of courses a user started/completed
|
||||
/// Get the total playtime
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public List<Progress> courses = new List<Progress>();
|
||||
/// <returns></returns>
|
||||
public double GetPlaytime() { return storedUserData.playtime; }
|
||||
|
||||
/// <summary>
|
||||
/// List of minigames a user played
|
||||
/// Get the avatar
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public List<Progress> minigames = new List<Progress>();
|
||||
/// <returns></returns>
|
||||
public Sprite GetAvatar() { return UserList.AVATARS[storedUserData.avatarIndex]; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all recently started courses
|
||||
@@ -46,10 +51,10 @@ public class User
|
||||
{
|
||||
// TODO: return better results (for now only return all courses)
|
||||
List<Tuple<CourseIndex, float>> recentCourses = new List<Tuple<CourseIndex, float>>();
|
||||
foreach (Progress courseProgress in courses)
|
||||
foreach (var courseProgress in storedUserData.courses)
|
||||
{
|
||||
CourseIndex idx = courseProgress.Get<CourseIndex>("courseIndex");
|
||||
float progress = courseProgress.Get<float>("courseProgress");
|
||||
CourseIndex idx = courseProgress.courseIndex;
|
||||
float progress = courseProgress.progress;
|
||||
recentCourses.Add(Tuple.Create<CourseIndex, float>(idx, progress));
|
||||
}
|
||||
return recentCourses.Take(3).ToList();
|
||||
@@ -63,31 +68,58 @@ public class User
|
||||
public List<Tuple<CourseIndex, float>> GetRecommendedCourses()
|
||||
{
|
||||
List<Tuple<CourseIndex, float>> recommenedCourses = new List<Tuple<CourseIndex, float>>();
|
||||
if (courses.Count == 0)
|
||||
if (storedUserData.courses.Count == 0)
|
||||
{
|
||||
recommenedCourses.Add(Tuple.Create<CourseIndex, float>(CourseIndex.FINGERSPELLING, 0.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: return better results (for now only return all courses)
|
||||
foreach (Progress courseProgress in courses)
|
||||
foreach (var courseProgress in storedUserData.courses)
|
||||
{
|
||||
CourseIndex idx = courseProgress.Get<CourseIndex>("courseIndex");
|
||||
float progress = courseProgress.Get<float>("courseProgress");
|
||||
CourseIndex idx = courseProgress.courseIndex;
|
||||
float progress = courseProgress.progress;
|
||||
recommenedCourses.Add(Tuple.Create<CourseIndex, float>(idx, progress));
|
||||
}
|
||||
}
|
||||
return recommenedCourses.Take(3).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the progress of all courses the user did
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<SavedCourseProgress> GetCourses()
|
||||
{
|
||||
return storedUserData.courses;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the progress of a certain course
|
||||
/// </summary>
|
||||
/// <param name="courseIndex">Index of course</param>
|
||||
/// <returns><c>Progress</c> belonging to the <c>courseIndex</c>, <c>null</c> if course was not found</returns>
|
||||
public Progress GetCourseProgress(CourseIndex courseIndex)
|
||||
public SavedCourseProgress GetCourseProgress(CourseIndex courseIndex)
|
||||
{
|
||||
return courses.Find((p) => p.Get<CourseIndex>("courseIndex") == courseIndex);
|
||||
return storedUserData.courses.Find((p) => p.courseIndex == courseIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add new progress of a course
|
||||
/// </summary>
|
||||
/// <param name="progress">SavedCourseProgress data record</param>
|
||||
public void AddCourseProgress(SavedCourseProgress progress)
|
||||
{
|
||||
storedUserData.courses.Add(progress);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the progress of all minigames the user did
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<SavedMinigameProgress> GetMinigames()
|
||||
{
|
||||
return storedUserData.minigames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,8 +127,17 @@ public class User
|
||||
/// </summary>
|
||||
/// <param name="minigameIndex">Index of the minigame</param>
|
||||
/// <returns><c>Progress</c> belonging to the <c>minigameIndex</c>, <c>null</c> if minigame was not found</returns>
|
||||
public Progress GetMinigameProgress(MinigameIndex minigameIndex)
|
||||
public SavedMinigameProgress GetMinigameProgress(MinigameIndex minigameIndex)
|
||||
{
|
||||
return minigames.Find((p) => p.Get<MinigameIndex>("minigameIndex") == minigameIndex);
|
||||
return storedUserData.minigames.Find((p) => p.minigameIndex == minigameIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add new progress of a minigame
|
||||
/// </summary>
|
||||
/// <param name="progress">SavedMinigameProgress data record</param>
|
||||
public void AddMinigameProgress(SavedMinigameProgress progress)
|
||||
{
|
||||
storedUserData.minigames.Add(progress);
|
||||
}
|
||||
}
|
||||
|
||||
19
Assets/Accounts/Scripts/UserAvatarList.cs
Normal file
19
Assets/Accounts/Scripts/UserAvatarList.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Container to hold a reference to all possible user avatar sprites
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Create new Scriptable/User Avatar List")]
|
||||
public class UserAvatarList : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// List of avatar sprites
|
||||
/// </summary>
|
||||
public List<Sprite> avatars = new List<Sprite>();
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
UserList.AVATARS = avatars;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30234b937b9c84460ad4846ae1941484
|
||||
guid: 81765ea55baf15d45b01b02187b02429
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -8,11 +8,6 @@ using UnityEngine.UI;
|
||||
/// </summary>
|
||||
public class UserCard : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the userlist
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// User to upload info into this card
|
||||
/// </summary>
|
||||
@@ -53,8 +48,8 @@ public class UserCard : MonoBehaviour
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
avatar.sprite = user.avatar;
|
||||
username.text = user.username;
|
||||
avatar.sprite = user.GetAvatar();
|
||||
username.text = user.GetUsername();
|
||||
button.onClick.AddListener(selectUser);
|
||||
}
|
||||
|
||||
@@ -63,10 +58,9 @@ public class UserCard : MonoBehaviour
|
||||
/// </summary>
|
||||
public void DeleteUser()
|
||||
{
|
||||
if (userList.DeleteUser(user))
|
||||
if (UserList.DeleteUser(user.GetUsername()))
|
||||
{
|
||||
// User is removed, update and save
|
||||
userList.Save();
|
||||
updateUserCardContainer();
|
||||
}
|
||||
else
|
||||
|
||||
@@ -34,20 +34,9 @@ public class UserCreationScreen : MonoBehaviour
|
||||
/// </summary>
|
||||
public GameObject avatarPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// List of all sprites that are supported as avatars
|
||||
/// </summary>
|
||||
public List<Sprite> sprites = new List<Sprite>();
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the UserList ScriptableObject
|
||||
/// </summary>
|
||||
public UserList users;
|
||||
|
||||
/// <summary>
|
||||
/// Current selected avatar
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private int selectedAvatar = 0;
|
||||
|
||||
/// <summary>
|
||||
@@ -63,7 +52,7 @@ public class UserCreationScreen : MonoBehaviour
|
||||
{
|
||||
errorMessage.SetActive(false);
|
||||
|
||||
for (int i = 0; i < sprites.Count; i++)
|
||||
for (int i = 0; i < UserList.AVATARS.Count; i++)
|
||||
{
|
||||
// Create instance of prefab
|
||||
GameObject instance = GameObject.Instantiate(avatarPrefab, avatarsContainer);
|
||||
@@ -79,7 +68,7 @@ public class UserCreationScreen : MonoBehaviour
|
||||
// Set background color
|
||||
background.color = selectedAvatar == i ? Color.blue : Color.gray;
|
||||
// Find correct component for setting the sprite
|
||||
instance.transform.Find("Image").GetComponent<Image>().sprite = sprites[i];
|
||||
instance.transform.Find("Image").GetComponent<Image>().sprite = UserList.AVATARS[i];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +102,10 @@ public class UserCreationScreen : MonoBehaviour
|
||||
string username = inputName.text;
|
||||
if (IsValidUsername(username))
|
||||
{
|
||||
if (users.GetUserByUsername(username) == null)
|
||||
if (UserList.GetUserByUsername(username) == null)
|
||||
{
|
||||
// Create a new entry in the UserList ScriptableObject
|
||||
users.ChangeCurrentUser(users.CreateAndAddNewUser(username, sprites[selectedAvatar]));
|
||||
UserList.AddUser(username, UserList.AVATARS[selectedAvatar]);
|
||||
SystemController.GetInstance().BackToPreviousScene();
|
||||
}
|
||||
// Warn user that username already exists
|
||||
|
||||
@@ -1,87 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Keep track of all users
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Create new Scriptable/UserList")]
|
||||
public class UserList : ScriptableObject
|
||||
public static class UserList
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class to enable serialization of the UserList class
|
||||
/// (<c>ScriptableObject</c>s cannot be serialized)
|
||||
/// List of possible avatar sprites
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class StoredUserList
|
||||
{
|
||||
/// <summary>
|
||||
/// The index of the current/last logged in user in the <c>storedUsers</c> list
|
||||
/// </summary>
|
||||
public int currentUserIndex = -1;
|
||||
|
||||
/// <summary>
|
||||
/// A list containing all users (which can be serialized)
|
||||
/// </summary>
|
||||
public List<User> storedUsers = new List<User>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the serializable version of <c>UserList</c>
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private StoredUserList storedUserList = new StoredUserList();
|
||||
|
||||
/// <summary>
|
||||
/// Path of the <c>.json</c>-file to store all serialized data
|
||||
/// </summary>
|
||||
public static string PATH = null;
|
||||
|
||||
/// <summary>
|
||||
/// OnEnable will make sure the <c>PATH</c>-variable is correctly initialized
|
||||
/// </summary>
|
||||
void OnEnable()
|
||||
{
|
||||
// The PATH variable can be set by the testing framework,
|
||||
// so we don't overwrite the actual userlist with test data
|
||||
if (PATH == null)
|
||||
{
|
||||
PATH = $"{Application.persistentDataPath}/users.json";
|
||||
Load();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new user
|
||||
/// </summary>
|
||||
/// <param name="name">The username of the new user</param>
|
||||
/// <param name="avatar">Reference to the user avatar</param>
|
||||
/// <returns>A newly created user</returns>
|
||||
public User CreateNewUser(string name, Sprite avatar)
|
||||
{
|
||||
User user = new User();
|
||||
user.username = name;
|
||||
user.avatar = avatar;
|
||||
return user;
|
||||
}
|
||||
public static List<Sprite> AVATARS = new List<Sprite>();
|
||||
|
||||
/// <summary>
|
||||
/// Create a new user and save (add to list)
|
||||
/// </summary>
|
||||
/// <param name="name">The username of the new user</param>
|
||||
/// <param name="username">The username of the new user</param>
|
||||
/// <param name="avatar">Reference to the user avatar</param>
|
||||
/// <returns>A newly created user</returns>
|
||||
public User CreateAndAddNewUser(string name, Sprite avatar)
|
||||
public static User AddUser(string username, Sprite avatar)
|
||||
{
|
||||
User user = CreateNewUser(name, avatar);
|
||||
storedUserList.storedUsers.Add(user);
|
||||
if (storedUserList.storedUsers.Count == 1)
|
||||
{
|
||||
storedUserList.currentUserIndex = 0;
|
||||
}
|
||||
Save();
|
||||
return user;
|
||||
PersistentDataController pdc = PersistentDataController.GetInstance();
|
||||
PersistentDataController.SavedUserData data = new PersistentDataController.SavedUserData();
|
||||
data.username = username;
|
||||
data.playtime = 0.0;
|
||||
data.avatarIndex = AVATARS.IndexOf(avatar);
|
||||
|
||||
pdc.AddUser(data);
|
||||
return new User(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,43 +35,61 @@ public class UserList : ScriptableObject
|
||||
/// </summary>
|
||||
/// <param name="username">The username of the user</param>
|
||||
/// <returns><c>User</c>-object if a user with such username was found, <c>null</c> otherwise</returns>
|
||||
public User GetUserByUsername(string username)
|
||||
public static User GetUserByUsername(string username)
|
||||
{
|
||||
foreach (User user in storedUserList.storedUsers)
|
||||
if (user.username == username) return user;
|
||||
foreach (User user in GetUsers())
|
||||
if (user.GetUsername() == username) return user;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a user by index
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the user</param>
|
||||
/// <returns>User object</returns>
|
||||
/// <exception cref="IndexOutOfRangeException"></exception>
|
||||
public static User GetUserByIndex(int index)
|
||||
{
|
||||
List<User> users = GetUsers();
|
||||
if (index < 0 || users.Count <= index)
|
||||
throw new IndexOutOfRangeException();
|
||||
|
||||
return users[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all users currently stored
|
||||
/// </summary>
|
||||
/// <returns>A list of all users</returns>
|
||||
public List<User> GetUsers()
|
||||
public static List<User> GetUsers()
|
||||
{
|
||||
return storedUserList.storedUsers;
|
||||
PersistentDataController pdc = PersistentDataController.GetInstance();
|
||||
return pdc.GetUsers().ConvertAll((d) => new User(d));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current logged in user
|
||||
/// </summary>
|
||||
/// <returns>The current logged in user</returns>
|
||||
public User GetCurrentUser()
|
||||
public static User GetCurrentUser()
|
||||
{
|
||||
if (storedUserList.storedUsers.Count == 0)
|
||||
{
|
||||
List<User> users = GetUsers();
|
||||
if (users.Count == 0)
|
||||
return null;
|
||||
}
|
||||
return storedUserList.storedUsers[storedUserList.currentUserIndex];
|
||||
return users[PersistentDataController.GetInstance().GetCurrentUser()];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the index in the userlist of the current playing user
|
||||
/// Get the index in the userlist of a user
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetCurrentUserIndex()
|
||||
public static int IndexOf(string username)
|
||||
{
|
||||
return storedUserList.currentUserIndex;
|
||||
int idx = GetUsers().FindIndex((e) => e.GetUsername() == username);
|
||||
if (idx < 0)
|
||||
throw new KeyNotFoundException();
|
||||
return idx;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -133,89 +97,66 @@ public class UserList : ScriptableObject
|
||||
/// </summary>
|
||||
/// <param name="index">Index of the user in the userlist</param>
|
||||
/// <exception cref="IndexOutOfRangeException"></exception>
|
||||
public void ChangeCurrentUser(int index)
|
||||
public static void ChangeCurrentUser(int index)
|
||||
{
|
||||
if (0 <= index && index < storedUserList.storedUsers.Count)
|
||||
{
|
||||
storedUserList.currentUserIndex = index;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (index < 0 || GetUsers().Count <= index)
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
PersistentDataController.GetInstance().SetCurrentUser(index, true);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Change the current user
|
||||
/// </summary>
|
||||
/// <param name="user">Reference to the user in the userlist</param>
|
||||
/// <param name="index">Username of the user</param>
|
||||
/// <exception cref="KeyNotFoundException"></exception>
|
||||
public void ChangeCurrentUser(User user)
|
||||
public static void ChangeCurrentUser(string username)
|
||||
{
|
||||
int idx = storedUserList.storedUsers.IndexOf(user);
|
||||
if (idx < 0)
|
||||
{
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
storedUserList.currentUserIndex = idx;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove the user
|
||||
/// </summary>
|
||||
/// <param name="index">The index of the user in the userlist</param>
|
||||
/// <returns>true if user was successful removed, false otherwise</returns
|
||||
public bool DeleteUser(int index)
|
||||
{
|
||||
if (0 <= index && index < storedUserList.storedUsers.Count)
|
||||
{
|
||||
return DeleteUser(storedUserList.storedUsers[index]);
|
||||
}
|
||||
return false;
|
||||
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
|
||||
try { ChangeCurrentUser(index); }
|
||||
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I am inevitable, *snap*
|
||||
/// </summary>
|
||||
/// <param name="user">Reference to the user to be removed</param>
|
||||
/// <param name="index">The index of the user in the userlist</param>
|
||||
/// <returns>true if the user was successful removed, false otherwise</returns>
|
||||
public bool DeleteUser(User user)
|
||||
/// <exception cref="IndexOutOfRangeException"></exception>
|
||||
public static bool DeleteUser(int index)
|
||||
{
|
||||
if (1 < storedUserList.storedUsers.Count)
|
||||
{
|
||||
if (storedUserList.currentUserIndex == storedUserList.storedUsers.Count - 1)
|
||||
{
|
||||
storedUserList.currentUserIndex--;
|
||||
}
|
||||
List<User> users = GetUsers();
|
||||
if (index < 0 || users.Count <= index)
|
||||
throw new IndexOutOfRangeException();
|
||||
|
||||
return storedUserList.storedUsers.Remove(user);
|
||||
if (1 < users.Count)
|
||||
{
|
||||
PersistentDataController.GetInstance().DeleteUser(index);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the users
|
||||
/// Delete a user from the userliset
|
||||
/// </summary>
|
||||
public void Save()
|
||||
/// <param name="username">Username of the user</param>
|
||||
/// <returns>true if the user was successful removed, false otherwise</returns>
|
||||
/// <exception cref="KeyNotFoundException"></exception>
|
||||
public static bool DeleteUser(string username)
|
||||
{
|
||||
string json = JsonUtility.ToJson(storedUserList);
|
||||
File.CreateText(PATH).Close();
|
||||
File.WriteAllText(PATH, json);
|
||||
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
|
||||
|
||||
try { return DeleteUser(index); }
|
||||
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override the current content of the userlist by what is stored on disk
|
||||
/// Save the current UserList
|
||||
/// </summary>
|
||||
public void Load()
|
||||
public static void Save()
|
||||
{
|
||||
storedUserList.storedUsers.Clear();
|
||||
storedUserList.currentUserIndex = -1;
|
||||
|
||||
if (!File.Exists(PATH))
|
||||
{
|
||||
Save();
|
||||
}
|
||||
string text = File.ReadAllText(PATH);
|
||||
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
|
||||
PersistentDataController.GetInstance().Save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,11 +10,6 @@ using UnityEngine.UI;
|
||||
/// </summary>
|
||||
public class UserProgressScreen : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the userlist
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the current user
|
||||
/// </summary>
|
||||
@@ -112,12 +107,12 @@ public class UserProgressScreen : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
// Assign the current user
|
||||
userList.Load();
|
||||
user = userList.GetCurrentUser();
|
||||
PersistentDataController.GetInstance().Load();
|
||||
user = UserList.GetCurrentUser();
|
||||
|
||||
// Set correct displayed items
|
||||
username.text = user.username;
|
||||
avatar.sprite = user.avatar;
|
||||
username.text = user.GetUsername();
|
||||
avatar.sprite = user.GetAvatar();
|
||||
// TODO: implement total playtime
|
||||
//playtime.text = $"Totale speeltijd: {user.playtime.ToString("0.00")}";
|
||||
|
||||
@@ -126,9 +121,10 @@ public class UserProgressScreen : MonoBehaviour
|
||||
|
||||
int i = 0;
|
||||
// Display courses
|
||||
coursesContainer.SetActive(user.courses.Count > 0);
|
||||
emptyCourses.SetActive(user.courses.Count <= 0);
|
||||
foreach (Progress courseProgress in user.courses)
|
||||
var courses = user.GetCourses();
|
||||
coursesContainer.SetActive(courses.Count > 0);
|
||||
emptyCourses.SetActive(courses.Count <= 0);
|
||||
foreach (var courseProgress in courses)
|
||||
{
|
||||
// Create instance of prefab
|
||||
GameObject instance = GameObject.Instantiate(courseCardPrefab, coursesContainer.transform.Find("Viewport").Find("Content").transform);
|
||||
@@ -142,13 +138,14 @@ public class UserProgressScreen : MonoBehaviour
|
||||
// Store reference to background so we can apply fancy coloring
|
||||
Image background = instance.GetComponent<Image>();
|
||||
background.color = Color.gray;
|
||||
activities.Add(Tuple.Create(background, (int)courseProgress.Get<CourseIndex>("courseIndex")));
|
||||
activities.Add(Tuple.Create(background, (int)courseProgress.courseIndex));
|
||||
}
|
||||
|
||||
// Display minigames
|
||||
minigamesContainer.SetActive(user.minigames.Count > 0);
|
||||
emptyMinigames.SetActive(user.minigames.Count <= 0);
|
||||
foreach (Progress minigameProgress in user.minigames)
|
||||
var minigames = user.GetMinigames();
|
||||
minigamesContainer.SetActive(minigames.Count > 0);
|
||||
emptyMinigames.SetActive(minigames.Count <= 0);
|
||||
foreach (var minigameProgress in minigames)
|
||||
{
|
||||
// Create instance of prefab
|
||||
GameObject instance = GameObject.Instantiate(minigameCardPrefab, minigamesContainer.transform.Find("Viewport").Find("Content").transform);
|
||||
@@ -162,7 +159,7 @@ public class UserProgressScreen : MonoBehaviour
|
||||
// Store reference to background so we can apply fancy coloring
|
||||
Image background = instance.GetComponent<Image>();
|
||||
background.color = Color.gray;
|
||||
activities.Add(Tuple.Create(background, (int)minigameProgress.Get<MinigameIndex>("minigameIndex")));
|
||||
activities.Add(Tuple.Create(background, (int)minigameProgress.minigameIndex));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,7 +180,7 @@ public class UserProgressScreen : MonoBehaviour
|
||||
|
||||
selectedActivity = newActivity;
|
||||
activities[selectedActivity].Item1.color = Color.blue;
|
||||
if (selectedActivity < user.courses.Count)
|
||||
if (selectedActivity < user.GetCourses().Count)
|
||||
{
|
||||
// TODO: create a better graph
|
||||
//DisplayCourseGraph((CourseIndex)activities[selectedActivity].Item2);
|
||||
@@ -211,9 +208,9 @@ public class UserProgressScreen : MonoBehaviour
|
||||
/// <param name="minigameIndex">Index of the minigame</param>
|
||||
private void DisplayMinigameGraph(MinigameIndex minigameIndex)
|
||||
{
|
||||
Progress progress = user.GetMinigameProgress(minigameIndex);
|
||||
List<Score> latestScores = progress.Get<List<Score>>("latestScores");
|
||||
List<Score> highestScores = progress.Get<List<Score>>("highestScores");
|
||||
var progress = user.GetMinigameProgress(minigameIndex);
|
||||
List<Score> latestScores = progress.latestScores;
|
||||
List<Score> highestScores = progress.highestScores;
|
||||
if (0 < highestScores.Count)
|
||||
{
|
||||
PlotGraph(latestScores.ConvertAll<double>((s) => (double)s.scoreValue), highestScores[0].scoreValue);
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner",
|
||||
"AccountsScripts",
|
||||
"InterfacesScripts"
|
||||
"InterfacesScripts",
|
||||
"SystemArchitecture"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
|
||||
@@ -1,213 +0,0 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Test the Progress class
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class ProgressTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the progress object to be tested
|
||||
/// </summary>
|
||||
private Progress progress;
|
||||
|
||||
/// <summary>
|
||||
/// A dummy serializable struct to perform test operations on
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
private struct SerializableStruct
|
||||
{
|
||||
public int r, g, b;
|
||||
public float x, y, z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A dummy non-serializable struct to perform test operations on
|
||||
/// </summary>
|
||||
private struct NonSerializableStruct
|
||||
{
|
||||
public int r, g, b;
|
||||
public float x, y, z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup the tests
|
||||
/// </summary>
|
||||
[SetUp]
|
||||
public void Setup_Progress()
|
||||
{
|
||||
progress = new Progress();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for creation of a new progress
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_New_Progress()
|
||||
{
|
||||
Assert.IsNotNull(progress);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether invalid data will not be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_InvalidData()
|
||||
{
|
||||
Assert.IsFalse(progress.AddOrUpdate<GameObject>("key", null));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a duplicated key will be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_DuplicateKey()
|
||||
{
|
||||
progress.AddOrUpdate<int>("key 1", 0);
|
||||
Assert.IsTrue(progress.AddOrUpdate<int>("key 1", 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>int</c> value can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_Int()
|
||||
{
|
||||
Assert.IsTrue(progress.AddOrUpdate<int>("key", 1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>double</c> value can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_Double()
|
||||
{
|
||||
Assert.IsTrue(progress.AddOrUpdate<double>("key", 1.0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>string</c> value can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_String()
|
||||
{
|
||||
Assert.IsTrue(progress.AddOrUpdate<string>("key", "Hello World!"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a serializable struct can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_SerializableStruct()
|
||||
{
|
||||
Assert.IsTrue(progress.AddOrUpdate<SerializableStruct>("key", new SerializableStruct()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a non-serializable struct will throw an error
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Add_NonSerializableStruct()
|
||||
{
|
||||
NonSerializableStruct nss = new NonSerializableStruct();
|
||||
Assert.Throws<SerializationException>(delegate { progress.AddOrUpdate<NonSerializableStruct>("key", nss); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a key is present
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Has_ValidKey()
|
||||
{
|
||||
progress.AddOrUpdate<int>("key", 1);
|
||||
Assert.IsTrue(progress.Has("key"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a key is not present
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Has_InvalidKey()
|
||||
{
|
||||
Assert.IsFalse(progress.Has("non-existing key"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether an invalid key will throw an error
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_InvalidKey()
|
||||
{
|
||||
Assert.Throws<KeyNotFoundException>(delegate { progress.Get<int>("non-existing key"); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether an invalid type will throw an error
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_InvalidType()
|
||||
{
|
||||
progress.AddOrUpdate<int>("key", 123456789);
|
||||
Assert.Throws<InvalidCastException>(delegate { progress.Get<double>("key"); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a value is correctly updated
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Update()
|
||||
{
|
||||
progress.AddOrUpdate<int>("key", 1);
|
||||
Assert.AreEqual(progress.Get<int>("key"), 1);
|
||||
progress.AddOrUpdate<int>("key", 2);
|
||||
Assert.AreEqual(progress.Get<int>("key"), 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>int</c> value can be read
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_Int()
|
||||
{
|
||||
progress.AddOrUpdate<int>("key", 1);
|
||||
Assert.AreEqual(progress.Get<int>("key"), 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>double</c> value can be read
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_Double()
|
||||
{
|
||||
progress.AddOrUpdate<double>("key", 1.0);
|
||||
Assert.AreEqual(progress.Get<double>("key"), 1.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a <c>string</c> value can be read
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_String()
|
||||
{
|
||||
progress.AddOrUpdate<string>("key", "Hello World!");
|
||||
Assert.AreEqual(progress.Get<string>("key"), "Hello World!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a serializable struct can be read
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Progress_Get_Struct()
|
||||
{
|
||||
int R = 1, G = 10, B = 100;
|
||||
float X = 0.1f, Y = 0.01f, Z = 0.001f;
|
||||
SerializableStruct data = new SerializableStruct { r = R, g = G, b = B, x = X, y = Y, z = Z };
|
||||
progress.AddOrUpdate<SerializableStruct>("key", data);
|
||||
SerializableStruct result = progress.Get<SerializableStruct>("key");
|
||||
Assert.AreEqual(result, data);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
@@ -12,20 +12,10 @@ using UnityEngine;
|
||||
public class UserListTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new path so the existing users.json file will not be overwritten
|
||||
/// Create a new path so the existing .json file will not be overwritten
|
||||
/// </summary>
|
||||
private static string PATH = $"{Application.persistentDataPath}/unit_test_users.json";
|
||||
|
||||
/// <summary>
|
||||
/// NUnit test magic (for skipping the setup)
|
||||
/// </summary>
|
||||
public const string SKIP_SETUP = "SKIP_SETUP";
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the userlist to be tested
|
||||
/// </summary>
|
||||
private UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// Helper variable for quick user creation
|
||||
/// </summary>
|
||||
@@ -34,11 +24,7 @@ public class UserListTest
|
||||
/// <summary>
|
||||
/// Helper variable for quick user creation
|
||||
/// </summary>
|
||||
private Sprite avatar = Sprite.Create(
|
||||
Texture2D.blackTexture,
|
||||
new Rect(0, 0, Texture2D.blackTexture.width, Texture2D.blackTexture.height),
|
||||
new Vector2(0.5f, 0.5f)
|
||||
);
|
||||
private Sprite avatar = null;
|
||||
|
||||
/// <summary>
|
||||
/// Setup the tests
|
||||
@@ -46,27 +32,14 @@ public class UserListTest
|
||||
[SetUp]
|
||||
public void Setup_UserList()
|
||||
{
|
||||
// Check whether the current test needs to skip the setup
|
||||
ArrayList cat = TestContext.CurrentContext.Test.Properties["_CATEGORIES"] as ArrayList;
|
||||
bool skip = cat != null && cat.Contains(SKIP_SETUP);
|
||||
if (!skip)
|
||||
{
|
||||
// The actual setup code
|
||||
UserList.PATH = UserListTest.PATH;
|
||||
userList = ScriptableObject.CreateInstance<UserList>();
|
||||
}
|
||||
}
|
||||
PersistentDataController.PATH = UserListTest.PATH;
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the UserList.PATH is correctly set
|
||||
/// </summary>
|
||||
[Test]
|
||||
[Category(SKIP_SETUP)]
|
||||
public void Test_UserList_OnEnable()
|
||||
{
|
||||
UserList.PATH = null;
|
||||
userList = ScriptableObject.CreateInstance<UserList>();
|
||||
Assert.AreEqual($"{Application.persistentDataPath}/users.json", UserList.PATH);
|
||||
if (File.Exists(PATH))
|
||||
File.Delete(PATH);
|
||||
|
||||
PersistentDataController.GetInstance().Load();
|
||||
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
|
||||
avatar = UserList.AVATARS[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,36 +48,22 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_New_UserList()
|
||||
{
|
||||
Assert.IsNotNull(userList);
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for creation of new user (without adding the user to the list)
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_CreateNewUser()
|
||||
public void Test_UserList_AddUser()
|
||||
{
|
||||
User user = userList.CreateNewUser(username, avatar);
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
User user = UserList.AddUser(username, avatar);
|
||||
|
||||
Assert.IsNotNull(user);
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
Assert.AreEqual(username, user.username);
|
||||
Assert.AreEqual(avatar, user.avatar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for creating a new user and adding the user to the list
|
||||
/// </summary>
|
||||
public void Test_UserList_CreateAndAddNewUser()
|
||||
{
|
||||
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
|
||||
User user = userList.CreateAndAddNewUser(username, avatar);
|
||||
|
||||
Assert.IsNotNull(user);
|
||||
Assert.AreEqual(1, userList.GetUsers().Count);
|
||||
Assert.AreEqual(user, userList.GetUsers()[0]);
|
||||
Assert.Zero(userList.GetCurrentUserIndex());
|
||||
Assert.AreEqual(1, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(username, user.GetUsername());
|
||||
Assert.AreEqual(avatar, user.GetAvatar());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -113,9 +72,9 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByUsername_Valid()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser(username, avatar);
|
||||
User v = userList.GetUserByUsername(username);
|
||||
Assert.AreEqual(u, v);
|
||||
User u = UserList.AddUser(username, avatar);
|
||||
User v = UserList.GetUserByUsername(username);
|
||||
Assert.AreEqual(u.GetUsername(), v.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -124,32 +83,70 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByUsername_Null()
|
||||
{
|
||||
User user = userList.GetUserByUsername("not-a-user");
|
||||
User user = UserList.GetUserByUsername("not-a-user");
|
||||
Assert.IsNull(user);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test whether an existing user can be found by its username
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByIndex_Valid()
|
||||
{
|
||||
User u = UserList.AddUser(username, avatar);
|
||||
User v = UserList.GetUserByIndex(0);
|
||||
Assert.AreEqual(u.GetUsername(), v.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a non-existing user can not be found
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByIndex_Exception()
|
||||
{
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.GetUserByIndex(0); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a userlist is correctly returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUsers()
|
||||
{
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
u.Add(UserList.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
List<User> v = UserList.GetUsers();
|
||||
Assert.AreEqual(v.Count, u.Count);
|
||||
for (int i = 0; i < 5; i++)
|
||||
Assert.AreEqual(u[i].GetUsername(), v[i].GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct current user is returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrentUser()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(2);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
User W = userList.GetCurrentUser();
|
||||
Assert.AreEqual(w, W);
|
||||
User W = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(w.GetUsername(), W.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a null user is returned when the userlist is empty
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrent_Empty()
|
||||
public void Test_UserList_GetCurrentUser_Empty()
|
||||
{
|
||||
User user = userList.GetCurrentUser();
|
||||
User user = UserList.GetCurrentUser();
|
||||
Assert.IsNull(user);
|
||||
}
|
||||
|
||||
@@ -157,24 +154,30 @@ public class UserListTest
|
||||
/// Test whether the correct index is returned for the current user
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrentUserIndex()
|
||||
public void Test_UserList_IndexOf_Valid()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(2);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
int idx = userList.GetCurrentUserIndex();
|
||||
Assert.AreEqual(2, idx);
|
||||
Assert.AreEqual(0, UserList.IndexOf(u.GetUsername()));
|
||||
Assert.AreEqual(1, UserList.IndexOf(v.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.IndexOf(w.GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a bad index is returned when the userlist is empty
|
||||
/// Test whether the correct index is returned for the current user
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrentUserIndex_Empty()
|
||||
public void Test_UserList_IndexOf_Exception()
|
||||
{
|
||||
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
|
||||
User u = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = username,
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.IndexOf(u.GetUsername()); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -183,13 +186,13 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_ValidIndex()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(2);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
User W = userList.GetCurrentUser();
|
||||
Assert.AreEqual(w, W);
|
||||
User W = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(w.GetUsername(), W.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -198,11 +201,11 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_InvalidIndex()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(-1); });
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(5); });
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.ChangeCurrentUser(-1); });
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.ChangeCurrentUser(5); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -211,41 +214,45 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_IndexEmpty()
|
||||
{
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(0); });
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.ChangeCurrentUser(0); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user is correctly changed
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_ValidUser()
|
||||
public void Test_UserList_ChangeCurrentUser_ValidUsername()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(v);
|
||||
User V = userList.GetCurrentUser();
|
||||
Assert.AreEqual(v, V);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(v.GetUsername());
|
||||
User V = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(v.GetUsername(), V.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user is not changed when a non-existing user is given
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_InvalidUser()
|
||||
public void Test_UserList_ChangeCurrentUser_InvalidUsername()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateNewUser($"{username}_{'v'}", avatar);
|
||||
Assert.Throws<KeyNotFoundException>(delegate { userList.ChangeCurrentUser(v); });
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = $"{username}_{'v'}",
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.ChangeCurrentUser(v.GetUsername()); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user is not changed when a null user is given
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_NullUser()
|
||||
public void Test_UserList_ChangeCurrentUser_NullUsername()
|
||||
{
|
||||
Assert.Throws<KeyNotFoundException>(delegate { userList.ChangeCurrentUser(null); });
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.ChangeCurrentUser(null); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -254,10 +261,10 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_ValidIndex()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
Assert.IsTrue(userList.DeleteUser(1));
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
Assert.IsTrue(UserList.DeleteUser(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -266,11 +273,11 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_InValidIndex()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
Assert.IsFalse(userList.DeleteUser(-1));
|
||||
Assert.IsFalse(userList.DeleteUser(5));
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.DeleteUser(-1); });
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.DeleteUser(5); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -279,44 +286,61 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_IndexEmpty()
|
||||
{
|
||||
Assert.IsFalse(userList.DeleteUser(0));
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.DeleteUser(0); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_LastValid()
|
||||
public void Test_UserList_DeleteUser_BeforeCurrentUser()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(2);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, userList.GetUsers().Count);
|
||||
Assert.AreEqual(2, userList.GetCurrentUserIndex());
|
||||
Assert.IsTrue(userList.DeleteUser(w));
|
||||
Assert.AreEqual(2, userList.GetUsers().Count);
|
||||
Assert.AreEqual(1, userList.GetCurrentUserIndex());
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(u.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(0, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting an existing user will remove the user correctly
|
||||
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
|
||||
/// </summary>
|
||||
/// <remarks>This will change the currentUserIndex to point to another user</remarks>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_FirstValid()
|
||||
public void Test_UserList_DeleteUser_CurrentUser()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
|
||||
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
|
||||
userList.ChangeCurrentUser(0);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, userList.GetUsers().Count);
|
||||
Assert.AreEqual(0, userList.GetCurrentUserIndex());
|
||||
Assert.IsTrue(userList.DeleteUser(u));
|
||||
Assert.AreEqual(2, userList.GetUsers().Count);
|
||||
Assert.AreEqual(0, userList.GetCurrentUserIndex());
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(v.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(0, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_AfterCurrentUser()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(w.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -325,12 +349,16 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_Invalid()
|
||||
{
|
||||
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
|
||||
User v = userList.CreateNewUser($"{username}_{'v'}", avatar);
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = $"{username}_{'v'}",
|
||||
avatarIndex = 0
|
||||
});
|
||||
|
||||
Assert.AreEqual(1, userList.GetUsers().Count);
|
||||
Assert.IsFalse(userList.DeleteUser(v));
|
||||
Assert.AreEqual(1, userList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.GetUsers().Count);
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.DeleteUser(v.GetUsername()); });
|
||||
Assert.AreEqual(1, UserList.GetUsers().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -339,10 +367,14 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_Empty()
|
||||
{
|
||||
User user = userList.CreateNewUser(username, avatar);
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
Assert.IsFalse(userList.DeleteUser(user));
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
User user = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = username,
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.DeleteUser(user.GetUsername()); });
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -351,23 +383,16 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_Save_New()
|
||||
{
|
||||
if (File.Exists(PATH))
|
||||
{
|
||||
File.Delete(PATH);
|
||||
}
|
||||
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
|
||||
}
|
||||
userList.ChangeCurrentUser(3);
|
||||
userList.Save();
|
||||
u.Add(UserList.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
UserList.ChangeCurrentUser(3);
|
||||
UserList.Save();
|
||||
FileAssert.Exists(PATH);
|
||||
|
||||
string content = File.ReadAllText(PATH);
|
||||
int id = avatar.GetInstanceID();
|
||||
string expected = $"{{\"currentUserIndex\":3,\"storedUsers\":[{{\"username\":\"u5erNam3_0\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_1\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_2\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_3\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_4\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}}]}}";
|
||||
string expected = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[{{\"entries\":[],\"username\":\"u5erNam3_0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_1\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_2\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_3\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_4\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":3,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
Assert.AreEqual(expected, content);
|
||||
}
|
||||
|
||||
@@ -385,16 +410,14 @@ public class UserListTest
|
||||
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
|
||||
}
|
||||
userList.ChangeCurrentUser(3);
|
||||
userList.Save();
|
||||
u.Add(UserList.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
UserList.ChangeCurrentUser(3);
|
||||
UserList.Save();
|
||||
FileAssert.Exists(PATH);
|
||||
|
||||
string content = File.ReadAllText(PATH);
|
||||
int id = avatar.GetInstanceID();
|
||||
string expected = $"{{\"currentUserIndex\":3,\"storedUsers\":[{{\"username\":\"u5erNam3_0\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_1\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_2\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_3\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_4\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}}]}}";
|
||||
string expected = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[{{\"entries\":[],\"username\":\"u5erNam3_0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_1\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_2\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_3\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_4\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":3,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
Assert.AreEqual(expected, content);
|
||||
}
|
||||
|
||||
@@ -404,67 +427,11 @@ public class UserListTest
|
||||
[Test]
|
||||
public void Test_UserList_Save_Empty()
|
||||
{
|
||||
userList.Save();
|
||||
UserList.Save();
|
||||
FileAssert.Exists(PATH);
|
||||
|
||||
string content = File.ReadAllText(PATH);
|
||||
string expected = "{\"currentUserIndex\":-1,\"storedUsers\":[]}";
|
||||
string expected = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
Assert.AreEqual(expected, content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a userlist (containing some users) is correrctly loaded
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_Load()
|
||||
{
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
|
||||
}
|
||||
userList.ChangeCurrentUser(3);
|
||||
userList.Save();
|
||||
|
||||
userList.Load();
|
||||
Assert.AreEqual(userList.GetUsers().Count, u.Count);
|
||||
Assert.AreEqual(userList.GetCurrentUserIndex(), 3);
|
||||
List<User> v = userList.GetUsers();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
Assert.AreEqual(u[i].username, v[i].username);
|
||||
Assert.AreEqual(u[i].avatar, v[i].avatar);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether an empty userlist is correctly loaded
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_Load_Empty()
|
||||
{
|
||||
userList.Save();
|
||||
userList.Load();
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test if the user save file is not found, a new one will be created and an empty userlist will be loaded
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_Load_NotFound()
|
||||
{
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
|
||||
}
|
||||
userList.ChangeCurrentUser(3);
|
||||
userList.Save();
|
||||
|
||||
File.Delete(PATH);
|
||||
userList.Load();
|
||||
Assert.Zero(userList.GetUsers().Count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,10 @@ public class UserTest
|
||||
[SetUp]
|
||||
public void Setup_User()
|
||||
{
|
||||
user = new User();
|
||||
PersistentDataController.SavedUserData data = new PersistentDataController.SavedUserData();
|
||||
data.username = "username";
|
||||
data.avatarIndex = 0;
|
||||
user = new User(data);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,8 +33,8 @@ public class UserTest
|
||||
public void Test_New_User()
|
||||
{
|
||||
Assert.NotNull(user);
|
||||
Assert.Zero(user.courses.Count);
|
||||
Assert.Zero(user.minigames.Count);
|
||||
Assert.Zero(user.GetCourses().Count);
|
||||
Assert.Zero(user.GetMinigames().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,10 +43,10 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_AddCourse()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
user.courses.Add(p);
|
||||
Assert.AreEqual(user.courses.Count, 1);
|
||||
Assert.Zero(user.minigames.Count);
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
user.AddCourseProgress(p);
|
||||
Assert.AreEqual(user.GetCourses().Count, 1);
|
||||
Assert.Zero(user.GetMinigames().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,10 +55,10 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_AddMinigame()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
user.minigames.Add(p);
|
||||
Assert.Zero(user.courses.Count);
|
||||
Assert.AreEqual(user.minigames.Count, 1);
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
user.AddMinigameProgress(p);
|
||||
Assert.Zero(user.GetCourses().Count);
|
||||
Assert.AreEqual(user.GetMinigames().Count, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -73,10 +76,10 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_GetRecentCourses_All()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
|
||||
p.AddOrUpdate<float>("courseProgress", 0.5f);
|
||||
user.courses.Add(p);
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 0.5f;
|
||||
user.AddCourseProgress(p);
|
||||
List<Tuple<CourseIndex, float>> list = user.GetRecentCourses();
|
||||
Assert.AreEqual(list.Count, 1);
|
||||
Assert.AreEqual(list[0].Item1, CourseIndex.FINGERSPELLING);
|
||||
@@ -101,10 +104,10 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_GetRecommendedCourses_All()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
|
||||
p.AddOrUpdate<float>("courseProgress", 0.5f);
|
||||
user.courses.Add(p);
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 0.5f;
|
||||
user.AddCourseProgress(p);
|
||||
List<Tuple<CourseIndex, float>> list = user.GetRecommendedCourses();
|
||||
Assert.AreEqual(list.Count, 1);
|
||||
Assert.AreEqual(list[0].Item1, CourseIndex.FINGERSPELLING);
|
||||
@@ -127,13 +130,13 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_GetCourseProgress_Valid()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
|
||||
p.AddOrUpdate<float>("courseProgress", 3.14159265f);
|
||||
user.courses.Add(p);
|
||||
Progress q = user.GetCourseProgress(CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.Get<CourseIndex>("courseIndex"), CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.Get<float>("courseProgress"), 3.14159265f);
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 3.14159265f;
|
||||
user.AddCourseProgress(p);
|
||||
var q = user.GetCourseProgress(CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.courseIndex, CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.progress, 3.14159265f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -145,9 +148,9 @@ public class UserTest
|
||||
Assert.Null(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE));
|
||||
Assert.Null(user.GetMinigameProgress((MinigameIndex)100));
|
||||
|
||||
Progress p = new Progress();
|
||||
p.AddOrUpdate<MinigameIndex>("minigameIndex", MinigameIndex.SPELLING_BEE);
|
||||
user.minigames.Add(p);
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
p.minigameIndex = MinigameIndex.SPELLING_BEE;
|
||||
user.AddMinigameProgress(p);
|
||||
Assert.Null(user.GetMinigameProgress(MinigameIndex.HANGMAN));
|
||||
}
|
||||
|
||||
@@ -157,10 +160,12 @@ public class UserTest
|
||||
[Test]
|
||||
public void Test_User_GetMinigameProgress_Valid()
|
||||
{
|
||||
Progress p = new Progress();
|
||||
p.AddOrUpdate<MinigameIndex>("minigameIndex", MinigameIndex.SPELLING_BEE);
|
||||
user.minigames.Add(p);
|
||||
Progress q = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE);
|
||||
Assert.AreEqual(q.Get<CourseIndex>("minigameIndex"), CourseIndex.FINGERSPELLING);
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
p.minigameIndex = MinigameIndex.SPELLING_BEE;
|
||||
user.AddMinigameProgress(p);
|
||||
var q = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE);
|
||||
Assert.AreEqual(q.minigameIndex, MinigameIndex.SPELLING_BEE);
|
||||
Assert.Zero(q.latestScores.Count);
|
||||
Assert.Zero(q.highestScores.Count);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user