From 03d65cfbcb884c0923002b5c1dbdcd548cb026f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A5=E1=86=BC=E1=84=8B=E1=85=B5=E1=84=89?= =?UTF-8?q?=E1=85=A1=E1=86=A8?= Date: Fri, 7 May 2021 10:23:16 +0900 Subject: [PATCH] [#6] feat: Create GameService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 야구 게임방의 서비스 레이어입니다. 2. 게임 만들기, 게임 참가하기, 게임진행하기, 게임결과받기 의 기본기능이 있습니다. --- .../team9/baseball/service/GameService.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 BE/baseball/src/main/java/team9/baseball/service/GameService.java diff --git a/BE/baseball/src/main/java/team9/baseball/service/GameService.java b/BE/baseball/src/main/java/team9/baseball/service/GameService.java new file mode 100644 index 000000000..d9fd1acb3 --- /dev/null +++ b/BE/baseball/src/main/java/team9/baseball/service/GameService.java @@ -0,0 +1,104 @@ +package team9.baseball.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import team9.baseball.DTO.response.GameStatusDTO; +import team9.baseball.domain.aggregate.game.Game; +import team9.baseball.domain.aggregate.team.Team; +import team9.baseball.domain.aggregate.user.User; +import team9.baseball.domain.enums.PitchResult; +import team9.baseball.domain.enums.Venue; +import team9.baseball.exception.NotFoundException; +import team9.baseball.repository.GameRepository; +import team9.baseball.repository.TeamRepository; +import team9.baseball.repository.UserRepository; + +@Service +public class GameService { + private final GameRepository gameRepository; + private final TeamRepository teamRepository; + private final UserRepository userRepository; + + @Autowired + public GameService(GameRepository gameRepository, TeamRepository teamRepository, UserRepository userRepository) { + this.gameRepository = gameRepository; + this.teamRepository = teamRepository; + this.userRepository = userRepository; + } + + public void applyPitchResult(long userId, PitchResult pitchResult) { + User user = getUser(userId); + if (user.getCurrentGameId() == null) { + throw new RuntimeException(userId + "사용자는 게임중이 아닙니다."); + } + Game game = getGame(user.getCurrentGameId()); + Team awayTeam = getTeam(game.getAwayTeamId()); + Team homeTeam = getTeam(game.getHomeTeamId()); + + //현재 내가 공격팀이면 공을 던질 수 없다. + if (game.getCurrentHalves() == user.getCurrentGameVenue().getHalves()) { + throw new RuntimeException(userId + "번 사용자는 현재 공격팀입니다."); + } + + switch (pitchResult) { + case HIT: + game.proceedHit(awayTeam, homeTeam); + break; + case STRIKE: + game.proceedStrike(awayTeam, homeTeam); + break; + case BALL: + game.proceedBall(awayTeam, homeTeam); + break; + } + gameRepository.save(game); + } + + public GameStatusDTO getCurrentGameStatus(long userId) { + User user = getUser(userId); + if (user.getCurrentGameId() == null) { + throw new RuntimeException(userId + "사용자는 게임중이 아닙니다."); + } + Game game = getGame(user.getCurrentGameId()); + Team awayTeam = getTeam(game.getAwayTeamId()); + Team homeTeam = getTeam(game.getHomeTeamId()); + + return GameStatusDTO.of(game, awayTeam, homeTeam, user.getCurrentGameVenue()); + } + + public void createNewGame(long userId, int awayTeamId, int homeTeamId) { + User user = getUser(userId); + Team awayTeam = getTeam(awayTeamId); + Team homeTeam = getTeam(homeTeamId); + + Game game = new Game(awayTeam, homeTeam); + game = gameRepository.save(game); + } + + public void joinGame(long userId, long gameId, Venue venue) { + User user = getUser(userId); + if (user.getCurrentGameId() != null) { + throw new RuntimeException(userId + "사용자는 이미 게임중입니다."); + } + Game game = getGame(gameId); + if (userRepository.existsByCurrentGameIdAndCurrentGameVenue(gameId, venue)) { + throw new RuntimeException(gameId + "번 게임의 " + venue + "팀은 다른 사용자가 참가했습니다."); + } + + user.setCurrentGameId(gameId); + user.setCurrentGameVenue(venue); + userRepository.save(user); + } + + private User getUser(long userId) { + return userRepository.findById(userId).orElseThrow(() -> new NotFoundException(userId + " 사용자는 존재하지 않습니다.")); + } + + private Game getGame(long gameId) { + return gameRepository.findById(gameId).orElseThrow(() -> new NotFoundException(gameId + "번 게임은 존재하지 않습니다.")); + } + + private Team getTeam(int teamId) { + return teamRepository.findById(teamId).orElseThrow(() -> new NotFoundException(teamId + "번 팀은 존재하지 않습니다.")); + } +}