87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
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());
|
||
}
|
||
}
|
||
}
|