using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
///
/// Keep track of all users
///
[CreateAssetMenu(menuName = "Create new Scriptable/UserList")]
public class UserList : ScriptableObject
{
///
/// Helper class to enable serialization of the UserList class
/// (ScriptableObjects cannot be serialized)
///
[Serializable]
public class StoredUserList
{
///
/// The index of the current/last logged in user in the storedUsers list
///
public int currentUserIndex = -1;
///
/// A list containing all users (which can be serialized)
///
public List storedUsers = new List();
}
///
/// Reference to the serializable version of UserList
///
[SerializeField]
private StoredUserList storedUserList = new StoredUserList();
///
/// Path of the .json-file to store all serialized data
///
public static string PATH = null;
///
/// OnEnable will make sure the PATH-variable is correctly initialized
///
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();
}
}
///
/// Create a new user
///
/// The username of the new user
/// Reference to the user avatar
/// A newly created user
public User CreateNewUser(string name, Sprite avatar)
{
User user = new User();
user.username = name;
user.avatar = avatar;
return user;
}
///
/// Create a new user and save (add to list)
///
/// The username of the new user
/// Reference to the user avatar
/// A newly created user
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;
}
///
/// Get a user by username
///
/// The username of the user
/// User-object if a user with such username was found, null otherwise
public User GetUserByUsername(string username)
{
foreach (User user in storedUserList.storedUsers)
if (user.username == username) return user;
return null;
}
///
/// Get a list of all users currently stored
///
/// A list of all users
public List GetUsers()
{
return storedUserList.storedUsers;
}
///
/// Get the current logged in user
///
/// The current logged in user
public User GetCurrentUser()
{
if (storedUserList.storedUsers.Count == 0)
{
return null;
}
return storedUserList.storedUsers[storedUserList.currentUserIndex];
}
///
/// Get the index in the userlist of the current playing user
///
///
public int GetCurrentUserIndex()
{
return storedUserList.currentUserIndex;
}
///
/// Change the current user
///
/// Index of the user in the userlist
///
public void ChangeCurrentUser(int index)
{
if (0 <= index && index < storedUserList.storedUsers.Count)
{
storedUserList.currentUserIndex = index;
}
else
{
throw new IndexOutOfRangeException();
}
}
///
/// Change the current user
///
/// Reference to the user in the userlist
///
public void ChangeCurrentUser(User user)
{
int idx = storedUserList.storedUsers.IndexOf(user);
if (idx < 0)
{
throw new KeyNotFoundException();
}
storedUserList.currentUserIndex = idx;
}
///
/// Remove the user
///
/// The index of the user in the userlist
/// true if user was successful removed, false otherwise
/// I am inevitable, *snap*
///
/// Reference to the user to be removed
/// true if the user was successful removed, false otherwise
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;
}
///
/// Save the users
///
public void Save()
{
string json = JsonUtility.ToJson(storedUserList);
File.CreateText(PATH).Close();
File.WriteAllText(PATH, json);
}
///
/// Override the current content of the userlist by what is stored on disk
///
public void Load()
{
storedUserList.storedUsers.Clear();
storedUserList.currentUserIndex = -1;
if (!File.Exists(PATH))
{
Save();
}
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson(text);
}
}