-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] 게임방 생성 api 구현 #13
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7d75568
:sparkles: feat: 게임 방 생성 구현
sehee123 6eb0942
:sparkles: feat: repository, service 책임 분리, mapper추가
sehee123 5697d6f
chore: Java 스타일 수정
797ed65
:recycle: 리뷰 반영 수정
sehee123 f41b2e2
Merge remote-tracking branch 'origin/feat/12' into feat/12
sehee123 07f6413
chore: Java 스타일 수정
2924c0c
:adhesive_bandage: 컨트롤러 import 추가
sehee123 2bee9e9
Merge remote-tracking branch 'origin/feat/12' into feat/12
sehee123 abbf606
chore: Java 스타일 수정
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
backend/src/main/java/io/f1/backend/domain/game/Player.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
backend/src/main/java/io/f1/backend/domain/game/RoomManager.java
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
backend/src/main/java/io/f1/backend/domain/game/RoomSetting.java
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/io/f1/backend/domain/game/api/RoomController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.f1.backend.domain.game.api; | ||
|
|
||
| import io.f1.backend.domain.game.app.RoomService; | ||
| import io.f1.backend.domain.game.dto.request.RoomCreateRequest; | ||
| import io.f1.backend.domain.game.dto.response.RoomCreateResponse; | ||
|
|
||
| import jakarta.validation.Valid; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/rooms") | ||
| @RequiredArgsConstructor | ||
| public class RoomController { | ||
|
|
||
| private final RoomService roomService; | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| public RoomCreateResponse saveRoom(@RequestBody @Valid RoomCreateRequest request) { | ||
|
|
||
| Map<String, Object> loginUser = new HashMap<>(); | ||
| loginUser.put("id", 1L); | ||
| loginUser.put("nickname", "빵야빵야"); | ||
|
|
||
| return roomService.saveRoom(request, loginUser); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package io.f1.backend.domain.game.app; | ||
|
|
||
| import static io.f1.backend.domain.game.mapper.RoomMapper.toRoomSetting; | ||
|
|
||
| import io.f1.backend.domain.game.dto.request.RoomCreateRequest; | ||
| import io.f1.backend.domain.game.dto.response.RoomCreateResponse; | ||
| import io.f1.backend.domain.game.model.GameSetting; | ||
| import io.f1.backend.domain.game.model.Player; | ||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.model.RoomSetting; | ||
| import io.f1.backend.domain.game.store.RoomRepository; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RoomService { | ||
|
|
||
| private final RoomRepository roomRepository; | ||
| private final AtomicLong roomIdGenerator = new AtomicLong(0); | ||
|
|
||
| public RoomCreateResponse saveRoom(RoomCreateRequest request, Map<String, Object> loginUser) { | ||
|
|
||
| // todo 제일 작은 index quizId 가져와서 gameSetting(round 설정) | ||
| GameSetting gameSetting = new GameSetting(1L, 10, 60); | ||
| // todo security에서 가져오는걸로 변경 | ||
| Player host = new Player((Long) loginUser.get("id"), loginUser.get("nickname").toString()); | ||
| RoomSetting roomSetting = toRoomSetting(request); | ||
|
|
||
| Long newId = roomIdGenerator.incrementAndGet(); | ||
|
|
||
| roomRepository.saveRoom(new Room(newId, roomSetting, gameSetting, host)); | ||
|
|
||
| return new RoomCreateResponse(newId); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/io/f1/backend/domain/game/dto/request/RoomCreateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record RoomCreateRequest( | ||
| @NotBlank(message = "방 제목은 필수입니다.") String roomName, | ||
| @NotNull(message = "인원 수 입력은 필수입니다.") | ||
| @Min(value = 2, message = "방 인원 수는 최소 2명입니다.") | ||
| @Max(value = 8, message = "방 인원 수는 최대 8명 입니다.") | ||
| Integer maxUserCount, | ||
| @NotNull String password, | ||
| @NotNull boolean locked) {} |
3 changes: 3 additions & 0 deletions
3
backend/src/main/java/io/f1/backend/domain/game/dto/response/RoomCreateResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package io.f1.backend.domain.game.dto.response; | ||
|
|
||
| public record RoomCreateResponse(Long roomId) {} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/io/f1/backend/domain/game/mapper/RoomMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.f1.backend.domain.game.mapper; | ||
|
|
||
| import io.f1.backend.domain.game.dto.request.RoomCreateRequest; | ||
| import io.f1.backend.domain.game.model.RoomSetting; | ||
|
|
||
| public class RoomMapper { | ||
|
|
||
| public static RoomSetting toRoomSetting(RoomCreateRequest request) { | ||
| return new RoomSetting( | ||
| request.roomName(), request.maxUserCount(), request.locked(), request.password()); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
.../backend/domain/game/ConnectionState.java → ...nd/domain/game/model/ConnectionState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...o/f1/backend/domain/game/GameSetting.java → ...ackend/domain/game/model/GameSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/io/f1/backend/domain/game/model/Player.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package io.f1.backend.domain.game.model; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class Player { | ||
|
|
||
| public final Long id; | ||
|
|
||
| public final String nickname; | ||
|
|
||
| private boolean isReady = false; | ||
|
|
||
| private ConnectionState state = ConnectionState.CONNECTED; | ||
|
|
||
| private int correctCount = 0; | ||
|
|
||
| public Player(Long id, String nickname) { | ||
| this.id = id; | ||
| this.nickname = nickname; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/io/f1/backend/domain/game/model/Room.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.f1.backend.domain.game.model; | ||
|
|
||
| import io.f1.backend.domain.question.entity.Question; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Getter | ||
| public class Room { | ||
|
|
||
| private final Long id; | ||
|
|
||
| private final RoomSetting roomSetting; | ||
|
|
||
| private GameSetting gameSetting; | ||
|
|
||
| private RoomState state = RoomState.WAITING; | ||
|
|
||
| private Player host; | ||
|
|
||
| private List<Question> questions = new ArrayList<>(); | ||
|
|
||
| private Map<String, Player> playerSessionMap = new ConcurrentHashMap<>(); | ||
|
|
||
| private final LocalDateTime createdAt = LocalDateTime.now(); | ||
|
|
||
| public Room(Long id, RoomSetting roomSetting, GameSetting gameSetting, Player host) { | ||
| this.id = id; | ||
| this.roomSetting = roomSetting; | ||
| this.gameSetting = gameSetting; | ||
| this.host = host; | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
backend/src/main/java/io/f1/backend/domain/game/model/RoomSetting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package io.f1.backend.domain.game.model; | ||
|
|
||
| public record RoomSetting(String roomName, int maxUserCount, boolean locked, String password) {} |
2 changes: 1 addition & 1 deletion
2
.../io/f1/backend/domain/game/RoomState.java → .../backend/domain/game/model/RoomState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package io.f1.backend.domain.game; | ||
| package io.f1.backend.domain.game.model; | ||
|
|
||
| public enum RoomState { | ||
| WAITING, | ||
|
|
||
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/io/f1/backend/domain/game/store/RoomRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.f1.backend.domain.game.store; | ||
|
|
||
| import io.f1.backend.domain.game.model.Room; | ||
|
|
||
| public interface RoomRepository { | ||
| void saveRoom(Room room); | ||
| } |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/io/f1/backend/domain/game/store/RoomRepositoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package io.f1.backend.domain.game.store; | ||
|
|
||
| import io.f1.backend.domain.game.model.Room; | ||
|
|
||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| @Repository | ||
| public class RoomRepositoryImpl implements RoomRepository { | ||
|
|
||
| private final Map<Long, Room> roomMap = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public void saveRoom(Room room) { | ||
| roomMap.put(room.getId(), room); | ||
| } | ||
|
|
||
| // 테스트 전용 메소드 | ||
| public Room getRoomForTest(Long roomId) { | ||
| return roomMap.get(roomId); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
backend/src/test/java/io/f1/backend/domain/game/store/RoomRepositoryTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package io.f1.backend.domain.game.store; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.f1.backend.domain.game.dto.request.RoomCreateRequest; | ||
| import io.f1.backend.domain.game.model.GameSetting; | ||
| import io.f1.backend.domain.game.model.Player; | ||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.model.RoomSetting; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| class RoomRepositoryTests { | ||
|
|
||
| RoomRepositoryImpl roomRepository; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| roomRepository = new RoomRepositoryImpl(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("게임 방 생성 테스트") | ||
| void saveRoom_test() { | ||
|
|
||
| RoomCreateRequest request = new RoomCreateRequest("방제", 3, "password1", true); | ||
| Map<String, Object> loginUser = new HashMap<>(); | ||
|
|
||
| loginUser.put("id", 1L); | ||
| loginUser.put("nickname", "빵야빵야"); | ||
|
|
||
| GameSetting gameSetting = new GameSetting(1L, 10, 60); | ||
|
|
||
| Player host = new Player((Long) loginUser.get("id"), loginUser.get("nickname").toString()); | ||
|
|
||
| RoomSetting roomSetting = | ||
| new RoomSetting( | ||
| request.roomName(), | ||
| request.maxUserCount(), | ||
| request.locked(), | ||
| request.password()); | ||
|
|
||
| Long newId = 1L; | ||
|
|
||
| Room newRoom = new Room(newId, roomSetting, gameSetting, host); | ||
|
|
||
| roomRepository.saveRoom(newRoom); | ||
|
|
||
| Room savedRoom = roomRepository.getRoomForTest(newId); | ||
|
|
||
| assertThat(savedRoom.getHost().getId()).isEqualTo(loginUser.get("id")); | ||
| assertThat(savedRoom.getHost().getNickname()).isEqualTo(loginUser.get("nickname")); | ||
|
|
||
| assertThat(savedRoom.getRoomSetting().roomName()).isEqualTo(request.roomName()); | ||
| assertThat(savedRoom.getRoomSetting().maxUserCount()).isEqualTo(request.maxUserCount()); | ||
| assertThat(savedRoom.getRoomSetting().locked()).isEqualTo(request.locked()); | ||
| assertThat(savedRoom.getRoomSetting().password()).isEqualTo(request.password()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.