1 | package org.hplr.tournament.core.usecases.service; | |
2 | ||
3 | import lombok.RequiredArgsConstructor; | |
4 | import lombok.extern.slf4j.Slf4j; | |
5 | import org.hplr.game.core.model.Game; | |
6 | import org.hplr.game.core.model.vo.GameDeployment; | |
7 | import org.hplr.game.core.model.vo.GameMission; | |
8 | import org.hplr.game.core.usecases.port.out.query.SelectAllGameDeploymentsQueryInterface; | |
9 | import org.hplr.game.core.usecases.port.out.query.SelectAllGameMissionsQueryInterface; | |
10 | import org.hplr.library.core.util.ConstValues; | |
11 | import org.hplr.library.exception.HPLRIllegalStateException; | |
12 | import org.hplr.library.exception.HPLRValidationException; | |
13 | import org.hplr.tournament.core.model.Tournament; | |
14 | import org.hplr.tournament.core.model.TournamentRound; | |
15 | import org.hplr.tournament.core.model.TournamentValidator; | |
16 | import org.hplr.tournament.core.model.dto.TournamentGameDto; | |
17 | import org.hplr.tournament.core.model.vo.TournamentPairing; | |
18 | import org.hplr.tournament.core.model.vo.TournamentPlayer; | |
19 | import org.hplr.tournament.core.usecases.port.dto.TournamentSelectDto; | |
20 | import org.hplr.tournament.core.usecases.port.in.StartTournamentUseCaseInterface; | |
21 | import org.hplr.tournament.core.usecases.port.out.command.StartTournamentCommandInterface; | |
22 | import org.hplr.tournament.core.usecases.port.out.query.SelectTournamentByTournamentIdQueryInterface; | |
23 | import org.springframework.stereotype.Service; | |
24 | ||
25 | import java.security.SecureRandom; | |
26 | import java.time.format.DateTimeFormatter; | |
27 | import java.util.*; | |
28 | ||
29 | @Service | |
30 | @RequiredArgsConstructor | |
31 | @Slf4j | |
32 | public class StartTournamentUseCaseService implements StartTournamentUseCaseInterface { | |
33 | private final SelectTournamentByTournamentIdQueryInterface selectTournamentByTournamentIdQueryInterface; | |
34 | private final SelectAllGameDeploymentsQueryInterface selectAllGameDeploymentsQueryInterface; | |
35 | private final SelectAllGameMissionsQueryInterface selectAllGameMissionsQueryInterface; | |
36 | private final StartTournamentCommandInterface startTournamentCommandInterface; | |
37 | private final Random random = new SecureRandom(); | |
38 | @Override | |
39 | public UUID startTournament(UUID tournamentId) { | |
40 | Optional<TournamentSelectDto> tournamentSelectDtoOptional | |
41 | = selectTournamentByTournamentIdQueryInterface | |
42 | .selectTournamentByTournamentId(tournamentId); | |
43 | ||
44 | Tournament tournament = Tournament.fromSelectDto(tournamentSelectDtoOptional | |
45 | .orElseThrow(NoSuchElementException::new)); | |
46 | try{ | |
47 |
1
1. startTournament : removed call to org/hplr/tournament/core/model/TournamentValidator::validateIfTournamentCanBeStarted → TIMED_OUT |
TournamentValidator.validateIfTournamentCanBeStarted(tournament); |
48 | } catch (HPLRIllegalStateException e){ | |
49 | throw new HPLRValidationException(e.getMessage()); | |
50 | } | |
51 |
1
1. startTournament : removed call to org/hplr/tournament/core/model/Tournament::setClosed → KILLED |
tournament.setClosed(true); |
52 | List<TournamentPairing> usedParings = new ArrayList<>(); | |
53 | List<TournamentRound> roundList = new ArrayList<>(); | |
54 | ||
55 |
2
1. startTournament : changed conditional boundary → TIMED_OUT 2. startTournament : negated conditional → KILLED |
for(int i=0;i<3;i++){ |
56 | TournamentRound tournamentRound = new TournamentRound(new ArrayList<>()); | |
57 | List<TournamentPairing> currentPairings = getTournamentPairings(usedParings, tournament); | |
58 | usedParings.addAll(currentPairings); | |
59 | ||
60 | List<GameMission> gameMissionList = selectAllGameMissionsQueryInterface.getAllGameMissions(); | |
61 | List<GameDeployment> gameDeploymentList = selectAllGameDeploymentsQueryInterface.getAllGameDeployments(); | |
62 |
1
1. startTournament : Replaced long multiplication with division → KILLED |
long hoursToAddToStart = (long) tournament.getTournamentData().gameLength() * i; |
63 |
2
1. startTournament : Replaced integer addition with subtraction → KILLED 2. startTournament : Replaced long multiplication with division → KILLED |
long hoursToAddToEnd = (long) tournament.getTournamentData().gameLength() * (1 + i) ; |
64 |
1
1. startTournament : removed call to java/util/List::forEach → KILLED |
currentPairings.forEach(pairing ->{ |
65 | TournamentGameDto tournamentGameDto = new TournamentGameDto( | |
66 | pairing.firstPlayer(), | |
67 | pairing.secondPlayer(), | |
68 | tournament.getTournamentData().pointSize(), | |
69 | tournament.getTournamentData().gameTurnAmount(), | |
70 | tournament.getTournamentData().tournamentStart().plusHours(hoursToAddToStart).format(DateTimeFormatter.ofPattern(ConstValues.DATE_PATTERN)), | |
71 | tournament.getTournamentData().tournamentStart().plusHours(hoursToAddToEnd).format(DateTimeFormatter.ofPattern(ConstValues.DATE_PATTERN)), | |
72 | tournament.getTournamentData().gameLength(), | |
73 | tournament.getTournamentLocation().location(), | |
74 | gameMissionList.get(random.nextInt(gameMissionList.size())), | |
75 | gameDeploymentList.get(random.nextInt(gameDeploymentList.size())) | |
76 | ); | |
77 | tournamentRound.getGameList().add(Game.fromTournamentDto(tournamentGameDto)); | |
78 | }); | |
79 | roundList.add(tournamentRound); | |
80 | } | |
81 |
1
1. startTournament : removed call to org/hplr/tournament/core/model/Tournament::setTournamentRoundList → KILLED |
tournament.setTournamentRoundList(roundList); |
82 | startTournamentCommandInterface.startTournament(tournament.toSnapshot()); | |
83 |
1
1. startTournament : replaced return value with null for org/hplr/tournament/core/usecases/service/StartTournamentUseCaseService::startTournament → KILLED |
return tournament.getTournamentId().tournamentId(); |
84 | } | |
85 | ||
86 | private static List<TournamentPairing> getTournamentPairings(List<TournamentPairing> usedParings, Tournament tournament) { | |
87 | List<TournamentPairing> currentPairings = new ArrayList<>(); | |
88 | ||
89 |
2
1. getTournamentPairings : negated conditional → TIMED_OUT 2. getTournamentPairings : negated conditional → KILLED |
while(currentPairings.isEmpty() || !Collections.disjoint(usedParings, currentPairings)){ |
90 | log.info("Reshuffling"); | |
91 |
1
1. getTournamentPairings : removed call to java/util/List::clear → TIMED_OUT |
currentPairings.clear(); |
92 | List<TournamentPlayer> tournamentPlayerList = tournament.getPlayerList(); | |
93 |
1
1. getTournamentPairings : removed call to java/util/Collections::shuffle → TIMED_OUT |
Collections.shuffle(tournamentPlayerList); |
94 |
3
1. getTournamentPairings : negated conditional → TIMED_OUT 2. getTournamentPairings : Replaced integer addition with subtraction → KILLED 3. getTournamentPairings : changed conditional boundary → KILLED |
for(int i = 0; i< tournament.getPlayerList().size(); i=i+2){ |
95 | currentPairings.add( | |
96 | new TournamentPairing( | |
97 |
1
1. getTournamentPairings : Replaced integer addition with subtraction → KILLED |
tournamentPlayerList.get(i), |
98 | tournamentPlayerList.get(i + 1) | |
99 | ) | |
100 | ); | |
101 | } | |
102 | } | |
103 |
1
1. getTournamentPairings : replaced return value with Collections.emptyList for org/hplr/tournament/core/usecases/service/StartTournamentUseCaseService::getTournamentPairings → KILLED |
return currentPairings; |
104 | } | |
105 | ||
106 | ||
107 | } | |
Mutations | ||
47 |
1.1 |
|
51 |
1.1 |
|
55 |
1.1 2.2 |
|
62 |
1.1 |
|
63 |
1.1 2.2 |
|
64 |
1.1 |
|
81 |
1.1 |
|
83 |
1.1 |
|
89 |
1.1 2.2 |
|
91 |
1.1 |
|
93 |
1.1 |
|
94 |
1.1 2.2 3.3 |
|
97 |
1.1 |
|
103 |
1.1 |