Skip to content
Snippets Groups Projects
Commit 3a04cd62 authored by Schneider Stefan's avatar Schneider Stefan
Browse files

15.01.2024 - added linkMatchesInNextRound method #24

parent 539089fe
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
...@@ -3,7 +3,6 @@ package hdm.mi.sthbackend.service; ...@@ -3,7 +3,6 @@ package hdm.mi.sthbackend.service;
import hdm.mi.sthbackend.dto.MatchDTO; import hdm.mi.sthbackend.dto.MatchDTO;
import hdm.mi.sthbackend.dto.PlayerDTO; import hdm.mi.sthbackend.dto.PlayerDTO;
import hdm.mi.sthbackend.dto.TeamDTO; import hdm.mi.sthbackend.dto.TeamDTO;
import hdm.mi.sthbackend.dto.TournamentDTO;
import hdm.mi.sthbackend.exeptions.*; import hdm.mi.sthbackend.exeptions.*;
import hdm.mi.sthbackend.mapper.ModelToDTOMapper; import hdm.mi.sthbackend.mapper.ModelToDTOMapper;
import hdm.mi.sthbackend.model.*; import hdm.mi.sthbackend.model.*;
...@@ -11,7 +10,6 @@ import hdm.mi.sthbackend.repository.IMatchRepository; ...@@ -11,7 +10,6 @@ import hdm.mi.sthbackend.repository.IMatchRepository;
import hdm.mi.sthbackend.repository.IPlayerRepository; import hdm.mi.sthbackend.repository.IPlayerRepository;
import hdm.mi.sthbackend.repository.ITeamRepository; import hdm.mi.sthbackend.repository.ITeamRepository;
import hdm.mi.sthbackend.repository.ITournamentRepository; import hdm.mi.sthbackend.repository.ITournamentRepository;
import hdm.mi.sthbackend.types.TeamMatchScore;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -263,10 +261,41 @@ public class TournamentService { ...@@ -263,10 +261,41 @@ public class TournamentService {
else{ else{
throw new BracketAlreadyInitializedException(tournamentId); throw new BracketAlreadyInitializedException(tournamentId);
} }
linkMatchesInNextRound(tournamentId);
tournamentRepository.save(tournament); tournamentRepository.save(tournament);
return 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 { public Tournament fillBracketRandom(UUID tournamentId) throws TournamentIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId) Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
...@@ -275,7 +304,7 @@ public class TournamentService { ...@@ -275,7 +304,7 @@ public class TournamentService {
Random random = new Random(); Random random = new Random();
for(int i = 0; i < 2; i++){ for(int i = 0; i < 2; i++){
for(Match match: tournament.getBracket().get(0).getMatches().values()){ for(Match match: tournament.getBracket().get(0).getMatches().values()){
if(teams.size() > 0){ if(!teams.isEmpty()){
int teamIndex = random.nextInt(0, teams.size()); int teamIndex = random.nextInt(0, teams.size());
match.getTeamScores().put(teams.get(teamIndex), 0); match.getTeamScores().put(teams.get(teamIndex), 0);
teams.remove(teamIndex); teams.remove(teamIndex);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment