| 1 | package org.hplr.game.infrastructure.dbadapter.adapters; | |
| 2 | ||
| 3 | import lombok.extern.slf4j.Slf4j; | |
| 4 | import org.hplr.game.core.model.GameSnapshot; | |
| 5 | import org.hplr.game.core.model.vo.GameHistoricalElo; | |
| 6 | import org.hplr.game.core.model.vo.GameSidePlayerDataSnapshot; | |
| 7 | import org.hplr.game.core.model.vo.GameSideSnapshot; | |
| 8 | import org.hplr.game.core.usecases.port.out.command.*; | |
| 9 | import org.hplr.game.infrastructure.dbadapter.entities.*; | |
| 10 | ||
| 11 | import org.hplr.location.infrastructure.dbadapter.entities.LocationEntity; | |
| 12 | import org.hplr.location.infrastructure.dbadapter.mappers.LocationMapper; | |
| 13 | ||
| 14 | import org.hplr.game.infrastructure.dbadapter.mappers.GameDatabaseMapper; | |
| 15 | import org.hplr.game.infrastructure.dbadapter.repositories.*; | |
| 16 | ||
| 17 | import org.hplr.library.exception.HPLRIllegalStateException; | |
| 18 | ||
| 19 | import org.hplr.location.infrastructure.dbadapter.repositories.LocationRepository; | |
| 20 | import org.hplr.user.infrastructure.dbadapter.entities.PlayerEntity; | |
| 21 | import org.hplr.user.infrastructure.dbadapter.repositories.PlayerQueryRepository; | |
| 22 | import org.springframework.stereotype.Service; | |
| 23 | ||
| 24 | import java.util.*; | |
| 25 | import java.util.concurrent.atomic.AtomicReference; | |
| 26 | ||
| 27 | import static org.hplr.game.infrastructure.dbadapter.mappers.GameDatabaseMapper.mapScore; | |
| 28 | ||
| 29 | @Slf4j | |
| 30 | @Service | |
| 31 | public class GameCommandAdapter implements SaveGameCommandInterface, | |
| 32 | StartAllDueGamesCommandInterface, | |
| 33 | SaveFinishedGameCommandInterface, | |
| 34 | StartGameCommandInterface { | |
| 35 | ||
| 36 | final LocationRepository locationRepository; | |
| 37 | final PlayerQueryRepository playerQueryRepository; | |
| 38 | final GameRepository gameRepository; | |
| 39 | final GameMissionRepository gameMissionRepository; | |
| 40 | final GameDeploymentRepository gameDeploymentRepository; | |
| 41 | final GameArmyTypeRepository gameArmyTypeRepository; | |
| 42 | final GameHistoricalEloRepository gameHistoricalEloRepository; | |
| 43 | ||
| 44 | public GameCommandAdapter(LocationRepository locationRepository, PlayerQueryRepository playerQueryRepository, GameRepository gameRepository, GameMissionRepository gameMissionRepository, GameDeploymentRepository gameDeploymentRepository, GameArmyTypeRepository gameArmyTypeRepository, | |
| 45 | GameHistoricalEloRepository gameHistoricalEloRepository) { | |
| 46 | this.locationRepository = locationRepository; | |
| 47 | this.playerQueryRepository = playerQueryRepository; | |
| 48 | this.gameRepository = gameRepository; | |
| 49 | this.gameMissionRepository = gameMissionRepository; | |
| 50 | this.gameDeploymentRepository = gameDeploymentRepository; | |
| 51 | this.gameArmyTypeRepository = gameArmyTypeRepository; | |
| 52 | this.gameHistoricalEloRepository = gameHistoricalEloRepository; | |
| 53 | } | |
| 54 | ||
| 55 | ||
| 56 | @Override | |
| 57 | public void saveGame(GameSnapshot gameSnapshot) { | |
| 58 | List<PlayerEntity> allPlayerEntityList = playerQueryRepository.findAll(); | |
| 59 |
1
1. saveGame : negated conditional → KILLED |
if (allPlayerEntityList.isEmpty()) { |
| 60 | throw new HPLRIllegalStateException("Not enough players!"); | |
| 61 | } | |
| 62 | List<GameArmyTypeEntity> armyTypeEntityList = gameArmyTypeRepository.findAll(); | |
| 63 |
1
1. saveGame : negated conditional → KILLED |
if (armyTypeEntityList.isEmpty()) { |
| 64 | throw new HPLRIllegalStateException("No army types!"); | |
| 65 | } | |
| 66 | LocationEntity locationEntity = null; | |
| 67 |
1
1. saveGame : negated conditional → KILLED |
if (Objects.nonNull(gameSnapshot.gameLocation().location().getLocationId())) { |
| 68 | Optional<LocationEntity> locationEntityOptional = locationRepository.findByLocationId(gameSnapshot.gameLocation().location().getLocationId().locationId()); | |
| 69 |
1
1. lambda$saveGame$0 : replaced return value with null for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$saveGame$0 → KILLED |
locationEntity = locationEntityOptional.orElseGet(() -> LocationMapper.fromSnapshot(gameSnapshot.gameLocation().location().toSnapshot())); |
| 70 | } | |
| 71 | GameMissionEntity gameMissionEntity; | |
| 72 | Optional<GameMissionEntity> gameMissionEntityOptional = gameMissionRepository.findByName(gameSnapshot.gameData().gameMission().name()); | |
| 73 |
1
1. lambda$saveGame$1 : replaced return value with null for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$saveGame$1 → KILLED |
gameMissionEntity = gameMissionEntityOptional.orElseGet(() -> new GameMissionEntity( |
| 74 | null, | |
| 75 | gameSnapshot.gameData().gameMission().name() | |
| 76 | )); | |
| 77 | GameDeploymentEntity gameDeploymentEntity; | |
| 78 | Optional<GameDeploymentEntity> gameDeploymentEntityOptional = gameDeploymentRepository.findByName(gameSnapshot.gameData().gameDeployment().name()); | |
| 79 |
1
1. lambda$saveGame$2 : replaced return value with null for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$saveGame$2 → KILLED |
gameDeploymentEntity = gameDeploymentEntityOptional.orElseGet(() -> new GameDeploymentEntity( |
| 80 | null, | |
| 81 | gameSnapshot.gameData().gameDeployment().name() | |
| 82 | )); | |
| 83 | GameEntity gameEntity = GameDatabaseMapper.fromSnapshot(gameSnapshot); | |
| 84 |
1
1. saveGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setLocationEntity → KILLED |
gameEntity.setLocationEntity(locationEntity); |
| 85 |
1
1. saveGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setGameMissionEntity → KILLED |
gameEntity.setGameMissionEntity(gameMissionEntity); |
| 86 |
1
1. saveGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setGameDeploymentEntity → KILLED |
gameEntity.setGameDeploymentEntity(gameDeploymentEntity); |
| 87 |
1
1. saveGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setFirstGameSide → KILLED |
gameEntity.setFirstGameSide(new GameSideEntity( |
| 88 | null, | |
| 89 | gameSnapshot.firstGameSide().sideId().sideId(), | |
| 90 | gameSnapshot.firstGameSide().allegiance(), | |
| 91 | mapGamePlayerDataEntityList(gameSnapshot.firstGameSide(), allPlayerEntityList, armyTypeEntityList), | |
| 92 | gameSnapshot.firstGameSide().isFirst(), | |
| 93 | mapScore(gameSnapshot.firstGameSide()))); | |
| 94 |
1
1. saveGame : negated conditional → KILLED |
if(Objects.nonNull(gameSnapshot.secondGameSide())){ |
| 95 |
1
1. saveGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setSecondGameSide → KILLED |
gameEntity.setSecondGameSide(new GameSideEntity( |
| 96 | null, | |
| 97 | gameSnapshot.secondGameSide().sideId().sideId(), | |
| 98 | gameSnapshot.secondGameSide().allegiance(), | |
| 99 | mapGamePlayerDataEntityList(gameSnapshot.secondGameSide(), allPlayerEntityList, armyTypeEntityList), | |
| 100 | gameSnapshot.secondGameSide().isFirst(), | |
| 101 | mapScore(gameSnapshot.secondGameSide()))); | |
| 102 | } | |
| 103 | gameRepository.save(gameEntity); | |
| 104 | } | |
| 105 | ||
| 106 | @Override | |
| 107 | public void startAllDueGames(List<GameSnapshot> gameToStartList) { | |
| 108 | List<UUID> gameToStartIdList = gameToStartList | |
| 109 | .stream() | |
| 110 |
1
1. lambda$startAllDueGames$3 : replaced return value with null for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$startAllDueGames$3 → KILLED |
.map(game -> game.gameId().gameId()) |
| 111 | .toList(); | |
| 112 |
1
1. startAllDueGames : removed call to java/util/List::forEach → SURVIVED |
gameToStartIdList.forEach( |
| 113 | id -> log.info("Games started: {}",id) | |
| 114 | ); | |
| 115 |
1
1. startAllDueGames : negated conditional → KILLED |
if(!gameToStartIdList.isEmpty()){ |
| 116 |
1
1. startAllDueGames : removed call to org/hplr/game/infrastructure/dbadapter/repositories/GameRepository::startAllDueGames → KILLED |
gameRepository.startAllDueGames(gameToStartIdList); |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | @Override | |
| 121 | public List<Integer> saveFinishedGame(GameSnapshot gameSnapshot, GameHistoricalElo gameHistoricalElo) { | |
| 122 | AtomicReference<Integer> firstUpdated = new AtomicReference<>(0); | |
| 123 | AtomicReference<Integer> secondUpdated = new AtomicReference<>(0); | |
| 124 |
1
1. lambda$saveFinishedGame$5 : replaced return value with null for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$saveFinishedGame$5 → KILLED |
GameEntity gameEntity = gameRepository.findByGameId(gameSnapshot.gameId().gameId()).orElseThrow(() -> new NoSuchElementException("Game not found!")); |
| 125 | ||
| 126 |
1
1. saveFinishedGame : removed call to org/hplr/game/infrastructure/dbadapter/entities/GameEntity::setStatus → KILLED |
gameEntity.setStatus(gameSnapshot.gameStatus()); |
| 127 |
1
1. saveFinishedGame : removed call to java/util/List::forEach → KILLED |
gameEntity.getFirstGameSide().getGamePlayerDataEntityList().forEach(player -> { |
| 128 |
1
1. lambda$saveFinishedGame$6 : negated conditional → KILLED |
if(Boolean.TRUE.equals(updateScore(player, gameSnapshot, true))){ |
| 129 |
1
1. lambda$saveFinishedGame$6 : Replaced integer addition with subtraction → KILLED |
firstUpdated.getAndSet(firstUpdated.get() + 1); |
| 130 | } | |
| 131 | else{ | |
| 132 | log.warn("Player score not updated: "+player.getPlayerEntity().getUserId()); | |
| 133 | } | |
| 134 | }); | |
| 135 |
2
1. saveFinishedGame : negated conditional → SURVIVED 2. saveFinishedGame : changed conditional boundary → SURVIVED |
if(firstUpdated.get()<gameEntity.getFirstGameSide().getGamePlayerDataEntityList().size()){ |
| 136 | log.warn("Not all players scores updated"); | |
| 137 | } | |
| 138 | ||
| 139 |
1
1. saveFinishedGame : removed call to java/util/List::forEach → KILLED |
gameEntity.getSecondGameSide().getGamePlayerDataEntityList().forEach(player -> { |
| 140 |
1
1. lambda$saveFinishedGame$7 : negated conditional → KILLED |
if (Boolean.TRUE.equals(updateScore(player, gameSnapshot, false))) { |
| 141 |
1
1. lambda$saveFinishedGame$7 : Replaced integer addition with subtraction → KILLED |
secondUpdated.getAndSet(secondUpdated.get() + 1); |
| 142 | } else { | |
| 143 | log.warn("Player score not updated: " + player.getPlayerEntity().getUserId()); | |
| 144 | } | |
| 145 | }); | |
| 146 |
2
1. saveFinishedGame : changed conditional boundary → SURVIVED 2. saveFinishedGame : negated conditional → SURVIVED |
if(secondUpdated.get()<gameEntity.getSecondGameSide().getGamePlayerDataEntityList().size()){ |
| 147 | log.warn("Not all players scores updated"); | |
| 148 | } | |
| 149 | gameRepository.save(gameEntity); | |
| 150 | GameHistoricalEloEntity gameHistoricalEloEntity = new GameHistoricalEloEntity( | |
| 151 | null, | |
| 152 | gameEntity, | |
| 153 | gameHistoricalElo.firstSideElo().ELOValue(), | |
| 154 | gameHistoricalElo.secondSideElo().ELOValue() | |
| 155 | ); | |
| 156 | gameHistoricalEloRepository.save(gameHistoricalEloEntity); | |
| 157 |
1
1. saveFinishedGame : replaced return value with Collections.emptyList for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::saveFinishedGame → KILLED |
return List.of(firstUpdated.get(), secondUpdated.get()); |
| 158 | } | |
| 159 | ||
| 160 | @Override | |
| 161 | public void startGame(GameSnapshot gameSnapshot) { | |
| 162 |
1
1. startGame : removed call to org/hplr/game/infrastructure/dbadapter/repositories/GameRepository::startGame → KILLED |
gameRepository.startGame(gameSnapshot.gameId().gameId()); |
| 163 | log.info("Game started: {}",gameSnapshot.gameId().gameId()); | |
| 164 | } | |
| 165 | ||
| 166 | ||
| 167 | public static List<GamePlayerDataEntity> mapGamePlayerDataEntityList(GameSideSnapshot gameSide, List<PlayerEntity> playerEntityList, List<GameArmyTypeEntity> gameArmyTypeEntityList) { | |
| 168 | List<GamePlayerDataEntity> gamePlayerDataEntityList = new ArrayList<>(); | |
| 169 |
1
1. mapGamePlayerDataEntityList : removed call to java/util/List::forEach → KILLED |
gameSide.gameSidePlayerDataList().forEach(gameSidePlayerData -> |
| 170 | { | |
| 171 |
2
1. lambda$mapGamePlayerDataEntityList$8 : replaced boolean return with true for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$8 → SURVIVED 2. lambda$mapGamePlayerDataEntityList$8 : replaced boolean return with false for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$8 → KILLED |
PlayerEntity playerEntity = playerEntityList.stream().filter(playerEntity1 -> playerEntity1.getUserId().equals(gameSidePlayerData.player().userId().id())).findFirst().orElseThrow(NoSuchElementException::new); |
| 172 |
2
1. lambda$mapGamePlayerDataEntityList$9 : replaced boolean return with true for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$9 → SURVIVED 2. lambda$mapGamePlayerDataEntityList$9 : replaced boolean return with false for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$9 → KILLED |
GameArmyTypeEntity primaryGameArmyTypeEntity = gameArmyTypeEntityList.stream().filter(gameArmyTypeEntity -> gameArmyTypeEntity.getName().equals(gameSidePlayerData.armyPrimary().army().name())).findFirst().orElseThrow(NoSuchElementException::new); |
| 173 | List<GameArmyEntity> allyArmyEntityList; | |
| 174 |
1
1. lambda$mapGamePlayerDataEntityList$12 : negated conditional → KILLED |
if (Objects.nonNull(gameSidePlayerData.allyArmyList())) { |
| 175 | allyArmyEntityList = new ArrayList<>(); | |
| 176 |
1
1. lambda$mapGamePlayerDataEntityList$12 : removed call to java/util/List::forEach → SURVIVED |
gameSidePlayerData.allyArmyList().forEach(allyArmy -> { |
| 177 |
2
1. lambda$mapGamePlayerDataEntityList$10 : replaced boolean return with false for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$10 → NO_COVERAGE 2. lambda$mapGamePlayerDataEntityList$10 : replaced boolean return with true for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$mapGamePlayerDataEntityList$10 → NO_COVERAGE |
GameArmyTypeEntity allyArmyTypeEntity = gameArmyTypeEntityList.stream().filter(gameArmyTypeEntity -> gameArmyTypeEntity.getName().equals(allyArmy.name())).findFirst().orElseThrow(NoSuchElementException::new); |
| 178 | allyArmyEntityList.add(new GameArmyEntity( | |
| 179 | null, | |
| 180 | allyArmyTypeEntity, | |
| 181 | allyArmy.name(), | |
| 182 | allyArmy.pointValue() | |
| 183 | ||
| 184 | )); | |
| 185 | }); | |
| 186 | } else { | |
| 187 | allyArmyEntityList = null; | |
| 188 | } | |
| 189 | gamePlayerDataEntityList.add( | |
| 190 | new GamePlayerDataEntity( | |
| 191 | null, | |
| 192 | playerEntity, | |
| 193 | new GameArmyEntity( | |
| 194 | null, | |
| 195 | primaryGameArmyTypeEntity, | |
| 196 | gameSidePlayerData.armyPrimary().name(), | |
| 197 | gameSidePlayerData.armyPrimary().pointValue() | |
| 198 | ), | |
| 199 | allyArmyEntityList | |
| 200 | ||
| 201 | ) | |
| 202 | ); | |
| 203 | } | |
| 204 | ); | |
| 205 |
1
1. mapGamePlayerDataEntityList : replaced return value with Collections.emptyList for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::mapGamePlayerDataEntityList → KILLED |
return gamePlayerDataEntityList; |
| 206 | } | |
| 207 | ||
| 208 | Boolean updateScore(GamePlayerDataEntity gamePlayerDataEntity, GameSnapshot gameSnapshot, Boolean first) { | |
| 209 | Optional<GameSidePlayerDataSnapshot> gameSidePlayerDataSnapshot; | |
| 210 |
1
1. updateScore : negated conditional → KILLED |
if(Boolean.TRUE.equals(first)){ |
| 211 | gameSidePlayerDataSnapshot = gameSnapshot.firstGameSide().gameSidePlayerDataList() | |
| 212 | .stream() | |
| 213 | .filter(playerSnapshot -> | |
| 214 |
2
1. lambda$updateScore$13 : replaced boolean return with true for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$updateScore$13 → SURVIVED 2. lambda$updateScore$13 : replaced boolean return with false for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$updateScore$13 → KILLED |
gamePlayerDataEntity.getPlayerEntity().getUserId().equals(playerSnapshot.player().userId().id())) |
| 215 | .findFirst(); | |
| 216 | } | |
| 217 | else{ | |
| 218 | gameSidePlayerDataSnapshot = gameSnapshot.secondGameSide().gameSidePlayerDataList() | |
| 219 | .stream() | |
| 220 | .filter(playerSnapshot -> | |
| 221 |
2
1. lambda$updateScore$14 : replaced boolean return with true for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$updateScore$14 → SURVIVED 2. lambda$updateScore$14 : replaced boolean return with false for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::lambda$updateScore$14 → KILLED |
gamePlayerDataEntity.getPlayerEntity().getUserId().equals(playerSnapshot.player().userId().id())) |
| 222 | .findFirst(); | |
| 223 | } | |
| 224 | ||
| 225 |
1
1. updateScore : negated conditional → KILLED |
if(gameSidePlayerDataSnapshot.isPresent()) |
| 226 | { | |
| 227 |
1
1. updateScore : removed call to org/hplr/user/infrastructure/dbadapter/entities/PlayerEntity::setScore → SURVIVED |
gamePlayerDataEntity.getPlayerEntity().setScore(gameSidePlayerDataSnapshot.get().player().playerRanking().score()); |
| 228 |
1
1. updateScore : replaced Boolean return with False for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::updateScore → KILLED |
return true; |
| 229 | } | |
| 230 |
1
1. updateScore : replaced Boolean return with True for org/hplr/game/infrastructure/dbadapter/adapters/GameCommandAdapter::updateScore → NO_COVERAGE |
return false; |
| 231 | } | |
| 232 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 63 |
1.1 |
|
| 67 |
1.1 |
|
| 69 |
1.1 |
|
| 73 |
1.1 |
|
| 79 |
1.1 |
|
| 84 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 87 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 110 |
1.1 |
|
| 112 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 124 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 135 |
1.1 2.2 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 141 |
1.1 |
|
| 146 |
1.1 2.2 |
|
| 157 |
1.1 |
|
| 162 |
1.1 |
|
| 169 |
1.1 |
|
| 171 |
1.1 2.2 |
|
| 172 |
1.1 2.2 |
|
| 174 |
1.1 |
|
| 176 |
1.1 |
|
| 177 |
1.1 2.2 |
|
| 205 |
1.1 |
|
| 210 |
1.1 |
|
| 214 |
1.1 2.2 |
|
| 221 |
1.1 2.2 |
|
| 225 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 230 |
1.1 |