servicepoint-tanks/TanksServer/ControlsServer.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2024-04-07 01:27:11 +02:00
using System.Net.WebSockets;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
2024-04-07 11:19:14 +02:00
using TanksServer.Helpers;
2024-04-07 13:02:49 +02:00
using TanksServer.Models;
2024-04-07 01:27:11 +02:00
namespace TanksServer;
2024-04-07 01:27:11 +02:00
internal sealed class ControlsServer(ILogger<ControlsServer> logger, ILoggerFactory loggerFactory)
: IHostedLifecycleService
{
2024-04-07 01:27:11 +02:00
private readonly List<ControlsServerConnection> _connections = new();
public Task HandleClient(WebSocket ws, Player player)
{
logger.LogDebug("control client connected {}", player.Id);
var clientLogger = loggerFactory.CreateLogger<ControlsServerConnection>();
var sock = new ControlsServerConnection(ws, clientLogger, this, player);
_connections.Add(sock);
return sock.Done;
}
public Task StoppingAsync(CancellationToken cancellationToken)
{
return Task.WhenAll(_connections.Select(c => c.CloseAsync()));
}
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;
private void Remove(ControlsServerConnection connection)
{
_connections.Remove(connection);
}
2024-04-07 13:02:49 +02:00
private sealed class ControlsServerConnection(
WebSocket socket, ILogger<ControlsServerConnection> logger,
ControlsServer server, Player player)
2024-04-07 01:27:11 +02:00
: EasyWebSocket(socket, logger, new byte[2])
{
private enum MessageType : byte
{
Enable = 0x01,
Disable = 0x02,
}
private enum InputType : byte
{
Forward = 0x01,
Backward = 0x02,
Left = 0x03,
Right = 0x04,
Shoot = 0x05
}
protected override Task ReceiveAsync(ArraySegment<byte> buffer)
{
2024-04-07 11:19:14 +02:00
var type = (MessageType)buffer[0];
var control = (InputType)buffer[1];
2024-04-07 13:02:49 +02:00
Logger.LogTrace("player input {} {} {}", player.Id, type, control);
2024-04-07 01:27:11 +02:00
2024-04-07 11:19:14 +02:00
var isEnable = type switch
2024-04-07 01:27:11 +02:00
{
2024-04-07 11:19:14 +02:00
MessageType.Enable => true,
MessageType.Disable => false,
_ => throw new ArgumentException("invalid message type")
};
2024-04-07 01:27:11 +02:00
2024-04-07 11:19:14 +02:00
switch (control)
2024-04-07 01:27:11 +02:00
{
case InputType.Forward:
player.Controls.Forward = isEnable;
break;
case InputType.Backward:
player.Controls.Backward = isEnable;
break;
case InputType.Left:
player.Controls.TurnLeft = isEnable;
break;
case InputType.Right:
player.Controls.TurnRight = isEnable;
break;
case InputType.Shoot:
player.Controls.Shoot = isEnable;
break;
default:
2024-04-07 11:19:14 +02:00
throw new ArgumentException("invalid control type");
2024-04-07 01:27:11 +02:00
}
return Task.CompletedTask;
}
protected override Task ClosingAsync()
{
server.Remove(this);
return Task.CompletedTask;
}
}
}