From 3a04cd62f6c436ec7e1e1a51574698db7d73769e Mon Sep 17 00:00:00 2001
From: msiStefan <ss576@hdm-stuttgart.de>
Date: Mon, 15 Jan 2024 16:04:08 +0100
Subject: [PATCH] 15.01.2024 - added  linkMatchesInNextRound method #24

---
 .../sthbackend/service/TournamentService.java | 35 +++++++++++++++++--
 1 file changed, 32 insertions(+), 3 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 fadff03..c044605 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);
-- 
GitLab