Пример #1
0
    public static async Task Main()
    {
        string?localProfile = null;

        if (DebugHelpers.UseDevelopmentAuthentication)
        {
            Console.Write("Profile for local token: ");
            localProfile = Console.ReadLine() ?? "default";
        }

        Console.Clear();
        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.Gray;
        Console.CursorVisible   = false;

        using var cts           = new CancellationTokenSource();
        Console.CancelKeyPress += (s, e) =>
        {
            Console.WriteLine("Canceling...");
            cts.Cancel();
            e.Cancel = true;
        };

        var cancellationToken = cts.Token;

        using var host = HostFactory.CreateConsoleAppHostBuilder(messageTypes =>
        {
            var services = messageTypes.Services;

            if (localProfile != null && DebugHelpers.UseDevelopmentAuthentication)
            {
                services.AddLocalProfileTokenProvider(localProfile);
            }

            // For every singleton screen handler - it's own typer pool.
            services.AddTransient <ITyperPool, UniqueTyperPool>();

            services.AddSingleton <IOutput, ConsoleOutputWithoutFlicker>();
            services.AddSingleton <ITextGenerator, TextGenerator>();

            services.AddSingleton <ScreenFactory>();
            services.AddTransient <IScreenFactory>(p => p.GetRequiredService <ScreenFactory>());
            services.AddTransient <IScreenNavigation>(p => p.GetRequiredService <ScreenFactory>().ScreenNavigation);

            services.AddSingleton <DialogScreenHandler>();
            services.AddSingleton <IScreenProvider>(p => (IScreenProvider)p.GetRequiredService <IScreenNavigation>());
            services.AddSingleton <IConnectionManager, ConnectionManager>();

            services.AddTransient <IPrinter <MainMenuState>, MainMenuPrinter>();
            services.AddTransient <IPrinter <CharacterCreationState>, CharacterCreationPrinter>();
            services.AddTransient <IPrinter <WorldScreenState>, WorldPrinter>();

            services.AddSingleton <IScreenFactory, ScreenFactory>();
        }).Build();

        await host.StartAsync(cancellationToken)
        .ConfigureAwait(false);

        {
            // Create a couple of characters.
            var charactersClient = host.Services.GetRequiredService <ICharactersClient>();
            await charactersClient.CreateAsync(
                new CreateCharacterDto("my character"),
                default).ConfigureAwait(false);

            await charactersClient.CreateAsync(
                new CreateCharacterDto("my character 2"),
                default).ConfigureAwait(false);
        }

        RunApplication(
            host.Services.GetRequiredService <IScreenNavigation>(),
            host.Services.GetRequiredService <IScreenProvider>(),
            cancellationToken);

        await host.StopAsync(cancellationToken)
        .ConfigureAwait(false);
    }