Files
unity-application/Assets/Accounts/Scripts/UserList.cs
Dries Van Schuylenbergh 7b6eb4db69 Test accounts
2023-03-25 15:35:26 +00:00

222 lines
6.3 KiB
C#

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
{
/// <summary>
/// Helper class to enable serialization of the UserList class
/// (<c>ScriptableObject</c>s cannot be serialized)
/// </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;
}
/// <summary>
/// Create a new user and save (add to list)
/// </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 CreateAndAddNewUser(string name, Sprite avatar)
{
User user = CreateNewUser(name, avatar);
storedUserList.storedUsers.Add(user);
if (storedUserList.storedUsers.Count == 1)
{
storedUserList.currentUserIndex = 0;
}
Save();
return user;
}
/// <summary>
/// Get a user by username
/// </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)
{
foreach (User user in storedUserList.storedUsers)
if (user.username == username) return user;
return null;
}
/// <summary>
/// Get a list of all users currently stored
/// </summary>
/// <returns>A list of all users</returns>
public List<User> GetUsers()
{
return storedUserList.storedUsers;
}
/// <summary>
/// Get the current logged in user
/// </summary>
/// <returns>The current logged in user</returns>
public User GetCurrentUser()
{
if (storedUserList.storedUsers.Count == 0)
{
return null;
}
return storedUserList.storedUsers[storedUserList.currentUserIndex];
}
/// <summary>
/// Get the index in the userlist of the current playing user
/// </summary>
/// <returns></returns>
public int GetCurrentUserIndex()
{
return storedUserList.currentUserIndex;
}
/// <summary>
/// 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)
{
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)
{
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;
}
/// <summary>
/// I am inevitable, *snap*
/// </summary>
/// <param name="user">Reference to the user to be removed</param>
/// <returns>true if the user was successful removed, false otherwise</returns>
public bool DeleteUser(User user)
{
if (1 < storedUserList.storedUsers.Count)
{
if (storedUserList.currentUserIndex == storedUserList.storedUsers.Count - 1)
{
storedUserList.currentUserIndex--;
}
return storedUserList.storedUsers.Remove(user);
}
return false;
}
/// <summary>
/// Save the users
/// </summary>
public void Save()
{
string json = JsonUtility.ToJson(storedUserList);
File.CreateText(PATH).Close();
File.WriteAllText(PATH, json);
}
/// <summary>
/// Override the current content of the userlist by what is stored on disk
/// </summary>
public void Load()
{
storedUserList.storedUsers.Clear();
storedUserList.currentUserIndex = -1;
if (!File.Exists(PATH))
{
Save();
}
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
}
}