store session to db only after ended, allow test runs without writing to db
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using EscapeRoomEngine.Engine.Runtime.Modules;
|
||||
using EscapeRoomEngine.Engine.Runtime.Utilities;
|
||||
using UnityEngine;
|
||||
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
|
||||
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
|
||||
@@ -8,7 +9,13 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
{
|
||||
public static class Measure
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether to store the taken measurements in the database. Disable for test runs.
|
||||
/// </summary>
|
||||
public static bool Store { get; set; }
|
||||
|
||||
private static Dictionary<int, PuzzleMeasurement> _runningMeasurements;
|
||||
private static Session _currentSession;
|
||||
|
||||
public static void StartMeasuring(PuzzleModuleDescription puzzle)
|
||||
{
|
||||
@@ -23,22 +30,43 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
|
||||
public static void Solve(PuzzleModuleDescription puzzle)
|
||||
{
|
||||
var measurement = _runningMeasurements[puzzle.Id];
|
||||
if (_currentSession == null)
|
||||
{
|
||||
throw new EngineException("Measuring session must be started before taking measurements.");
|
||||
}
|
||||
|
||||
PuzzleStorage.Instance.EndMeasurement(puzzle, measurement);
|
||||
var measurement = _runningMeasurements[puzzle.Id];
|
||||
|
||||
if (Store)
|
||||
{
|
||||
PuzzleStorage.Instance.EndMeasurement(_currentSession, puzzle, measurement);
|
||||
}
|
||||
_runningMeasurements.Remove(puzzle.Id);
|
||||
|
||||
Logger.Log($"Solved {puzzle} with measurement {measurement}", LogType.Measuring);
|
||||
}
|
||||
|
||||
public static void StartSession() => PuzzleStorage.Instance.StartSession();
|
||||
public static void StartSession()
|
||||
{
|
||||
_currentSession = new Session();
|
||||
|
||||
Logger.Log($"Started {_currentSession}", LogType.Measuring);
|
||||
}
|
||||
|
||||
public static void EndSession(float time)
|
||||
{
|
||||
PuzzleStorage.Instance.EndSession(time);
|
||||
if (Store)
|
||||
{
|
||||
PuzzleStorage.Instance.EndSession(_currentSession, time);
|
||||
}
|
||||
|
||||
Logger.Log(PuzzleStorage.Instance.Session.ToString(), LogType.Measuring);
|
||||
Logger.Log($"Ended {_currentSession}", LogType.Measuring);
|
||||
}
|
||||
|
||||
public static void Clear() => _runningMeasurements = new Dictionary<int, PuzzleMeasurement>();
|
||||
public static void Clear()
|
||||
{
|
||||
_runningMeasurements = new Dictionary<int, PuzzleMeasurement>();
|
||||
Store = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,6 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
[SerializeField]
|
||||
private string databasePath = "measurements.realm";
|
||||
|
||||
public Session Session { get; private set; }
|
||||
|
||||
private Realm _realm;
|
||||
|
||||
private void OnEnable()
|
||||
@@ -45,15 +43,21 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
|
||||
#region Session
|
||||
|
||||
public void StartSession() => _realm.Write(() => Session = _realm.Add(new Session()));
|
||||
|
||||
public void EndSession(float time) =>_realm.Write(() => Session.Time = time);
|
||||
public void EndSession(Session session, float time)
|
||||
{
|
||||
session.Time = time;
|
||||
|
||||
_realm.Write(() =>
|
||||
{
|
||||
_realm.Add(session);
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Puzzles
|
||||
|
||||
public Puzzle New(PuzzleModuleDescription puzzle)
|
||||
private Puzzle New(PuzzleModuleDescription puzzle)
|
||||
{
|
||||
Puzzle created = null;
|
||||
|
||||
@@ -61,10 +65,10 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
|
||||
return created;
|
||||
}
|
||||
|
||||
public Puzzle LoadOrNew(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
|
||||
|
||||
public Puzzle Load(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id);
|
||||
|
||||
private Puzzle LoadOrNew(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id) ?? New(puzzle);
|
||||
|
||||
private Puzzle Load(PuzzleModuleDescription puzzle) => _realm.Find<Puzzle>(puzzle.Id);
|
||||
|
||||
public void Delete(PuzzleModuleDescription puzzle)
|
||||
{
|
||||
@@ -75,7 +79,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
}
|
||||
}
|
||||
|
||||
public void EndMeasurement(PuzzleModuleDescription puzzle, PuzzleMeasurement measurement)
|
||||
public void EndMeasurement(Session session, PuzzleModuleDescription puzzle, PuzzleMeasurement measurement)
|
||||
{
|
||||
var found = LoadOrNew(puzzle);
|
||||
|
||||
@@ -85,7 +89,7 @@ namespace EscapeRoomEngine.Engine.Runtime.Measurements
|
||||
found.Measurements.Add(measurement);
|
||||
|
||||
// add solved puzzle to session
|
||||
Session.PuzzlesSolved.Add(found);
|
||||
session.PuzzlesSolved.Add(found);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user