private async Task RunOneGame(OneRoundTempData round) { await round.Semaphore.WaitAsync(); int[] teams; int gameId = -1; try { teams = round.SelectPlayers(simulator.NumPlayers); { await using var cmd = new NpgsqlCommand("insert into Game (round, teams, start_time) VALUES (@round, @teams, @starttime) RETURNING id", conn); cmd.Parameters.AddWithValue("round", round.Round); cmd.Parameters.AddWithValue("teams", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Integer, teams); Int64 unixTimestamp = (Int64)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; cmd.Parameters.AddWithValue("starttime", unixTimestamp); gameId = (int)await cmd.ExecuteScalarAsync(); } } finally { round.Semaphore.Release(); } Console.WriteLine("Simulating game between " + String.Join(", ", teams)); GameLaunchParams launch = new GameLaunchParams(); launch.Practice = false; foreach (int team in teams) { launch.Teams.Add(new TeamData { Id = team, BinaryPath = round.Teams[team].BinaryPath }); } var result = await simulator.Launch(launch); double[] eloDeltas = Elo.ComputeEloDelta(teams.Select(t => round.Teams[t].StartingElo).ToArray(), result.Points.ToArray(), round.EloKFactor); await round.Semaphore.WaitAsync(); try { round.UpdateRatings(teams, eloDeltas, gameId, false); { await using var cmd = new NpgsqlCommand( "update Game set end_time = @endtime, scores = @scores, result_path = @resultpath, " + "elo_deltas = @elodeltas where id = @id", conn); Int64 unixTimestamp = (Int64)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; cmd.Parameters.AddWithValue("endtime", unixTimestamp); cmd.Parameters.AddWithValue("scores", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Double, result.Points.ToArray()); cmd.Parameters.AddWithValue("elodeltas", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Double, eloDeltas); cmd.Parameters.AddWithValue("id", gameId); cmd.Parameters.AddWithValue("resultpath", result.ResultPath); await cmd.ExecuteNonQueryAsync(); } } finally { round.Semaphore.Release(); } }