Skip to content

Commit

Permalink
chore: 충돌해결
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewon121 committed Sep 27, 2023
2 parents f2c4b92 + 0628f07 commit 7a9a825
Show file tree
Hide file tree
Showing 43 changed files with 634 additions and 262 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/backend_dev_pr_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ jobs:
- name: Test with Gradle
run: ./gradlew test

- name: 테스트 결과를 PR에 코멘트로 등록합니다
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
with:
files: 'backend/build/test-results/test/TEST-*.xml'

- name: 테스트 실패 시, 실패한 코드 라인에 Check 코멘트를 등록합니다
uses: mikepenz/action-junit-report@v3
if: always()
with:
report_paths: '**/build/test-results/test/TEST-*.xml'
token: ${{ github.token }}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.now.naaga.auth.infrastructure.dto.MemberAuth;
import com.now.naaga.auth.infrastructure.jwt.AuthTokenGenerator;
import com.now.naaga.auth.persistence.AuthRepository;
import com.now.naaga.member.application.CreateMemberCommand;
import com.now.naaga.member.application.DeleteMemberCommand;
import com.now.naaga.member.application.dto.CreateMemberCommand;
import com.now.naaga.member.application.dto.DeleteMemberCommand;
import com.now.naaga.member.application.MemberService;
import com.now.naaga.member.domain.Member;
import com.now.naaga.player.application.PlayerService;
Expand Down
28 changes: 28 additions & 0 deletions backend/src/main/java/com/now/naaga/common/MdcFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.now.naaga.common;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.MDC;

import java.io.IOException;
import java.util.UUID;

import static com.now.naaga.common.MdcToken.*;

public class MdcFilter implements Filter {

@Override
public void doFilter(final ServletRequest request,
final ServletResponse response,
final FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest httpServletRequest = (HttpServletRequest) request;

MDC.put(REQUEST_ID.getKey(), UUID.randomUUID().toString());
MDC.put(URI.getKey(), httpServletRequest.getRequestURI());
MDC.put(METHOD.getKey(), httpServletRequest.getMethod());

chain.doFilter(request, response);
}

}
22 changes: 11 additions & 11 deletions backend/src/main/java/com/now/naaga/common/config/BeanConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.now.naaga.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.now.naaga.game.domain.gamescore.FailGameScorePolicy;
import com.now.naaga.game.domain.gamescore.GameScoreCalculator;
import com.now.naaga.game.domain.gamescore.GameScorePolicy;
import com.now.naaga.game.domain.gamescore.SuccessGameScorePolicy;
import com.now.naaga.gameresult.domain.gamescore.FailResultScorePolicy;
import com.now.naaga.gameresult.domain.gamescore.ResultScoreCalculator;
import com.now.naaga.gameresult.domain.gamescore.ResultScorePolicy;
import com.now.naaga.gameresult.domain.gamescore.SuccessResultScorePolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
Expand All @@ -25,21 +25,21 @@ public ObjectMapper objectMapper() {
}

@Bean
public SuccessGameScorePolicy successGameScorePolicy() {
return new SuccessGameScorePolicy();
public SuccessResultScorePolicy successGameScorePolicy() {
return new SuccessResultScorePolicy();
}

@Bean
public FailGameScorePolicy failGameScorePolicy() {
return new FailGameScorePolicy();
public FailResultScorePolicy failGameScorePolicy() {
return new FailResultScorePolicy();
}

@Bean
public GameScoreCalculator gameScoreCalculator() {
final List<GameScorePolicy> gameScorePolicies = List.of(
public ResultScoreCalculator gameScoreCalculator() {
final List<ResultScorePolicy> gameScorePolicies = List.of(
successGameScorePolicy(),
failGameScorePolicy()
);
return new GameScoreCalculator(gameScorePolicies);
return new ResultScoreCalculator(gameScorePolicies);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.now.naaga.game.application;

import com.now.naaga.game.application.dto.CreateGameResultCommand;

public interface GameFinishService {

void createGameResult(final CreateGameResultCommand createGameResultCommand);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.now.naaga.game.application;

import com.now.naaga.game.application.dto.CreateGameCommand;
import com.now.naaga.game.application.dto.EndGameCommand;
import com.now.naaga.game.application.dto.FindAllGamesCommand;
import com.now.naaga.game.application.dto.FindGameByIdCommand;
import com.now.naaga.game.application.dto.FindGameByStatusCommand;
import com.now.naaga.game.application.dto.*;
import com.now.naaga.game.domain.*;
import com.now.naaga.game.domain.gamescore.GameScoreCalculator;
import com.now.naaga.game.exception.GameException;
import com.now.naaga.game.exception.GameNotArrivalException;
import com.now.naaga.game.exception.GameNotFinishedException;
import com.now.naaga.game.repository.GameRepository;
import com.now.naaga.game.repository.GameResultRepository;

// TODO: 8/31/23 제거할 대상 - 이슈 범위를 벗어나서 일단은 제거하지 않음
import com.now.naaga.gameresult.domain.GameResult;
import com.now.naaga.gameresult.exception.GameResultException;
import com.now.naaga.gameresult.repository.GameResultRepository;

import com.now.naaga.place.application.PlaceService;
import com.now.naaga.place.application.dto.RecommendPlaceCommand;
import com.now.naaga.place.domain.Place;
Expand All @@ -19,7 +19,6 @@
import com.now.naaga.player.application.PlayerService;
import com.now.naaga.player.domain.Player;
import com.now.naaga.player.presentation.dto.PlayerRequest;
import com.now.naaga.score.domain.Score;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,30 +27,34 @@

import static com.now.naaga.game.exception.GameExceptionType.*;

// TODO: 8/31/23 제거할 대상 - 이슈 범위를 벗어나서 일단은 제거하지 않음
import static com.now.naaga.gameresult.exception.GameResultExceptionType.GAME_RESULT_NOT_EXIST;

@Transactional
@Service
public class GameService {

private final GameRepository gameRepository;

// TODO: 8/31/23 제거할 대상 - 이슈 범위를 벗어나서 일단은 제거하지 않음
private final GameResultRepository gameResultRepository;

private final PlayerService playerService;

private final PlaceService placeService;

private final GameScoreCalculator gameScoreCalculator;
private final GameFinishService gameFinishService;

public GameService(final GameRepository gameRepository,
final GameResultRepository gameResultRepository,
final PlayerService playerService,
final PlaceService placeService,
final GameScoreCalculator gameScoreCalculator) {
final GameResultRepository gameResultRepository,
final PlayerService playerService,
final PlaceService placeService,
final GameFinishService gameFinishService) {
this.gameRepository = gameRepository;
this.gameResultRepository = gameResultRepository;
this.playerService = playerService;
this.placeService = placeService;
this.gameScoreCalculator = gameScoreCalculator;
this.gameFinishService = gameFinishService;
}

public Game createGame(final CreateGameCommand createGameCommand) {
Expand All @@ -72,16 +75,19 @@ public Game createGame(final CreateGameCommand createGameCommand) {
}
}

@Transactional(noRollbackFor = {GameNotArrivalException.class})
@Transactional(noRollbackFor = {GameNotFinishedException.class})
public void endGame(final EndGameCommand endGameCommand) {
final Game game = gameRepository.findById(endGameCommand.gameId())
.orElseThrow(() -> new GameException(NOT_EXIST));
final Game game = gameRepository.findById(endGameCommand.gameId()).orElseThrow(() -> new GameException(NOT_EXIST));
final Player player = playerService.findPlayerById(endGameCommand.playerId());
game.validateOwner(player);
final ResultType resultType = game.endGame(endGameCommand.endType(), endGameCommand.position());
final Score score = gameScoreCalculator.calculate(game, resultType);
player.addScore(score);
gameResultRepository.save(new GameResult(resultType, score, game));

final EndType endType = endGameCommand.endType();
final Position position = endGameCommand.position();

game.endGame(position, endType);

final CreateGameResultCommand createGameResultCommand = new CreateGameResultCommand(player, game, position, endType);
gameFinishService.createGameResult(createGameResultCommand);
}

@Transactional(readOnly = true)
Expand All @@ -104,7 +110,7 @@ public GameResult findGameResultByGameId(final Long gameId) {
final List<GameResult> gameResultsByGameId = gameResultRepository.findByGameId(gameId);

if (gameResultsByGameId.isEmpty()) {
throw new GameException(GAME_RESULT_NOT_EXIST);
throw new GameResultException(GAME_RESULT_NOT_EXIST);
}

return gameResultsByGameId.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.now.naaga.game.application.dto;

import com.now.naaga.game.domain.EndType;
import com.now.naaga.game.domain.Game;
import com.now.naaga.place.domain.Position;
import com.now.naaga.player.domain.Player;

// TODO: 8/31/23 player -> ID / game -> game -> ID
public record CreateGameResultCommand(Player player,
Game game,
Position position,
EndType endType) {
}
115 changes: 54 additions & 61 deletions backend/src/main/java/com/now/naaga/game/domain/Game.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
package com.now.naaga.game.domain;

import static com.now.naaga.game.domain.EndType.GIVE_UP;
import static com.now.naaga.game.domain.GameStatus.DONE;
import static com.now.naaga.game.domain.GameStatus.IN_PROGRESS;
import static com.now.naaga.game.domain.ResultType.FAIL;
import static com.now.naaga.game.domain.ResultType.SUCCESS;
import static com.now.naaga.game.exception.GameExceptionType.ALREADY_DONE;
import static com.now.naaga.game.exception.GameExceptionType.INACCESSIBLE_AUTHENTICATION;
import static com.now.naaga.game.exception.GameExceptionType.NOT_ARRIVED;

import com.now.naaga.common.domain.BaseEntity;
import com.now.naaga.game.exception.GameException;
import com.now.naaga.game.exception.GameNotArrivalException;
import com.now.naaga.game.exception.GameExceptionType;
import com.now.naaga.game.exception.GameNotFinishedException;
import com.now.naaga.place.domain.Place;
import com.now.naaga.place.domain.Position;
import com.now.naaga.player.domain.Player;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.math.BigDecimal;
import jakarta.persistence.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.springframework.data.geo.Distance;

import static com.now.naaga.game.domain.EndType.ARRIVED;
import static com.now.naaga.game.domain.EndType.GIVE_UP;
import static com.now.naaga.game.domain.GameStatus.DONE;
import static com.now.naaga.game.domain.GameStatus.IN_PROGRESS;
import static com.now.naaga.game.exception.GameExceptionType.*;

@Entity
public class Game extends BaseEntity {
Expand Down Expand Up @@ -76,7 +61,9 @@ public class Game extends BaseEntity {
protected Game() {
}

public Game(final Player player, final Place place, final Position startPosition) {
public Game(final Player player,
final Place place,
final Position startPosition) {
this(null, IN_PROGRESS, player, place, startPosition, MAX_ATTEMPT_COUNT, new ArrayList<>(), LocalDateTime.now(), null);
}

Expand Down Expand Up @@ -121,56 +108,62 @@ public boolean canUseMoreHint() {
return hints.size() < MAX_HINT_COUNT;
}

public ResultType endGame(final EndType endType, final Position position) {
if (isDone()) {
throw new GameException(ALREADY_DONE);
}
if (endType == GIVE_UP) {
return giveUpGame();
}
return endGameByArrival(position);
public double findDistance() {
final Position destinationPosition = place.getPosition();
return startPosition.calculateDistance(destinationPosition);
}

private boolean isDone() {
return gameStatus == DONE;
public void endGame(final Position position,
final EndType endType) {
validateInProgressing();

if (endType == ARRIVED) {
subtractAttempts();
}

setGameStatusDone(position, endType);
}

private ResultType giveUpGame() {
gameStatus = DONE;
endTime = LocalDateTime.now();
return FAIL;
private void validateInProgressing() {
if (gameStatus == DONE) {
throw new GameException(ALREADY_DONE);
}
}

private ResultType endGameByArrival(final Position position) {
private void subtractAttempts() {
validateAvailableAttempts();
remainingAttempts--;
if (isPlayerArrived(position)) {
return endGameWithSuccess();
}
return endGameWithFailure();
}

private boolean isPlayerArrived(final Position position) {
return place.isCoordinateInsideBounds(position);
private void validateAvailableAttempts() {
if (remainingAttempts <= 0) {
throw new GameException(GameExceptionType.NOT_REMAIN_ATTEMPTS);
}
}

private ResultType endGameWithSuccess() {
gameStatus = DONE;
endTime = LocalDateTime.now();
return SUCCESS;
private void setGameStatusDone(final Position position,
final EndType endType) {
validateForEnd(position, endType);

this.endTime = LocalDateTime.now();
this.gameStatus = DONE;
}

private ResultType endGameWithFailure() {
if (remainingAttempts == 0) {
gameStatus = DONE;
endTime = LocalDateTime.now();
return FAIL;
private void validateForEnd(final Position position,
final EndType endType) {
final boolean isUnfinishedCondition = remainingAttempts > 0
&& !place.isCoordinateInsideBounds(position)
&& endType != GIVE_UP;

if (isUnfinishedCondition) {
throw new GameNotFinishedException(NOT_ARRIVED);
}
throw new GameNotArrivalException(NOT_ARRIVED);
}

public double findDistance() {
final Position destinationPosition = place.getPosition();
return startPosition.calculateDistance(destinationPosition);

public void validateDoneGame() {
if(gameStatus == IN_PROGRESS) {
throw new GameException(NOT_DONE);
}
}

public Long getId() {
Expand Down
Loading

0 comments on commit 7a9a825

Please sign in to comment.