Files
unity-application/Assets/Accounts/Scripts/UserList.cs
2023-04-04 17:00:47 +00:00

163 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Keep track of all users
/// </summary>
public static class UserList
{
/// <summary>
/// List of possible avatar sprites
/// </summary>
public static List<Sprite> AVATARS = new List<Sprite>();
/// <summary>
/// Create a new user and save (add to list)
/// </summary>
/// <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 static User AddUser(string username, Sprite avatar)
{
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>
/// 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 static User GetUserByUsername(string username)
{
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 static List<User> GetUsers()
{
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 static User GetCurrentUser()
{
List<User> users = GetUsers();
if (users.Count == 0)
return null;
return users[PersistentDataController.GetInstance().GetCurrentUser()];
}
/// <summary>
/// Get the index in the userlist of a user
/// </summary>
/// <returns></returns>
public static int IndexOf(string username)
{
int idx = GetUsers().FindIndex((e) => e.GetUsername() == username);
if (idx < 0)
throw new KeyNotFoundException();
return idx;
}
/// <summary>
/// Change the current user
/// </summary>
/// <param name="index">Index of the user in the userlist</param>
/// <exception cref="IndexOutOfRangeException"></exception>
public static void ChangeCurrentUser(int index)
{
if (index < 0 || GetUsers().Count <= index)
throw new IndexOutOfRangeException();
PersistentDataController.GetInstance().SetCurrentUser(index, true);
}
/// <summary>
/// Change the current user
/// </summary>
/// <param name="index">Username of the user</param>
/// <exception cref="KeyNotFoundException"></exception>
public static void ChangeCurrentUser(string username)
{
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
try { ChangeCurrentUser(index); }
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
}
/// <summary>
/// I am inevitable, *snap*
/// </summary>
/// <param name="index">The index of the user in the userlist</param>
/// <returns>true if the user was successful removed, false otherwise</returns>
/// <exception cref="IndexOutOfRangeException"></exception>
public static bool DeleteUser(int index)
{
List<User> users = GetUsers();
if (index < 0 || users.Count <= index)
throw new IndexOutOfRangeException();
if (1 < users.Count)
{
PersistentDataController.GetInstance().DeleteUser(index);
return true;
}
return false;
}
/// <summary>
/// Delete a user from the userliset
/// </summary>
/// <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)
{
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
try { return DeleteUser(index); }
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
}
/// <summary>
/// Save the current UserList
/// </summary>
public static void Save()
{
PersistentDataController.GetInstance().Save();
}
}