diff --git a/sth-backend/pom.xml b/sth-backend/pom.xml index 539b22fe07c9d786494a8053c653dd92db1b1414..a8a7dfccec988293fe8b2be0e07959e229ea8d7e 100644 --- a/sth-backend/pom.xml +++ b/sth-backend/pom.xml @@ -20,6 +20,7 @@ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> + <version>3.2.1</version> </dependency> <dependency> <groupId>org.mockito</groupId> @@ -71,8 +72,12 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> - - </dependencies> + <dependency> + <groupId>org.springdoc</groupId> + <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> + <version>2.3.0</version> + </dependency> + </dependencies> <build> <plugins> 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 8b69b4372e70902b405299cc1c092e636d942dfa..9f8bda9ceffe65476e529662e6fa5be639835c35 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 @@ -261,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)); @@ -273,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).getTeamId(), 0); teams.remove(teamIndex);