67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using TOOHUCardAPI.Data;
|
|
using TOOHUCardAPI.Data.Models;
|
|
using TOOHUCardAPI.Data.Repositories;
|
|
using TOOHUCardAPI.DTO;
|
|
|
|
namespace TOOHUCardAPI.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class PlayerDataController : MethodBasedController
|
|
{
|
|
private readonly ILogger<PlayerDataController> _logger;
|
|
private readonly UserRepository _userRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public PlayerDataController(ILogger<PlayerDataController> logger, UserRepository userRepository, IMapper mapper)
|
|
{
|
|
_logger = logger;
|
|
_userRepository = userRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/**
|
|
* 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<object> EntryPoint([FromBody] object bodyObj)
|
|
{
|
|
string body = JsonConvert.SerializeObject(bodyObj);
|
|
JObject request = JObject.Parse(body);
|
|
string method = request["method"].ToString();
|
|
return await InvokeEndpointHandlerForMethod<PlayerDataController>(this, method, body);
|
|
}
|
|
|
|
[EndpointHandler("get")]
|
|
private async Task<object> Get(string body)
|
|
{
|
|
PlayerDataGetRequestObject requestObject = JsonConvert.DeserializeObject<PlayerDataGetRequestObject>(body);
|
|
Dictionary<string, User> users = requestObject.Ids.Aggregate(new Dictionary<string, User>(),
|
|
(dictionary, pair) =>
|
|
{
|
|
User user = _userRepository.GetOrCreateUser(pair.Value);
|
|
dictionary[pair.Key] = user;
|
|
return dictionary;
|
|
});
|
|
PlayerDataGetResponseObject response = new PlayerDataGetResponseObject(users, _mapper);
|
|
|
|
return response;
|
|
}
|
|
|
|
}
|
|
} |