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 ca46634f7317374b65a1d8ea0c5161aa4d07020b..0185e761e1c0b6f459841365ca2225546357f8ac 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 @@ -2,6 +2,7 @@ package hdm.mi.sthbackend.controller; import hdm.mi.sthbackend.dto.PlayerDTO; +import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; import hdm.mi.sthbackend.dto.TeamDTO; import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException; @@ -67,6 +68,11 @@ public class TournamentController { @PathVariable UUID playerId) throws PlayerIdNotFoundException, TeamIdNotFoundException { return service.removePlayerFromTeam(teamId, playerId); } + @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 58ee4b45e4f68a8556a1dc84bf610281d6a345fd..757f74f57002f796e2aeba5e48b441533e6e058f 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 @@ -2,9 +2,11 @@ package hdm.mi.sthbackend.service; import hdm.mi.sthbackend.dto.PlayerDTO; import hdm.mi.sthbackend.dto.TeamDTO; +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.Match; import hdm.mi.sthbackend.model.Player; import hdm.mi.sthbackend.model.Team; import hdm.mi.sthbackend.model.Tournament; @@ -17,16 +19,13 @@ 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.Map; -import java.util.UUID; +import java.util.*; @Service @AllArgsConstructor public class TournamentService { - private final Logger logger = LogManager.getLogger(TournamentService.class); + private final static Logger log = LogManager.getLogger(TournamentService.class); ITournamentRepository tournamentRepository; IPlayerRepository playerRepository; @@ -44,7 +43,7 @@ public class TournamentService { playerRepository.insert(newPlayer); teamRepository.save(team); - + log.debug("Player " + newPlayer.getPlayerId() + " added to Team " + teamId); return newPlayer; } @@ -55,6 +54,12 @@ public class TournamentService { return new PlayerDTO(player.getPlayerId(), player.getName()); } + /* + 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 PlayerDTO removePlayerFromTeam(UUID teamId, UUID playerId) throws PlayerIdNotFoundException, TeamIdNotFoundException { Team team = teamRepository.findById(teamId) @@ -70,10 +75,16 @@ public class TournamentService { playerRepository.delete(playerToDelete); teamRepository.save(team); + log.debug("Player " + playerId + " removed from Team " + teamId); return new PlayerDTO(playerToDelete.getPlayerId(), playerToDelete.getName()); } + /* + 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 playerId, String newPlayerName) throws PlayerIdNotFoundException { Player player = playerRepository.findById(playerId) @@ -125,16 +136,59 @@ public class TournamentService { } teamRepository.delete(teamToDelete); tournamentRepository.save(tournament); + log.debug("Team " + teamId + " added to Tournament " + tournamentId); return teamId; } public Tournament createTournament(String tournamentName) { Tournament tournament = new Tournament(UUID.randomUUID(), tournamentName, new ArrayList<>(), new ArrayList<>()); tournamentRepository.insert(tournament); + log.debug("created Tournament " + tournament.getTournamentId()); return tournament; } - // determineWinner and assignTeamToMatch + 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, + + */ }