2021-05-16 10:43:37 -04:00
|
|
|
|
using IniParser;
|
|
|
|
|
using IniParser.Model;
|
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.ModelBinding;
|
|
|
|
|
using Nancy.Responses;
|
2021-05-17 12:02:10 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2021-05-16 10:43:37 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace EmpathicQbt.ConsoleServer {
|
|
|
|
|
public class CommandRequest
|
|
|
|
|
{
|
|
|
|
|
public string Command { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ServerModule : NancyModule {
|
2021-05-17 12:02:10 -04:00
|
|
|
|
private string[] weaponImages;
|
2021-05-16 10:43:37 -04:00
|
|
|
|
private SkyrimInterop skyrimInterop;
|
2021-05-17 12:02:10 -04:00
|
|
|
|
private IDictionary<string, string> weaponMap;
|
2021-05-16 10:43:37 -04:00
|
|
|
|
|
2021-05-17 12:02:10 -04:00
|
|
|
|
public ServerModule(SkyrimInterop skyrimInterop, IRootPathProvider pathy)
|
2021-05-16 10:43:37 -04:00
|
|
|
|
{
|
2021-05-17 12:02:10 -04:00
|
|
|
|
this.weaponImages = Directory.GetFiles(pathy.GetRootPath() + "/static/weapons");
|
|
|
|
|
this.weaponMap = JsonConvert.DeserializeObject<IDictionary<string, string>>(File.ReadAllText(pathy.GetRootPath() + "/weapon_map.json"));
|
|
|
|
|
|
2021-05-16 10:43:37 -04:00
|
|
|
|
this.skyrimInterop = skyrimInterop;
|
|
|
|
|
Get("/api/ping", _ => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
|
|
|
|
|
Get("/api/favorites", GetFavorites);
|
|
|
|
|
Post("/api/command", PostCommand);
|
2021-05-16 11:09:43 -04:00
|
|
|
|
Get("/", _ => Response.AsFile("static/index.html"));
|
2021-05-17 12:02:10 -04:00
|
|
|
|
Get("/weapons/{filename}.png", GetImage);
|
2021-05-16 10:43:37 -04:00
|
|
|
|
Get("/{filename}", _ => Response.AsFile("static/" + (string)_.filename));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 12:02:10 -04:00
|
|
|
|
public object GetImage(dynamic x) {
|
|
|
|
|
string filename = x.filename;
|
|
|
|
|
foreach(var kvp in weaponMap)
|
|
|
|
|
{
|
|
|
|
|
if(filename.EndsWith(kvp.Key))
|
|
|
|
|
{
|
|
|
|
|
return Response.AsFile($"static/weapons/{kvp.Value}.png");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var weaponImage in weaponImages)
|
|
|
|
|
{
|
|
|
|
|
var weaponName = Path.GetFileNameWithoutExtension(weaponImage);
|
|
|
|
|
if (filename.EndsWith(weaponName))
|
|
|
|
|
{
|
|
|
|
|
return Response.AsFile(weaponImage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trace.TraceWarning("Item image missing: " + filename);
|
|
|
|
|
|
|
|
|
|
return Response.AsJson(new { status = "error" }, HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-16 10:43:37 -04:00
|
|
|
|
public object PostCommand(dynamic x) {
|
|
|
|
|
var cmd = this.Bind<CommandRequest>();
|
|
|
|
|
skyrimInterop.SubmitCommand("COMMAND|" + cmd.Command);
|
|
|
|
|
return Response.AsJson(new { Status = "OK" }, HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object GetFavorites(dynamic x) {
|
|
|
|
|
return Response.AsJson(skyrimInterop.GetFavorites(), HttpStatusCode.OK);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|