map switching

This commit is contained in:
Vinzenz Schroeter 2024-04-14 23:11:00 +02:00
parent 359e0235f9
commit fcd84d2c83
4 changed files with 35 additions and 6 deletions

View file

@ -12,15 +12,17 @@ internal sealed class MapService
public const ushort PixelsPerRow = TilesPerRow * TileSize; public const ushort PixelsPerRow = TilesPerRow * TileSize;
public const ushort PixelsPerColumn = TilesPerColumn * TileSize; public const ushort PixelsPerColumn = TilesPerColumn * TileSize;
public Map Current { get; } public Map[] All { get; }
public Map Current { get; set; }
public MapService() public MapService()
{ {
var textMaps = Directory.EnumerateFiles("./assets/maps/", "*.txt").Select(LoadMapString); var textMaps = Directory.EnumerateFiles("./assets/maps/", "*.txt").Select(LoadMapString);
var pngMaps = Directory.EnumerateFiles("./assets/maps/", "*.png").Select(LoadMapPng); var pngMaps = Directory.EnumerateFiles("./assets/maps/", "*.png").Select(LoadMapPng);
var maps = textMaps.Concat(pngMaps).ToList(); All = textMaps.Concat(pngMaps).ToArray();
Current = maps[Random.Shared.Next(maps.Count)]; Current = All[Random.Shared.Next(All.Length)];
} }
private static Map LoadMapPng(string file) private static Map LoadMapPng(string file)
@ -37,7 +39,7 @@ internal sealed class MapService
for (var x = 0; x < image.Width; x++) for (var x = 0; x < image.Width; x++)
walls[x, y] = image[x, y] == whitePixel; walls[x, y] = image[x, y] == whitePixel;
return new Map(walls); return new Map(Path.GetFileName(file),walls);
} }
private static Map LoadMapString(string file) 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(int x, int y) => walls[x, y];
public bool IsWall(PixelPosition position) => walls[position.X, position.Y]; public bool IsWall(PixelPosition position) => walls[position.X, position.Y];

View file

@ -6,4 +6,5 @@ namespace TanksServer.Interactivity;
[JsonSerializable(typeof(IEnumerable<Player>))] [JsonSerializable(typeof(IEnumerable<Player>))]
[JsonSerializable(typeof(Guid))] [JsonSerializable(typeof(Guid))]
[JsonSerializable(typeof(NameId))] [JsonSerializable(typeof(NameId))]
[JsonSerializable(typeof(Dictionary<int, string>))]
internal sealed partial class AppSerializerContext : JsonSerializerContext; internal sealed partial class AppSerializerContext : JsonSerializerContext;

View file

@ -22,6 +22,7 @@ public static class Program
var clientScreenServer = app.Services.GetRequiredService<ClientScreenServer>(); var clientScreenServer = app.Services.GetRequiredService<ClientScreenServer>();
var playerService = app.Services.GetRequiredService<PlayerServer>(); var playerService = app.Services.GetRequiredService<PlayerServer>();
var controlsServer = app.Services.GetRequiredService<ControlsServer>(); var controlsServer = app.Services.GetRequiredService<ControlsServer>();
var mapService = app.Services.GetRequiredService<MapService>();
var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client")); var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client"));
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider }); app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider });
@ -40,6 +41,7 @@ public static class Program
? Results.Ok(new NameId(player.Name, player.Id)) ? Results.Ok(new NameId(player.Name, player.Id))
: Results.Unauthorized(); : Results.Unauthorized();
}); });
app.MapGet("/player", ([FromQuery] Guid id) => app.MapGet("/player", ([FromQuery] Guid id) =>
playerService.TryGet(id, out var foundPlayer) playerService.TryGet(id, out var foundPlayer)
? Results.Ok((object?)foundPlayer) ? Results.Ok((object?)foundPlayer)
@ -71,6 +73,22 @@ public static class Program
return Results.Empty; 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(); app.Run();
} }

View file

@ -6,3 +6,9 @@ GET localhost/scores
### ###
GET localhost/map
###
POST localhost/map?index=0