show available custom maps and let them download (#8)

This commit is contained in:
CommonLoon102 2020-01-28 00:29:27 +00:00 committed by GitHub
parent 90ed231088
commit 3417571b70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 127 additions and 5 deletions

View File

@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace Common
{
public static class CommandLineUtils
{
public static string BloodDir => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "blood");
public static string GetClientLaunchCommand(string host, int port, string modCommandLine) =>
$"nblood -client {host} -port {port} {modCommandLine}";
}

View File

@ -2,8 +2,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Linq;
@ -18,7 +16,6 @@ namespace Common
private const int maximumServers = 80;
private const string nBloodExecutable = "nblood_server";
private static readonly string workingDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "blood");
private static readonly Random rnd = new Random();
public static SpawnedServerInfo SpawnServer(int players, string modName)
@ -67,7 +64,7 @@ namespace Common
var psi = new ProcessStartInfo(GetExecutableName(), $"-server {maxPlayers} -port {port} -pname Server {mod.CommandLine}")
{
UseShellExecute = true,
WorkingDirectory = workingDir
WorkingDirectory = CommandLineUtils.BloodDir
};
return psi;

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebInterface.Services;
namespace WebInterface.Controllers
{
public class CustomMapsController : Controller
{
private readonly ICustomMapService _customMapService;
public CustomMapsController(ICustomMapService customMapService)
{
_customMapService = customMapService;
}
[Route("nblood/custommaps", Name = "CustomMaps")]
public IActionResult Index()
{
var viewModel = _customMapService.ListCustomMaps();
return View(viewModel);
}
[Route("nblood/custommaps/download", Name = "DownloadCustomMap")]
public FileResult Index([FromQuery] string map)
{
byte[] customMap = _customMapService.GetCustomMapBytes(map);
return File(customMap, System.Net.Mime.MediaTypeNames.Application.Octet, map);
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace WebInterface.Services
{
public class CustomMapService : ICustomMapService
{
private static readonly List<string> crypticMaps = new List<string>()
{
"CPSL.MAP",
"CP01.MAP",
"CP02.MAP",
"CP03.MAP",
"CP04.MAP",
"CP05.MAP",
"CP06.MAP",
"CP07.MAP",
"CP08.MAP",
"CP09.MAP",
"CPBB01.MAP",
"CPBB02.MAP",
"CPBB03.MAP",
"CPBB04.MAP",
};
private List<string> ListableCustomMaps => Directory.GetFiles(Common.CommandLineUtils.BloodDir,
"*.map", SearchOption.TopDirectoryOnly)
.Select(m => Path.GetFileName(m))
.Where(m => !ContainsString(crypticMaps, m))
.ToList();
public IList<string> ListCustomMaps() => ListableCustomMaps;
public byte[] GetCustomMapBytes(string map)
{
if (ListableCustomMaps.Any(m => StringsAreSame(m, map)))
{
return File.ReadAllBytes(Path.Combine(Common.CommandLineUtils.BloodDir, map));
}
else
{
throw new Exception($"Cannot download this map: {map}");
}
}
private bool ContainsString(IEnumerable<string> list, string value) =>
list.Any(e => StringsAreSame(e, value));
private bool StringsAreSame(string left, string right) =>
string.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebInterface.Services
{
public interface ICustomMapService
{
IList<string> ListCustomMaps();
byte[] GetCustomMapBytes(string map);
}
}

View File

@ -42,6 +42,7 @@ namespace WebInterface
services.Add(new ServiceDescriptor(typeof(IStateService), typeof(StateService), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(IPrivateServerService), typeof(PrivateServerService), ServiceLifetime.Transient));
services.Add(new ServiceDescriptor(typeof(IRateLimiterService), typeof(RateLimiterService), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(ICustomMapService), typeof(CustomMapService), ServiceLifetime.Transient));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -0,0 +1,14 @@
@model List<string>
@{
ViewData["Title"] = "Custom Maps";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>List of available custom maps</h1>
<ul>
@foreach (var map in Model)
{
<li><a href="@Url.Link("DownloadCustomMap", new { map = map })">@map</a></li>
}
</ul>

View File

@ -35,8 +35,13 @@
<body style="background-color: cornsilk">
<div class="main">
<header>
<div style="float: left; margin-right: 1em;">
<form method="get">
<button asp-route="Home">Home</button>
</form>
</div>
<form method="get">
<button asp-route="Home">Home</button>
<button asp-route="CustomMaps">Custom Maps</button>
</form>
<hr />
<p>I live... Again!</p>