Files
unity-application/Assets/Accounts/Scripts/User.cs
2023-05-04 09:09:49 +00:00

159 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using static PersistentDataController;
/// <summary>
/// A class holding all information of a user
/// </summary>
public class User
{
/// <summary>
/// Reference to the user stored data record
/// </summary>
private SavedUserData storedUserData;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">Reference to the user stored data record</param>
public User(SavedUserData data)
{
this.storedUserData = data;
}
/// <summary>
/// Get the username
/// </summary>
public string GetUsername() { return storedUserData.username; }
/// <summary>
/// Get the total playtime
/// </summary>
public double GetPlaytime() { return storedUserData.playtime; }
/// <summary>
/// Get the avatar
/// </summary>
public Sprite GetAvatar() { return UserList.AVATARS[storedUserData.avatarIndex]; }
/// <summary>
/// Get a list of all recently started minigameCards
/// </summary>
/// <returns>A <c>List</c> of <c>Tuples</c>, containing the <c>CourseIndex</c>
/// and a <c>float</c> holding the progress (value between 0 and 1) of the user in this course</returns>
public List<Tuple<CourseIndex, float>> GetRecentCourses()
{
// TODO: return better results (for now only return all minigameCards)
List<Tuple<CourseIndex, float>> recentCourses = new List<Tuple<CourseIndex, float>>();
foreach (var courseProgress in storedUserData.courses)
{
CourseIndex idx = courseProgress.courseIndex;
float progress = courseProgress.progress;
recentCourses.Add(Tuple.Create<CourseIndex, float>(idx, progress));
}
return recentCourses.Take(3).ToList();
}
/// <summary>
/// Get a list of all recommended minigameCards
/// </summary>
/// <returns>A <c>List</c> of <c>Tuples</c>, containing the <c>CourseIndex</c>
/// and a <c>float</c> holding the progress (value between 0 and 1) of the user in this course</returns>
public List<Tuple<CourseIndex, float>> GetRecommendedCourses()
{
List<Tuple<CourseIndex, float>> recommenedCourses = new List<Tuple<CourseIndex, float>>();
if (storedUserData.courses.Count == 0)
{
recommenedCourses.Add(Tuple.Create<CourseIndex, float>(CourseIndex.FINGERSPELLING, 0.0f));
}
else
{
// TODO: return better results (for now only return all minigameCards)
foreach (var courseProgress in storedUserData.courses)
{
CourseIndex idx = courseProgress.courseIndex;
float progress = courseProgress.progress;
recommenedCourses.Add(Tuple.Create<CourseIndex, float>(idx, progress));
}
}
return recommenedCourses.Take(3).ToList();
}
/// <summary>
/// Get the progress of all minigameCards the user did
/// </summary>
/// <returns></returns>
public List<SavedCourseProgress> GetCourses()
{
return storedUserData.courses;
}
/// <summary>
/// Get the progress of a certain course
/// </summary>
/// <param name="courseIndex">Index of course</param>
/// <returns><c>Progress</c> belonging to the <c>courseIndex</c>, <c>null</c> if course was not found</returns>
public SavedCourseProgress GetCourseProgress(CourseIndex courseIndex)
{
return storedUserData.courses.Find((p) => p.courseIndex == courseIndex);
}
/// <summary>
/// Add new progress of a course
/// </summary>
/// <param name="progress">SavedCourseProgress data record</param>
public void AddCourseProgress(SavedCourseProgress progress)
{
storedUserData.courses.Add(progress);
}
/// <summary>
/// Reset progress of a course
/// </summary>
/// <param name="courseIndex">Index of course</param>
public void ResetCourseProgress(CourseIndex courseIndex)
{
storedUserData.courses.RemoveAll((p) => p.courseIndex == courseIndex);
}
/// <summary>
/// Get the progress of all minigames the user did
/// </summary>
/// <returns></returns>
public List<SavedMinigameProgress> GetMinigames()
{
return storedUserData.minigames;
}
/// <summary>
/// Get the progress of certain minigame
/// </summary>
/// <param name="minigameIndex">Index of the minigame</param>
/// <returns><c>Progress</c> belonging to the <c>minigameIndex</c>, <c>null</c> if minigame was not found</returns>
public SavedMinigameProgress GetMinigameProgress(MinigameIndex minigameIndex)
{
return storedUserData.minigames.Find((p) => p.minigameIndex == minigameIndex);
}
/// <summary>
/// Add new progress of a minigame
/// </summary>
/// <param name="progress">SavedMinigameProgress data record</param>
public void AddMinigameProgress(SavedMinigameProgress progress)
{
storedUserData.minigames.Add(progress);
}
/// <summary>
/// Reset progress of a minigame
/// </summary>
/// <param name="minigameIndex">Index of the minigame</param>
public void ResetMinigameProgress(MinigameIndex minigameIndex)
{
storedUserData.minigames.RemoveAll((p) => p.minigameIndex == minigameIndex);
}
}