skyrim-console-server/EmpathicQubit.ConsoleServer/ServerModule.cs

76 lines
2.6 KiB
C#
Raw Permalink Normal View History

2021-05-16 10:43:37 -04:00
using IniParser;
using IniParser.Model;
using Nancy;
using Nancy.ModelBinding;
using Nancy.Responses;
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 {
private string[] weaponImages;
2021-05-16 10:43:37 -04:00
private SkyrimInterop skyrimInterop;
private IDictionary<string, string> weaponMap;
2021-05-16 10:43:37 -04:00
public ServerModule(SkyrimInterop skyrimInterop, IRootPathProvider pathy)
2021-05-16 10:43:37 -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"));
Get("/weapons/{filename}.png", GetImage);
2021-05-16 10:43:37 -04:00
Get("/{filename}", _ => Response.AsFile("static/" + (string)_.filename));
}
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);
}
}
}