2d61562487
Revert if needed
121 lines
3.9 KiB
C#
121 lines
3.9 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;
|
||
private readonly ILogger<GameController> _logger;
|
||
public GameController(DatabaseService databaseService, ILogger<GameController> logger)
|
||
{
|
||
_databaseService = databaseService;
|
||
_logger = logger;
|
||
}
|
||
|
||
|
||
[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());
|
||
}
|
||
|
||
[HttpGet("tags/{code:int}")]
|
||
public async Task<IActionResult> GetTags(int code)
|
||
{
|
||
Cities city = (Cities)code;
|
||
var result = await _databaseService.GetTagsAsync();
|
||
if (result == null) return NotFound();
|
||
|
||
var tags = result.Where(x => x.city == city).ToList();
|
||
if (tags.Count == 0) return NotFound();
|
||
else return Ok(tags);
|
||
}
|
||
|
||
[HttpPost("tags/add")]
|
||
public async Task SetTags([FromBody] Tag tag)
|
||
{
|
||
var tagsInUse = await _databaseService.GetTagsAsync();
|
||
if(tagsInUse == null)
|
||
{
|
||
await _databaseService.CreateAsync(tag);
|
||
return;
|
||
}
|
||
|
||
if (!tagsInUse.Any(x => x.game == tag.game && x.city == tag.city))
|
||
{
|
||
await _databaseService.CreateAsync(tag);
|
||
}
|
||
else
|
||
{
|
||
await _databaseService.UpdateAsync(tag);
|
||
}
|
||
}
|
||
}
|
||
}
|