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 fadff0303c71dcdc2ff2b86f30a00b7de6821e2c..c044605410f5ee5bce6cbb8f9de24c1966ab18a1 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 @@ -3,7 +3,6 @@ package hdm.mi.sthbackend.service; import hdm.mi.sthbackend.dto.MatchDTO; import hdm.mi.sthbackend.dto.PlayerDTO; import hdm.mi.sthbackend.dto.TeamDTO; -import hdm.mi.sthbackend.dto.TournamentDTO; import hdm.mi.sthbackend.exeptions.*; import hdm.mi.sthbackend.mapper.ModelToDTOMapper; import hdm.mi.sthbackend.model.*; @@ -11,7 +10,6 @@ import hdm.mi.sthbackend.repository.IMatchRepository; import hdm.mi.sthbackend.repository.IPlayerRepository; import hdm.mi.sthbackend.repository.ITeamRepository; import hdm.mi.sthbackend.repository.ITournamentRepository; -import hdm.mi.sthbackend.types.TeamMatchScore; import lombok.AllArgsConstructor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -263,10 +261,41 @@ public class TournamentService { else{ throw new BracketAlreadyInitializedException(tournamentId); } + + linkMatchesInNextRound(tournamentId); + tournamentRepository.save(tournament); return tournament; } + public void linkMatchesInNextRound(UUID tournamentId)throws TournamentIdNotFoundException { + Tournament tournament = tournamentRepository.findById(tournamentId) + .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); + + int timesUsed = 0; + Map<UUID, Match> nextRoundMatches; + + for (int i = 0; i < tournament.getBracket().size() - 1; i++) { + nextRoundMatches = tournament.getBracket().get(i + 1).getMatches(); + List<UUID> nextRoundMatchIds = nextRoundMatches.keySet().stream().toList(); + + for (Map.Entry<UUID, Match> entry : tournament.getBracket().get(i).getMatches().entrySet()) { + UUID nextMatch = nextRoundMatchIds.get(0); + entry.getValue().setNextMatchId(nextMatch); + + if (timesUsed > 0) { + nextRoundMatchIds.removeFirst(); + timesUsed = 0; + } else { + timesUsed += 1; + } + + } + } + tournamentRepository.save(tournament); + } + + public Tournament fillBracketRandom(UUID tournamentId) throws TournamentIdNotFoundException { Tournament tournament = tournamentRepository.findById(tournamentId) .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); @@ -275,7 +304,7 @@ public class TournamentService { Random random = new Random(); for(int i = 0; i < 2; i++){ for(Match match: tournament.getBracket().get(0).getMatches().values()){ - if(teams.size() > 0){ + if(!teams.isEmpty()){ int teamIndex = random.nextInt(0, teams.size()); match.getTeamScores().put(teams.get(teamIndex), 0); teams.remove(teamIndex);