servicepoint-tanks/tanks-backend/TanksServer/Program.cs

119 lines
5.2 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics.CodeAnalysis;
2024-04-06 16:38:26 +02:00
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
2024-04-06 16:38:26 +02:00
using Microsoft.Extensions.FileProviders;
2024-04-10 19:25:45 +02:00
using TanksServer.GameLogic;
using TanksServer.Graphics;
using TanksServer.Interactivity;
namespace TanksServer;
2024-04-10 22:03:36 +02:00
public static class Program
{
[RequiresUnreferencedCode("Calls Endpoints.Map")]
[RequiresDynamicCode("Calls Endpoints.Map")]
2024-04-21 12:38:03 +02:00
public static async Task Main(string[] args)
{
var app = Configure(args);
2024-04-06 16:38:26 +02:00
var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client"));
2024-04-06 16:38:26 +02:00
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider });
app.UseStaticFiles(new StaticFileOptions { FileProvider = clientFileProvider });
app.Services.GetRequiredService<Endpoints>().Map(app);
2024-04-14 23:11:00 +02:00
2024-04-21 12:38:03 +02:00
await app.RunAsync();
}
[RequiresUnreferencedCode("Calls Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<TOptions>(IConfiguration)")]
[RequiresDynamicCode("Calls Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<TOptions>(IConfiguration)")]
private static WebApplication Configure(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);
2024-04-11 20:48:21 +02:00
builder.Logging.AddSimpleConsole(options =>
{
options.SingleLine = true;
options.IncludeScopes = true;
options.TimestampFormat = "HH:mm:ss ";
});
builder.Services.AddCors(options => options
.AddDefaultPolicy(policy => policy
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin())
);
builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.TypeInfoResolverChain.Insert(0, new AppSerializerContext()));
2024-04-10 22:03:36 +02:00
builder.Services.AddHttpLogging(_ => { });
2024-04-07 20:16:22 +02:00
2024-05-07 20:41:06 +02:00
var healthCheckBuilder = builder.Services.AddHealthChecks();
healthCheckBuilder.AddCheck<UpdatesPerSecondCounter>("updates check");
builder.Services.AddSingleton<MapService>();
2024-04-17 19:34:19 +02:00
builder.Services.AddSingleton<MapEntityManager>();
2024-04-07 19:52:16 +02:00
builder.Services.AddSingleton<ControlsServer>();
builder.Services.AddSingleton<PlayerServer>();
2024-04-06 16:38:26 +02:00
builder.Services.AddSingleton<ClientScreenServer>();
2024-04-17 19:34:19 +02:00
builder.Services.AddSingleton<TankSpawnQueue>();
builder.Services.AddSingleton<Endpoints>();
2024-05-03 14:45:41 +02:00
builder.Services.AddSingleton<BufferPool>();
builder.Services.AddSingleton<EmptyTileFinder>();
builder.Services.AddSingleton<ChangeToRequestedMap>();
2024-05-07 20:41:06 +02:00
builder.Services.AddSingleton<UpdatesPerSecondCounter>();
2024-04-07 19:52:16 +02:00
2024-04-07 20:29:09 +02:00
builder.Services.AddHostedService<GameTickWorker>();
2024-11-12 19:14:02 +01:00
builder.Services.AddHostedService(FromServices<ControlsServer>);
builder.Services.AddHostedService(FromServices<ClientScreenServer>);
2024-05-07 20:41:06 +02:00
builder.Services.AddSingleton<ITickStep, ChangeToRequestedMap>(sp =>
sp.GetRequiredService<ChangeToRequestedMap>());
2024-04-07 19:52:16 +02:00
builder.Services.AddSingleton<ITickStep, MoveBullets>();
2024-04-17 19:34:19 +02:00
builder.Services.AddSingleton<ITickStep, CollideBullets>();
2024-04-07 19:52:16 +02:00
builder.Services.AddSingleton<ITickStep, RotateTanks>();
builder.Services.AddSingleton<ITickStep, MoveTanks>();
builder.Services.AddSingleton<ITickStep, ShootFromTanks>();
2024-04-17 19:34:19 +02:00
builder.Services.AddSingleton<ITickStep, CollectPowerUp>();
builder.Services.AddSingleton<ITickStep>(sp => sp.GetRequiredService<TankSpawnQueue>());
builder.Services.AddSingleton<ITickStep, SpawnPowerUp>();
builder.Services.AddSingleton<ITickStep, GeneratePixelsTickStep>();
2024-04-22 19:03:07 +02:00
builder.Services.AddSingleton<ITickStep, PlayerServer>(sp => sp.GetRequiredService<PlayerServer>());
2024-05-07 20:41:06 +02:00
builder.Services.AddSingleton<ITickStep, UpdatesPerSecondCounter>(sp =>
sp.GetRequiredService<UpdatesPerSecondCounter>());
builder.Services.AddSingleton<IDrawStep, DrawMapStep>();
2024-04-17 19:34:19 +02:00
builder.Services.AddSingleton<IDrawStep, DrawPowerUpsStep>();
builder.Services.AddSingleton<IDrawStep, DrawTanksStep>();
builder.Services.AddSingleton<IDrawStep, DrawBulletsStep>();
builder.Services.AddSingleton<IFrameConsumer, ClientScreenServer>(sp =>
sp.GetRequiredService<ClientScreenServer>());
2024-04-17 19:34:19 +02:00
builder.Services.Configure<GameRules>(builder.Configuration.GetSection("GameRules"));
builder.Services.Configure<HostConfiguration>(builder.Configuration.GetSection("Host"));
2024-05-13 01:23:34 +02:00
builder.Services.Configure<DisplayConfiguration>(builder.Configuration.GetSection("ServicePointDisplay"));
builder.Services.AddSingleton<IFrameConsumer, SendToServicePointDisplay>();
2024-05-13 01:23:34 +02:00
builder.Services.AddSingleton<Connection>(sp =>
{
var config = sp.GetRequiredService<IOptions<DisplayConfiguration>>().Value;
var connection = new Connection($"{config.Hostname}:{config.Port}");
return connection;
2024-05-13 01:23:34 +02:00
});
2024-04-07 20:16:22 +02:00
2024-04-10 22:03:36 +02:00
var app = builder.Build();
app.UseCors();
app.UseWebSockets();
app.UseHttpLogging();
return app;
}
2024-11-12 19:14:02 +01:00
private static T FromServices<T>(IServiceProvider sp) where T : notnull => sp.GetRequiredService<T>();
2024-04-13 18:35:36 +02:00
}