Lots of additions and restructuring

This commit is contained in:
2021-10-30 21:58:43 -04:00
parent 9cf11e982f
commit 20cac8c378
37 changed files with 4465 additions and 117 deletions

View File

@@ -4,15 +4,37 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TOOHUCardAPI.Data;
using TOOHUCardAPI.Data.Services;
using MethodMap = System.Collections.Generic.Dictionary<string, System.Reflection.MethodInfo>;
namespace TOOHUCardAPI.Controllers
{
public class MethodBasedController : ControllerBase
public class MethodBasedController<TChildType> : ControllerBase
{
public delegate Task<IActionResult> EndpointHandler(string requestBody);
private static Dictionary<Type, MethodMap> MethodMapByType = new Dictionary<Type, MethodMap>();
/**
* The game uses a single endpoint for player data.
* The object they send has a method field that's used to decide what's being done
* So let's use a single entry point and redirect based on that
*/
[HttpPost]
public async Task<IActionResult> EntryPoint([FromBody] object bodyObj)
{
string body = JsonConvert.SerializeObject(bodyObj);
JObject request = JObject.Parse(body);
string method = request["method"].ToString();
try
{
return await InvokeEndpointHandlerForMethod<TChildType>(this, method, body);
}
catch (TooHooException e)
{
return Ok(e.response);
}
}
protected Task<IActionResult> InvokeEndpointHandlerForMethod<T>(object _this, string method, string body)
{
@@ -27,11 +49,12 @@ namespace TOOHUCardAPI.Controllers
private MethodMap GetMethodMapForType<T>()
{
if (!MethodMapByType.ContainsKey(typeof(T)))
var methodMapByType = MethodMapService.MethodMapByType;
if (!methodMapByType.ContainsKey(typeof(T)))
{
MethodMapByType[typeof(T)] = RegisterEndpointHandlers<T>();
methodMapByType[typeof(T)] = RegisterEndpointHandlers<T>();
}
return MethodMapByType[typeof(T)];
return methodMapByType[typeof(T)];
}
private static MethodMap RegisterEndpointHandlers<T>()
@@ -51,6 +74,8 @@ namespace TOOHUCardAPI.Controllers
}
}
public class MissingEndpointHandlerException : Exception
{
public MissingEndpointHandlerException(string method) : base($"Handler for [{method}] is either missing or incorrectly setup.")