Add race mode, serilog

This commit is contained in:
2025-03-03 07:39:28 -06:00
parent f44a7aec2e
commit a2dc4372bb
9 changed files with 81 additions and 18 deletions

View File

@@ -12,6 +12,10 @@
<PackageReference Include="Azure.Identity" Version="1.13.2" /> <PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Microsoft.Azure.WebPubSub.AspNetCore" Version="1.4.0" /> <PackageReference Include="Microsoft.Azure.WebPubSub.AspNetCore" Version="1.4.0" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="System.Text.Json" Version="9.0.2" /> <PackageReference Include="System.Text.Json" Version="9.0.2" />
</ItemGroup> </ItemGroup>

View File

@@ -3,7 +3,7 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
public static class JsonOptions { public static class JsonOptions {
public static JsonSerializerOptions Default = new JsonSerializerOptions() { public static JsonSerializerOptions Default = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
NumberHandling = JsonNumberHandling.Strict, NumberHandling = JsonNumberHandling.Strict,
}.WithStringEnum(); }.WithStringEnum();

View File

@@ -4,6 +4,9 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
public class SeedSettings { public class SeedSettings {
[NoSettingName]
public RaceMode Race { get; set; } = RaceMode.Normal;
public Mode Mode { get; set; } = Mode.Open; public Mode Mode { get; set; } = Mode.Open;
[SettingName("swords")] [SettingName("swords")]
@@ -54,6 +57,11 @@
public PrizeShuffle PrizeShuffle { get; set; } = PrizeShuffle.Vanilla; public PrizeShuffle PrizeShuffle { get; set; } = PrizeShuffle.Vanilla;
} }
public enum RaceMode {
Normal,
[AdditionalSetting("--securerandom")] Race,
}
public enum Mode { public enum Mode {
Open, Open,
Standard, Standard,

View File

@@ -1,6 +1,5 @@
namespace ALttPRandomizer namespace ALttPRandomizer
{ {
using System.Text.Json.Serialization;
using ALttPRandomizer.Azure; using ALttPRandomizer.Azure;
using ALttPRandomizer.Options; using ALttPRandomizer.Options;
using ALttPRandomizer.Service; using ALttPRandomizer.Service;
@@ -12,6 +11,7 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Serilog;
internal class Program internal class Program
{ {
@@ -26,8 +26,13 @@
builder.Services.Configure<ServiceOptions>(builder.Configuration.GetSection("ALttPRandomizer")); builder.Services.Configure<ServiceOptions>(builder.Configuration.GetSection("ALttPRandomizer"));
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.CreateLogger();
builder.Services.AddLogging(logger => { builder.Services.AddLogging(logger => {
logger.AddConsole(); logger.ClearProviders();
logger.AddSerilog();
}); });
var provider = builder.Services.BuildServiceProvider(); var provider = builder.Services.BuildServiceProvider();

View File

@@ -31,6 +31,8 @@
private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue; private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue;
public void Randomize(string id, SeedSettings settings) { 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() { var start = new ProcessStartInfo() {
FileName = Configuration.PythonPath, FileName = Configuration.PythonPath,
WorkingDirectory = Configuration.RandomizerPath, WorkingDirectory = Configuration.RandomizerPath,
@@ -112,7 +114,7 @@
this.Logger.LogDebug("Deleting file {filepath}", rom); this.Logger.LogDebug("Deleting file {filepath}", rom);
File.Delete(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) { private JsonDocument ProcessMetadata(string path) {

View File

@@ -2,16 +2,19 @@
using ALttPRandomizer.Model; using ALttPRandomizer.Model;
using ALttPRandomizer.Service; using ALttPRandomizer.Service;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks; using System.Threading.Tasks;
public class GenerateController : Controller { public class SeedController : Controller {
public GenerateController(RandomizeService randomizeService, SeedService seedService) { public SeedController(RandomizeService randomizeService, SeedService seedService, ILogger<SeedController> logger) {
this.RandomizeService = randomizeService; this.RandomizeService = randomizeService;
this.SeedService = seedService; this.SeedService = seedService;
this.Logger = logger;
} }
private RandomizeService RandomizeService { get; } private RandomizeService RandomizeService { get; }
private SeedService SeedService { get; } private SeedService SeedService { get; }
private ILogger<SeedController> Logger { get; }
[Route("/generate")] [Route("/generate")]
[HttpPost] [HttpPost]
@@ -25,7 +28,19 @@
[HttpGet] [HttpGet]
public async Task<ActionResult> GetSeed(string id) { public async Task<ActionResult> GetSeed(string id) {
var result = await this.SeedService.GetSeed(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);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
namespace ALttPRandomizer.Service { namespace ALttPRandomizer.Service {
using ALttPRandomizer.Azure; using ALttPRandomizer.Azure;
using ALttPRandomizer.Model;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json; using System.Text.Json;
@@ -17,24 +18,41 @@
var result = new Dictionary<string, object>(); var result = new Dictionary<string, object>();
if (files.TryGetValue("settings.json", out var settingsData)) { if (!files.TryGetValue("settings.json", out var settingsData)) {
var json = JsonDocument.Parse(settingsData.ToString()); result["status"] = 404;
result["settings"] = json; result["error"] = "seed not found";
return result;
} }
var settingsJson = JsonDocument.Parse(settingsData.ToString());
result["settings"] = settingsJson;
var settings = settingsJson.Deserialize<SeedSettings>(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)) { if (files.TryGetValue("meta.json", out var metaData)) {
var json = JsonDocument.Parse(metaData.ToString()); var json = JsonDocument.Parse(metaData.ToString());
result["meta"] = json; 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()); var json = JsonDocument.Parse(spoilerData.ToString());
result["spoiler"] = json; result["spoiler"] = json;
} }
if (files.TryGetValue("patch.bps", out var patchData)) { result["status"] = 200;
result["patch.bps"] = Convert.ToBase64String(patchData.ToArray());
}
return result; return result;
} }

View File

@@ -12,9 +12,12 @@
"blobstoreEndpoint": "https://alttprstorage.blob.core.windows.net/seeds" "blobstoreEndpoint": "https://alttprstorage.blob.core.windows.net/seeds"
} }
}, },
"Logging": { "Serilog": {
"LogLevel": { "Using": [ "Serilog.Sinks.Console" ],
"Default": "Information" "MinimumLevel": "Information",
} "WriteTo": [
{ "Name": "Console" }
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
} }
} }

View File

@@ -1,4 +1,12 @@
{ {
"ALttPRandomizer": { "ALttPRandomizer": {
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Information",
"WriteTo": [
{ "Name": "Console" }
],
"Enrich": [ "FromLogContext" ]
} }
} }