Skip to content

Commit

Permalink
Add map size validation to No unlimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 24, 2022
1 parent 656bfa9 commit 2faf6a2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions Src/RandomizerTMF.Logic/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class Constants
public const string Skipped = "Skipped";
public const string DefaultReplayFileFormat = "{0}_{1}_{2}.Replay.Gbx";
public const string OfficialBlocksYml = "OfficialBlocks.yml";
public const string MapSizesYml = "MapSizes.yml";
public const string Replays = "Replays";
public const string Tracks = "Tracks";
public const string Autosaves = "Autosaves";
Expand Down
29 changes: 29 additions & 0 deletions Src/RandomizerTMF.Logic/MapSizes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Rally:
- [10, 18, 150]
- [20, 18, 60]
- [30, 18, 30]
- [32, 12, 32]
- [45, 18, 45]
Island:
- [36, 36, 36]
- [45, 36, 45]
Speed:
- [10, 18, 150]
- [20, 18, 60]
- [30, 18, 30]
- [32, 12, 32]
- [45, 18, 45]
Stadium:
- [32, 32, 32]
Coast:
- [36, 36, 36]
- [45, 36, 45]
Alpine:
- [10, 18, 150]
- [20, 18, 60]
- [30, 18, 30]
- [32, 12, 32]
- [45, 18, 45]
Bay:
- [36, 36, 36]
- [45, 36, 45]
14 changes: 13 additions & 1 deletion Src/RandomizerTMF.Logic/RandomizerEngine.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.Logging;
using GBX.NET;
using Microsoft.Extensions.Logging;

namespace RandomizerTMF.Logic;

public static partial class RandomizerEngine
{
public static RandomizerConfig Config { get; }
public static Dictionary<string, HashSet<string>> OfficialBlocks { get; }
public static Dictionary<string, HashSet<Int3>> MapSizes { get; }

public static HttpClient Http { get; }

Expand Down Expand Up @@ -50,6 +52,10 @@ static RandomizerEngine()

OfficialBlocks = GetOfficialBlocks();

Logger.LogInformation("Loading map sizes...");

MapSizes = GetMapSizes();

Logger.LogInformation("Preparing HTTP client...");

var socketHandler = new SocketsHttpHandler()
Expand Down Expand Up @@ -83,6 +89,12 @@ public static void OnStatus(string status)
return Yaml.Deserializer.Deserialize<Dictionary<string, HashSet<string>>>(reader);
}

private static Dictionary<string, HashSet<Int3>> GetMapSizes()
{
using var reader = new StreamReader(Constants.MapSizesYml);
return Yaml.Deserializer.Deserialize<Dictionary<string, HashSet<Int3>>>(reader);
}

/// <summary>
/// Starts the randomizer session by creating a new <see cref="Session"/> that will handle randomization on different thread from the UI thread.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
</ItemGroup>

<ItemGroup>
<None Update="MapSizes.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="OfficialBlocks.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
42 changes: 42 additions & 0 deletions Src/RandomizerTMF.Logic/TypeConverters/Int3Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using GBX.NET;
using TmEssentials;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;

namespace RandomizerTMF.Logic.TypeConverters;

internal sealed class Int3Converter : IYamlTypeConverter
{
public bool Accepts(Type type) => type == typeof(Int3) || type == typeof(Int3?);

public object? ReadYaml(IParser parser, Type type)
{
_ = parser.Consume<SequenceStart>();

var x = int.Parse(parser.Consume<Scalar>().Value);
var y = int.Parse(parser.Consume<Scalar>().Value);
var z = int.Parse(parser.Consume<Scalar>().Value);

_ = parser.Consume<SequenceEnd>();

return new Int3(x, y, z);
}

public void WriteYaml(IEmitter emitter, object? value, Type type)
{
if (value is null)
{
emitter.Emit(new Scalar("~"));
return;
}

var val = (Int3)value!;

emitter.Emit(new SequenceStart(default, default, isImplicit: true, SequenceStyle.Flow));
emitter.Emit(new Scalar(val.X.ToString()));
emitter.Emit(new Scalar(val.Y.ToString()));
emitter.Emit(new Scalar(val.Z.ToString()));
emitter.Emit(new SequenceEnd());
}
}
8 changes: 8 additions & 0 deletions Src/RandomizerTMF.Logic/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ public static bool ValidateMap(RandomizerConfig config, CGameCtnChallenge map, o
return false;
}

if (RandomizerEngine.MapSizes.TryGetValue(map.Collection, out var sizes))
{
if (map.Size is null || !sizes.Contains(map.Size.Value))
{
return false;
}
}

if (RandomizerEngine.OfficialBlocks.TryGetValue(map.Collection, out var officialBlocks))
{
foreach (var block in map.GetBlocks())
Expand Down
2 changes: 2 additions & 0 deletions Src/RandomizerTMF.Logic/Yaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public static class Yaml
.WithTypeConverter(new DateOnlyConverter())
.WithTypeConverter(new DateTimeOffsetConverter())
.WithTypeConverter(new TimeInt32Converter())
.WithTypeConverter(new Int3Converter())
.Build();

public static IDeserializer Deserializer { get; } = new DeserializerBuilder()
.WithTypeConverter(new DateOnlyConverter())
.WithTypeConverter(new DateTimeOffsetConverter())
.WithTypeConverter(new TimeInt32Converter())
.WithTypeConverter(new Int3Converter())
.IgnoreUnmatchedProperties()
.Build();
}

0 comments on commit 2faf6a2

Please sign in to comment.