|
| 1 | +package team9.baseball.service; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.stereotype.Service; |
| 5 | +import team9.baseball.DTO.response.GameStatusDTO; |
| 6 | +import team9.baseball.domain.aggregate.game.Game; |
| 7 | +import team9.baseball.domain.aggregate.team.Team; |
| 8 | +import team9.baseball.domain.aggregate.user.User; |
| 9 | +import team9.baseball.domain.enums.PitchResult; |
| 10 | +import team9.baseball.domain.enums.Venue; |
| 11 | +import team9.baseball.exception.NotFoundException; |
| 12 | +import team9.baseball.repository.GameRepository; |
| 13 | +import team9.baseball.repository.TeamRepository; |
| 14 | +import team9.baseball.repository.UserRepository; |
| 15 | + |
| 16 | +@Service |
| 17 | +public class GameService { |
| 18 | + private final GameRepository gameRepository; |
| 19 | + private final TeamRepository teamRepository; |
| 20 | + private final UserRepository userRepository; |
| 21 | + |
| 22 | + @Autowired |
| 23 | + public GameService(GameRepository gameRepository, TeamRepository teamRepository, UserRepository userRepository) { |
| 24 | + this.gameRepository = gameRepository; |
| 25 | + this.teamRepository = teamRepository; |
| 26 | + this.userRepository = userRepository; |
| 27 | + } |
| 28 | + |
| 29 | + public void applyPitchResult(long userId, PitchResult pitchResult) { |
| 30 | + User user = getUser(userId); |
| 31 | + if (user.getCurrentGameId() == null) { |
| 32 | + throw new RuntimeException(userId + "사용자는 게임중이 아닙니다."); |
| 33 | + } |
| 34 | + Game game = getGame(user.getCurrentGameId()); |
| 35 | + Team awayTeam = getTeam(game.getAwayTeamId()); |
| 36 | + Team homeTeam = getTeam(game.getHomeTeamId()); |
| 37 | + |
| 38 | + //현재 내가 공격팀이면 공을 던질 수 없다. |
| 39 | + if (game.getCurrentHalves() == user.getCurrentGameVenue().getHalves()) { |
| 40 | + throw new RuntimeException(userId + "번 사용자는 현재 공격팀입니다."); |
| 41 | + } |
| 42 | + |
| 43 | + switch (pitchResult) { |
| 44 | + case HIT: |
| 45 | + game.proceedHit(awayTeam, homeTeam); |
| 46 | + break; |
| 47 | + case STRIKE: |
| 48 | + game.proceedStrike(awayTeam, homeTeam); |
| 49 | + break; |
| 50 | + case BALL: |
| 51 | + game.proceedBall(awayTeam, homeTeam); |
| 52 | + break; |
| 53 | + } |
| 54 | + gameRepository.save(game); |
| 55 | + } |
| 56 | + |
| 57 | + public GameStatusDTO getCurrentGameStatus(long userId) { |
| 58 | + User user = getUser(userId); |
| 59 | + if (user.getCurrentGameId() == null) { |
| 60 | + throw new RuntimeException(userId + "사용자는 게임중이 아닙니다."); |
| 61 | + } |
| 62 | + Game game = getGame(user.getCurrentGameId()); |
| 63 | + Team awayTeam = getTeam(game.getAwayTeamId()); |
| 64 | + Team homeTeam = getTeam(game.getHomeTeamId()); |
| 65 | + |
| 66 | + return GameStatusDTO.of(game, awayTeam, homeTeam, user.getCurrentGameVenue()); |
| 67 | + } |
| 68 | + |
| 69 | + public void createNewGame(long userId, int awayTeamId, int homeTeamId) { |
| 70 | + User user = getUser(userId); |
| 71 | + Team awayTeam = getTeam(awayTeamId); |
| 72 | + Team homeTeam = getTeam(homeTeamId); |
| 73 | + |
| 74 | + Game game = new Game(awayTeam, homeTeam); |
| 75 | + game = gameRepository.save(game); |
| 76 | + } |
| 77 | + |
| 78 | + public void joinGame(long userId, long gameId, Venue venue) { |
| 79 | + User user = getUser(userId); |
| 80 | + if (user.getCurrentGameId() != null) { |
| 81 | + throw new RuntimeException(userId + "사용자는 이미 게임중입니다."); |
| 82 | + } |
| 83 | + Game game = getGame(gameId); |
| 84 | + if (userRepository.existsByCurrentGameIdAndCurrentGameVenue(gameId, venue)) { |
| 85 | + throw new RuntimeException(gameId + "번 게임의 " + venue + "팀은 다른 사용자가 참가했습니다."); |
| 86 | + } |
| 87 | + |
| 88 | + user.setCurrentGameId(gameId); |
| 89 | + user.setCurrentGameVenue(venue); |
| 90 | + userRepository.save(user); |
| 91 | + } |
| 92 | + |
| 93 | + private User getUser(long userId) { |
| 94 | + return userRepository.findById(userId).orElseThrow(() -> new NotFoundException(userId + " 사용자는 존재하지 않습니다.")); |
| 95 | + } |
| 96 | + |
| 97 | + private Game getGame(long gameId) { |
| 98 | + return gameRepository.findById(gameId).orElseThrow(() -> new NotFoundException(gameId + "번 게임은 존재하지 않습니다.")); |
| 99 | + } |
| 100 | + |
| 101 | + private Team getTeam(int teamId) { |
| 102 | + return teamRepository.findById(teamId).orElseThrow(() -> new NotFoundException(teamId + "번 팀은 존재하지 않습니다.")); |
| 103 | + } |
| 104 | +} |
0 commit comments