private static List <PlayerScore> GetWinnersOfCurrentRound(PutScoresInput scores, DataStore dataStore, int currentRound) { var winners = new List <PlayerScore>(); var games = dataStore.GetTournamentGames(scores.tournamentId, (RoundTypes)currentRound); foreach (TournamentGames game in games) { AddWinner(game, winners); } return(winners); }
private static void StartNextRound(PutScoresInput scores, DataStore dataStore) { var currentRound = dataStore.GetCurrentRound(scores.tournamentId); if (currentRound == (int)RoundTypes.GROUP) { TournamentGenerator.CreateKnockoutStageGames(dataStore, dataStore.GetTournament(scores.tournamentId)); } else { var winners = GetWinnersOfCurrentRound(scores, dataStore, currentRound); if (dataStore.GetCurrentRound(scores.tournamentId) != (int)RoundTypes.FINALS) { TournamentGenerator.CreateKnockoutStageGames(dataStore, dataStore.GetTournament(scores.tournamentId), winners); } } }
public Response PutGameScores([FromBody] PutScoresInput scores) { var response = new Response(); if (scores == null) { AddErrorToResponse(response, "Invalid score inputs! Please try again!"); return(response); } if (DoValidationOnInt(response, scores.tournamentId < 1, "Tournament ID is not valid.") || DoValidationOnInt(response, scores.groupId < 1, "Group ID is not valid.") || DoValidationOnInt(response, scores.player1Id < 1, "Player 1 ID is not valid.") || DoValidationOnInt(response, scores.player2Id < 1, "Player 2 ID is not valid.") || DoValidationOnInt(response, scores.player1Score < 0, "Player 1 score is not valid.") || DoValidationOnInt(response, scores.player2Score < 0, "Player 2 score is not valid.")) { return(response); } try { var connectionString = _settings.TournamentDB; using (var dataStore = new DataStore(new SqlConnection(connectionString))) { var userId = dataStore.GetPlayerIdFromUsername(Utils.GetUserName(User)); dataStore.SetScores(userId, scores.tournamentId, scores.groupId, scores.player1Id, scores.player2Id, scores.player1Score, scores.player2Score); if (dataStore.IsRoundCompleted(scores.tournamentId)) { StartNextRound(scores, dataStore); } dataStore.Commit(); } } catch (Exception e) { AddErrorToResponse(response, e.Message); } return(response); }