prepare to send different data per client
This commit is contained in:
parent
fcd84d2c83
commit
fbaad86555
|
@ -1,13 +1,16 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
public void Draw(GamePixelGrid pixels)
|
||||
{
|
||||
foreach (var position in bullets.GetAll().Select(b => b.Position.ToPixelPosition()))
|
||||
buffer[(ushort)position.X, (ushort)position.Y] = true;
|
||||
foreach (var bullet in bullets.GetAll())
|
||||
{
|
||||
var position = bullet.Position.ToPixelPosition();
|
||||
pixels[position.X, position.Y].EntityType = GamePixelEntityType.Bullet;
|
||||
pixels[position.X, position.Y].BelongsTo = bullet.Owner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class DrawMapStep(MapService map) : IDrawStep
|
||||
{
|
||||
public void Draw(PixelGrid buffer)
|
||||
public void Draw(GamePixelGrid pixels)
|
||||
{
|
||||
for (ushort y = 0; y < MapService.PixelsPerColumn; y++)
|
||||
for (ushort x = 0; x < MapService.PixelsPerRow; x++)
|
||||
|
@ -13,7 +12,8 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep
|
|||
var pixel = new PixelPosition(x, y);
|
||||
if (!map.Current.IsWall(pixel))
|
||||
continue;
|
||||
buffer[x, y] = true;
|
||||
|
||||
pixels[x, y].EntityType = GamePixelEntityType.Wall;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ internal sealed class DrawTanksStep : IDrawStep
|
|||
_tankSpriteWidth = tankImage.Width;
|
||||
}
|
||||
|
||||
public void Draw(PixelGrid buffer)
|
||||
public void Draw(GamePixelGrid pixels)
|
||||
{
|
||||
foreach (var tank in _tanks)
|
||||
{
|
||||
|
@ -40,7 +40,8 @@ internal sealed class DrawTanksStep : IDrawStep
|
|||
continue;
|
||||
|
||||
var (x, y) = tankPosition.GetPixelRelative(dx, dy);
|
||||
buffer[(ushort)x, (ushort)y] = true;
|
||||
pixels[x, y].EntityType = GamePixelEntityType.Tank;
|
||||
pixels[x, y].BelongsTo = tank.Owner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
21
TanksServer/Graphics/GamePixel.cs
Normal file
21
TanksServer/Graphics/GamePixel.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class GamePixel
|
||||
{
|
||||
public Player? BelongsTo { get; set; }
|
||||
|
||||
public GamePixelEntityType? EntityType { get; set; }
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
BelongsTo = null;
|
||||
EntityType = null;
|
||||
}
|
||||
}
|
||||
|
||||
internal enum GamePixelEntityType : byte
|
||||
{
|
||||
Wall = 0x0,
|
||||
Tank = 0x1,
|
||||
Bullet = 0x2
|
||||
}
|
47
TanksServer/Graphics/GamePixelGrid.cs
Normal file
47
TanksServer/Graphics/GamePixelGrid.cs
Normal file
|
@ -0,0 +1,47 @@
|
|||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class GamePixelGrid : IEnumerable<GamePixel>
|
||||
{
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
|
||||
private readonly GamePixel[,] _pixels;
|
||||
|
||||
public GamePixelGrid(int width, int height)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
|
||||
_pixels = new GamePixel[height, width];
|
||||
for (var row = 0; row < height; row++)
|
||||
for (var column = 0; column < width; column++)
|
||||
_pixels[row, column] = new GamePixel();
|
||||
}
|
||||
|
||||
public GamePixel this[int x, int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
Debug.Assert(y * Width + x < _pixels.Length);
|
||||
return _pixels[y, x];
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (var pixel in _pixels)
|
||||
pixel.Clear();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public IEnumerator<GamePixel> GetEnumerator()
|
||||
{
|
||||
for (var row = 0; row < Height; row++)
|
||||
for (var column = 0; column < Width; column++)
|
||||
yield return _pixels[row, column];
|
||||
}
|
||||
}
|
|
@ -5,18 +5,30 @@ namespace TanksServer.Graphics;
|
|||
|
||||
internal sealed class GeneratePixelsTickStep(
|
||||
IEnumerable<IDrawStep> drawSteps,
|
||||
LastFinishedFrameProvider lastFrameProvider
|
||||
IEnumerable<IFrameConsumer> consumers
|
||||
) : ITickStep
|
||||
{
|
||||
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
|
||||
private readonly PixelGrid _drawGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
||||
private readonly List<IFrameConsumer> _consumers = consumers.ToList();
|
||||
|
||||
public Task TickAsync()
|
||||
private readonly PixelGrid _observerPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
||||
private readonly GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
||||
|
||||
public async Task TickAsync()
|
||||
{
|
||||
_drawGrid.Clear();
|
||||
_gamePixelGrid.Clear();
|
||||
foreach (var step in _drawSteps)
|
||||
step.Draw(_drawGrid);
|
||||
lastFrameProvider.LastFrame = _drawGrid;
|
||||
return Task.CompletedTask;
|
||||
step.Draw(_gamePixelGrid);
|
||||
|
||||
_observerPixelGrid.Clear();
|
||||
for (var y = 0; y < MapService.PixelsPerColumn; y++)
|
||||
for (var x = 0; x < MapService.PixelsPerRow; x++)
|
||||
{
|
||||
if (_gamePixelGrid[x, y].EntityType.HasValue)
|
||||
_observerPixelGrid[(ushort)x, (ushort)y] = true;
|
||||
}
|
||||
|
||||
foreach (var consumer in _consumers)
|
||||
await consumer.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,5 +4,5 @@ namespace TanksServer.Graphics;
|
|||
|
||||
internal interface IDrawStep
|
||||
{
|
||||
void Draw(PixelGrid buffer);
|
||||
void Draw(GamePixelGrid pixels);
|
||||
}
|
||||
|
|
8
TanksServer/Graphics/IFrameConsumer.cs
Normal file
8
TanksServer/Graphics/IFrameConsumer.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
using DisplayCommands;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal interface IFrameConsumer
|
||||
{
|
||||
Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels);
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
using DisplayCommands;
|
||||
|
||||
namespace TanksServer.Graphics;
|
||||
|
||||
internal sealed class LastFinishedFrameProvider
|
||||
{
|
||||
private PixelGrid? _lastFrame;
|
||||
|
||||
public PixelGrid LastFrame
|
||||
{
|
||||
get => _lastFrame ?? throw new InvalidOperationException("first frame not yet drawn");
|
||||
set => _lastFrame = value;
|
||||
}
|
||||
}
|
|
@ -7,8 +7,8 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int
|
|||
{
|
||||
private readonly byte[] _buffer = new byte[messageSize];
|
||||
|
||||
public ValueTask SendAsync(ReadOnlyMemory<byte> data) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, true, CancellationToken.None);
|
||||
public ValueTask SendAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
|
||||
|
||||
public async IAsyncEnumerable<Memory<byte>> ReadAllAsync()
|
||||
{
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Channels;
|
||||
using DisplayCommands;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory
|
||||
) : IHostedLifecycleService
|
||||
) : IHostedLifecycleService, IFrameConsumer
|
||||
{
|
||||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
private bool _closing;
|
||||
|
@ -37,76 +37,21 @@ internal sealed class ClientScreenServer(
|
|||
return connection.Done;
|
||||
}
|
||||
|
||||
private void Remove(ClientScreenServerConnection connection)
|
||||
{
|
||||
_connections.TryRemove(connection, out _);
|
||||
}
|
||||
public void Remove(ClientScreenServerConnection connection) => _connections.TryRemove(connection, out _);
|
||||
|
||||
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||
|
||||
|
||||
public Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
{
|
||||
var tasks = _connections.Keys
|
||||
.Select(c => c.SendAsync(observerPixels, gamePixelGrid));
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
||||
internal sealed class ClientScreenServerConnection : IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
|
||||
public ClientScreenServerConnection(WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels)
|
||||
{
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.SendAsync(pixels.Data);
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "send failed");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
115
TanksServer/Interactivity/ClientScreenServerConnection.cs
Normal file
115
TanksServer/Interactivity/ClientScreenServerConnection.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ClientScreenServerConnection : IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
private readonly Guid? _playerGuid = null;
|
||||
private readonly PlayerScreenData? _playerScreenData = null;
|
||||
|
||||
public ClientScreenServerConnection(
|
||||
WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server,
|
||||
Guid? playerGuid = null
|
||||
)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
|
||||
_playerGuid = playerGuid;
|
||||
if (playerGuid.HasValue)
|
||||
_playerScreenData = new PlayerScreenData();
|
||||
|
||||
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_playerScreenData != null)
|
||||
RefreshPlayerSpecificData(gamePixelGrid);
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.SendAsync(pixels.Data, _playerScreenData == null);
|
||||
if (_playerScreenData != null)
|
||||
await _channel.SendAsync(_playerScreenData.GetPacket());
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "send failed");
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshPlayerSpecificData(GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
Debug.Assert(_playerScreenData != null);
|
||||
_playerScreenData.Clear();
|
||||
foreach (var gamePixel in gamePixelGrid)
|
||||
{
|
||||
if (!gamePixel.EntityType.HasValue)
|
||||
continue;
|
||||
_playerScreenData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Id == _playerGuid);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlayerScreenData
|
||||
{
|
||||
private Memory<byte> _data = new byte[MapService.PixelsPerRow * MapService.PixelsPerColumn];
|
||||
|
||||
public int Count { get; private set; } = 0;
|
||||
|
||||
public void Clear() => Count = 0;
|
||||
|
||||
public ReadOnlyMemory<byte> GetPacket() => _data[..Count];
|
||||
|
||||
public void Add(GamePixelEntityType entityKind, bool isCurrentPlayer)
|
||||
{
|
||||
var result = (byte)(isCurrentPlayer ? 0x1b : 0x0b);
|
||||
var kind = (byte)entityKind;
|
||||
Debug.Assert(kind < 3);
|
||||
result += (byte)(kind << 2);
|
||||
_data.Span[Count] = result;
|
||||
Count++;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class SendToClientScreen(
|
||||
ClientScreenServer clientScreenServer,
|
||||
LastFinishedFrameProvider lastFinishedFrameProvider
|
||||
) : ITickStep
|
||||
{
|
||||
public Task TickAsync()
|
||||
{
|
||||
var tasks = clientScreenServer
|
||||
.GetConnections()
|
||||
.Select(c => c.SendAsync(lastFinishedFrameProvider.LastFrame));
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
|
@ -6,14 +6,13 @@ using TanksServer.Graphics;
|
|||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class SendToServicePointDisplay : ITickStep
|
||||
internal sealed class SendToServicePointDisplay : IFrameConsumer
|
||||
{
|
||||
private const int ScoresWidth = 12;
|
||||
private const int ScoresHeight = 20;
|
||||
private const int ScoresPlayerRows = ScoresHeight - 5;
|
||||
|
||||
private readonly IDisplayConnection _displayConnection;
|
||||
private readonly LastFinishedFrameProvider _lastFinishedFrameProvider;
|
||||
private readonly ILogger<SendToServicePointDisplay> _logger;
|
||||
private readonly PlayerServer _players;
|
||||
private readonly Cp437Grid _scoresBuffer;
|
||||
|
@ -22,13 +21,11 @@ internal sealed class SendToServicePointDisplay : ITickStep
|
|||
private DateTime _nextFailLog = DateTime.Now;
|
||||
|
||||
public SendToServicePointDisplay(
|
||||
LastFinishedFrameProvider lastFinishedFrameProvider,
|
||||
PlayerServer players,
|
||||
ILogger<SendToServicePointDisplay> logger,
|
||||
IDisplayConnection displayConnection
|
||||
)
|
||||
{
|
||||
_lastFinishedFrameProvider = lastFinishedFrameProvider;
|
||||
_players = players;
|
||||
_logger = logger;
|
||||
_displayConnection = displayConnection;
|
||||
|
@ -45,17 +42,16 @@ internal sealed class SendToServicePointDisplay : ITickStep
|
|||
};
|
||||
}
|
||||
|
||||
public async Task TickAsync()
|
||||
public async Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
{
|
||||
RefreshScores();
|
||||
try
|
||||
{
|
||||
await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer);
|
||||
|
||||
var currentFrame = _lastFinishedFrameProvider.LastFrame;
|
||||
if (_lastSentFrame == currentFrame)
|
||||
if (_lastSentFrame == observerPixels)
|
||||
return;
|
||||
_lastSentFrame = currentFrame;
|
||||
_lastSentFrame = observerPixels;
|
||||
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastSentFrame);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
|
|
|
@ -123,7 +123,6 @@ public static class Program
|
|||
builder.Services.AddSingleton<ControlsServer>();
|
||||
builder.Services.AddSingleton<PlayerServer>();
|
||||
builder.Services.AddSingleton<ClientScreenServer>();
|
||||
builder.Services.AddSingleton<LastFinishedFrameProvider>();
|
||||
builder.Services.AddSingleton<SpawnQueue>();
|
||||
|
||||
builder.Services.AddHostedService<GameTickWorker>();
|
||||
|
@ -138,13 +137,15 @@ public static class Program
|
|||
builder.Services.AddSingleton<ITickStep, ShootFromTanks>();
|
||||
builder.Services.AddSingleton<ITickStep, SpawnNewTanks>();
|
||||
builder.Services.AddSingleton<ITickStep, GeneratePixelsTickStep>();
|
||||
builder.Services.AddSingleton<ITickStep, SendToServicePointDisplay>();
|
||||
builder.Services.AddSingleton<ITickStep, SendToClientScreen>();
|
||||
|
||||
builder.Services.AddSingleton<IDrawStep, DrawMapStep>();
|
||||
builder.Services.AddSingleton<IDrawStep, DrawTanksStep>();
|
||||
builder.Services.AddSingleton<IDrawStep, DrawBulletsStep>();
|
||||
|
||||
builder.Services.AddSingleton<IFrameConsumer, SendToServicePointDisplay>();
|
||||
builder.Services.AddSingleton<IFrameConsumer, ClientScreenServer>(sp =>
|
||||
sp.GetRequiredService<ClientScreenServer>());
|
||||
|
||||
builder.Services.Configure<TanksConfiguration>(
|
||||
builder.Configuration.GetSection("Tanks"));
|
||||
builder.Services.Configure<PlayersConfiguration>(
|
||||
|
|
Loading…
Reference in a new issue