From f38d00c24ec10e3dc7835231c5b107addd3c00ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Mon, 12 Dec 2022 20:26:04 +0100 Subject: [PATCH 1/7] Fix equal distribution ESite.Any --- Src/RandomizerTMF.Logic/RandomizerEngine.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RandomizerEngine.cs b/Src/RandomizerTMF.Logic/RandomizerEngine.cs index 4cf2548..4c925c9 100644 --- a/Src/RandomizerTMF.Logic/RandomizerEngine.cs +++ b/Src/RandomizerTMF.Logic/RandomizerEngine.cs @@ -826,13 +826,15 @@ public static void ValidateRules() } if (Config.Rules.RequestRules.EqualEnvironmentDistribution - && Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)) + && (Config.Rules.RequestRules.Site == 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"); } if (Config.Rules.RequestRules.EqualVehicleDistribution - && Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)) + && (Config.Rules.RequestRules.Site == 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"); } From 3f9bf6802af60d8e7e375f0d5ad0bd535496d049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Mon, 12 Dec 2022 20:59:36 +0100 Subject: [PATCH 2/7] Add version, improve bool visualization --- Src/RandomizerTMF.Logic/RandomizerEngine.cs | 8 ++- Src/RandomizerTMF.Logic/SessionData.cs | 6 +- .../ViewModels/SessionDataViewModel.cs | 59 ++++++++++++------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RandomizerEngine.cs b/Src/RandomizerTMF.Logic/RandomizerEngine.cs index 4c925c9..0e820f3 100644 --- a/Src/RandomizerTMF.Logic/RandomizerEngine.cs +++ b/Src/RandomizerTMF.Logic/RandomizerEngine.cs @@ -111,7 +111,9 @@ private set public static StreamWriter LogWriter { get; private set; } public static StreamWriter? CurrentSessionLogWriter { get; private set; } public static bool SessionEnding { get; private set; } - + + public static string? Version { get; } = typeof(RandomizerEngine).Assembly.GetName().Version?.ToString(3); + [GeneratedRegex("[^a-zA-Z0-9_.]+")] private static partial Regex SpecialCharRegex(); @@ -148,7 +150,7 @@ static RandomizerEngine() }; Http = new HttpClient(socketHandler); - Http.DefaultRequestHeaders.UserAgent.TryParseAdd($"Randomizer TMF {typeof(RandomizerEngine).Assembly.GetName().Version}"); + Http.DefaultRequestHeaders.UserAgent.TryParseAdd($"Randomizer TMF {Version}"); Logger.LogInformation("Preparing general events..."); @@ -749,7 +751,7 @@ private static void InitializeSessionData() { var startedAt = DateTimeOffset.Now; - CurrentSessionData = new SessionData(startedAt, Config.Rules); + CurrentSessionData = new SessionData(Version, startedAt, Config.Rules); if (CurrentSessionDataDirectoryPath is null) { diff --git a/Src/RandomizerTMF.Logic/SessionData.cs b/Src/RandomizerTMF.Logic/SessionData.cs index aa7efd3..67f0768 100644 --- a/Src/RandomizerTMF.Logic/SessionData.cs +++ b/Src/RandomizerTMF.Logic/SessionData.cs @@ -4,6 +4,7 @@ namespace RandomizerTMF.Logic; public class SessionData { + public string? Version { get; set; } public DateTimeOffset StartedAt { get; set; } public RandomizerRules Rules { get; set; } @@ -12,13 +13,14 @@ public class SessionData public List Maps { get; set; } = new(); - public SessionData() : this(DateTimeOffset.Now, new()) + public SessionData() : this(null, DateTimeOffset.Now, new()) { } - public SessionData(DateTimeOffset startedAt, RandomizerRules rules) + public SessionData(string? version, DateTimeOffset startedAt, RandomizerRules rules) { + Version = version; StartedAt = startedAt; Rules = rules; } diff --git a/Src/RandomizerTMF/ViewModels/SessionDataViewModel.cs b/Src/RandomizerTMF/ViewModels/SessionDataViewModel.cs index 81e7690..2423f9d 100644 --- a/Src/RandomizerTMF/ViewModels/SessionDataViewModel.cs +++ b/Src/RandomizerTMF/ViewModels/SessionDataViewModel.cs @@ -4,8 +4,7 @@ using ReactiveUI; using System.Collections; using System.Collections.ObjectModel; -using System.Diagnostics; -using System.Linq; +using System.Reflection; using System.Text.RegularExpressions; using TmEssentials; @@ -107,7 +106,10 @@ public SessionDataViewModel(SessionDataModel model) private ObservableCollection ConstructRules() { - var rules = new ObservableCollection(); + var rules = new ObservableCollection + { + "Version: " + (Model.Data.Version is null ? "< 1.0.3" : Model.Data.Version) + }; if (Model.Data.Rules is null) // To handle sessions made in early Randomizer TMF version { @@ -121,39 +123,54 @@ private ObservableCollection ConstructRules() continue; } - rules.Add($"{ToSentenceCase(prop.Name)}: {prop.GetValue(Model.Data.Rules)}"); + AddRuleString(rules, prop, Model.Data.Rules); } foreach (var prop in Model.Data.Rules.RequestRules.GetType().GetProperties()) { - var val = prop.GetValue(Model.Data.Rules.RequestRules); + AddRuleString(rules, prop, Model.Data.Rules.RequestRules); + } + + return rules; + } + + private static void AddRuleString(IList rules, PropertyInfo prop, object owner) + { + var val = prop.GetValue(owner); + + if (val is null) + { + return; + } - if (val is null) + if (val is bool valBool) + { + if (valBool) { - continue; + rules.Add(ToSentenceCase(prop.Name)); } - if (val is not string and IEnumerable enumerable) + return; + } + + if (val is not string and IEnumerable enumerable) + { + if (enumerable.Cast().Any()) { - if (enumerable.Cast().Any()) - { - val = string.Join(", ", enumerable.Cast().Select(x => x.ToString())); - } - else - { - val = null; - } + val = string.Join(", ", enumerable.Cast().Select(x => x.ToString())); } - - if (val is TimeInt32 timeInt32) + else { - val = timeInt32.ToString(useHundredths: true); + val = null; } + } - rules.Add($"{ToSentenceCase(prop.Name)}: {val}"); + if (val is TimeInt32 timeInt32) + { + val = timeInt32.ToString(useHundredths: true); } - return rules; + rules.Add($"{ToSentenceCase(prop.Name)}: {val}"); } public void OpenSessionFolderClick() From e3f7994c5e0c70e3c0edeab80d69781f400e8cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Tue, 13 Dec 2022 01:23:12 +0100 Subject: [PATCH 3/7] Discard Nations-invalid params --- Src/RandomizerTMF.Logic/RequestRules.cs | 40 ++++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RequestRules.cs b/Src/RandomizerTMF.Logic/RequestRules.cs index 926c820..6df8f63 100644 --- a/Src/RandomizerTMF.Logic/RequestRules.cs +++ b/Src/RandomizerTMF.Logic/RequestRules.cs @@ -52,17 +52,19 @@ public string ToUrl() // Not very efficient but does the job done fast enough .Where(x => x != ESite.Any && (Site & x) == x) .ToArray(); - var siteUrl = GetSiteUrl(matchingSites.Length == 0 + var site = GetSite(matchingSites.Length == 0 ? siteValues.Where(x => x is not ESite.Any).ToArray() : matchingSites); + var siteUrl = GetSiteUrl(site); + b.Append(siteUrl); b.Append("/trackrandom"); var first = true; - foreach (var prop in GetType().GetProperties().Where(DoesNotSkip)) + foreach (var prop in GetType().GetProperties().Where(IsQueryProperty)) { var val = prop.GetValue(this); @@ -81,6 +83,12 @@ public string ToUrl() // Not very efficient but does the job done fast enough continue; } + // Adjust url on weird combinations + if (site is ESite.TMNF or ESite.Nations && !IsValidInNations(prop, val)) + { + continue; + } + if (first) { b.Append('?'); @@ -109,13 +117,28 @@ public string ToUrl() // Not very efficient but does the job done fast enough return b.ToString(); } - private bool DoesNotSkip(PropertyInfo prop) + private bool IsQueryProperty(PropertyInfo prop) { return prop.Name is not nameof(Site) and not nameof(EqualEnvironmentDistribution) and not nameof(EqualVehicleDistribution); } + private bool IsValidInNations(PropertyInfo prop, object val) + { + if (prop.Name is nameof(Environment) or nameof(Vehicle) && !val.Equals(EEnvironment.Stadium)) + { + return false; + } + + if (prop.Name is nameof(PrimaryType) && !val.Equals(EPrimaryType.Race)) + { + return false; + } + + return true; + } + private static EEnvironment GetRandomEnvironment(HashSet? container) { if (container is null || container.Count == 0) @@ -131,16 +154,19 @@ private static HashSet GetRandomEnvironmentThroughSet(HashSet() { GetRandomEnvironment(container) }; } - private static string GetSiteUrl(ESite[] matchingSites) + private static ESite GetSite(ESite[] matchingSites) { - var randomSite = matchingSites[Random.Shared.Next(matchingSites.Length)]; + return matchingSites[Random.Shared.Next(matchingSites.Length)]; + } - return randomSite switch + private static string GetSiteUrl(ESite site) + { + return site switch { ESite.Any => throw new UnreachableException("Any is not a valid site"), ESite.TMNF => "tmnf.exchange", ESite.TMUF => "tmuf.exchange", - _ => $"{randomSite.ToString().ToLower()}.tm-exchange.com", + _ => $"{site.ToString().ToLower()}.tm-exchange.com", }; } From 071d3b63927d7133ba1d516fd4aff6757837ccc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Tue, 13 Dec 2022 01:24:31 +0100 Subject: [PATCH 4/7] Add retry policy to failed replay parses, improve rules validation --- Src/RandomizerTMF.Logic/RandomizerConfig.cs | 3 + Src/RandomizerTMF.Logic/RandomizerEngine.cs | 104 +++++++++----------- 2 files changed, 47 insertions(+), 60 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RandomizerConfig.cs b/Src/RandomizerTMF.Logic/RandomizerConfig.cs index ad67b40..e409643 100644 --- a/Src/RandomizerTMF.Logic/RandomizerConfig.cs +++ b/Src/RandomizerTMF.Logic/RandomizerConfig.cs @@ -17,4 +17,7 @@ public class RandomizerConfig /// {0} is the map name, {1} is the replay score (example: 9'59''59 in Race/Puzzle or 999_9'59''59 in Platform/Stunts), {2} is the player login. /// public string? ReplayFileFormat { get; set; } = Constants.DefaultReplayFileFormat; + + public int ReplayParseFailRetries { get; set; } = 10; + public int ReplayParseFailDelayMs { get; set; } = 50; } diff --git a/Src/RandomizerTMF.Logic/RandomizerEngine.cs b/Src/RandomizerTMF.Logic/RandomizerEngine.cs index 0e820f3..0898e6e 100644 --- a/Src/RandomizerTMF.Logic/RandomizerEngine.cs +++ b/Src/RandomizerTMF.Logic/RandomizerEngine.cs @@ -189,34 +189,52 @@ private static void AutosaveCreatedOrChanged(object sender, FileSystemEventArgs lastAutosaveUpdate = lastWriteTime; // + var retryCounter = 0; + CGameCtnReplayRecord replay; - try + while (true) { - // Any kind of autosave update section - - Logger.LogInformation("Analyzing a new file {autosavePath} in autosaves folder...", e.FullPath); - - if (GameBox.ParseNode(e.FullPath) is not CGameCtnReplayRecord r) + try { - Logger.LogWarning("Found file {file} that is not a replay.", e.FullPath); - return; - } + // Any kind of autosave update section - if (r.MapInfo is null) - { - Logger.LogWarning("Found replay {file} that has no map info.", e.FullPath); - return; + Logger.LogInformation("Analyzing a new file {autosavePath} in autosaves folder...", e.FullPath); + + if (GameBox.ParseNode(e.FullPath) is not CGameCtnReplayRecord r) + { + Logger.LogWarning("Found file {file} that is not a replay.", e.FullPath); + return; + } + + if (r.MapInfo is null) + { + Logger.LogWarning("Found replay {file} that has no map info.", e.FullPath); + return; + } + + AutosaveHeaders.TryAdd(r.MapInfo.Id, new AutosaveHeader(Path.GetFileName(e.FullPath), r)); + + replay = r; } + catch (Exception ex) + { + retryCounter++; + + Logger.LogError(ex, "Error while analyzing a new file {autosavePath} in autosaves folder (retry {counter}/{maxRetries}).", + e.FullPath, retryCounter, Config.ReplayParseFailRetries); - AutosaveHeaders.TryAdd(r.MapInfo.Id, new AutosaveHeader(Path.GetFileName(e.FullPath), r)); + if (retryCounter >= Config.ReplayParseFailRetries) + { + return; + } - replay = r; - } - catch (Exception ex) - { - Logger.LogError(ex, "Error while analyzing a new file {autosavePath} in autosaves folder.", e.FullPath); - return; + Thread.Sleep(Config.ReplayParseFailDelayMs); + + continue; + } + + break; } try @@ -786,59 +804,25 @@ public static void ValidateRules() throw new RuleValidationException("Time limit cannot be above 9:59:59"); } - foreach (var primaryType in Enum.GetValues()) - { - if (primaryType is EPrimaryType.Race) - { - continue; - } - - if (Config.Rules.RequestRules.PrimaryType == primaryType - && (Config.Rules.RequestRules.Site == ESite.Any - || Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) || Config.Rules.RequestRules.Site.HasFlag(ESite.Nations))) - { - throw new RuleValidationException($"{primaryType} is not valid with TMNF or Nations Exchange"); - } - } - - if (Config.Rules.RequestRules.Environment is not null || Config.Rules.RequestRules.Vehicle is not null) + if (Config.Rules.RequestRules.EqualEnvironmentDistribution + && Config.Rules.RequestRules.EqualVehicleDistribution + && Config.Rules.RequestRules.Site != ESite.TMUF) { - foreach (var env in Enum.GetValues()) - { - if (env is EEnvironment.Stadium) - { - continue; - } - - if (Config.Rules.RequestRules.Site != ESite.Any && !Config.Rules.RequestRules.Site.HasFlag(ESite.TMNF) && !Config.Rules.RequestRules.Site.HasFlag(ESite.Nations)) - { - continue; - } - - if (Config.Rules.RequestRules.Environment?.Contains(env) == true) - { - throw new RuleValidationException($"{env} is not valid with TMNF or Nations Exchange"); - } - - if (Config.Rules.RequestRules.Vehicle?.Contains(env) == true) - { - throw new RuleValidationException($"{env}Car is not valid with TMNF or Nations 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 == 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 == 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"); } } From 02fc39b32634292ca79701d2cce0d5ea2aea152c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Tue, 13 Dec 2022 20:10:18 +0100 Subject: [PATCH 5/7] Update to 1.0.3 --- README.md | 2 +- Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj | 2 +- Src/RandomizerTMF/RandomizerTMF.csproj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0b1d03e..2da7fe0 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ The project combines features of [TMX](https://tm-exchange.com/), autosave Gbx f [![GitHub all releases](https://img.shields.io/github/downloads/BigBang1112/randomizer-tmf/total?style=for-the-badge)](https://github.com/BigBang1112/randomizer-tmf/releases) - ✔️ **50 downloads within 1 week** - Guaranteed support throughout 2023 -- **100 downloads** - Discord Rich Presence integration +- ✔️ **100 downloads** - Discord Rich Presence integration **(coming right after refactoring)** - **300 downloads** - TMUF theme - **500 downloads** - Profile management (fresh account randomization) - **2000 downloads** - Automated RMC leaderboards diff --git a/Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj b/Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj index 2979829..0d5611b 100644 --- a/Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj +++ b/Src/RandomizerTMF.Logic/RandomizerTMF.Logic.csproj @@ -1,7 +1,7 @@ - 1.0.2 + 1.0.3 net7.0 enable enable diff --git a/Src/RandomizerTMF/RandomizerTMF.csproj b/Src/RandomizerTMF/RandomizerTMF.csproj index d4e6849..76a03dc 100644 --- a/Src/RandomizerTMF/RandomizerTMF.csproj +++ b/Src/RandomizerTMF/RandomizerTMF.csproj @@ -20,7 +20,7 @@ - 1.0.2 + 1.0.3 true true From d6b6e9d5cd68c028ba4ac11e2d3c0aa3cf622405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Tue, 13 Dec 2022 20:54:42 +0100 Subject: [PATCH 6/7] Improve validation in cases where strip would fail --- Src/RandomizerTMF.Logic/RandomizerEngine.cs | 37 +++++++++++++++++-- .../Views/RequestRulesControl.axaml | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RandomizerEngine.cs b/Src/RandomizerTMF.Logic/RandomizerEngine.cs index 0898e6e..40f7e8e 100644 --- a/Src/RandomizerTMF.Logic/RandomizerEngine.cs +++ b/Src/RandomizerTMF.Logic/RandomizerEngine.cs @@ -804,22 +804,53 @@ public static void ValidateRules() throw new RuleValidationException("Time limit cannot be above 9:59:59"); } + foreach (var primaryType in Enum.GetValues()) + { + if (primaryType is EPrimaryType.Race) + { + continue; + } + + if (Config.Rules.RequestRules.PrimaryType == primaryType + && (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"); + } + } + + 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))) + { + throw new RuleValidationException("Stadium has to be selected when environments are specified and TMNF or Nations 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))) + { + 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 != ESite.TMUF) + && Config.Rules.RequestRules.Site is not ESite.TMUF) { throw new RuleValidationException("Equal environment and car distribution combined is only valid with TMUF Exchange"); } if (Config.Rules.RequestRules.EqualEnvironmentDistribution - && (Config.Rules.RequestRules.Site == ESite.Any + && (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"); } if (Config.Rules.RequestRules.EqualVehicleDistribution - && (Config.Rules.RequestRules.Site == ESite.Any + && (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"); diff --git a/Src/RandomizerTMF/Views/RequestRulesControl.axaml b/Src/RandomizerTMF/Views/RequestRulesControl.axaml index 587f088..7ae4253 100644 --- a/Src/RandomizerTMF/Views/RequestRulesControl.axaml +++ b/Src/RandomizerTMF/Views/RequestRulesControl.axaml @@ -145,7 +145,7 @@ If any horizontal layer above has no buttons toggled, it simply means "any of those". + Opacity="0.5">If any horizontal layer above has no buttons toggled, it simply means "all selected". From 514cc62729dde87d5e6d1d91c30197176834eba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Pivo=C5=88ka?= Date: Tue, 13 Dec 2022 21:57:35 +0100 Subject: [PATCH 7/7] Fix Sunrise/Original site validations --- Src/RandomizerTMF.Logic/RandomizerEngine.cs | 79 ++++++++++++++++----- Src/RandomizerTMF.Logic/RequestRules.cs | 72 +++++++++++++++---- 2 files changed, 121 insertions(+), 30 deletions(-) diff --git a/Src/RandomizerTMF.Logic/RandomizerEngine.cs b/Src/RandomizerTMF.Logic/RandomizerEngine.cs index 40f7e8e..2ba8a98 100644 --- a/Src/RandomizerTMF.Logic/RandomizerEngine.cs +++ b/Src/RandomizerTMF.Logic/RandomizerEngine.cs @@ -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()) @@ -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."); } } diff --git a/Src/RandomizerTMF.Logic/RequestRules.cs b/Src/RandomizerTMF.Logic/RequestRules.cs index 6df8f63..38816f6 100644 --- a/Src/RandomizerTMF.Logic/RequestRules.cs +++ b/Src/RandomizerTMF.Logic/RequestRules.cs @@ -1,4 +1,5 @@ -using System.Collections; +using RandomizerTMF.Logic.Exceptions; +using System.Collections; using System.Diagnostics; using System.Reflection; using System.Text; @@ -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); @@ -124,6 +129,50 @@ and not nameof(EqualEnvironmentDistribution) and not nameof(EqualVehicleDistribution); } + private static bool IsSiteValidWithEnvironments(ESite site, HashSet? 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)) @@ -154,21 +203,18 @@ private static HashSet GetRandomEnvironmentThroughSet(HashSet() { 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) {