public async Task <IActionResult> AddTournamentStateBell(Guid id) { Tournament tournament = this.TournamentRepository.Get(id); if (tournament == null) { return(NotFound()); } // Get the current tournament status TournamentState initialState = tournament.State; TournamentStateUpdateModel tournamentStateChange = this.TournamentLogic.AddBell(tournament); // Set tournament state based on logic rules SetState(tournament, initialState, tournamentStateChange); // Update the tournament state this.TournamentRepository.Update(tournament); // Broadcast to all subscribers that the state has changed if (initialState != tournamentStateChange.State) { await this.TournamentBroadcast.TournamentStateChangeAsync(id, tournament.State, tournament.PreplayGameId, tournament.InplayGameId, tournament.PostplayGameId); } // Return the updated tournament return(Ok(Mapper.Map <Tournament, TournamentModel>(this.TournamentRepository.Get(id)))); }
public IActionResult Update(Guid id, [FromBody] JsonPatchDocument <TournamentStateUpdateModel> jsonPatchDocument) { // Get the tournament Tournament tournament = this.TournamentRepository.Get(id); if (tournament == null) { return(NotFound()); } // Get the current tournament status TournamentState initialState = tournament.State; // Update the tournament TournamentStateUpdateModel tournamentUpdate = new TournamentStateUpdateModel(); jsonPatchDocument.ApplyTo(tournamentUpdate); // Only honour the update of a state if (tournamentUpdate.State != initialState) { // Set tournament state based on logic rules SetState(tournament, initialState, tournamentUpdate); // Update the tournament state this.TournamentRepository.Update(tournament); } // Return success return(NoContent()); }
private void SetState(Tournament tournament, TournamentState initialState, TournamentStateUpdateModel tournamentUpdate) { // If the current state is No play then treat any state change as an update to PrePlay if (initialState == TournamentState.NoPlay) { tournamentUpdate.State = TournamentState.PrePlay; } Game newPreplayGame; // Apply logic rules to state change switch (tournamentUpdate.State) { // Updating to preplay case TournamentState.PrePlay: // Apply the rules for setting the new Preplay state tournament = this.TournamentLogic.SetPreplay(tournament, out newPreplayGame); // Save the new preplay if one is created if (newPreplayGame != null) { this.TournamentRepository.UpdateGame(newPreplayGame); } break; // Updating to preplay case TournamentState.InPlay: // Apply the rules for setting the new Inplay state tournament = this.TournamentLogic.SetInplay(tournament, out newPreplayGame); // Save the new preplay if one is created if (newPreplayGame != null) { this.TournamentRepository.UpdateGame(newPreplayGame); } break; // Updating to post play case TournamentState.PostPlay: // Apply the rules for setting the new Inplay state tournament = this.TournamentLogic.SetPostplay(tournament, out newPreplayGame); // Save the new preplay if one is created if (newPreplayGame != null) { this.TournamentRepository.UpdateGame(newPreplayGame); } break; default: break; } }