1 | package org.hplr.tournament.infrastructure.dbadapter.adapters; | |
2 | ||
3 | import lombok.AllArgsConstructor; | |
4 | import org.hplr.game.core.model.GameSide; | |
5 | import org.hplr.game.core.model.vo.GameSidePlayerData; | |
6 | import org.hplr.game.core.model.vo.GameSideSnapshot; | |
7 | import org.hplr.game.infrastructure.dbadapter.adapters.GameCommandAdapter; | |
8 | import org.hplr.game.infrastructure.dbadapter.entities.*; | |
9 | import org.hplr.game.infrastructure.dbadapter.mappers.GameDatabaseMapper; | |
10 | import org.hplr.game.infrastructure.dbadapter.mappers.GameSideDatabaseMapper; | |
11 | import org.hplr.game.infrastructure.dbadapter.repositories.GameArmyTypeRepository; | |
12 | import org.hplr.game.infrastructure.dbadapter.repositories.GameDeploymentRepository; | |
13 | import org.hplr.game.infrastructure.dbadapter.repositories.GameMissionRepository; | |
14 | import org.hplr.library.core.util.ConstValues; | |
15 | import org.hplr.library.exception.HPLRIllegalStateException; | |
16 | import org.hplr.location.infrastructure.dbadapter.entities.LocationEntity; | |
17 | import org.hplr.location.infrastructure.dbadapter.mappers.LocationMapper; | |
18 | import org.hplr.location.infrastructure.dbadapter.repositories.LocationRepository; | |
19 | import org.hplr.tournament.core.model.TournamentSnapshot; | |
20 | import org.hplr.tournament.core.model.vo.TournamentPlayer; | |
21 | import org.hplr.tournament.core.usecases.port.out.command.SaveTournamentCommandInterface; | |
22 | import org.hplr.tournament.core.usecases.port.out.command.StartTournamentCommandInterface; | |
23 | import org.hplr.tournament.core.usecases.port.out.command.UpdateTournamentQueryInterface; | |
24 | import org.hplr.tournament.infrastructure.dbadapter.entities.TournamentEntity; | |
25 | import org.hplr.tournament.infrastructure.dbadapter.mappers.TournamentDatabaseMapper; | |
26 | import org.hplr.tournament.infrastructure.dbadapter.repositories.TournamentRepository; | |
27 | import org.hplr.user.infrastructure.dbadapter.entities.PlayerEntity; | |
28 | import org.hplr.user.infrastructure.dbadapter.repositories.PlayerQueryRepository; | |
29 | import org.springframework.stereotype.Service; | |
30 | ||
31 | import java.util.*; | |
32 | ||
33 | import static org.hplr.game.infrastructure.dbadapter.mappers.GameDatabaseMapper.mapScore; | |
34 | ||
35 | @Service | |
36 | @AllArgsConstructor | |
37 | public class TournamentCommandAdapter implements SaveTournamentCommandInterface, | |
38 | UpdateTournamentQueryInterface, StartTournamentCommandInterface { | |
39 | ||
40 | private final PlayerQueryRepository playerQueryRepository; | |
41 | private final GameArmyTypeRepository gameArmyTypeRepository; | |
42 | private final TournamentRepository tournamentRepository; | |
43 | private final LocationRepository locationRepository; | |
44 | private final GameMissionRepository gameMissionRepository; | |
45 | private final GameDeploymentRepository gameDeploymentRepository; | |
46 | ||
47 | @Override | |
48 | public void saveTournament(TournamentSnapshot tournamentSnapshot) { | |
49 | TournamentEntity tournamentEntity = TournamentDatabaseMapper.fromSnapshot(tournamentSnapshot); | |
50 | tournamentRepository.save(tournamentEntity); | |
51 | } | |
52 | ||
53 | @Override | |
54 | public void updateTournament(TournamentSnapshot tournamentSnapshot) { | |
55 | Optional<TournamentEntity> tournamentEntityOptional = tournamentRepository.findByTournamentId( | |
56 | tournamentSnapshot.tournamentId().tournamentId()); | |
57 |
1
1. updateTournament : negated conditional → KILLED |
if(tournamentEntityOptional.isPresent()){ |
58 |
1
1. updateTournament : negated conditional → KILLED |
if(Objects.equals(tournamentEntityOptional.get().getCurrentPlayers(), tournamentEntityOptional.get().getMaxPlayers())){ |
59 | throw new HPLRIllegalStateException("Player limit achieved"); | |
60 | } | |
61 | List<GameSidePlayerData> gameSidePlayerDataList = new ArrayList<>(); | |
62 | List<GameSideEntity> gameSideEntityList = new ArrayList<>(); | |
63 |
1
1. updateTournament : removed call to java/util/List::forEach → KILLED |
tournamentSnapshot.playerList().forEach( |
64 |
1
1. lambda$updateTournament$0 : removed call to org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::addToGameSideEntityList → KILLED |
player -> addToGameSideEntityList(player, gameSidePlayerDataList, gameSideEntityList) |
65 | ); | |
66 | ||
67 | TournamentEntity tournamentEntity = tournamentEntityOptional.get(); | |
68 |
1
1. updateTournament : removed call to org/hplr/tournament/infrastructure/dbadapter/entities/TournamentEntity::setGameSideEntityList → KILLED |
tournamentEntity.setGameSideEntityList(gameSideEntityList); |
69 | ||
70 | tournamentRepository.save(tournamentEntity); | |
71 | ||
72 | } | |
73 | } | |
74 | ||
75 | @Override | |
76 | public UUID startTournament(TournamentSnapshot tournamentSnapshot) { | |
77 | List<PlayerEntity> allPlayerEntityList = playerQueryRepository.findAll(); | |
78 |
1
1. startTournament : negated conditional → KILLED |
if (allPlayerEntityList.isEmpty()) { |
79 | throw new HPLRIllegalStateException("Not enough players!"); | |
80 | } | |
81 | List<GameArmyTypeEntity> armyTypeEntityList = gameArmyTypeRepository.findAll(); | |
82 |
1
1. startTournament : negated conditional → KILLED |
if (armyTypeEntityList.isEmpty()) { |
83 | throw new HPLRIllegalStateException("No army types!"); | |
84 | } | |
85 | Optional<TournamentEntity> tournamentEntityOptional = tournamentRepository | |
86 | .findByTournamentId(tournamentSnapshot.tournamentId().tournamentId()); | |
87 | TournamentEntity tournamentEntityFromSnapshot = TournamentDatabaseMapper.fromSnapshot(tournamentSnapshot); | |
88 |
1
1. startTournament : removed call to org/hplr/tournament/infrastructure/dbadapter/entities/TournamentEntity::setId → KILLED |
tournamentEntityFromSnapshot.setId(tournamentEntityOptional.orElseThrow(NoSuchElementException::new).getId()); |
89 | List<GameSidePlayerData> gameSidePlayerDataList = new ArrayList<>(); | |
90 | List<GameSideEntity> gameSideEntityList = new ArrayList<>(); | |
91 |
1
1. startTournament : removed call to java/util/List::forEach → KILLED |
tournamentSnapshot.playerList().forEach( |
92 |
1
1. lambda$startTournament$1 : removed call to org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::addToGameSideEntityList → KILLED |
player -> addToGameSideEntityList(player, gameSidePlayerDataList, gameSideEntityList) |
93 | ); | |
94 |
1
1. startTournament : removed call to org/hplr/tournament/infrastructure/dbadapter/entities/TournamentEntity::setGameSideEntityList → KILLED |
tournamentEntityFromSnapshot.setGameSideEntityList(gameSideEntityList); |
95 |
1
1. startTournament : removed call to java/util/List::forEach → KILLED |
tournamentEntityFromSnapshot.getTournamentRoundEntityList().forEach(tournamentRoundEntity -> |
96 |
1
1. lambda$startTournament$6 : removed call to java/util/List::forEach → KILLED |
tournamentRoundEntity.getGameSnapshotList().forEach(gameSnapshot -> { |
97 | LocationEntity locationEntity = null; | |
98 |
1
1. lambda$startTournament$5 : negated conditional → KILLED |
if (Objects.nonNull(gameSnapshot.gameLocation().location().getLocationId())) { |
99 | Optional<LocationEntity> locationEntityOptional = locationRepository.findByLocationId(gameSnapshot.gameLocation().location().getLocationId().locationId()); | |
100 |
1
1. lambda$startTournament$2 : replaced return value with null for org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::lambda$startTournament$2 → KILLED |
locationEntity = locationEntityOptional.orElseGet(() -> LocationMapper.fromSnapshot(gameSnapshot.gameLocation().location().toSnapshot())); |
101 | } | |
102 | GameMissionEntity gameMissionEntity; | |
103 | Optional<GameMissionEntity> gameMissionEntityOptional = gameMissionRepository.findByName(gameSnapshot.gameData().gameMission().name()); | |
104 |
1
1. lambda$startTournament$3 : replaced return value with null for org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::lambda$startTournament$3 → KILLED |
gameMissionEntity = gameMissionEntityOptional.orElseGet(() -> new GameMissionEntity( |
105 | null, | |
106 | gameSnapshot.gameData().gameMission().name() | |
107 | )); | |
108 | GameDeploymentEntity gameDeploymentEntity; | |
109 | Optional<GameDeploymentEntity> gameDeploymentEntityOptional = gameDeploymentRepository.findByName(gameSnapshot.gameData().gameDeployment().name()); | |
110 |
1
1. lambda$startTournament$4 : replaced return value with null for org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::lambda$startTournament$4 → KILLED |
gameDeploymentEntity = gameDeploymentEntityOptional.orElseGet(() -> new GameDeploymentEntity( |
111 | null, | |
112 | gameSnapshot.gameData().gameDeployment().name() | |
113 | )); | |
114 | GameEntity gameEntity = GameDatabaseMapper.fromSnapshot(gameSnapshot); | |
115 |
1
1. lambda$startTournament$5 : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setLocationEntity → KILLED |
gameEntity.setLocationEntity(locationEntity); |
116 |
1
1. lambda$startTournament$5 : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setGameMissionEntity → KILLED |
gameEntity.setGameMissionEntity(gameMissionEntity); |
117 |
1
1. lambda$startTournament$5 : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setGameDeploymentEntity → KILLED |
gameEntity.setGameDeploymentEntity(gameDeploymentEntity); |
118 |
1
1. lambda$startTournament$5 : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setFirstGameSide → KILLED |
gameEntity.setFirstGameSide(new GameSideEntity( |
119 | null, | |
120 | gameSnapshot.firstGameSide().sideId().sideId(), | |
121 | gameSnapshot.firstGameSide().allegiance(), | |
122 | mapGamePlayerDataEntityList(gameSnapshot.firstGameSide(), allPlayerEntityList, armyTypeEntityList), | |
123 | gameSnapshot.firstGameSide().isFirst(), | |
124 | mapScore(gameSnapshot.firstGameSide()))); | |
125 |
1
1. lambda$startTournament$5 : negated conditional → KILLED |
if(Objects.nonNull(gameSnapshot.secondGameSide())){ |
126 |
1
1. lambda$startTournament$5 : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setSecondGameSide → KILLED |
gameEntity.setSecondGameSide(new GameSideEntity( |
127 | null, | |
128 | gameSnapshot.secondGameSide().sideId().sideId(), | |
129 | gameSnapshot.secondGameSide().allegiance(), | |
130 | mapGamePlayerDataEntityList(gameSnapshot.secondGameSide(), allPlayerEntityList, armyTypeEntityList), | |
131 | gameSnapshot.secondGameSide().isFirst(), | |
132 | mapScore(gameSnapshot.secondGameSide()))); | |
133 | } | |
134 | tournamentRoundEntity.getGameEntityList().add(gameEntity); | |
135 | } | |
136 | )); | |
137 | tournamentRepository.save(tournamentEntityFromSnapshot); | |
138 |
1
1. startTournament : replaced return value with null for org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::startTournament → KILLED |
return tournamentEntityFromSnapshot.getTournamentId(); |
139 | } | |
140 | ||
141 | public static List<GamePlayerDataEntity> mapGamePlayerDataEntityList(GameSideSnapshot gameSide, List<PlayerEntity> playerEntityList, List<GameArmyTypeEntity> gameArmyTypeEntityList) { | |
142 |
1
1. mapGamePlayerDataEntityList : replaced return value with Collections.emptyList for org/hplr/tournament/infrastructure/dbadapter/adapters/TournamentCommandAdapter::mapGamePlayerDataEntityList → SURVIVED |
return GameCommandAdapter.mapGamePlayerDataEntityList(gameSide, playerEntityList, gameArmyTypeEntityList); |
143 | } | |
144 | ||
145 | private void addToGameSideEntityList(TournamentPlayer player, List<GameSidePlayerData> gameSidePlayerDataList, List<GameSideEntity> gameSideEntityList){ | |
146 | List<GamePlayerDataEntity> gamePlayerDataEntityList = new ArrayList<>(); | |
147 | List<GameArmyEntity> allyArmyEntityList = new ArrayList<>(); | |
148 | ||
149 | gameSidePlayerDataList.add(player.gameSidePlayerData()); | |
150 | GameSideEntity gameSideEntity = GameSideDatabaseMapper.fromSnapshot(new GameSideSnapshot(GameSide.fromDto( | |
151 | player.allegiance(), | |
152 | gameSidePlayerDataList, | |
153 | ConstValues.TURN_LENGTH | |
154 | ||
155 | ))); | |
156 | ||
157 | ||
158 |
1
1. addToGameSideEntityList : removed call to java/util/List::forEach → KILLED |
player.gameSidePlayerData().allyArmyList().forEach(army -> allyArmyEntityList.add( |
159 | new GameArmyEntity( | |
160 | null, | |
161 | gameArmyTypeRepository.findByName(army.army().name()) | |
162 | .orElseThrow(NoSuchElementException::new), | |
163 | army.name(), | |
164 | army.pointValue() | |
165 | ) | |
166 | ||
167 | )); | |
168 | gamePlayerDataEntityList.add(new GamePlayerDataEntity( | |
169 | null, | |
170 | playerQueryRepository.findByUserId(player.gameSidePlayerData().player().getUserId().id()).orElseThrow(NoSuchElementException::new), | |
171 | new GameArmyEntity( | |
172 | null, | |
173 | gameArmyTypeRepository.findByName(player.gameSidePlayerData().armyPrimary().army().name()).orElseThrow(NoSuchElementException::new), | |
174 | player.gameSidePlayerData().armyPrimary().name(), | |
175 | player.gameSidePlayerData().armyPrimary().pointValue() | |
176 | ||
177 | ), | |
178 | allyArmyEntityList | |
179 | )); | |
180 |
1
1. addToGameSideEntityList : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameSideEntity::setGamePlayerDataEntityList → KILLED |
gameSideEntity.setGamePlayerDataEntityList(gamePlayerDataEntityList); |
181 | gameSideEntityList.add(gameSideEntity); | |
182 | } | |
183 | } | |
Mutations | ||
57 |
1.1 |
|
58 |
1.1 |
|
63 |
1.1 |
|
64 |
1.1 |
|
68 |
1.1 |
|
78 |
1.1 |
|
82 |
1.1 |
|
88 |
1.1 |
|
91 |
1.1 |
|
92 |
1.1 |
|
94 |
1.1 |
|
95 |
1.1 |
|
96 |
1.1 |
|
98 |
1.1 |
|
100 |
1.1 |
|
104 |
1.1 |
|
110 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
117 |
1.1 |
|
118 |
1.1 |
|
125 |
1.1 |
|
126 |
1.1 |
|
138 |
1.1 |
|
142 |
1.1 |
|
158 |
1.1 |
|
180 |
1.1 |