diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java b/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java index 6849707885532642b401654385ded74898030498..434c4dc0e5c249d01aa079426d5b4839b526d604 100644 --- a/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java @@ -1,6 +1,7 @@ package hdm.mi.sthbackend.controller; +import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException; import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException; @@ -43,7 +44,7 @@ public class TournamentController { public UUID removeTeamFromTournament(@PathVariable UUID tournamentId, @PathVariable UUID teamId) throws TournamentIdNotFoundException, TeamIdNotFoundException{ return service.removeTeamFromTournament(tournamentId, teamId); - }; + } @PostMapping("/{tournamentId}/teams/{teamId}/addPlayer") public Player addPlayerToTeam(@PathVariable UUID tournamentId, @@ -55,8 +56,14 @@ public class TournamentController { @DeleteMapping("/{tournamentId}/teams/{teamId}/players/{playerId}") public Player removePlayerFromTeam(@PathVariable UUID tournamentId, @PathVariable UUID teamId, - @PathVariable UUID playerId) throws PlayerIdNotFoundException, TournamentIdNotFoundException { - return service.removePlayerFromTeam(tournamentId, teamId, playerId); + @PathVariable UUID playerId) throws PlayerIdNotFoundException, TournamentIdNotFoundException { // muss hier die PlayerIdNotFoundException eingefügt werden, + return service.removePlayerFromTeam(tournamentId, teamId, playerId); // wenn die nicht im TournamentService drin ist? + } + + @PatchMapping + public UUID determineWinner(@PathVariable UUID tournamentId, + @PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException { + return service.determineWinner(tournamentId, matchId); } diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/MatchIdNotFoundException.java b/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/MatchIdNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..9824ff80fa23990340efbee8210519736a8a44ad --- /dev/null +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/MatchIdNotFoundException.java @@ -0,0 +1,10 @@ +package hdm.mi.sthbackend.exeptions; + +import java.util.UUID; + +public class MatchIdNotFoundException extends Exception{ + + public MatchIdNotFoundException(UUID matchId){ + super(String.format("MatchID %s not found", matchId.toString())); + } +} diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java b/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java index 03a372686c6730286245dbdc264b235504ed8c5d..4cfed80ceec3c352d3a125330875986f32dd0563 100644 --- a/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java @@ -1,26 +1,26 @@ package hdm.mi.sthbackend.service; import com.mongodb.client.MongoClient; +import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException; import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException; -import hdm.mi.sthbackend.model.Player; -import hdm.mi.sthbackend.model.Team; -import hdm.mi.sthbackend.model.Tournament; +import hdm.mi.sthbackend.model.*; import hdm.mi.sthbackend.repository.ITournamentRepository; import hdm.mi.sthbackend.repository.MongoManager; import lombok.AllArgsConstructor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Optional; -import java.util.UUID; +import java.util.*; @Service @AllArgsConstructor public class TournamentService { + private final static Logger log = LogManager.getLogger(TournamentService.class); + ITournamentRepository tournamentRepository; public Player addPlayerToTeam(UUID tournamentId, @@ -35,9 +35,21 @@ public class TournamentService { .getTeamMembers() .put(newPlayer.getPlayerId(), newPlayer); tournamentRepository.save(tournament); + log.debug("Player " + newPlayer.getPlayerId() + " added to Team " + teamId + " in Tournament " + tournamentId); return newPlayer; } + + + /* + Wie gehen wir damit um, wenn zwei Player denselben Namen haben? Also: Stefan vs Stefan = Stefan gewinnt + Sollte man hier in den Methoden das ausschließen und dann geben die Spieler ihren ersten Buchstaben vom Nachnamen an? z.B. Stefan S. vs Stefan w. + oder geben wir die UUID auch mit an zum PlayerName + */ + + + + public Player removePlayerFromTeam(UUID tournamentId, UUID teamId, UUID playerId) throws TournamentIdNotFoundException { @@ -50,9 +62,16 @@ public class TournamentService { .remove(playerId); tournamentRepository.save(tournament); + log.debug("Player " + playerId + " removed from Team " + teamId + " in Tournament " + tournamentId); return removedPlayer; } + + /* + Kann es passieren, dass hier bei der Methode removePlayerFromTeam() es den Player gar nicht gibt? + und dann eine PlayerIdNotFoundException geworfen werden muss? oder ist durch die Übergabe von UUID playerId schon safe die playerId vorhanden + */ + public UUID updatePlayerName(UUID tournamentId, UUID teamId, UUID playerId, @@ -67,6 +86,7 @@ public class TournamentService { .setName(newPlayerName); tournamentRepository.save(tournament); + log.debug("Player " + playerId + " changed Name to " + newPlayerName + " in " + teamId + " in Tournament " + tournamentId); return playerId; } @@ -81,6 +101,7 @@ public class TournamentService { .put(team.getId(), team); tournamentRepository.save(tournament); + log.debug("Team " + team.getId() + " added to Tournament " + tournamentId); return team; } @@ -93,6 +114,7 @@ public class TournamentService { .remove(teamId)) .orElseThrow(() -> new TeamIdNotFoundException(tournamentId)); tournamentRepository.save(tournament); + log.debug("Team " + teamId + " added to Tournament " + tournamentId); return teamId; } @@ -103,8 +125,53 @@ public class TournamentService { Tournament tournament = new Tournament(UUID.randomUUID(), tournamentName, new ArrayList<>(), new HashMap<>()); tournamentRepository.insert(tournament); + tournamentRepository.save(tournament);// Muss es hier nochmal gespeichert werden? + log.debug("created Tournament " + tournament.getTournamentId()); return tournament; } + public UUID determineWinner (UUID tournamentId, UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException{ + + Tournament tournament = tournamentRepository.findById(tournamentId) + .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); + + Match match = tournament.getMatches().stream() + .filter(m -> m.getMatchId().equals(matchId)) + .findFirst() + .orElseThrow(() -> new MatchIdNotFoundException(matchId)); + + UUID winnerTeamId = match.getTeamScores().entrySet().stream() + .max(Comparator.<Map.Entry<UUID, Number>, Double>comparing(entry -> entry.getValue().doubleValue())) + .map(Map.Entry::getKey) + .orElse(null); + + match.setWinnerTeamId(winnerTeamId); + tournamentRepository.save(tournament); + log.debug("The Winner " + winnerTeamId + " is determined for Match " + matchId + " in Tournament " + tournamentId); + return winnerTeamId; + } + + public void assignTeamToMatch(UUID tournamentId, UUID matchId, UUID teamId, int score) throws TournamentIdNotFoundException, MatchIdNotFoundException { + Tournament tournament = tournamentRepository.findById(tournamentId) + .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); + + Match match = tournament.getMatches().stream() + .filter(m -> m.getMatchId().equals(matchId)) + .findFirst() + .orElseThrow(() -> new MatchIdNotFoundException(matchId)); + + + match.getTeamScores().put(teamId,score); + tournamentRepository.save(tournament); + log.debug("Team " + teamId + " assign to Match " + matchId + " in Tournament " + tournamentId); + } + + /* + Weitere Methoden: + Gewinner bestimmen, defineWinner() + Teams Matches zuordnen, + + */ + }