Add formatting rules
This commit is contained in:
@@ -3,31 +3,55 @@ 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
|
||||
{
|
||||
// Serializable UserList content
|
||||
/// <summary>
|
||||
/// Helper class to enable serialization of the UserList class
|
||||
/// (<c>ScriptableObkect</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;
|
||||
/// <summary>
|
||||
/// A list containing all users (which can be serialized)
|
||||
/// </summary>
|
||||
public List<User> storedUsers = new List<User>();
|
||||
}
|
||||
[Header("Users")]
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the serializable version of <c>UserList</c>
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
// Reference to serializable version of UserList
|
||||
private StoredUserList storedUserList = new StoredUserList();
|
||||
|
||||
// Path to .json file
|
||||
/// <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()
|
||||
{
|
||||
PATH = $"{Application.dataPath}/users.json";
|
||||
Load();
|
||||
}
|
||||
|
||||
// Create a new User
|
||||
/// <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();
|
||||
@@ -36,7 +60,12 @@ public class UserList : ScriptableObject
|
||||
return user;
|
||||
}
|
||||
|
||||
// Create a new User and add to list
|
||||
/// <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);
|
||||
@@ -45,7 +74,11 @@ public class UserList : ScriptableObject
|
||||
return user;
|
||||
}
|
||||
|
||||
// Get user by username, returns `null` if no user can be found with such name
|
||||
/// <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)
|
||||
@@ -54,19 +87,27 @@ public class UserList : ScriptableObject
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get a list of all users
|
||||
/// <summary>
|
||||
/// Get a list of all users currently stored
|
||||
/// </summary>
|
||||
/// <returns>A list of all users</returns>
|
||||
public List<User> GetUsers()
|
||||
{
|
||||
return storedUserList.storedUsers;
|
||||
}
|
||||
|
||||
// Get the current active user
|
||||
/// <summary>
|
||||
/// Get the current logged in user
|
||||
/// </summary>
|
||||
/// <returns>The current logged in user</returns>
|
||||
public User GetCurrentUser()
|
||||
{
|
||||
return storedUserList.storedUsers[storedUserList.currentUserIndex];
|
||||
}
|
||||
|
||||
// Save the userList
|
||||
/// <summary>
|
||||
/// Save the users
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
string json = JsonUtility.ToJson(storedUserList);
|
||||
@@ -74,7 +115,9 @@ public class UserList : ScriptableObject
|
||||
File.WriteAllText(PATH, json);
|
||||
}
|
||||
|
||||
// Load the userList into this object
|
||||
/// <summary>
|
||||
/// Override the current content of the userlist by what is stored on disk
|
||||
/// </summary>
|
||||
public void Load()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user