-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change validation logic to be group-specific.
- Loading branch information
1 parent
a5d4bde
commit 4dbfbed
Showing
6 changed files
with
121 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using RandomizerCore.Exceptions; | ||
using RandomizerCore.Logic; | ||
using System.Text; | ||
|
||
namespace RandomizerCore.Randomization | ||
{ | ||
/// <summary> | ||
/// Base class which handles determining whether errors exist in the randomizer output for a group. | ||
/// <br/>By default, checks that the placement list has the right counts by name, and that all locations are reachable. | ||
/// </summary> | ||
public class Validator | ||
{ | ||
/// <summary> | ||
/// Tests the randomizer output for the given group and sends an exception if it is invalid. | ||
/// </summary> | ||
/// <exception cref="ValidationException"></exception> | ||
public virtual void Validate(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
ValidateCounts(group, pm, placements, entries); | ||
ValidateNameCounts(group, pm, placements, entries); | ||
ValidateAllLocationsReachable(group, pm, placements, entries); | ||
} | ||
|
||
protected virtual void ValidateCounts(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
if (group.Items.Length > placements.Count) | ||
{ | ||
throw new ValidationException($"Items deleted from final placement for randomization group {group.Label}. Group expected {group.Items.Length} placements, but has {placements.Count} placements."); | ||
} | ||
else if (group.Items.Length < placements.Count) | ||
{ | ||
throw new ValidationException($"Too many items found in final placement for randomization group {group.Label}. Group expected {group.Items.Length} placements, but has {placements.Count} placements."); | ||
} | ||
} | ||
|
||
protected virtual void ValidateNameCounts(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
Dictionary<string, int> nameCounts = new(); | ||
foreach (IRandoItem r in group.Items) | ||
{ | ||
nameCounts.TryGetValue(r.Name, out int value); | ||
nameCounts[r.Name] = value + 1; | ||
} | ||
foreach (RandoPlacement p in placements) | ||
{ | ||
nameCounts.TryGetValue(p.Item.Name, out int value); | ||
nameCounts[p.Item.Name] = value - 1; | ||
} | ||
foreach (KeyValuePair<string, int> kvp in nameCounts) | ||
{ | ||
if (kvp.Value != 0) throw new ValidationException($"Improper item counts found in randomization group {group.Label}. Item {kvp.Key} was accounted for a net {kvp.Value} times"); | ||
} | ||
} | ||
|
||
protected virtual void ValidateAllLocationsReachable(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
StringBuilder sb = null; | ||
foreach (PrePlacedItemUpdateEntry entry in entries) | ||
{ | ||
if (!entry.obtained) | ||
{ | ||
sb ??= new StringBuilder($"Unreachable placement(s) detected in group {group.Label}:").AppendLine(); | ||
sb.AppendLine($" {entry.item.Name} at {entry.location.Name}"); | ||
} | ||
} | ||
if (sb != null) throw new ValidationException(sb.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using RandomizerCore.Logic; | ||
using System.Text; | ||
|
||
namespace RandomizerCore.Randomization | ||
{ | ||
/// <summary> | ||
/// Validator which assumes that its group is composed of RandoTransitions, and only checks that their terms are obtained, rather than that they are reachable. | ||
/// </summary> | ||
public class WeakTransitionValidator : Validator | ||
{ | ||
public override void Validate(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
ValidateCounts(group, pm, placements, entries); | ||
ValidateNameCounts(group, pm, placements, entries); | ||
WeakValidateTransitionsObtained(group, pm, placements, entries); | ||
} | ||
|
||
protected virtual void WeakValidateTransitionsObtained(RandomizationGroup group, ProgressionManager pm, List<RandoPlacement> placements, List<PrePlacedItemUpdateEntry> entries) | ||
{ | ||
StringBuilder sb = null; | ||
foreach (RandoPlacement p in placements) | ||
{ | ||
RandoTransition source = (RandoTransition)p.Location; | ||
RandoTransition target = (RandoTransition)p.Item; | ||
|
||
if (!pm.Has(source.lt.term)) | ||
{ | ||
sb ??= new StringBuilder($"Inaccessible transitions(s) detected in group {group.Label}:").AppendLine(); | ||
sb.AppendLine($" {p.Location.Name} in {p.Location.Name} --> {p.Item.Name}"); | ||
} | ||
if (!pm.Has(target.lt.term)) | ||
{ | ||
sb ??= new StringBuilder($"Inaccessible transitions(s) detected in group {group.Label}:").AppendLine(); | ||
sb.AppendLine($" {p.Item.Name} in {p.Location.Name} --> {p.Item.Name}"); | ||
} | ||
} | ||
} | ||
} | ||
} |