Test accounts

This commit is contained in:
Dries Van Schuylenbergh
2023-03-25 15:35:26 +00:00
committed by Jelle De Geest
parent fee989006c
commit 7b6eb4db69
29 changed files with 691 additions and 1269 deletions

View File

@@ -19,7 +19,7 @@ public class UserList : ScriptableObject
/// <summary>
/// The index of the current/last logged in user in the <c>storedUsers</c> list
/// </summary>
public int currentUserIndex;
public int currentUserIndex = -1;
/// <summary>
/// A list containing all users (which can be serialized)
@@ -43,8 +43,13 @@ public class UserList : ScriptableObject
/// </summary>
void OnEnable()
{
PATH = $"{Application.persistentDataPath}/users.json";
Load();
// 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>
@@ -71,6 +76,10 @@ public class UserList : ScriptableObject
{
User user = CreateNewUser(name, avatar);
storedUserList.storedUsers.Add(user);
if (storedUserList.storedUsers.Count == 1)
{
storedUserList.currentUserIndex = 0;
}
Save();
return user;
}
@@ -103,6 +112,10 @@ public class UserList : ScriptableObject
/// <returns>The current logged in user</returns>
public User GetCurrentUser()
{
if (storedUserList.storedUsers.Count == 0)
{
return null;
}
return storedUserList.storedUsers[storedUserList.currentUserIndex];
}
@@ -119,28 +132,46 @@ public class UserList : ScriptableObject
/// Change the current user
/// </summary>
/// <param name="index">Index of the user in the userlist</param>
/// <exception cref="IndexOutOfRangeException"></exception>
public void ChangeCurrentUser(int index)
{
storedUserList.currentUserIndex = index;
if (0 <= index && index < storedUserList.storedUsers.Count)
{
storedUserList.currentUserIndex = index;
}
else
{
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Change the current user
/// </summary>
/// <param name="user">Reference to the user in the userlist</param>
/// <exception cref="KeyNotFoundException"></exception>
public void ChangeCurrentUser(User user)
{
storedUserList.currentUserIndex = storedUserList.storedUsers.IndexOf(user);
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>
/// <returns>true if user was successful removed, false otherwise</returns
public bool DeleteUser(int index)
{
return DeleteUser(storedUserList.storedUsers[index]);
if (0 <= index && index < storedUserList.storedUsers.Count)
{
return DeleteUser(storedUserList.storedUsers[index]);
}
return false;
}
/// <summary>
@@ -177,13 +208,14 @@ public class UserList : ScriptableObject
/// </summary>
public void Load()
{
try
{
storedUserList.storedUsers.Clear();
storedUserList.storedUsers.Clear();
storedUserList.currentUserIndex = -1;
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
if (!File.Exists(PATH))
{
Save();
}
catch (FileNotFoundException) { Debug.Log($"Path '{PATH}' not found"); }
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
}
}