remove guid, reduce latency (gets stuck sometimes tho)
This commit is contained in:
parent
6bc6a039bd
commit
7044ffda79
19 changed files with 291 additions and 251 deletions
|
@ -1,85 +1,87 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Interactivity;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
internal static class Endpoints
|
||||
internal sealed class Endpoints(
|
||||
ClientScreenServer clientScreenServer,
|
||||
PlayerServer playerService,
|
||||
ControlsServer controlsServer,
|
||||
MapService mapService
|
||||
)
|
||||
{
|
||||
public static void MapEndpoints(WebApplication app)
|
||||
public void Map(WebApplication app)
|
||||
{
|
||||
var clientScreenServer = app.Services.GetRequiredService<ClientScreenServer>();
|
||||
var playerService = app.Services.GetRequiredService<PlayerServer>();
|
||||
var controlsServer = app.Services.GetRequiredService<ControlsServer>();
|
||||
var mapService = app.Services.GetRequiredService<MapService>();
|
||||
|
||||
app.MapPost("/player", (string name, Guid? id) =>
|
||||
{
|
||||
name = name.Trim().ToUpperInvariant();
|
||||
if (name == string.Empty)
|
||||
return Results.BadRequest("name cannot be blank");
|
||||
if (name.Length > 12)
|
||||
return Results.BadRequest("name too long");
|
||||
|
||||
if (!id.HasValue || id.Value == Guid.Empty)
|
||||
id = Guid.NewGuid();
|
||||
|
||||
var player = playerService.GetOrAdd(name, id.Value);
|
||||
return player != null
|
||||
? Results.Ok(new NameId(player.Name, player.Id))
|
||||
: Results.Unauthorized();
|
||||
});
|
||||
|
||||
app.MapGet("/player", async (HttpContext context, [FromQuery] Guid id) =>
|
||||
{
|
||||
if (!playerService.TryGet(id, out var foundPlayer))
|
||||
return Results.NotFound();
|
||||
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return Results.Ok((object?)foundPlayer);
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await playerService.HandleClientAsync(ws, foundPlayer);
|
||||
return Results.Empty;
|
||||
});
|
||||
|
||||
app.MapGet("/scores", () => playerService.GetAll());
|
||||
|
||||
app.Map("/screen", async (HttpContext context, [FromQuery] Guid? player) =>
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return Results.BadRequest();
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await clientScreenServer.HandleClientAsync(ws, player);
|
||||
return Results.Empty;
|
||||
});
|
||||
|
||||
app.Map("/controls", async (HttpContext context, [FromQuery] Guid playerId) =>
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return Results.BadRequest();
|
||||
|
||||
if (!playerService.TryGet(playerId, out var player))
|
||||
return Results.NotFound();
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await controlsServer.HandleClientAsync(ws, player);
|
||||
return Results.Empty;
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
app.MapPost("/map", ([FromQuery] string name) =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return Results.BadRequest("invalid map name");
|
||||
if (!mapService.TrySwitchTo(name))
|
||||
return Results.NotFound("map with name not found");
|
||||
return Results.Ok();
|
||||
});
|
||||
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();
|
||||
}
|
||||
|
||||
private async Task<Results<BadRequest, NotFound, EmptyHttpResult>> ConnectControlsAsync(HttpContext context,
|
||||
[FromQuery] string playerName)
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return TypedResults.BadRequest();
|
||||
|
||||
if (!playerService.TryGet(playerName, out var player))
|
||||
return TypedResults.NotFound();
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await controlsServer.HandleClientAsync(ws, player);
|
||||
return TypedResults.Empty;
|
||||
}
|
||||
|
||||
private async Task<Results<BadRequest, EmptyHttpResult>> ConnectScreenAsync(HttpContext context,
|
||||
[FromQuery] string? playerName)
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return TypedResults.BadRequest();
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await clientScreenServer.HandleClientAsync(ws, playerName);
|
||||
return TypedResults.Empty;
|
||||
}
|
||||
|
||||
private async Task<Results<NotFound, Ok<Player>, EmptyHttpResult>> GetPlayerAsync(HttpContext context,
|
||||
[FromQuery] string name)
|
||||
{
|
||||
if (!playerService.TryGet(name, out var foundPlayer))
|
||||
return TypedResults.NotFound();
|
||||
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return TypedResults.Ok(foundPlayer);
|
||||
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await playerService.HandleClientAsync(ws, foundPlayer);
|
||||
return TypedResults.Empty;
|
||||
}
|
||||
|
||||
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);
|
||||
return player != null
|
||||
? TypedResults.Ok(player.Name)
|
||||
: TypedResults.Unauthorized();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue