diff --git a/ALttPRandomizer/ALttPRandomizer.csproj b/ALttPRandomizer/ALttPRandomizer.csproj index 08537fd..c76c1ea 100644 --- a/ALttPRandomizer/ALttPRandomizer.csproj +++ b/ALttPRandomizer/ALttPRandomizer.csproj @@ -12,6 +12,10 @@ + + + + diff --git a/ALttPRandomizer/JsonOptions.cs b/ALttPRandomizer/JsonOptions.cs index ee0ecd6..3af48e8 100644 --- a/ALttPRandomizer/JsonOptions.cs +++ b/ALttPRandomizer/JsonOptions.cs @@ -3,7 +3,7 @@ using System.Text.Json.Serialization; public static class JsonOptions { - public static JsonSerializerOptions Default = new JsonSerializerOptions() { + public static JsonSerializerOptions Default = new JsonSerializerOptions(JsonSerializerDefaults.Web) { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, NumberHandling = JsonNumberHandling.Strict, }.WithStringEnum(); diff --git a/ALttPRandomizer/Model/SeedSettings.cs b/ALttPRandomizer/Model/SeedSettings.cs index 5be1165..78de692 100644 --- a/ALttPRandomizer/Model/SeedSettings.cs +++ b/ALttPRandomizer/Model/SeedSettings.cs @@ -4,6 +4,9 @@ using System.Text.Json.Serialization; public class SeedSettings { + [NoSettingName] + public RaceMode Race { get; set; } = RaceMode.Normal; + public Mode Mode { get; set; } = Mode.Open; [SettingName("swords")] @@ -54,6 +57,11 @@ public PrizeShuffle PrizeShuffle { get; set; } = PrizeShuffle.Vanilla; } + public enum RaceMode { + Normal, + [AdditionalSetting("--securerandom")] Race, + } + public enum Mode { Open, Standard, diff --git a/ALttPRandomizer/Program.cs b/ALttPRandomizer/Program.cs index 5670cfe..0b5bc93 100644 --- a/ALttPRandomizer/Program.cs +++ b/ALttPRandomizer/Program.cs @@ -1,6 +1,5 @@ namespace ALttPRandomizer { - using System.Text.Json.Serialization; using ALttPRandomizer.Azure; using ALttPRandomizer.Options; using ALttPRandomizer.Service; @@ -12,6 +11,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; + using Serilog; internal class Program { @@ -26,8 +26,13 @@ builder.Services.Configure(builder.Configuration.GetSection("ALttPRandomizer")); + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(builder.Configuration) + .CreateLogger(); + builder.Services.AddLogging(logger => { - logger.AddConsole(); + logger.ClearProviders(); + logger.AddSerilog(); }); var provider = builder.Services.BuildServiceProvider(); diff --git a/ALttPRandomizer/Randomizer.cs b/ALttPRandomizer/Randomizer.cs index 1547f9a..d3372e3 100644 --- a/ALttPRandomizer/Randomizer.cs +++ b/ALttPRandomizer/Randomizer.cs @@ -31,6 +31,8 @@ private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue; public void Randomize(string id, SeedSettings settings) { + this.Logger.LogDebug("Recieved request for id {id} to randomize settings {@settings}", id, settings); + var start = new ProcessStartInfo() { FileName = Configuration.PythonPath, WorkingDirectory = Configuration.RandomizerPath, @@ -112,7 +114,7 @@ this.Logger.LogDebug("Deleting file {filepath}", rom); File.Delete(rom); - this.Logger.LogDebug("Finished uploading seed id {id}", id); + this.Logger.LogInformation("Finished uploading seed id {id}", id); } private JsonDocument ProcessMetadata(string path) { diff --git a/ALttPRandomizer/SeedController.cs b/ALttPRandomizer/SeedController.cs index 300da8d..e771f0e 100644 --- a/ALttPRandomizer/SeedController.cs +++ b/ALttPRandomizer/SeedController.cs @@ -2,16 +2,19 @@ using ALttPRandomizer.Model; using ALttPRandomizer.Service; using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.Logging; using System.Threading.Tasks; - public class GenerateController : Controller { - public GenerateController(RandomizeService randomizeService, SeedService seedService) { + public class SeedController : Controller { + public SeedController(RandomizeService randomizeService, SeedService seedService, ILogger logger) { this.RandomizeService = randomizeService; this.SeedService = seedService; + this.Logger = logger; } private RandomizeService RandomizeService { get; } private SeedService SeedService { get; } + private ILogger Logger { get; } [Route("/generate")] [HttpPost] @@ -25,7 +28,19 @@ [HttpGet] public async Task GetSeed(string id) { var result = await this.SeedService.GetSeed(id); - return Ok(result); + if (result.TryGetValue("status", out var responseCode)) { + switch (responseCode) { + case 200: + return Ok(result); + case 404: + return NotFound(result); + case 409: + return Conflict(result); + } + } + + this.Logger.LogWarning("Unexpected result from SeedService: {@result}", result); + return StatusCode(500); } } } diff --git a/ALttPRandomizer/Service/SeedService.cs b/ALttPRandomizer/Service/SeedService.cs index a08ab8f..122392a 100644 --- a/ALttPRandomizer/Service/SeedService.cs +++ b/ALttPRandomizer/Service/SeedService.cs @@ -1,5 +1,6 @@ namespace ALttPRandomizer.Service { using ALttPRandomizer.Azure; + using ALttPRandomizer.Model; using System; using System.Collections.Generic; using System.Text.Json; @@ -17,24 +18,41 @@ var result = new Dictionary(); - if (files.TryGetValue("settings.json", out var settingsData)) { - var json = JsonDocument.Parse(settingsData.ToString()); - result["settings"] = json; + if (!files.TryGetValue("settings.json", out var settingsData)) { + result["status"] = 404; + result["error"] = "seed not found"; + return result; } + var settingsJson = JsonDocument.Parse(settingsData.ToString()); + result["settings"] = settingsJson; + + var settings = settingsJson.Deserialize(JsonOptions.Default) ?? new SeedSettings(); + + if (!files.TryGetValue("patch.bps", out var patchData)) { + if (files.ContainsKey("generating")) { + result["status"] = 409; + result["error"] = "generation still in progress"; + return result; + } else { + result["status"] = 404; + result["error"] = "generation failed"; + return result; + } + } + result["patch"] = Convert.ToBase64String(patchData.ToArray()); + if (files.TryGetValue("meta.json", out var metaData)) { var json = JsonDocument.Parse(metaData.ToString()); result["meta"] = json; } - if (files.TryGetValue("spoiler.json", out var spoilerData)) { + if (settings.Race != RaceMode.Race && files.TryGetValue("spoiler.json", out var spoilerData)) { var json = JsonDocument.Parse(spoilerData.ToString()); result["spoiler"] = json; } - if (files.TryGetValue("patch.bps", out var patchData)) { - result["patch.bps"] = Convert.ToBase64String(patchData.ToArray()); - } + result["status"] = 200; return result; } diff --git a/ALttPRandomizer/appsettings.Docker.json b/ALttPRandomizer/appsettings.Docker.json index 7aad910..40f3fd9 100644 --- a/ALttPRandomizer/appsettings.Docker.json +++ b/ALttPRandomizer/appsettings.Docker.json @@ -12,9 +12,12 @@ "blobstoreEndpoint": "https://alttprstorage.blob.core.windows.net/seeds" } }, - "Logging": { - "LogLevel": { - "Default": "Information" - } + "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], + "MinimumLevel": "Information", + "WriteTo": [ + { "Name": "Console" } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ] } } \ No newline at end of file diff --git a/ALttPRandomizer/appsettings.json b/ALttPRandomizer/appsettings.json index 6a4adb9..9821d01 100644 --- a/ALttPRandomizer/appsettings.json +++ b/ALttPRandomizer/appsettings.json @@ -1,4 +1,12 @@ { "ALttPRandomizer": { + }, + "Serilog": { + "Using": [ "Serilog.Sinks.Console" ], + "MinimumLevel": "Information", + "WriteTo": [ + { "Name": "Console" } + ], + "Enrich": [ "FromLogContext" ] } } \ No newline at end of file