Resolve WES-133 "Multiple choice"

This commit is contained in:
Tibe Habils
2023-04-10 15:05:11 +00:00
committed by Jelle De Geest
parent 04d9a4bf2b
commit 4e9d801e61
49 changed files with 3310 additions and 1244 deletions

View File

@@ -19,7 +19,7 @@ public class PersistentDataController
/// Current implementation version of the PersistentDataController
/// </summary>
/// <remarks>MSB represent sprint version, LSB represent subversion</remarks>
public static readonly int VERSION = 0x04_01;
public static readonly int VERSION = 0x04_03;
/// <summary>
/// Path of the <c>.json</c>-file to store all serialized data
@@ -184,8 +184,139 @@ public class PersistentDataController
[Serializable]
public class SavedCourseProgress : PersistentDataContainer
{
/// <summary>
/// Update the progress value of the SavedLearnableProgress with the given learnableName.
/// </summary>
/// <param name="learnableName"></param>
/// <param name="addValue"></param>
public void UpdateLearnable(string learnableName, float addValue)
{
SavedLearnableProgress learnable = learnables.Find(l => l.name == learnableName);
if (learnable == null)
{
return;
}
// Update the progress value of the SavedLearnableProgress
learnable.progress += addValue;
// crop the learnable progress around -5 and 5
if (learnable.progress > 5.0f)
{
learnable.progress = 5.0f;
}
else if (learnable.progress < -5.0f)
{
learnable.progress = -5.0f;
}
// if learnable progress is big enough it is "completed"
if (learnable.progress > 3)
{
completedLearnables++;
}
}
/// <summary>
///
/// </summary>
/// <returns> bool which indicates if there are enough inUseLearnables </returns>
private bool EnoughLearnables()
{
// There need to be more then 5 non completed learnables
return inUseLearnables - completedLearnables > 5 || totalLearnables == inUseLearnables;
}
/// <summary>
/// Find a SavedLearnableProgress with the given name
/// </summary>
/// <param name="name"></param>
/// <returns> SavedLearnableProgress with the given name </returns>
public SavedLearnableProgress FindLearnable(string name)
{
return learnables.Find(l => l.name == name);
}
/// <summary>
/// find learnable in learnables which is not yet in use, and set it active
/// </summary>
/// <returns> bool which indicates the success of the function </returns>
public SavedLearnableProgress AddNewLearnable()
{
SavedLearnableProgress learnable = learnables.Find(l => !l.inUse);
if (learnable == null)
{
return null;
}
learnable.inUse = true;
inUseLearnables++;
return learnable;
}
/// <summary>
/// Gets a random inUse learnable
/// </summary>
/// <returns> a randomly selected inUse SavedLearnable which is not yet completed</returns>
public SavedLearnableProgress GetRandomLearnable()
{
if (!EnoughLearnables())
{
return AddNewLearnable();
}
else
{
// only select inUse learnables which are not yet completed (progress < 3.5f)
List<SavedLearnableProgress> inUseLearnables = learnables.FindAll(l => l.inUse && l.progress <= 3.5f);
if (inUseLearnables.Count == 0)
{
return null;
}
// Select a random index from the in-use learnables list
int randomIndex = UnityEngine.Random.Range(0, inUseLearnables.Count);
return inUseLearnables[randomIndex];
}
}
/// <summary>
/// Create new SavedLearnableProgress object and assigns the index and name values
/// </summary>
/// <param name="name"></param>
/// <param name="index"></param>
/// <returns> bool which indicates the success of the function</returns>
public bool AddLearnable(string name, int index)
{
if (learnables.Any(learnable => learnable.name == name || learnable.index == index))
return false;
SavedLearnableProgress savedLearnableProgress = new SavedLearnableProgress();
savedLearnableProgress.index = index;
savedLearnableProgress.name = name;
learnables.Add(savedLearnableProgress);
totalLearnables++;
return true;
}
public CourseIndex courseIndex;
public float progress = -1.0f;
public int completedLearnables = 0;
public int inUseLearnables = 0;
public int totalLearnables = 0;
public List<SavedLearnableProgress> learnables = new List<SavedLearnableProgress>();
}
/// <summary>
/// Stored individual learnable progress
/// </summary>
[Serializable]
public class SavedLearnableProgress : PersistentDataContainer
{
public int index;
public bool inUse = false;
public string name;
public float progress = 0.0f;
}
/// <summary>

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO;
using UnityEngine;
[TestFixture]
public class PersistentDataTests
{
@@ -145,7 +146,7 @@ public class PersistentDataTests
[Test]
public void Test_PersistentDataController_Version()
{
const int VERSION = 0x04_01;
const int VERSION = 0x04_03;
Assert.AreEqual(VERSION, PersistentDataController.VERSION);
}
@@ -611,4 +612,158 @@ public class PersistentDataTests
c.Set<int>("key", 123);
Assert.IsFalse(c.Has("KEY"));
}
[Test]
public void Test_AddLearnable_AddsLearnable()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
bool added = progress.AddLearnable("test learnable", 0);
Assert.IsTrue(added);
Assert.AreEqual(progress.learnables.Count, 1);
Assert.AreEqual(progress.learnables[0].name, "test learnable");
Assert.AreEqual(progress.learnables[0].index, 0);
}
[Test]
public void Test_AddLearnable_FailsWithDuplicateName()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable", 0);
bool added = progress.AddLearnable("test learnable", 1);
Assert.IsFalse(added);
Assert.AreEqual(progress.learnables.Count, 1);
}
[Test]
public void Test_AddLearnable_FailsWithDuplicateIndex()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable", 0);
bool added = progress.AddLearnable("test learnable 2", 0);
Assert.IsFalse(added);
Assert.AreEqual(progress.learnables.Count, 1);
}
[Test]
public void Test_UpdateLearnable_UpdatesProgress()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable", 0);
progress.UpdateLearnable("test learnable", 3.0f);
Assert.AreEqual(progress.learnables[0].progress, 3.0f);
}
[Test]
public void Test_UpdateLearnable_CropsProgressAtFive()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable", 0);
progress.UpdateLearnable("test learnable", 10.0f);
Assert.AreEqual(progress.learnables[0].progress, 5.0f);
Assert.AreEqual(progress.completedLearnables, 1);
}
[Test]
public void Test_UpdateLearnable_CropsProgressAtNegativeFive()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable", 0);
progress.UpdateLearnable("test learnable", -10.0f);
Assert.AreEqual(progress.learnables[0].progress, -5.0f);
}
[Test]
public void Test_FindLearnable_ReturnsNullWhenNotFound()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable 1", 0);
progress.AddLearnable("test learnable 2", 1);
PersistentDataController.SavedLearnableProgress learnable = progress.FindLearnable("not found");
Assert.IsNull(learnable);
}
[Test]
public void Test_FindLearnable_ReturnsLearnableByName()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable 1", 0);
progress.AddLearnable("test learnable 2", 1);
PersistentDataController.SavedLearnableProgress learnable = progress.FindLearnable("test learnable 2");
Assert.AreEqual(learnable.index, 1);
}
[Test]
public void Test_AddNewLearnable_ReturnsFalseWhenNoUnusedLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable 1", 0);
progress.learnables[0].inUse = true;
PersistentDataController.SavedLearnableProgress learnable = progress.AddNewLearnable();
Assert.IsNull(learnable);
}
[Test]
public void Test_AddNewLearnable_ReturnsTrueWhenUnusedLearnableFound()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable 1", 0);
progress.AddLearnable("test learnable 2", 1);
PersistentDataController.SavedLearnableProgress learnable = progress.AddNewLearnable();
Assert.IsNotNull(learnable);
Assert.AreEqual(progress.inUseLearnables, 1);
}
[Test]
public void Test_GetRandomLearnable_ReturnsNullWhenNoLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
PersistentDataController.SavedLearnableProgress learnable = progress.GetRandomLearnable();
Assert.IsNull(learnable);
}
[Test]
public void Test_GetRandomLearnable_ReturnsNullWhenOnlyCompletedLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("test learnable 1", 0);
progress.AddLearnable("test learnable 2", 1);
progress.learnables[0].progress = 4.0f;
progress.learnables[0].inUse = true;
progress.learnables[1].progress = 4.0f;
progress.learnables[1].inUse = true;
progress.completedLearnables = 2;
progress.inUseLearnables = 0;
PersistentDataController.SavedLearnableProgress learnable = progress.GetRandomLearnable();
Assert.IsNull(learnable);
}
}