Add basic randomizer
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
ALttPRandomizer/GenerationFailedException.cs
Normal file
7
ALttPRandomizer/GenerationFailedException.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace ALttPRandomizer {
|
||||
using System;
|
||||
|
||||
public class GenerationFailedException : Exception {
|
||||
public GenerationFailedException(string message) : base(message) { }
|
||||
}
|
||||
}
|
||||
19
ALttPRandomizer/IdGenerator.cs
Normal file
19
ALttPRandomizer/IdGenerator.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
ALttPRandomizer/Options/ServiceOptions.cs
Normal file
6
ALttPRandomizer/Options/ServiceOptions.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace ALttPRandomizer.Options {
|
||||
public class ServiceOptions {
|
||||
public string PythonPath { get; set; } = null!;
|
||||
public string RandomizerPath { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
56
ALttPRandomizer/Randomizer.cs
Normal file
56
ALttPRandomizer/Randomizer.cs
Normal 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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
6
ALttPRandomizer/appsettings.Development.json
Normal file
6
ALttPRandomizer/appsettings.Development.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"ALttPRandomizer": {
|
||||
"pythonPath": "C:\\Program Files (x86)\\Python38-32\\python",
|
||||
"randomizerPath": "C:\\Users\\ardna\\git-projects\\ALttPRandomizer\\ALttPDoorRandomizer"
|
||||
}
|
||||
}
|
||||
4
ALttPRandomizer/appsettings.json
Normal file
4
ALttPRandomizer/appsettings.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"ALttPRandomizer": {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user