initial project, implement binary format of display
This commit is contained in:
commit
ea7f12b0c2
25
.dockerignore
Normal file
25
.dockerignore
Normal file
|
@ -0,0 +1,25 @@
|
|||
**/.dockerignore
|
||||
**/.env
|
||||
**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/.idea
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
10
.editorconfig
Normal file
10
.editorconfig
Normal file
|
@ -0,0 +1,10 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
max_line_length = 120
|
||||
tab_width = 4
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
bin
|
||||
obj
|
||||
.idea
|
16
TanksServer.sln
Normal file
16
TanksServer.sln
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TanksServer", "TanksServer\TanksServer.csproj", "{D88BF376-47A4-4C72-ADD1-983F9285C351}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D88BF376-47A4-4C72-ADD1-983F9285C351}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
55
TanksServer/DisplayPixelBuffer.cs
Normal file
55
TanksServer/DisplayPixelBuffer.cs
Normal file
|
@ -0,0 +1,55 @@
|
|||
namespace TanksServer;
|
||||
|
||||
public sealed class DisplayPixelBuffer(byte[] data)
|
||||
{
|
||||
public byte[] Data => data;
|
||||
|
||||
public byte Magic1
|
||||
{
|
||||
get => data[0];
|
||||
set => data[0] = value;
|
||||
}
|
||||
|
||||
public byte Magic2
|
||||
{
|
||||
get => data[1];
|
||||
set => data[1] = value;
|
||||
}
|
||||
|
||||
public ushort X
|
||||
{
|
||||
get => GetTwoBytes(2);
|
||||
set => SetTwoBytes(2, value);
|
||||
}
|
||||
|
||||
public ushort Y
|
||||
{
|
||||
get => GetTwoBytes(4);
|
||||
set => SetTwoBytes(4, value);
|
||||
}
|
||||
|
||||
public ushort WidthInTiles
|
||||
{
|
||||
get => GetTwoBytes(6);
|
||||
set => SetTwoBytes(6, value);
|
||||
}
|
||||
|
||||
public ushort HeightInPixels
|
||||
{
|
||||
get => GetTwoBytes(8);
|
||||
set => SetTwoBytes(8, value);
|
||||
}
|
||||
|
||||
public FixedSizeBitFieldView Pixels { get; } = new(data.AsMemory(10));
|
||||
|
||||
private ushort GetTwoBytes(int index)
|
||||
{
|
||||
return (ushort)(data[index] * byte.MaxValue + data[index + 1]);
|
||||
}
|
||||
|
||||
private void SetTwoBytes(int index, ushort value)
|
||||
{
|
||||
data[index] = (byte)(value / byte.MaxValue);
|
||||
data[index + 1] = (byte)(value % byte.MaxValue);
|
||||
}
|
||||
}
|
20
TanksServer/Dockerfile
Normal file
20
TanksServer/Dockerfile
Normal file
|
@ -0,0 +1,20 @@
|
|||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
EXPOSE 443
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
WORKDIR /src
|
||||
COPY ["TanksServer/TanksServer.csproj", "TanksServer/"]
|
||||
RUN dotnet restore "TanksServer/TanksServer.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/TanksServer"
|
||||
RUN dotnet build "TanksServer.csproj" -c Release -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "TanksServer.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "TanksServer.dll"]
|
101
TanksServer/FixedSizeBitFieldView.cs
Normal file
101
TanksServer/FixedSizeBitFieldView.cs
Normal file
|
@ -0,0 +1,101 @@
|
|||
using System.Collections;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
public sealed class FixedSizeBitFieldView(Memory<byte> data) : IList<bool>
|
||||
{
|
||||
public int Count => data.Length * 8;
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<bool> GetEnumerator()
|
||||
{
|
||||
return Enumerable().GetEnumerator();
|
||||
|
||||
IEnumerable<bool> Enumerable()
|
||||
{
|
||||
for (var i = 0; i < Count; i++)
|
||||
yield return this[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
var span = data.Span;
|
||||
for (var i = 0; i < data.Length; i++)
|
||||
span[i] = 0;
|
||||
}
|
||||
|
||||
public void CopyTo(bool[] array, int arrayIndex)
|
||||
{
|
||||
for (var i = 0; i < Count && i + arrayIndex < array.Length; i++)
|
||||
array[i + arrayIndex] = this[i];
|
||||
}
|
||||
|
||||
private static (int byteIndex, int bitInByteIndex) GetIndexes(int bitIndex)
|
||||
{
|
||||
var byteIndex = bitIndex / 8;
|
||||
var bitInByteIndex = 7 - bitIndex % 8;
|
||||
return (byteIndex, bitInByteIndex);
|
||||
}
|
||||
|
||||
public bool this[int bitIndex]
|
||||
{
|
||||
get
|
||||
{
|
||||
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
||||
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
||||
return (data.Span[byteIndex] & bitInByteMask) != 0;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
||||
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
||||
|
||||
if (value)
|
||||
{
|
||||
data.Span[byteIndex] |= bitInByteMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
var withoutBitMask = (byte)(ushort.MaxValue ^ bitInByteMask);
|
||||
data.Span[byteIndex] &= withoutBitMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(bool item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public bool Contains(bool item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public bool Remove(bool item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public int IndexOf(bool item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Insert(int index, bool item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
3
TanksServer/GlobalUsings.cs
Normal file
3
TanksServer/GlobalUsings.cs
Normal file
|
@ -0,0 +1,3 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Threading.Tasks;
|
40
TanksServer/MapDrawer.cs
Normal file
40
TanksServer/MapDrawer.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
namespace TanksServer;
|
||||
|
||||
public class MapDrawer(MapService map)
|
||||
{
|
||||
private const uint GameFieldPixelCount = MapService.PixelsPerRow * MapService.PixelsPerColumn;
|
||||
|
||||
public void DrawInto(DisplayPixelBuffer buf)
|
||||
{
|
||||
for (var tileY = 0; tileY < MapService.TilesPerColumn; tileY++)
|
||||
for (var tileX = 0; tileX < MapService.TilesPerRow; tileX++)
|
||||
{
|
||||
if (!map.IsCurrentlyWall(tileX, tileY))
|
||||
continue;
|
||||
|
||||
var absoluteTilePixelY = tileY * MapService.TileSize;
|
||||
for (var pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++)
|
||||
{
|
||||
var absoluteRowStartPixelIndex = (absoluteTilePixelY + pixelInTileY) * MapService.PixelsPerRow
|
||||
+ tileX * MapService.TileSize;
|
||||
for (var pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++)
|
||||
buf.Pixels[absoluteRowStartPixelIndex + pixelInTileX] = pixelInTileX % 2 == pixelInTileY % 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DisplayPixelBuffer CreateGameFieldPixelBuffer()
|
||||
{
|
||||
var data = new byte[10 + GameFieldPixelCount / 8];
|
||||
var result = new DisplayPixelBuffer(data)
|
||||
{
|
||||
Magic1 = 0,
|
||||
Magic2 = 19,
|
||||
X = 0,
|
||||
Y = 0,
|
||||
WidthInTiles = MapService.TilesPerRow,
|
||||
HeightInPixels = MapService.PixelsPerColumn
|
||||
};
|
||||
return result;
|
||||
}
|
||||
}
|
41
TanksServer/MapService.cs
Normal file
41
TanksServer/MapService.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
namespace TanksServer;
|
||||
|
||||
public class MapService
|
||||
{
|
||||
public const int TilesPerRow = 44;
|
||||
public const int TilesPerColumn = 20;
|
||||
public const int TileSize = 8;
|
||||
public const int PixelsPerRow = TilesPerRow * TileSize;
|
||||
public const int PixelsPerColumn = TilesPerColumn * TileSize;
|
||||
|
||||
private string _map = """
|
||||
############################################
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
#.....####......................####.......#
|
||||
#..........................................#
|
||||
#............###...........###.............#
|
||||
#............#...............#.............#
|
||||
#...##.......#...............#......##.....#
|
||||
#....#..............................#......#
|
||||
#....#..##......................##..#......#
|
||||
#....#..##......................##..#......#
|
||||
#....#..............................#......#
|
||||
#...##.......#...............#......##.....#
|
||||
#............#...............#.............#
|
||||
#............###...........###.............#
|
||||
#..........................................#
|
||||
#.....####......................####.......#
|
||||
#...................##.....................#
|
||||
#...................##.....................#
|
||||
############################################
|
||||
"""
|
||||
.ReplaceLineEndings(string.Empty);
|
||||
|
||||
private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow];
|
||||
|
||||
public bool IsCurrentlyWall(int tileX, int tileY)
|
||||
{
|
||||
return this[tileX, tileY] == '#';
|
||||
}
|
||||
}
|
41
TanksServer/Program.cs
Normal file
41
TanksServer/Program.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var app = Configure(args);
|
||||
|
||||
var display = app.Services.GetRequiredService<ServicePointDisplay>();
|
||||
var mapDrawer = app.Services.GetRequiredService<MapDrawer>();
|
||||
|
||||
var buffer = mapDrawer.CreateGameFieldPixelBuffer();
|
||||
mapDrawer.DrawInto(buffer);
|
||||
await display.Send(buffer);
|
||||
|
||||
app.UseWebSockets();
|
||||
|
||||
app.Map("/screen", async context =>
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest)
|
||||
return;
|
||||
using var ws = await context.WebSockets.AcceptWebSocketAsync();
|
||||
});
|
||||
|
||||
await app.RunAsync();
|
||||
}
|
||||
|
||||
private static WebApplication Configure(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateSlimBuilder(args);
|
||||
|
||||
builder.Services.AddSingleton<ServicePointDisplay>();
|
||||
builder.Services.AddSingleton<MapService>();
|
||||
builder.Services.AddSingleton<MapDrawer>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
12
TanksServer/Properties/launchSettings.json
Normal file
12
TanksServer/Properties/launchSettings.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
TanksServer/ServicePointDisplay.cs
Normal file
14
TanksServer/ServicePointDisplay.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System.Net.Sockets;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
public class ServicePointDisplay(IOptions<ServicePointDisplayConfiguration> options)
|
||||
{
|
||||
private readonly UdpClient _udpClient = new(options.Value.Hostname, options.Value.Port);
|
||||
|
||||
public ValueTask<int> Send(DisplayPixelBuffer buffer)
|
||||
{
|
||||
return _udpClient.SendAsync(buffer.Data);
|
||||
}
|
||||
}
|
7
TanksServer/ServicePointDisplayConfiguration.cs
Normal file
7
TanksServer/ServicePointDisplayConfiguration.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace TanksServer;
|
||||
|
||||
public class ServicePointDisplayConfiguration
|
||||
{
|
||||
public string Hostname { get; set; } = string.Empty;
|
||||
public int Port { get; set; }
|
||||
}
|
18
TanksServer/TanksServer.csproj
Normal file
18
TanksServer/TanksServer.csproj
Normal file
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<PublishAot>true</PublishAot>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
8
TanksServer/appsettings.Development.json
Normal file
8
TanksServer/appsettings.Development.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
9
TanksServer/appsettings.json
Normal file
9
TanksServer/appsettings.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
7
global.json
Normal file
7
global.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"sdk": {
|
||||
"version": "8.0.0",
|
||||
"rollForward": "latestMajor",
|
||||
"allowPrerelease": true
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue