Resolve WES-117 "Persistent data handling"
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user