| 1 | package org.hplr.game.core.usecases.service; | |
| 2 | ||
| 3 | import jakarta.servlet.http.HttpServletRequest; | |
| 4 | import lombok.RequiredArgsConstructor; | |
| 5 | ||
| 6 | import org.hplr.elo.core.model.vo.Elo; | |
| 7 | import org.hplr.game.core.enums.Status; | |
| 8 | import org.hplr.game.core.model.Game; | |
| 9 | import org.hplr.game.core.model.GameValidator; | |
| 10 | import org.hplr.game.core.model.vo.GameHistoricalElo; | |
| 11 | import org.hplr.game.core.usecases.port.dto.GameSelectDto; | |
| 12 | import org.hplr.game.core.usecases.port.in.FinishGameUseCaseInterface; | |
| 13 | import org.hplr.game.core.usecases.port.out.command.SaveFinishedGameCommandInterface; | |
| 14 | import org.hplr.game.core.usecases.port.out.query.SelectGameByGameIdQueryInterface; | |
| 15 | ||
| 16 | import org.hplr.library.exception.HPLRAccessDeniedException; | |
| 17 | import org.hplr.library.infrastructure.controller.AccessValidator; | |
| 18 | import org.hplr.user.core.model.vo.PlayerRanking; | |
| 19 | ||
| 20 | import org.hplr.elo.core.model.vo.Score; | |
| 21 | import org.hplr.elo.core.usecases.port.in.CalculateAverageELOForGameSideUseCaseInterface; | |
| 22 | import org.hplr.elo.core.usecases.port.in.CalculateELOChangeForGameUseCaseInterface; | |
| 23 | import org.hplr.elo.core.usecases.port.in.CalculateScoreForGameUseCaseInterface; | |
| 24 | ||
| 25 | import org.springframework.stereotype.Service; | |
| 26 | ||
| 27 | import java.util.Map; | |
| 28 | import java.util.NoSuchElementException; | |
| 29 | import java.util.Optional; | |
| 30 | import java.util.UUID; | |
| 31 | import java.util.concurrent.atomic.AtomicReference; | |
| 32 | ||
| 33 | @RequiredArgsConstructor | |
| 34 | @Service | |
| 35 | public class FinishGameManualUseCaseService implements FinishGameUseCaseInterface { | |
| 36 | ||
| 37 | final SelectGameByGameIdQueryInterface selectGameByGameIdQueryInterface; | |
| 38 | final SaveFinishedGameCommandInterface saveFinishedGameCommandInterface; | |
| 39 | final CalculateELOChangeForGameUseCaseInterface calculateELOChangeForGameUseCaseInterface; | |
| 40 | final CalculateAverageELOForGameSideUseCaseInterface calculateAverageELOForGameSideUseCaseInterface; | |
| 41 | final CalculateScoreForGameUseCaseInterface calculateScoreForGameUseCaseInterface; | |
| 42 | final AccessValidator accessValidator; | |
| 43 | ||
| 44 | @Override | |
| 45 | public UUID finishGame(HttpServletRequest httpServletRequest, UUID gameId) { | |
| 46 | AtomicReference<Boolean> firstSidePresent = new AtomicReference<>(false); | |
| 47 | AtomicReference<Boolean> secondSidePresent = new AtomicReference<>(false); | |
| 48 | Optional<GameSelectDto> gameSelectDtoOptional = | |
| 49 | selectGameByGameIdQueryInterface.selectGameByGameId(gameId); | |
| 50 | ||
| 51 | Game game = Game.fromDto(gameSelectDtoOptional.orElseThrow(NoSuchElementException::new)); | |
| 52 |
1
1. finishGame : removed call to java/util/List::forEach → SURVIVED |
game.getFirstGameSide().getGameSidePlayerDataList().forEach(player->{ |
| 53 |
1
1. lambda$finishGame$0 : negated conditional → SURVIVED |
if(Boolean.TRUE.equals(accessValidator.validateUserAccess(httpServletRequest,player.player().getUserId().id()))){ |
| 54 |
1
1. lambda$finishGame$0 : removed call to java/util/concurrent/atomic/AtomicReference::set → SURVIVED |
firstSidePresent.set(true); |
| 55 | } | |
| 56 | }); | |
| 57 |
1
1. finishGame : removed call to java/util/List::forEach → SURVIVED |
game.getSecondGameSide().getGameSidePlayerDataList().forEach(player->{ |
| 58 |
1
1. lambda$finishGame$1 : negated conditional → SURVIVED |
if(Boolean.TRUE.equals(accessValidator.validateUserAccess(httpServletRequest,player.player().getUserId().id()))){ |
| 59 |
1
1. lambda$finishGame$1 : removed call to java/util/concurrent/atomic/AtomicReference::set → SURVIVED |
secondSidePresent.set(true); |
| 60 | } | |
| 61 | }); | |
| 62 | ||
| 63 |
2
1. finishGame : negated conditional → SURVIVED 2. finishGame : negated conditional → NO_COVERAGE |
if(!(firstSidePresent.get() || secondSidePresent.get())){ |
| 64 | throw new HPLRAccessDeniedException("Player cannot end this game!"); | |
| 65 | } | |
| 66 | ||
| 67 |
1
1. finishGame : removed call to org/hplr/game/core/model/GameValidator::validateFinishedGame → KILLED |
GameValidator.validateFinishedGame(game); |
| 68 |
1
1. finishGame : removed call to org/hplr/game/core/model/Game::setGameStatus → KILLED |
game.setGameStatus(Status.FINISHED); |
| 69 | ||
| 70 | Long firstElo = calculateAverageELOForGameSideUseCaseInterface.calculateAverageELO( | |
| 71 | game.getFirstGameSide().getGameSidePlayerDataList() | |
| 72 | .stream() | |
| 73 |
1
1. lambda$finishGame$2 : replaced Long return value with 0L for org/hplr/game/core/usecases/service/FinishGameManualUseCaseService::lambda$finishGame$2 → KILLED |
.map(gameSidePlayerData -> gameSidePlayerData.player().getRanking().score()) |
| 74 | .toList() | |
| 75 | ); | |
| 76 | Long secondElo = calculateAverageELOForGameSideUseCaseInterface.calculateAverageELO( | |
| 77 | game.getSecondGameSide().getGameSidePlayerDataList() | |
| 78 | .stream() | |
| 79 |
1
1. lambda$finishGame$3 : replaced Long return value with 0L for org/hplr/game/core/usecases/service/FinishGameManualUseCaseService::lambda$finishGame$3 → KILLED |
.map(gameSidePlayerData -> gameSidePlayerData.player().getRanking().score()) |
| 80 | .toList() | |
| 81 | ); | |
| 82 | Long gameScore; | |
| 83 | if(game.getFirstGameSide() | |
| 84 | .getScorePerTurnList() | |
| 85 | .stream() | |
| 86 |
1
1. finishGame : negated conditional → KILLED |
.anyMatch(Score::tabled) |
| 87 | ){ | |
| 88 | gameScore = -20L; | |
| 89 | } | |
| 90 | else if( | |
| 91 | game.getSecondGameSide().getScorePerTurnList() | |
| 92 | .stream() | |
| 93 |
1
1. finishGame : negated conditional → KILLED |
.anyMatch(Score::tabled) |
| 94 | ){ | |
| 95 | gameScore = 20L; | |
| 96 | } | |
| 97 | else { | |
| 98 | gameScore = calculateScoreForGameUseCaseInterface.calculateScoreForGame( | |
| 99 | game.getFirstGameSide().getScorePerTurnList() | |
| 100 | .stream() | |
| 101 | .map(Score::turnScore) | |
| 102 | .toList(), | |
| 103 | game.getSecondGameSide().getScorePerTurnList() | |
| 104 | .stream() | |
| 105 | .map(Score::turnScore) | |
| 106 | .toList() | |
| 107 | ); | |
| 108 | } | |
| 109 | ||
| 110 | Map<Integer, Long> eloMap = calculateELOChangeForGameUseCaseInterface.calculateChangeForGame(firstElo, secondElo, gameScore); | |
| 111 | game.getFirstGameSide().getGameSidePlayerDataList() | |
| 112 |
1
1. finishGame : removed call to java/util/List::forEach → KILLED |
.forEach(gameSidePlayerData -> { |
| 113 | Long score = gameSidePlayerData.player().getRanking().score(); | |
| 114 |
1
1. lambda$finishGame$4 : Replaced long addition with subtraction → KILLED |
PlayerRanking playerRanking = new PlayerRanking( score + eloMap.get(1)); |
| 115 |
1
1. lambda$finishGame$4 : removed call to org/hplr/user/core/model/Player::setRanking → KILLED |
gameSidePlayerData.player().setRanking(playerRanking); |
| 116 | } | |
| 117 | ); | |
| 118 | game.getSecondGameSide().getGameSidePlayerDataList() | |
| 119 |
1
1. finishGame : removed call to java/util/List::forEach → KILLED |
.forEach(gameSidePlayerData -> { |
| 120 | Long score = gameSidePlayerData.player().getRanking().score(); | |
| 121 |
1
1. lambda$finishGame$5 : Replaced long addition with subtraction → KILLED |
PlayerRanking playerRanking = new PlayerRanking(score + eloMap.get(2)); |
| 122 |
1
1. lambda$finishGame$5 : removed call to org/hplr/user/core/model/Player::setRanking → KILLED |
gameSidePlayerData.player().setRanking(playerRanking); |
| 123 | } | |
| 124 | ); | |
| 125 | GameHistoricalElo gameHistoricalElo = new GameHistoricalElo( | |
| 126 | game.getGameId(), | |
| 127 | new Elo(firstElo), | |
| 128 | new Elo(secondElo) | |
| 129 | ); | |
| 130 | saveFinishedGameCommandInterface.saveFinishedGame(game.toSnapshot(), gameHistoricalElo); | |
| 131 |
1
1. finishGame : replaced return value with null for org/hplr/game/core/usecases/service/FinishGameManualUseCaseService::finishGame → KILLED |
return game.getGameId().gameId(); |
| 132 | } | |
| 133 | } | |
Mutations | ||
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 59 |
1.1 |
|
| 63 |
1.1 2.2 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 73 |
1.1 |
|
| 79 |
1.1 |
|
| 86 |
1.1 |
|
| 93 |
1.1 |
|
| 112 |
1.1 |
|
| 114 |
1.1 |
|
| 115 |
1.1 |
|
| 119 |
1.1 |
|
| 121 |
1.1 |
|
| 122 |
1.1 |
|
| 131 |
1.1 |