From fcd84d2c83f1d0ad2cc8c28e5f72ced8a1828ff1 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 14 Apr 2024 23:11:00 +0200 Subject: [PATCH] map switching --- TanksServer/GameLogic/MapService.cs | 16 ++++++++++------ .../Interactivity/AppSerializerContext.cs | 1 + TanksServer/Program.cs | 18 ++++++++++++++++++ TanksServer/userRequests.http | 6 ++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/TanksServer/GameLogic/MapService.cs b/TanksServer/GameLogic/MapService.cs index 839dd2f..ce78b49 100644 --- a/TanksServer/GameLogic/MapService.cs +++ b/TanksServer/GameLogic/MapService.cs @@ -12,15 +12,17 @@ internal sealed class MapService public const ushort PixelsPerRow = TilesPerRow * TileSize; public const ushort PixelsPerColumn = TilesPerColumn * TileSize; - public Map Current { get; } + public Map[] All { get; } + + public Map Current { get; set; } public MapService() { var textMaps = Directory.EnumerateFiles("./assets/maps/", "*.txt").Select(LoadMapString); var pngMaps = Directory.EnumerateFiles("./assets/maps/", "*.png").Select(LoadMapPng); - var maps = textMaps.Concat(pngMaps).ToList(); - Current = maps[Random.Shared.Next(maps.Count)]; + All = textMaps.Concat(pngMaps).ToArray(); + Current = All[Random.Shared.Next(All.Length)]; } private static Map LoadMapPng(string file) @@ -37,7 +39,7 @@ internal sealed class MapService for (var x = 0; x < image.Width; x++) walls[x, y] = image[x, y] == whitePixel; - return new Map(walls); + return new Map(Path.GetFileName(file),walls); } private static Map LoadMapString(string file) @@ -63,12 +65,14 @@ internal sealed class MapService } } - return new Map(walls); + return new Map(Path.GetFileName(file), walls); } } -internal sealed class Map(bool[,] walls) +internal sealed class Map(string name, bool[,] walls) { + public string Name => name; + public bool IsWall(int x, int y) => walls[x, y]; public bool IsWall(PixelPosition position) => walls[position.X, position.Y]; diff --git a/TanksServer/Interactivity/AppSerializerContext.cs b/TanksServer/Interactivity/AppSerializerContext.cs index 12c4f4d..e7e5ad2 100644 --- a/TanksServer/Interactivity/AppSerializerContext.cs +++ b/TanksServer/Interactivity/AppSerializerContext.cs @@ -6,4 +6,5 @@ namespace TanksServer.Interactivity; [JsonSerializable(typeof(IEnumerable))] [JsonSerializable(typeof(Guid))] [JsonSerializable(typeof(NameId))] +[JsonSerializable(typeof(Dictionary))] internal sealed partial class AppSerializerContext : JsonSerializerContext; diff --git a/TanksServer/Program.cs b/TanksServer/Program.cs index 118de93..6fdcda9 100644 --- a/TanksServer/Program.cs +++ b/TanksServer/Program.cs @@ -22,6 +22,7 @@ public static class Program var clientScreenServer = app.Services.GetRequiredService(); var playerService = app.Services.GetRequiredService(); var controlsServer = app.Services.GetRequiredService(); + var mapService = app.Services.GetRequiredService(); var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client")); app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider }); @@ -40,6 +41,7 @@ public static class Program ? Results.Ok(new NameId(player.Name, player.Id)) : Results.Unauthorized(); }); + app.MapGet("/player", ([FromQuery] Guid id) => playerService.TryGet(id, out var foundPlayer) ? Results.Ok((object?)foundPlayer) @@ -71,6 +73,22 @@ public static class Program return Results.Empty; }); + app.MapGet("/map", () => + { + var dict = mapService.All + .Select((m, i) => (m.Name, i)) + .ToDictionary(pair => pair.i, pair => pair.Name); + return dict; + }); + + app.MapPost("/map", ([FromQuery] int index) => + { + if (index < 0 || index >= mapService.All.Length) + return Results.NotFound("index does not exist"); + mapService.Current = mapService.All[index]; + return Results.Ok(); + }); + app.Run(); } diff --git a/TanksServer/userRequests.http b/TanksServer/userRequests.http index 7a21d3e..49fb9c3 100644 --- a/TanksServer/userRequests.http +++ b/TanksServer/userRequests.http @@ -6,3 +6,9 @@ GET localhost/scores ### +GET localhost/map + +### + +POST localhost/map?index=0 +