From e84e61f3319248e64af6aaf85ec4e42cb3f0281f Mon Sep 17 00:00:00 2001 From: msiStefan <ss576@hdm-stuttgart.de> Date: Wed, 27 Dec 2023 19:07:08 +0100 Subject: [PATCH] added determineWinner Methode zu TournamentService #24 --- .../sthbackend/service/TournamentService.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) 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 0482189..de74213 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,9 +1,11 @@ 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.Match; import hdm.mi.sthbackend.model.Player; import hdm.mi.sthbackend.model.Team; import hdm.mi.sthbackend.model.Tournament; @@ -12,12 +14,11 @@ import hdm.mi.sthbackend.repository.MongoManager; import lombok.AllArgsConstructor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.data.mongodb.core.aggregation.VariableOperators; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Optional; -import java.util.UUID; +import javax.swing.text.html.parser.Entity; +import java.util.*; @Service @AllArgsConstructor @@ -134,6 +135,26 @@ public class TournamentService { 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; + } /* Weitere Methoden: -- GitLab