2024-04-17 19:34:19 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-04-28 12:53:18 +02:00
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
2024-04-17 19:34:19 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using TanksServer.GameLogic;
|
|
|
|
using TanksServer.Interactivity;
|
|
|
|
|
|
|
|
namespace TanksServer;
|
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
internal sealed class Endpoints(
|
|
|
|
ClientScreenServer clientScreenServer,
|
|
|
|
PlayerServer playerService,
|
|
|
|
ControlsServer controlsServer,
|
|
|
|
MapService mapService
|
|
|
|
)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
2024-04-28 12:53:18 +02:00
|
|
|
public void Map(WebApplication app)
|
2024-04-17 19:34:19 +02:00
|
|
|
{
|
2024-04-28 12:53:18 +02:00
|
|
|
app.MapPost("/player", PostPlayer);
|
|
|
|
app.MapGet("/player", GetPlayerAsync);
|
|
|
|
app.MapGet("/scores", () => playerService.GetAll() as IEnumerable<Player>);
|
|
|
|
app.Map("/screen", ConnectScreenAsync);
|
|
|
|
app.Map("/controls", ConnectControlsAsync);
|
|
|
|
app.MapGet("/map", () => mapService.MapNames);
|
|
|
|
app.MapPost("/map", PostMap);
|
|
|
|
}
|
2024-04-22 19:03:07 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private Results<BadRequest<string>, NotFound<string>, Ok> PostMap([FromQuery] string name)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
return TypedResults.BadRequest("invalid map name");
|
|
|
|
if (!mapService.TrySwitchTo(name))
|
|
|
|
return TypedResults.NotFound("map with name not found");
|
|
|
|
return TypedResults.Ok();
|
|
|
|
}
|
2024-04-22 19:03:07 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private async Task<Results<BadRequest, NotFound, EmptyHttpResult>> ConnectControlsAsync(HttpContext context,
|
|
|
|
[FromQuery] string playerName)
|
|
|
|
{
|
|
|
|
if (!context.WebSockets.IsWebSocketRequest)
|
|
|
|
return TypedResults.BadRequest();
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
if (!playerService.TryGet(playerName, out var player))
|
|
|
|
return TypedResults.NotFound();
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
|
|
|
await controlsServer.HandleClientAsync(ws, player);
|
|
|
|
return TypedResults.Empty;
|
|
|
|
}
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private async Task<Results<BadRequest, EmptyHttpResult>> ConnectScreenAsync(HttpContext context,
|
|
|
|
[FromQuery] string? playerName)
|
|
|
|
{
|
|
|
|
if (!context.WebSockets.IsWebSocketRequest)
|
|
|
|
return TypedResults.BadRequest();
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
|
|
|
await clientScreenServer.HandleClientAsync(ws, playerName);
|
|
|
|
return TypedResults.Empty;
|
|
|
|
}
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private async Task<Results<NotFound, Ok<Player>, EmptyHttpResult>> GetPlayerAsync(HttpContext context,
|
|
|
|
[FromQuery] string name)
|
|
|
|
{
|
|
|
|
if (!playerService.TryGet(name, out var foundPlayer))
|
|
|
|
return TypedResults.NotFound();
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
if (!context.WebSockets.IsWebSocketRequest)
|
|
|
|
return TypedResults.Ok(foundPlayer);
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
|
|
|
await playerService.HandleClientAsync(ws, foundPlayer);
|
|
|
|
return TypedResults.Empty;
|
|
|
|
}
|
2024-04-17 19:34:19 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private Results<BadRequest<string>, Ok<string>, UnauthorizedHttpResult> PostPlayer([FromQuery] string name)
|
|
|
|
{
|
|
|
|
name = name.Trim().ToUpperInvariant();
|
|
|
|
if (name == string.Empty) return TypedResults.BadRequest("name cannot be blank");
|
|
|
|
if (name.Length > 12) return TypedResults.BadRequest("name too long");
|
|
|
|
|
|
|
|
var player = playerService.GetOrAdd(name);
|
2024-04-28 15:34:32 +02:00
|
|
|
return TypedResults.Ok(player.Name);
|
2024-04-17 19:34:19 +02:00
|
|
|
}
|
|
|
|
}
|