Skip to content

Commit

Permalink
Fix Sunrise/Original site validations
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 13, 2022
1 parent d6b6e9d commit 514cc62
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 30 deletions.
79 changes: 62 additions & 17 deletions Src/RandomizerTMF.Logic/RandomizerEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,12 +796,12 @@ public static void ValidateRules()
{
if (Config.Rules.TimeLimit == TimeSpan.Zero)
{
throw new RuleValidationException("Time limit cannot be 0:00:00");
throw new RuleValidationException("Time limit cannot be 0:00:00.");
}

if (Config.Rules.TimeLimit > new TimeSpan(9, 59, 59))
{
throw new RuleValidationException("Time limit cannot be above 9:59:59");
throw new RuleValidationException("Time limit cannot be above 9:59:59.");
}

foreach (var primaryType in Enum.GetValues<EPrimaryType>())
Expand All @@ -815,45 +815,90 @@ public static void ValidateRules()
&& (Config.Rules.RequestRules.Site is ESite.Any
|| Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
{
throw new RuleValidationException($"{primaryType} cannot be specifically selected with TMNF or Nations Exchange");
throw new RuleValidationException($"{primaryType} cannot be specifically selected with TMNF or Nations Exchange.");
}
}

if (Config.Rules.RequestRules.Environment is not null
&& Config.Rules.RequestRules.Environment.Contains(EEnvironment.Stadium) == false
&& (Config.Rules.RequestRules.Site is ESite.Any
|| Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
if (Config.Rules.RequestRules.Environment?.Count > 0)
{
throw new RuleValidationException("Stadium has to be selected when environments are specified and TMNF or Nations Exchange is selected");
}
if (Config.Rules.RequestRules.Site.HasFlag(ESite.Sunrise)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Island)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Coast)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Bay))
{
throw new RuleValidationException("Island, Coast, or Bay has to be selected when environments are specified and Sunrise Exchange is selected.");
}

if (Config.Rules.RequestRules.Vehicle is not null
&& !Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Stadium)
&& (Config.Rules.RequestRules.Site is ESite.Any
|| Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
if (Config.Rules.RequestRules.Site.HasFlag(ESite.Original)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Snow)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Desert)
&& !Config.Rules.RequestRules.Environment.Contains(EEnvironment.Rally))
{
throw new RuleValidationException("Snow, Desert, or Rally has to be selected when environments are specified and Original Exchange is selected.");
}

if (!Config.Rules.RequestRules.Environment.Contains(EEnvironment.Stadium)
&& (Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
{
throw new RuleValidationException("Stadium has to be selected when environments are specified and TMNF or Nations Exchange is selected.");
}

if (Config.Rules.RequestRules.Site.HasFlag(ESite.Sunrise) || Config.Rules.RequestRules.Site.HasFlag(ESite.Original))
{
foreach (var env in Config.Rules.RequestRules.Environment)
{
if (Config.Rules.RequestRules.Vehicle?.Contains(env) == false)
{
throw new RuleValidationException("Envimix randomization is not allowed when Sunrise or Original Exchange is selected.");
}
}
}
}

if (Config.Rules.RequestRules.Vehicle?.Count > 0)
{
throw new RuleValidationException("StadiumCar has to be selected when cars are specified and TMNF or Nations Exchange is selected");
if (!Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Island)
&& !Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Coast)
&& !Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Bay)
&& Config.Rules.RequestRules.Site.HasFlag(ESite.Sunrise))
{
throw new RuleValidationException("IslandCar, CoastCar, or BayCar has to be selected when cars are specified and Sunrise Exchange is selected.");
}

if (!Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Snow)
&& !Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Desert)
&& !Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Rally)
&& Config.Rules.RequestRules.Site.HasFlag(ESite.Original))
{
throw new RuleValidationException("SnowCar, DesertCar, or RallyCar has to be selected when cars are specified and Original Exchange is selected.");
}

if (!Config.Rules.RequestRules.Vehicle.Contains(EEnvironment.Stadium)
&& (Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
{
throw new RuleValidationException("StadiumCar has to be selected when cars are specified and TMNF or Nations Exchange is selected.");
}
}

if (Config.Rules.RequestRules.EqualEnvironmentDistribution
&& Config.Rules.RequestRules.EqualVehicleDistribution
&& Config.Rules.RequestRules.Site is not ESite.TMUF)
{
throw new RuleValidationException("Equal environment and car distribution combined is only valid with TMUF Exchange");
throw new RuleValidationException("Equal environment and car distribution combined is only valid with TMUF Exchange.");
}

if (Config.Rules.RequestRules.EqualEnvironmentDistribution
&& (Config.Rules.RequestRules.Site is ESite.Any
|| Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
{
throw new RuleValidationException("Equal environment distribution is not valid with TMNF or Nations Exchange");
throw new RuleValidationException("Equal environment distribution is not valid with TMNF or Nations Exchange.");
}

if (Config.Rules.RequestRules.EqualVehicleDistribution
&& (Config.Rules.RequestRules.Site is ESite.Any
|| Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)))
{
throw new RuleValidationException("Equal vehicle distribution is not valid with TMNF or Nations Exchange");
throw new RuleValidationException("Equal vehicle distribution is not valid with TMNF or Nations Exchange.");
}
}

Expand Down
72 changes: 59 additions & 13 deletions Src/RandomizerTMF.Logic/RequestRules.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using RandomizerTMF.Logic.Exceptions;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -52,8 +53,12 @@ public string ToUrl() // Not very efficient but does the job done fast enough
.Where(x => x != ESite.Any && (Site & x) == x)
.ToArray();

var site = GetSite(matchingSites.Length == 0
? siteValues.Where(x => x is not ESite.Any).ToArray()
// If Site is Any, then it picks from sites that are valid within environments and cars
var site = GetRandomSite(matchingSites.Length == 0
? siteValues.Where(x => x is not ESite.Any
&& IsSiteValidWithEnvironments(x)
&& IsSiteValidWithVehicles(x)
&& IsSiteValidWithEnvimix(x)).ToArray()
: matchingSites);

var siteUrl = GetSiteUrl(site);
Expand Down Expand Up @@ -124,6 +129,50 @@ and not nameof(EqualEnvironmentDistribution)
and not nameof(EqualVehicleDistribution);
}

private static bool IsSiteValidWithEnvironments(ESite site, HashSet<EEnvironment>? envs)
{
if (envs is null)
{
return true;
}

return site switch
{
ESite.Sunrise => envs.Contains(EEnvironment.Island) || envs.Contains(EEnvironment.Coast) || envs.Contains(EEnvironment.Bay),
ESite.Original => envs.Contains(EEnvironment.Snow) || envs.Contains(EEnvironment.Desert) || envs.Contains(EEnvironment.Rally),
ESite.TMNF or ESite.Nations => envs.Contains(EEnvironment.Stadium),
_ => true,
};
}

private bool IsSiteValidWithEnvironments(ESite site)
{
return IsSiteValidWithEnvironments(site, Environment);
}

private bool IsSiteValidWithVehicles(ESite site)
{
return IsSiteValidWithEnvironments(site, Vehicle);
}

private bool IsSiteValidWithEnvimix(ESite site)
{
if (site is not ESite.Sunrise and not ESite.Original || Environment is null || Environment.Count == 0)
{
return true;
}

foreach (var env in Environment)
{
if (Vehicle?.Contains(env) == false)
{
return false;
}
}

return true;
}

private bool IsValidInNations(PropertyInfo prop, object val)
{
if (prop.Name is nameof(Environment) or nameof(Vehicle) && !val.Equals(EEnvironment.Stadium))
Expand Down Expand Up @@ -154,21 +203,18 @@ private static HashSet<EEnvironment> GetRandomEnvironmentThroughSet(HashSet<EEnv
return new HashSet<EEnvironment>() { GetRandomEnvironment(container) };
}

private static ESite GetSite(ESite[] matchingSites)
private static ESite GetRandomSite(ESite[] matchingSites)
{
return matchingSites[Random.Shared.Next(matchingSites.Length)];
}

private static string GetSiteUrl(ESite site)
private static string GetSiteUrl(ESite site) => site switch
{
return site switch
{
ESite.Any => throw new UnreachableException("Any is not a valid site"),
ESite.TMNF => "tmnf.exchange",
ESite.TMUF => "tmuf.exchange",
_ => $"{site.ToString().ToLower()}.tm-exchange.com",
};
}
ESite.Any => throw new UnreachableException("Any is not a valid site"),
ESite.TMNF => "tmnf.exchange",
ESite.TMUF => "tmuf.exchange",
_ => $"{site.ToString().ToLower()}.tm-exchange.com",
};

private static void AppendValue(StringBuilder b, Type type, object val, Type? genericType = null)
{
Expand Down

0 comments on commit 514cc62

Please sign in to comment.