2024-04-06 16:38:26 +02:00
|
|
|
using System.IO;
|
2024-04-12 14:29:26 +02:00
|
|
|
using DisplayCommands;
|
2024-04-06 13:46:34 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2024-04-16 21:34:54 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-04-06 13:46:34 +02:00
|
|
|
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;
|
2024-04-06 13:46:34 +02:00
|
|
|
|
|
|
|
namespace TanksServer;
|
|
|
|
|
2024-04-10 22:03:36 +02:00
|
|
|
public static class Program
|
2024-04-06 13:46:34 +02:00
|
|
|
{
|
2024-04-21 12:38:03 +02:00
|
|
|
public static async Task Main(string[] args)
|
2024-04-06 13:46:34 +02:00
|
|
|
{
|
|
|
|
var app = Configure(args);
|
|
|
|
|
2024-04-06 16:38:26 +02:00
|
|
|
var clientFileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "client"));
|
2024-04-28 12:53:18 +02:00
|
|
|
|
2024-04-06 16:38:26 +02:00
|
|
|
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = clientFileProvider });
|
|
|
|
app.UseStaticFiles(new StaticFileOptions { FileProvider = clientFileProvider });
|
2024-04-06 13:46:34 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
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();
|
2024-04-06 13:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ";
|
|
|
|
});
|
|
|
|
|
2024-04-07 00:33:54 +02:00
|
|
|
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-04-16 21:34:54 +02:00
|
|
|
builder.Services.Configure<HostConfiguration>(builder.Configuration.GetSection("Host"));
|
|
|
|
var hostConfiguration = builder.Configuration.GetSection("Host").Get<HostConfiguration>();
|
|
|
|
if (hostConfiguration == null)
|
|
|
|
throw new InvalidOperationException("'Host' configuration missing");
|
|
|
|
|
2024-04-06 13:46:34 +02:00
|
|
|
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>();
|
2024-04-28 12:53:18 +02:00
|
|
|
builder.Services.AddSingleton<Endpoints>();
|
2024-05-03 14:45:41 +02:00
|
|
|
builder.Services.AddSingleton<BufferPool>();
|
2024-05-03 15:47:33 +02:00
|
|
|
builder.Services.AddSingleton<EmptyTileFinder>();
|
|
|
|
builder.Services.AddSingleton<ChangeToRequestedMap>();
|
2024-04-07 19:52:16 +02:00
|
|
|
|
2024-04-07 20:29:09 +02:00
|
|
|
builder.Services.AddHostedService<GameTickWorker>();
|
2024-04-07 19:52:16 +02:00
|
|
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
2024-04-07 13:02:49 +02:00
|
|
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ClientScreenServer>());
|
2024-04-06 13:46:34 +02:00
|
|
|
|
2024-05-03 15:47:33 +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>();
|
2024-04-13 14:07:14 +02:00
|
|
|
builder.Services.AddSingleton<ITickStep, GeneratePixelsTickStep>();
|
2024-04-22 19:03:07 +02:00
|
|
|
builder.Services.AddSingleton<ITickStep, PlayerServer>(sp => sp.GetRequiredService<PlayerServer>());
|
2024-04-30 10:38:09 +02:00
|
|
|
builder.Services.AddSingleton<ITickStep, UpdatesPerSecondCounter>();
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-13 14:07:14 +02:00
|
|
|
builder.Services.AddSingleton<IDrawStep, DrawMapStep>();
|
2024-04-17 19:34:19 +02:00
|
|
|
builder.Services.AddSingleton<IDrawStep, DrawPowerUpsStep>();
|
2024-04-13 14:07:14 +02:00
|
|
|
builder.Services.AddSingleton<IDrawStep, DrawTanksStep>();
|
|
|
|
builder.Services.AddSingleton<IDrawStep, DrawBulletsStep>();
|
2024-04-07 19:05:50 +02:00
|
|
|
|
2024-04-15 20:34:23 +02:00
|
|
|
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"));
|
2024-04-16 21:34:54 +02:00
|
|
|
|
|
|
|
if (hostConfiguration.EnableServicePointDisplay)
|
|
|
|
{
|
|
|
|
builder.Services.AddSingleton<IFrameConsumer, SendToServicePointDisplay>();
|
|
|
|
builder.Services.AddDisplay(builder.Configuration.GetSection("ServicePointDisplay"));
|
|
|
|
}
|
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-04-06 13:46:34 +02:00
|
|
|
}
|
2024-04-13 18:35:36 +02:00
|
|
|
}
|