fisr commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text;
|
||||
using YandexGameServer.Models;
|
||||
using YandexGameServer.Services;
|
||||
|
||||
namespace YandexGameServer.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly DatabaseService _databaseService;
|
||||
private readonly CodeGenerationService _codeGenerationService;
|
||||
private readonly ILogger<AuthController> _logger;
|
||||
public AuthController(DatabaseService databaseService, ILogger<AuthController> logger, CodeGenerationService codeGenerationService)
|
||||
{
|
||||
_codeGenerationService = codeGenerationService;
|
||||
_logger = logger;
|
||||
_databaseService = databaseService;
|
||||
}
|
||||
|
||||
[HttpGet("{code:int}")]
|
||||
public async Task<IActionResult> Authorization(int code)
|
||||
{
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
CityUniqueCode? cityCode = cities.Find(x => x.key == code);
|
||||
|
||||
if (cityCode == null) return NotFound("Такого кода нет в базе");
|
||||
else
|
||||
{
|
||||
var client = new Client() { city = cityCode.cityEnum, games = new List<Games>() };
|
||||
await _databaseService.CreateAsync(client);
|
||||
|
||||
return Ok(client);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("admin/{code:int}")]
|
||||
public async Task<IActionResult> AdminAuthorization(int code)
|
||||
{
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
CityUniqueCode? cityCode = cities.Find(x => x.adminKey == code);
|
||||
|
||||
if (cityCode == null) return NotFound();
|
||||
else return Ok(cityCode);
|
||||
}
|
||||
|
||||
[HttpGet("secretcode/{cityId:int}")]
|
||||
public async Task<IActionResult> GetAuthorizationCodeAsync(int cityId)
|
||||
{
|
||||
Cities cityCode = (Cities)cityId;
|
||||
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
if (cities.Any(x => x.cityEnum == cityCode))
|
||||
{
|
||||
var cityData = cities.Find(x => x.cityEnum == cityCode);
|
||||
return Ok(cityData.key);
|
||||
}
|
||||
else return NotFound("Нет соответствующего секретного кода");
|
||||
}
|
||||
|
||||
[HttpGet("all")]
|
||||
public async Task<IActionResult> GetAllCodes()
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
var codes = await _databaseService.GetCitiesCodesAsync();
|
||||
foreach(var code in _codeGenerationService.citiesTranslation.Keys)
|
||||
{
|
||||
var data = codes.Find(x => x.cityEnum.Equals(code));
|
||||
stringBuilder.AppendLine($"{_codeGenerationService.citiesTranslation[code]} - {data.key}");
|
||||
}
|
||||
|
||||
return Ok(stringBuilder.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using YandexGameServer.Services;
|
||||
using YandexGameServer.Models;
|
||||
using System.Text;
|
||||
|
||||
namespace YandexGameServer.Controllers
|
||||
{
|
||||
public enum Games
|
||||
{
|
||||
Surprise,
|
||||
Excursion,
|
||||
MasterClass,
|
||||
Entertainment,
|
||||
Sport,
|
||||
Kids,
|
||||
GoodDeeds,
|
||||
Mask,
|
||||
Duck,
|
||||
Butterfly
|
||||
}
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class GameController : ControllerBase
|
||||
{
|
||||
private readonly DatabaseService _databaseService;
|
||||
public GameController(DatabaseService databaseService)
|
||||
{
|
||||
_databaseService = databaseService;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("find")]
|
||||
public async Task<IActionResult> OnClientFindTag([FromQuery] string id, [FromQuery] Games game)
|
||||
{
|
||||
var clients = await _databaseService.GetClientsAsync();
|
||||
var client = clients.Find(x => x.id == id);
|
||||
|
||||
client.games.Add(game);
|
||||
await _databaseService.UpdateAsync(client);
|
||||
|
||||
if (client.games.Count == 10)
|
||||
{
|
||||
client.gameFinishedDate = DateTime.UtcNow;
|
||||
await _databaseService.UpdateAsync(client);
|
||||
|
||||
clients = await _databaseService.GetClientsAsync();
|
||||
|
||||
var bestClients = clients
|
||||
.Where(x => x.gameFinishedDate != null && x.city == client.city)
|
||||
.OrderBy(x => x.gameFinishedDate)
|
||||
.ToList();
|
||||
|
||||
return Ok(bestClients.IndexOf(clients.Find(x => x.id == id)) + 1);
|
||||
}
|
||||
else return BadRequest();
|
||||
}
|
||||
|
||||
[HttpGet("best/{key:int}")]
|
||||
public async Task<IActionResult> GetBestClientsInCity(int key)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
var cities = await _databaseService.GetCitiesCodesAsync();
|
||||
var city = cities.Find(x => x.key == key);
|
||||
if (city == null) return NotFound();
|
||||
|
||||
var clients = await _databaseService.GetClientsAsync();
|
||||
|
||||
var bestClients = clients
|
||||
.Where(x => x.gameFinishedDate != null && x.city == city.cityEnum)
|
||||
.OrderBy(x => x.gameFinishedDate)
|
||||
.ToList();
|
||||
|
||||
if(bestClients.Count == 0)
|
||||
return BadRequest($"В городе {city.city} ещё нет игроков, завершивших квест");
|
||||
|
||||
sb.AppendLine($"количество игроков, завершивших квест в городе {city.city}:\n");
|
||||
foreach(var client in bestClients)
|
||||
{
|
||||
sb.AppendLine($"{bestClients.IndexOf(client) + 1} - закончил(а) квест {client.gameFinishedDate.Value.ToShortDateString()} в {client.gameFinishedDate.Value.ToShortTimeString()}");
|
||||
}
|
||||
|
||||
return Ok(sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user