Esempio n. 1
0
        public async Task CheckQueue()
        {
            var queue = await _battles.Queue();

            async Task <string> FormatFleet(Fleet?fleet)
            {
                return(await(fleet?.FormattedName(_client) ?? Task.FromResult("Unknown")));
            }

            async Task <string> FormatBattle(Battle battle, int index)
            {
                var fa = await FormatFleet(await _fleets.Load(battle.A));

                var fb = await FormatFleet(await _fleets.Load(battle.B));

                return($"{index + 1}. {fa} vs {fb}");
            }

            await DisplayItemList(
                queue,
                () => "No pending battles",
                FormatBattle
                );
        }
        private async Task RunSingle(Battle battle)
        {
            var fa = await _fleets.Load(battle.A);

            var fb = await _fleets.Load(battle.B);

            if (!fa.HasValue || !fb.HasValue)
            {
                return;
            }

            var a = await LoadFleet(battle.A);

            if (a == null)
            {
                return;
            }

            var b = await LoadFleet(battle.B);

            if (b == null)
            {
                return;
            }

            using var report = new Simulation(a, fa.Value.Name, b, fb.Value.Name).Run();

            var filename = $"{await fa.Value.FormattedName(_client)} vs {await fb.Value.FormattedName(_client)} ({DateTime.UtcNow:u})";

            await using (var file = File.Create(Path.Combine(_config.ReplayOutputDirectory, filename)))
                await using (var zip = new DeflateStream(file, CompressionLevel.Optimal))
                    await using (var stream = new StreamWriter(zip))
                        using (var writer = new JsonTextWriter(stream)
                        {
                            Formatting = Formatting.Indented
                        })
                        {
                            report.Serialize(writer);
                        }

            // Update ranking
            switch (report.Winner)
            {
            case 0:
                await _ranks.Update(battle.A, battle.B, false);

                break;

            case 1:
                await _ranks.Update(battle.B, battle.A, false);

                break;

            default:
                await _ranks.Update(battle.A, battle.B, true);

                break;
            }

            // Sims take a load of space, clean it up now
            GC.Collect();
        }