Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : 어그리거트 정의에 따른 네이밍 반영 및 API 수정 #103

Merged
merged 13 commits into from
Aug 25, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mafia.mafiatogether.chat.domain.ChatRepository;
import mafia.mafiatogether.chat.domain.Message;
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.config.exception.RoomException;
import mafia.mafiatogether.config.exception.GameException;
import mafia.mafiatogether.job.domain.PlayerJob;
import mafia.mafiatogether.job.domain.PlayerJobRepository;
import org.springframework.stereotype.Service;
Expand All @@ -25,9 +25,9 @@ public class ChatService {
@Transactional(readOnly = true)
public List<ChatResponse> findAllChat(final String code, final String name) {
final PlayerJob playerJobs = playerJobRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final Chat chat = chatRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final boolean isMafia = playerJobs.isMafia(name);
return chat.getMessages().stream()
.map(message -> ChatResponse.of(
Expand All @@ -42,7 +42,7 @@ public List<ChatResponse> findAllChat(final String code, final String name) {
@Transactional
public void saveChat(final String code, final String name, final ChatRequest chatRequest) {
final Chat chat = chatRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final Message message = new Message(name, chatRequest.contents(), Clock.systemDefaultZone().millis());
chat.saveMessage(message);
chatRepository.save(chat);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mafia.mafiatogether.config.exception;

public class RoomException extends GlobalException{
public class GameException extends GlobalException{

public RoomException(final ExceptionCode code) {
public GameException(final ExceptionCode code) {
super(code.getCode(),code.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import mafia.mafiatogether.chat.domain.Chat;
import mafia.mafiatogether.chat.domain.ChatRepository;
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.config.exception.RoomException;
import mafia.mafiatogether.config.exception.GameException;
import mafia.mafiatogether.game.application.dto.event.ClearJobTargetEvent;
import mafia.mafiatogether.game.application.dto.event.ClearVoteEvent;
import mafia.mafiatogether.game.application.dto.event.DeleteGameEvent;
Expand Down Expand Up @@ -41,9 +41,9 @@ public class GameEventListener {
@EventListener
public void listenVoteExecuteEvent(final VoteExecuteEvent voteExecuteEvent) {
final Game game = gameRepository.findById(voteExecuteEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final Vote vote = voteRepository.findById(voteExecuteEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final String target = vote.countVotes();
game.executeTarget(target);
gameRepository.save(game);
Expand All @@ -52,17 +52,17 @@ public void listenVoteExecuteEvent(final VoteExecuteEvent voteExecuteEvent) {
@EventListener
public void listenClearVoteEvent(final ClearVoteEvent clearVoteEvent) {
final Vote vote = voteRepository.findById(clearVoteEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
vote.clearVoteTargets();
voteRepository.save(vote);
}

@EventListener
public void listenJobExecuteEvent(final JobExecuteEvent jobExecuteEvent) {
final Game game = gameRepository.findById(jobExecuteEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final JobTarget jobTarget = jobTargetRepository.findById(jobExecuteEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final String target = jobTarget.findTarget();
game.executeTarget(target);
gameRepository.save(game);
Expand All @@ -71,7 +71,7 @@ public void listenJobExecuteEvent(final JobExecuteEvent jobExecuteEvent) {
@EventListener
public void listenClearJobTargetEvent(final ClearJobTargetEvent clearJobTargetEvent) {
final JobTarget jobTarget = jobTargetRepository.findById(clearJobTargetEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
jobTarget.clearJobTargets();
jobTargetRepository.save(jobTarget);
}
Expand Down Expand Up @@ -103,12 +103,12 @@ public void listenDeleteGameEvent(final DeleteGameEvent deleteGameEvent) {
@EventListener
public void listenAllPlayerVoteEvent(final AllPlayerVotedEvent allPlayerVotedEvent) {
final Game game = gameRepository.findById(allPlayerVotedEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
if (!game.getStatus().getType().equals(StatusType.DAY)) {
return;
}
final Vote vote = voteRepository.findById(allPlayerVotedEvent.getCode())
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
if (game.getAlivePlayerCount() == vote.getVotedCount()) {
game.skipStatus(Clock.systemDefaultZone().millis());
gameRepository.save(game);
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/mafia/mafiatogether/game/application/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.config.exception.RoomException;
import mafia.mafiatogether.game.application.dto.response.RoomInfoResponse;
import mafia.mafiatogether.game.application.dto.response.RoomResultResponse;
import mafia.mafiatogether.game.application.dto.response.RoomStatusResponse;
import mafia.mafiatogether.config.exception.GameException;
import mafia.mafiatogether.game.application.dto.response.GameInfoResponse;
import mafia.mafiatogether.game.application.dto.response.GameResultResponse;
import mafia.mafiatogether.game.application.dto.response.GameStatusResponse;
import mafia.mafiatogether.game.domain.Game;
import mafia.mafiatogether.game.domain.GameRepository;
import mafia.mafiatogether.game.domain.status.StatusType;
import mafia.mafiatogether.room.domain.Room;
import mafia.mafiatogether.room.domain.RoomRepository;
import mafia.mafiatogether.lobby.domain.Lobby;
import mafia.mafiatogether.lobby.domain.LobbyRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class GameService {

private final RoomRepository roomRepository;
private final LobbyRepository lobbyRepository;
private final GameRepository gameRepository;

@Transactional
public RoomStatusResponse findStatus(final String code) {
public GameStatusResponse findStatus(final String code) {
final Optional<Game> game = gameRepository.findById(code);
if (game.isPresent()) {
return new RoomStatusResponse(checkStatusChanged(game.get()));
return new GameStatusResponse(checkStatusChanged(game.get()));
}
if (roomRepository.existsById(code)) {
return new RoomStatusResponse(StatusType.WAIT);
if (lobbyRepository.existsById(code)) {
return new GameStatusResponse(StatusType.WAIT);
}
throw new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE);
throw new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE);
}

private StatusType checkStatusChanged(final Game game) {
Expand All @@ -49,36 +49,36 @@ private StatusType checkStatusChanged(final Game game) {
}

@Transactional
public void modifyStatus(final String code) {
final Room room = roomRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
Game game = Game.create(room, Clock.systemDefaultZone().millis());
public void startGame(final String code) {
final Lobby lobby = lobbyRepository.findById(code)
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
final Game game = Game.create(lobby, Clock.systemDefaultZone().millis());
game.distributeRole();
gameRepository.save(game);
}

@Transactional(readOnly = true)
public RoomInfoResponse findRoomInfo(final String code, final String name) {
public GameInfoResponse findGameInfo(final String code, final String name) {
final Optional<Game> game = gameRepository.findById(code);
if (!game.isPresent()) {
return getLobbyInfo(code, name);
}
return RoomInfoResponse.ofGame(game.get(), name);
return GameInfoResponse.ofGame(game.get(), name);
}

private RoomInfoResponse getLobbyInfo(final String code, final String name) {
final Room room = roomRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
return RoomInfoResponse.ofRoom(room, name);
private GameInfoResponse getLobbyInfo(final String code, final String name) {
final Lobby lobby = lobbyRepository.findById(code)
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
return GameInfoResponse.ofLobby(lobby, name);
}

@Transactional(readOnly = true)
public RoomResultResponse findResult(final String code) {
public GameResultResponse findResult(final String code) {
final Game game = gameRepository.findById(code)
.orElseThrow(() -> new RoomException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
.orElseThrow(() -> new GameException(ExceptionCode.INVALID_NOT_FOUND_ROOM_CODE));
if (!game.isEnd()) {
throw new RoomException(ExceptionCode.GAME_IS_NOT_FINISHED);
throw new GameException(ExceptionCode.GAME_IS_NOT_FINISHED);
}
return RoomResultResponse.from(game);
return GameResultResponse.from(game);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import mafia.mafiatogether.game.domain.Game;
import mafia.mafiatogether.game.domain.Player;
import mafia.mafiatogether.game.domain.PlayerCollection;
import mafia.mafiatogether.room.domain.ParticipantCollection;
import mafia.mafiatogether.room.domain.Room;
import mafia.mafiatogether.lobby.domain.ParticipantCollection;
import mafia.mafiatogether.lobby.domain.Lobby;

public record RoomInfoResponse(
public record GameInfoResponse(
Timestamp startTime,
Timestamp endTime,
Boolean isAlive,
Expand All @@ -19,9 +19,9 @@ public record RoomInfoResponse(
String myName,
List<PlayerResponse> players
) {
public static RoomInfoResponse ofGame(final Game game, final String name) {
public static GameInfoResponse ofGame(final Game game, final String name) {
final Player player = game.getPlayer(name);
return new RoomInfoResponse(
return new GameInfoResponse(
game.getStatus().getStartTime(),
game.getStatus().getEndTime(),
player.isAlive(),
Expand All @@ -32,15 +32,15 @@ public static RoomInfoResponse ofGame(final Game game, final String name) {
);
}

public static RoomInfoResponse ofRoom(Room room, String name) {
return new RoomInfoResponse(
public static GameInfoResponse ofLobby(Lobby lobby, String name) {
return new GameInfoResponse(
new Timestamp(Clock.systemDefaultZone().millis()),
new Timestamp(Clock.systemDefaultZone().millis()),
true,
room.getRoomInfo().getTotal(),
room.getMaster().getName().equals(name),
lobby.getLobbyInfo().getTotal(),
lobby.getMaster().getName().equals(name),
name,
convertFrom(room.getParticipants())
convertFrom(lobby.getParticipants())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import mafia.mafiatogether.game.domain.Player;

public record RoomResultPlayerDto(
public record GameResultPlayerDto(
String name,
Boolean isAlive,
String job
) {
public static RoomResultPlayerDto of(
public static GameResultPlayerDto of(
final Player player
) {
return new RoomResultPlayerDto(
return new GameResultPlayerDto(
player.getName(),
player.isAlive(),
player.getJobType().name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import java.util.List;
import mafia.mafiatogether.game.domain.Game;

public record RoomResultResponse(
public record GameResultResponse(
String winnerJob,
Timestamp endTime,
List<RoomResultPlayerDto> winner,
List<RoomResultPlayerDto> loser
List<GameResultPlayerDto> winner,
List<GameResultPlayerDto> loser
) {
public static RoomResultResponse from(final Game game) {
final List<RoomResultPlayerDto> winner = game.getWinners()
public static GameResultResponse from(final Game game) {
final List<GameResultPlayerDto> winner = game.getWinners()
.stream()
.map(RoomResultPlayerDto::of)
.map(GameResultPlayerDto::of)
.toList();
final List<RoomResultPlayerDto> loser = game.getLosers()
final List<GameResultPlayerDto> loser = game.getLosers()
.stream()
.map(RoomResultPlayerDto::of)
.map(GameResultPlayerDto::of)
.toList();

return new RoomResultResponse(
return new GameResultResponse(
game.getWinnerJob(),
game.getStatus().getEndTime(),
winner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import mafia.mafiatogether.game.domain.status.StatusType;

public record RoomStatusResponse(
public record GameStatusResponse(
StatusType statusType
) {
}
20 changes: 10 additions & 10 deletions src/main/java/mafia/mafiatogether/game/domain/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import mafia.mafiatogether.game.domain.status.StatusType;
import mafia.mafiatogether.job.domain.jobtype.Job;
import mafia.mafiatogether.job.domain.jobtype.JobType;
import mafia.mafiatogether.room.domain.Room;
import mafia.mafiatogether.room.domain.RoomInfo;
import mafia.mafiatogether.lobby.domain.Lobby;
import mafia.mafiatogether.lobby.domain.LobbyInfo;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.data.redis.core.RedisHash;
Expand All @@ -29,21 +29,21 @@ public class Game extends AbstractAggregateRoot<Game> {
@Id
private String code;
private Status status;
private RoomInfo roomInfo;
private LobbyInfo lobbyInfo;
private String master;
private PlayerCollection players;

private transient Status statusSnapshot;

public static Game create(final Room room, final Long now) {
room.validateToStart();
public static Game create(final Lobby lobby, final Long now) {
lobby.validateToStart();
final Status status = DayIntroStatus.create(now);
return new Game(
room.getCode(),
lobby.getCode(),
status,
room.getRoomInfo(),
room.getMaster().getName(),
PlayerCollection.create(room.getParticipants()),
lobby.getLobbyInfo(),
lobby.getMaster().getName(),
PlayerCollection.create(lobby.getParticipants()),
status
);
}
Expand All @@ -53,7 +53,7 @@ public Game() {
}

public void distributeRole() {
final Queue<Job> jobs = roomInfo.getRandomJobQueue();
final Queue<Job> jobs = lobbyInfo.getRandomJobQueue();
for (Player player : players.getPlayers()) {
if (jobs.isEmpty()) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import mafia.mafiatogether.config.exception.ExceptionCode;
import mafia.mafiatogether.config.exception.PlayerException;
import mafia.mafiatogether.job.domain.jobtype.JobType;
import mafia.mafiatogether.room.domain.Participant;
import mafia.mafiatogether.room.domain.ParticipantCollection;
import mafia.mafiatogether.lobby.domain.Participant;
import mafia.mafiatogether.lobby.domain.ParticipantCollection;

@Getter
@AllArgsConstructor
Expand Down
Loading
Loading