Add basic randomizer

This commit is contained in:
2025-02-22 13:01:35 -06:00
parent 6bdf0a67cb
commit c9407bd0bf
9 changed files with 127 additions and 1 deletions

View File

@@ -14,4 +14,13 @@
<PackageReference Include="System.Text.Json" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -3,10 +3,17 @@
using Microsoft.AspNetCore.Mvc;
public class GenerateController : Controller {
public GenerateController(Randomizer randomizer) {
this.Randomizer = randomizer;
}
private Randomizer Randomizer { get; }
[Route("/generate")]
[HttpPost]
public ActionResult Generate(SeedSettings settings) {
return Content("Hello world");
var result = this.Randomizer.Randomize();
return Content(result);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace ALttPRandomizer {
using System;
public class GenerationFailedException : Exception {
public GenerationFailedException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,19 @@
namespace ALttPRandomizer {
using System;
public class IdGenerator {
private const string chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
private const int length = 10;
private static Random random = new Random();
public string GenerateId() {
var str = new char[length];
for (int i = 0; i < length; i++) {
str[i] = chars[random.Next(chars.Length)];
}
return new string(str);
}
}
}

View File

@@ -0,0 +1,6 @@
namespace ALttPRandomizer.Options {
public class ServiceOptions {
public string PythonPath { get; set; } = null!;
public string RandomizerPath { get; set; } = null!;
}
}

View File

@@ -1,7 +1,9 @@
namespace ALttPRandomizer
{
using System.Text.Json.Serialization;
using ALttPRandomizer.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
internal class Program
@@ -10,10 +12,20 @@
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Development.json")
.AddEnvironmentVariables();
builder.Services.Configure<ServiceOptions>(builder.Configuration.GetSection("ALttPRandomizer"));
builder.Services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<Randomizer, Randomizer>();
builder.Services.AddScoped<IdGenerator, IdGenerator>();
var app = builder.Build();
app.UseHttpsRedirection();

View File

@@ -0,0 +1,56 @@
namespace ALttPRandomizer {
using ALttPRandomizer.Options;
using Microsoft.Extensions.Options;
using System.Diagnostics;
public class Randomizer {
public Randomizer(IdGenerator idGenerator, IOptionsMonitor<ServiceOptions> optionsMonitor) {
this.IdGenerator = idGenerator;
this.optionsMonitor = optionsMonitor;
}
private IOptionsMonitor<ServiceOptions> optionsMonitor;
private IdGenerator IdGenerator;
private ServiceOptions Configuration => optionsMonitor.CurrentValue;
public string Randomize() {
var start = new ProcessStartInfo() {
FileName = Configuration.PythonPath,
WorkingDirectory = Configuration.RandomizerPath,
RedirectStandardOutput = true,
};
var args = start.ArgumentList;
args.Add("DungeonRandomizer.py");
args.Add("--rom=../alttp.sfc");
args.Add("--bps");
args.Add("--quickswap");
var process = Process.Start(start) ?? throw new GenerationFailedException("Process failed to start.");
process.EnableRaisingEvents = true;
var id = IdGenerator.GenerateId();
process.Exited += (sender, args) => {
var exitcode = process.ExitCode;
process.Dispose();
if (exitcode != 0) {
this.GenerationFailed(id, exitcode);
} else {
this.GenerationSucceeded(id);
}
};
return id;
}
private void GenerationSucceeded(string id) {
}
private void GenerationFailed(string id, int exitcode) {
}
}
}

View File

@@ -0,0 +1,6 @@
{
"ALttPRandomizer": {
"pythonPath": "C:\\Program Files (x86)\\Python38-32\\python",
"randomizerPath": "C:\\Users\\ardna\\git-projects\\ALttPRandomizer\\ALttPDoorRandomizer"
}
}

View File

@@ -0,0 +1,4 @@
{
"ALttPRandomizer": {
}
}