using System.Collections.Generic;
using System.Linq;
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;
namespace EscapeRoomEngine.Engine.Runtime.Measurements
{
public static class Measure
{
///
/// Whether to store the taken measurements in the database. Disable for test runs.
///
public static bool Store { get; set; }
private static Dictionary _runningMeasurements;
private static Session _currentSession;
public static float AverageTime(IEnumerable puzzles) =>
puzzles.Sum(puzzle => PuzzleStorage.Instance.Load(puzzle).AverageTimeToSolve);
public static float AverageTime(PuzzleModuleDescription puzzle) =>
PuzzleStorage.Instance.Load(puzzle).AverageTimeToSolve;
public static float AverageTime(Room room) =>
room.puzzles.Sum(puzzle => AverageTime((PuzzleModuleDescription)puzzle.description));
public static void StartMeasuring(PuzzleModuleDescription puzzle)
{
_runningMeasurements[puzzle.Id] = new PuzzleMeasurement
{
TimeStarted = Time.time,
TimeSolved = Time.time
};
Logger.Log($"Started measuring {puzzle}", LogType.Measuring);
}
public static void Solve(PuzzleModuleDescription puzzle)
{
if (_currentSession == null)
{
throw new EngineException("Measuring session must be started before taking measurements.");
}
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()
{
_currentSession = new Session();
Logger.Log($"Started {_currentSession}", LogType.Measuring);
}
public static void EndSession(float time)
{
if (Store)
{
PuzzleStorage.Instance.EndSession(_currentSession, time);
}
Logger.Log($"Ended {_currentSession}", LogType.Measuring);
}
public static void Clear()
{
_runningMeasurements = new Dictionary();
Store = true;
}
}
}