Select Git revision
StartServer.cs
SolutionRecorder.cs 7.33 KiB
using MoreLinq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static CommunicationEvents;
/// <summary>
/// Solution of a <see cref="Stage"/>
/// </summary>
public class SolutionRecorder : FactRecorder, IJSONsavable<SolutionRecorder>
{
/// @{ <summary> adds to the end of the file name of a </summary>
private const string
/// <summary> SolutionFile (stores <see cref="SolutionRecorder">this</see>) </summary>
endingVal = "_val";
/// @}
/// <summary>
/// A collection of constrains of which *all* have to be <see langword="true"/>
/// <seealso cref="SubSolution"/>
/// <seealso cref="FactRecorder.DynamiclySolved(SolutionRecorder, out List<List<string>>, out List<List<string>>)"/>
/// </summary>
public List<SubSolution> ValidationSet = new();
/// <summary> Contains <see cref="Fact.Id"/>s which are being exposed to the Player </summary>
public List<string> ExposedSolutionFacts = new();
/// <summary> Maps <see cref="REST_JSON_API.Scroll.ScrollReference"/>s to settings to apply to itself. </summary>
public Dictionary<string, (string Id, int index, bool show)[]> ScrollOverwrites = new();
/// <summary>
/// Sits at the heart, but represents only a part of the whole Solution.
/// </summary>
public class SubSolution
// needs to be public for JSONWriter
{
/// <summary>
/// entails <b>{<see cref="FactRecorder.MyFactSpace">SolutionOrganizer.FacDict.Values</see>}</b> <br/>
/// <see cref="FactRecorder.MyFactSpace">SolutionFacts</see> to relate from.
/// </summary>
public HashSet<string> MasterIDs = new HashSet<string>();
/// <summary>
/// entails <b>{[],[0, <see cref="SolutionRecorder.ValidationSet"/><c>.IndexOf(<see cref="SubSolution">this</see>)</c> - 2]}</b> <br/>
/// Marks LevelFacts (<see cref="StageStatic.stage.factState"/>) found as solution (<see cref="FactRecorder.DynamiclySolved(SolutionRecorder, out List<List<string>>, out List<List<string>>)"/>)
/// in a previous entry of <see cref="SolutionRecorder.ValidationSet"/><br/>
/// to relate from *in addition* to <see cref="MasterIDs"/> <br/>
/// or _none_ if <c>empty</c>
/// </summary>
public List<int> SolutionIndex = new List<int>();
/// <summary>
/// entails <b>{[],[0, <see cref="SolutionRecorder.ValidationSet"/><c>.IndexOf(<see cref="SubSolution">this</see>)</c> - 2]}</b> <br/>
/// Marks LevelFacts (<see cref="StageStatic.stage.factState"/>) found as solution (<see cref="FactRecorder.DynamiclySolved(SolutionRecorder, out List<List<string>>, out List<List<string>>)"/>)
/// in a previous entry of <see cref="SolutionRecorder.ValidationSet"/><br/>
/// to relate to *instead of* all LevelFacts (<see cref="StageStatic.stage.factState"/>) <br/>
/// or _none_ if <c>empty</c>
/// </summary>
public List<int> RelationIndex = new List<int>();
/// <summary>
/// Comparer defining relation between <see cref="FactRecorder.MyFactSpace">SolutionFacts</see> and LevelFacts (<see cref="StageStatic.stage.factState"/>)
/// </summary>
[JsonIgnore]
public FactComparer Comparer = new FactEquivalentsComparer();
/// <summary>
/// Enables (especially <see cref="JsonConverter"/>) to read and set <see cref="Comparer"/> by its <c>string</c> representation.
/// </summary>
public string ComparerString
{
get { return Comparer.ToString(); }
set
{ // Select and create FactComparer by name
Type typ = fact_comparer.First(t => t.Name == value);
Comparer = Activator.CreateInstance(typ) as FactComparer;
}
}
/// <summary>
/// Collection of <c>Type</c>s of *all* available <see cref="FactComparer"/> to choose from.
/// </summary>
[JsonIgnore]
public static readonly Type[] fact_comparer = Assembly.GetExecutingAssembly().GetTypes().Where(typeof(FactComparer).IsAssignableFrom).ToArray();
/// <summary>
/// Only used by <see cref="JsonConverter"/> to initiate empty instance.
/// </summary>
public SubSolution() { }
/// <summary>
/// Standard Constructor
/// </summary>
/// <param name="MasterIDs">sets <see cref="MasterIDs"/> iff not <see langword="null"/></param>
/// <param name="SolutionIndex">sets <see cref="SolutionIndex"/> iff not <see langword="null"/></param>
/// <param name="RelationIndex">sets <see cref="RelationIndex"/> iff not <see langword="null"/></param>
/// <param name="Comparer">sets <see cref="Comparer"/> iff not <see langword="null"/></param>
public SubSolution(HashSet<string> MasterIDs, List<int> SolutionIndex, List<int> RelationIndex, FactComparer Comparer)
{
if (MasterIDs != null)
this.MasterIDs = MasterIDs;
if (SolutionIndex != null)
this.SolutionIndex = SolutionIndex;
if (RelationIndex != null)
this.RelationIndex = RelationIndex;
if (Comparer != null)
this.Comparer = Comparer;
}
/// <summary>
/// <see langword="true"/> if there is no solution to be deducted.
/// </summary>
/// <returns><c>MasterIDs.Count == 0 && SolutionIndex.Count == 0;</c></returns>
public bool IsEmpty()
{
return MasterIDs.Count == 0 && SolutionIndex.Count == 0;
}
}
static SolutionRecorder()
{
IJSONsavable<SolutionRecorder>.hierarchie = new List<Directories> { Directories.ValidationSets };
}
/// \copydoc FactOrganizer.FactOrganizer()
public SolutionRecorder() : base() { }
/// \copydoc FactOrganizer.FactOrganizer(bool)
public SolutionRecorder(bool invoke = false) : base(invoke) { }
/*
public List<Fact> getMasterFactsByIndex (int i)
{
return ValidationSet[i].MasterIDs.Select(id => this[id]).ToList();
}
*/
string IJSONsavable<SolutionRecorder>._IJGetName(string name)
=> name + endingVal;
SolutionRecorder IJSONsavable<SolutionRecorder>._IJPostProcess(SolutionRecorder raw_payload)
{
if (raw_payload == null)
return raw_payload;
SolutionRecorder payload = ReInitializeFactOrganizer<SolutionRecorder>(raw_payload, false, out Dictionary<string, string> old_to_new);
// Parse and add
foreach (var element in raw_payload.ValidationSet)
{
element.MasterIDs = new HashSet<string>(element.MasterIDs.Select(k => old_to_new[k]));
payload.ValidationSet.Add(element);
}
payload.ExposedSolutionFacts = raw_payload.ExposedSolutionFacts
.Select(fid => old_to_new[fid])
.ToList();
payload.ScrollOverwrites = raw_payload.ScrollOverwrites
.Select(kv => new KeyValuePair<string, (string Id, int index, bool show)[]>(
kv.Key,
kv.Value.Select(t => (old_to_new[t.Id], t.index, t.show)).ToArray())
).ToDictionary();
return payload;
}
SolutionRecorder IJSONsavable<SolutionRecorder>._IJPreProcess(SolutionRecorder payload)
=> (SolutionRecorder)IJSONsavable<FactRecorder>.preprocess(payload);
}