Parse settings from request for generation
This commit is contained in:
@@ -1,27 +1,44 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
namespace ALttPRandomizer.Model {
|
||||
using ALttPRandomizer.Settings;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ALttPRandomizer.Model {
|
||||
public class SeedSettings {
|
||||
public const string Omit = "<null>";
|
||||
|
||||
public Mode Mode { get; set; } = Mode.Open;
|
||||
|
||||
public Weapons Weapons { get; set; } = Weapons.Randomized;
|
||||
public Weapons Weapons { get; set; } = Weapons.Random;
|
||||
|
||||
public Goal Goal { get; set; } = Goal.Ganon;
|
||||
|
||||
public EntranceShuffle EntranceShuffle { get; set; } = EntranceShuffle.None;
|
||||
public EntranceShuffle EntranceShuffle { get; set; } = EntranceShuffle.Vanilla;
|
||||
public SkullWoodsShuffle SkullWoods { get; set; } = SkullWoodsShuffle.Original;
|
||||
public LinkedDrops LinkedDrops { get; set; } = LinkedDrops.Unset;
|
||||
|
||||
public BossShuffle BossShuffle { get; set; } = BossShuffle.None;
|
||||
public BossShuffle BossShuffle { get; set; } = BossShuffle.Vanilla;
|
||||
|
||||
public EnemyShuffle EnemyShuffle { get; set; } = EnemyShuffle.Vanilla;
|
||||
|
||||
[CommonValue("keyshuffle")]
|
||||
public DungeonItemLocations SmallKeys { get; set; } = DungeonItemLocations.Dungeon;
|
||||
|
||||
[CommonValue("bigkeyshuffle")]
|
||||
[DeniedValues(DungeonItemLocations.Universal)]
|
||||
public DungeonItemLocations BigKeys { get; set; } = DungeonItemLocations.Dungeon;
|
||||
|
||||
[CommonValue("mapshuffle")]
|
||||
[DeniedValues(DungeonItemLocations.Universal)]
|
||||
public DungeonItemLocations Maps { get; set; } = DungeonItemLocations.Dungeon;
|
||||
|
||||
[CommonValue("compassshuffle")]
|
||||
[DeniedValues(DungeonItemLocations.Universal)]
|
||||
public DungeonItemLocations Compasses { get; set; } = DungeonItemLocations.Dungeon;
|
||||
|
||||
public ShopShuffle ShopShuffle { get; set; } = ShopShuffle.Vanilla;
|
||||
public DropShuffle DropShuffle { get; set; } = DropShuffle.Vanilla;
|
||||
public Pottery Pottery { get; set; } = Pottery.Vanilla;
|
||||
|
||||
public PrizeShuffle PrizeShuffle { get; set; } = PrizeShuffle.Vanilla;
|
||||
}
|
||||
|
||||
public enum Mode {
|
||||
@@ -30,8 +47,9 @@ namespace ALttPRandomizer.Model {
|
||||
Inverted,
|
||||
}
|
||||
|
||||
[CommonValue("swords")]
|
||||
public enum Weapons {
|
||||
Randomized,
|
||||
Random,
|
||||
Assured,
|
||||
Vanilla,
|
||||
Swordless,
|
||||
@@ -39,33 +57,90 @@ namespace ALttPRandomizer.Model {
|
||||
|
||||
public enum Goal {
|
||||
Ganon,
|
||||
FastGanon,
|
||||
AllDungeons,
|
||||
[CommonValue("crystals")] FastGanon,
|
||||
[CommonValue("dungeons")] AllDungeons,
|
||||
Pedestal,
|
||||
TriforceHunt,
|
||||
[CommonValue("triforcehunt")]TriforceHunt,
|
||||
GanonHunt,
|
||||
Completionist,
|
||||
}
|
||||
|
||||
[CommonValue("shuffle")]
|
||||
public enum EntranceShuffle {
|
||||
None,
|
||||
Vanilla,
|
||||
Full,
|
||||
Crossed,
|
||||
Decoupled,
|
||||
Swapped,
|
||||
[CommonValue("insanity")] Decoupled,
|
||||
}
|
||||
|
||||
[CommonValue("skullwoods")]
|
||||
public enum SkullWoodsShuffle {
|
||||
Original,
|
||||
Restricted,
|
||||
Loose,
|
||||
FollowLinked,
|
||||
}
|
||||
|
||||
[CommonValue("linked_drops")]
|
||||
public enum LinkedDrops {
|
||||
Unset,
|
||||
Linked,
|
||||
Independent,
|
||||
}
|
||||
|
||||
[CommonValue("shufflebosses")]
|
||||
public enum BossShuffle {
|
||||
None,
|
||||
[CommonValue("none")] Vanilla,
|
||||
Simple,
|
||||
Full,
|
||||
Random,
|
||||
PrizeUnique,
|
||||
[CommonValue("unique")] PrizeUnique,
|
||||
}
|
||||
|
||||
[CommonValue("shuffleenemies")]
|
||||
public enum EnemyShuffle {
|
||||
[CommonValue("none")] Vanilla,
|
||||
Shuffled,
|
||||
Mimics,
|
||||
}
|
||||
|
||||
public enum DungeonItemLocations {
|
||||
Dungeon,
|
||||
[CommonValue("none")] Dungeon,
|
||||
Wild,
|
||||
Nearby,
|
||||
Universal,
|
||||
}
|
||||
|
||||
[CommonValue("shopsanity")]
|
||||
public enum ShopShuffle {
|
||||
[CommonValue(SeedSettings.Omit)] Vanilla,
|
||||
[CommonValue("true")] Shuffled,
|
||||
}
|
||||
|
||||
public enum DropShuffle {
|
||||
[CommonValue("none")] Vanilla,
|
||||
Keys,
|
||||
Underworld,
|
||||
}
|
||||
|
||||
public enum Pottery {
|
||||
[CommonValue("none")] Vanilla,
|
||||
Keys,
|
||||
Cave,
|
||||
CaveKeys,
|
||||
Reduced,
|
||||
Clustered,
|
||||
NonEmpty,
|
||||
Dungeon,
|
||||
Lottery,
|
||||
}
|
||||
|
||||
public enum PrizeShuffle {
|
||||
[CommonValue("none")] Vanilla,
|
||||
Dungeon,
|
||||
Nearby,
|
||||
Wild,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
using ALttPRandomizer.Azure;
|
||||
using ALttPRandomizer.Options;
|
||||
using ALttPRandomizer.Service;
|
||||
using global::Azure.Core;
|
||||
using ALttPRandomizer.Settings;
|
||||
using global::Azure.Identity;
|
||||
using global::Azure.Storage.Blobs;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@@ -55,7 +55,9 @@
|
||||
|
||||
builder.Services.AddSingleton(seedClient);
|
||||
builder.Services.AddSingleton<AzureStorage>();
|
||||
builder.Services.AddSingleton<CommonSettingsProcessor>();
|
||||
builder.Services.AddScoped<Randomizer>();
|
||||
builder.Services.AddScoped<RandomizeService>();
|
||||
builder.Services.AddScoped<SeedService>();
|
||||
builder.Services.AddScoped<IdGenerator>();
|
||||
|
||||
|
||||
@@ -1,31 +1,34 @@
|
||||
namespace ALttPRandomizer {
|
||||
using ALttPRandomizer.Azure;
|
||||
using ALttPRandomizer.Model;
|
||||
using ALttPRandomizer.Options;
|
||||
using ALttPRandomizer.Settings;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class Randomizer {
|
||||
public Randomizer(
|
||||
IdGenerator idGenerator,
|
||||
AzureStorage azureStorage,
|
||||
CommonSettingsProcessor settingsProcessor,
|
||||
IOptionsMonitor<ServiceOptions> optionsMonitor,
|
||||
ILogger<Randomizer> logger) {
|
||||
this.IdGenerator = idGenerator;
|
||||
this.AzureStorage = azureStorage;
|
||||
this.SettingsProcessor = settingsProcessor;
|
||||
this.OptionsMonitor = optionsMonitor;
|
||||
this.Logger = logger;
|
||||
}
|
||||
|
||||
private CommonSettingsProcessor SettingsProcessor { get; }
|
||||
private AzureStorage AzureStorage { get; }
|
||||
private IOptionsMonitor<ServiceOptions> OptionsMonitor { get; }
|
||||
private IdGenerator IdGenerator { get; }
|
||||
private ILogger<Randomizer> Logger { get; }
|
||||
private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue;
|
||||
|
||||
public string Randomize() {
|
||||
public void Randomize(string id, SeedSettings settings) {
|
||||
var start = new ProcessStartInfo() {
|
||||
FileName = Configuration.PythonPath,
|
||||
WorkingDirectory = Configuration.RandomizerPath,
|
||||
@@ -33,8 +36,6 @@
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
var id = IdGenerator.GenerateId();
|
||||
|
||||
var args = start.ArgumentList;
|
||||
args.Add("DungeonRandomizer.py");
|
||||
args.Add("--rom");
|
||||
@@ -47,8 +48,38 @@
|
||||
args.Add("--outputname");
|
||||
args.Add(id);
|
||||
|
||||
args.Add("--reduce_flashing");
|
||||
args.Add("--quickswap");
|
||||
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.Mode));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.Weapons));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.Goal));
|
||||
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(nameof(SeedSettings.SmallKeys), settings.SmallKeys));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(nameof(SeedSettings.BigKeys), settings.BigKeys));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(nameof(SeedSettings.Maps), settings.Maps));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(nameof(SeedSettings.Compasses), settings.Compasses));
|
||||
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.EntranceShuffle));
|
||||
if (settings.EntranceShuffle != EntranceShuffle.Vanilla) {
|
||||
args.Add("--shufflelinks");
|
||||
args.Add("--shuffletavern");
|
||||
}
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.SkullWoods));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.LinkedDrops));
|
||||
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.BossShuffle));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.EnemyShuffle));
|
||||
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.ShopShuffle));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.DropShuffle));
|
||||
this.AddArgs(args, this.SettingsProcessor.GetSettingPair(settings.Pottery));
|
||||
if (settings.Pottery != Pottery.Vanilla && settings.Pottery != Pottery.Lottery) {
|
||||
args.Add("--colorizepots");
|
||||
}
|
||||
|
||||
this.Logger.LogInformation("Randomizing with args: {args}", string.Join(" ", args));
|
||||
|
||||
var process = Process.Start(start) ?? throw new GenerationFailedException("Process failed to start.");
|
||||
process.EnableRaisingEvents = true;
|
||||
|
||||
@@ -60,7 +91,6 @@
|
||||
|
||||
process.Exited += async (sender, args) => {
|
||||
var exitcode = process.ExitCode;
|
||||
process.Dispose();
|
||||
|
||||
if (exitcode != 0) {
|
||||
this.GenerationFailed(id, exitcode);
|
||||
@@ -68,17 +98,21 @@
|
||||
await this.GenerationSucceeded(id);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return id;
|
||||
private void AddArgs(ICollection<string> args, KeyValuePair<string, string> setting) {
|
||||
if (setting.Value != null && setting.Value != "<null>") {
|
||||
args.Add(string.Format("--{0}={1}", setting.Key, setting.Value));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task GenerationSucceeded(string id) {
|
||||
var rom = Path.Join(Path.GetTempPath(), string.Format("DR_{0}.sfc", id));
|
||||
var rom = Path.Join(Path.GetTempPath(), string.Format("OR_{0}.sfc", id));
|
||||
|
||||
var bpsIn = Path.Join(Path.GetTempPath(), string.Format("DR_{0}.bps", id));
|
||||
var bpsIn = Path.Join(Path.GetTempPath(), string.Format("OR_{0}.bps", id));
|
||||
var bpsOut = string.Format("{0}/patch.bps", id);
|
||||
|
||||
var spoilerIn = Path.Join(Path.GetTempPath(), string.Format("DR_{0}_Spoiler.txt", id));
|
||||
var spoilerIn = Path.Join(Path.GetTempPath(), string.Format("OR_{0}_Spoiler.txt", id));
|
||||
var spoilerOut = string.Format("{0}/spoiler.txt", id);
|
||||
|
||||
var uploadPatch = this.AzureStorage.UploadFileAndDelete(bpsOut, bpsIn);
|
||||
|
||||
@@ -5,19 +5,20 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class GenerateController : Controller {
|
||||
public GenerateController(Randomizer randomizer, SeedService seedService) {
|
||||
this.Randomizer = randomizer;
|
||||
public GenerateController(RandomizeService randomizeService, SeedService seedService) {
|
||||
this.RandomizeService = randomizeService;
|
||||
this.SeedService = seedService;
|
||||
}
|
||||
|
||||
private Randomizer Randomizer { get; }
|
||||
private RandomizeService RandomizeService { get; }
|
||||
private SeedService SeedService { get; }
|
||||
|
||||
[Route("/generate")]
|
||||
[HttpPost]
|
||||
public ActionResult Generate(SeedSettings settings) {
|
||||
var result = this.Randomizer.Randomize();
|
||||
return Ok(result);
|
||||
var id = this.RandomizeService.RandomizeSeed(settings);
|
||||
var url = string.Format("/seed/{0}", id);
|
||||
return Accepted(url, id);
|
||||
}
|
||||
|
||||
[Route("/seed/{id}")]
|
||||
19
ALttPRandomizer/Service/RandomizeService.cs
Normal file
19
ALttPRandomizer/Service/RandomizeService.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using ALttPRandomizer.Model;
|
||||
|
||||
namespace ALttPRandomizer.Service {
|
||||
public class RandomizeService {
|
||||
public RandomizeService(IdGenerator idGenerator, Randomizer randomizer) {
|
||||
this.IdGenerator = idGenerator;
|
||||
this.Randomizer = randomizer;
|
||||
}
|
||||
|
||||
private IdGenerator IdGenerator { get; }
|
||||
private Randomizer Randomizer { get; }
|
||||
|
||||
public string RandomizeSeed(SeedSettings settings) {
|
||||
var id = this.IdGenerator.GenerateId();
|
||||
this.Randomizer.Randomize(id, settings);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
ALttPRandomizer/Settings/CommonSettingsProcessor.cs
Normal file
45
ALttPRandomizer/Settings/CommonSettingsProcessor.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace ALttPRandomizer.Settings {
|
||||
using ALttPRandomizer.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
public class CommonSettingsProcessor {
|
||||
public KeyValuePair<string, string> GetSettingPair<T>(T value) where T : Enum {
|
||||
var name = this.GetValueName(value);
|
||||
|
||||
Type type = typeof(T);
|
||||
|
||||
var settingName = type.GetCustomAttribute<CommonValueAttribute>()?.Name;
|
||||
if (settingName == null) {
|
||||
settingName = type.Name.ToLower();
|
||||
}
|
||||
|
||||
return new(settingName, name);
|
||||
}
|
||||
|
||||
public KeyValuePair<string, string> GetSettingPair<T>(string fieldName, T value) where T : Enum {
|
||||
var name = this.GetValueName(value);
|
||||
|
||||
var fi = typeof(SeedSettings).GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
var settingName = fi?.GetCustomAttribute<CommonValueAttribute>()?.Name ?? fieldName.ToLower();
|
||||
|
||||
return new(settingName, name);
|
||||
}
|
||||
|
||||
private string GetValueName<T>(T value) where T : Enum {
|
||||
Type type = typeof(T);
|
||||
|
||||
var fi = type.GetField(value.ToString(), BindingFlags.Static | BindingFlags.Public);
|
||||
|
||||
var name = fi?.GetCustomAttribute<CommonValueAttribute>()?.Name;
|
||||
|
||||
if (name != null) {
|
||||
return name;
|
||||
} else {
|
||||
return value.ToString().ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
ALttPRandomizer/Settings/CommonValueAttribute.cs
Normal file
11
ALttPRandomizer/Settings/CommonValueAttribute.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace ALttPRandomizer.Settings {
|
||||
using System;
|
||||
|
||||
internal class CommonValueAttribute : Attribute {
|
||||
public CommonValueAttribute(string name) {
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,10 @@
|
||||
"baserom": "/randomizer/alttp.sfc",
|
||||
"pythonPath": "/usr/bin/python3",
|
||||
"randomizerPath": "/randomizer",
|
||||
"allowedCors": [
|
||||
"https://new.alttpr.gwaa.kiwi",
|
||||
"http://localhost:8082"
|
||||
],
|
||||
"azureSettings": {
|
||||
"clientId": "a48d5ae1-fa0a-4e33-9586-18c0eca0a28c",
|
||||
"blobstoreEndpoint": "https://alttprstorage.blob.core.windows.net/seeds"
|
||||
|
||||
Reference in New Issue
Block a user