using System;
using System.Collections.Generic;
using UnityEngine;
///
/// Keep track of all users
///
public static class UserList
{
///
/// List of possible avatar sprites
///
public static List AVATARS = new List();
///
/// 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 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);
}
///
/// Get a user by username
///
/// The username of the user
/// User-object if a user with such username was found, null otherwise
public static User GetUserByUsername(string username)
{
foreach (User user in GetUsers())
if (user.GetUsername() == username) return user;
return null;
}
///
/// Get a user by index
///
/// The index of the user
/// User object
///
public static User GetUserByIndex(int index)
{
List users = GetUsers();
if (index < 0 || users.Count <= index)
throw new IndexOutOfRangeException();
return users[index];
}
///
/// Get a list of all users currently stored
///
/// A list of all users
public static List GetUsers()
{
PersistentDataController pdc = PersistentDataController.GetInstance();
return pdc.GetUsers().ConvertAll((d) => new User(d));
}
///
/// Get the current logged in user
///
/// The current logged in user
public static User GetCurrentUser()
{
List users = GetUsers();
if (users.Count == 0)
return null;
return users[PersistentDataController.GetInstance().GetCurrentUser()];
}
///
/// Get the index in the userlist of a user
///
///
public static int IndexOf(string username)
{
int idx = GetUsers().FindIndex((e) => e.GetUsername() == username);
if (idx < 0)
throw new KeyNotFoundException();
return idx;
}
///
/// Change the current user
///
/// Index of the user in the userlist
///
public static void ChangeCurrentUser(int index)
{
if (index < 0 || GetUsers().Count <= index)
throw new IndexOutOfRangeException();
PersistentDataController.GetInstance().SetCurrentUser(index, true);
}
///
/// Change the current user
///
/// Username of the user
///
public static void ChangeCurrentUser(string username)
{
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
try { ChangeCurrentUser(index); }
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
}
///
/// I am inevitable, *snap*
///
/// The index of the user in the userlist
/// true if the user was successful removed, false otherwise
///
public static bool DeleteUser(int index)
{
List users = GetUsers();
if (index < 0 || users.Count <= index)
throw new IndexOutOfRangeException();
if (1 < users.Count)
{
PersistentDataController.GetInstance().DeleteUser(index);
return true;
}
return false;
}
///
/// Delete a user from the userliset
///
/// Username of the user
/// true if the user was successful removed, false otherwise
///
public static bool DeleteUser(string username)
{
int index = GetUsers().FindIndex((e) => e.GetUsername() == username);
try { return DeleteUser(index); }
catch (IndexOutOfRangeException) { throw new KeyNotFoundException(); }
}
///
/// Save the current UserList
///
public static void Save()
{
PersistentDataController.GetInstance().Save();
}
}