Skip to content

Commit 518c8f5

Browse files
authored
๐Ÿ”– release: v0.0.5
2 parents 6005b83 + 1f88146 commit 518c8f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1800
-347
lines changed

โ€Žbackend/src/main/java/io/f1/backend/domain/admin/api/AdminController.javaโ€Ž

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
import lombok.RequiredArgsConstructor;
88

9+
import org.apache.commons.lang3.StringUtils;
910
import org.springframework.data.domain.Pageable;
1011
import org.springframework.http.ResponseEntity;
1112
import org.springframework.web.bind.annotation.GetMapping;
1213
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestParam;
1315
import org.springframework.web.bind.annotation.RestController;
1416

1517
@RestController
@@ -21,8 +23,15 @@ public class AdminController {
2123

2224
@LimitPageSize
2325
@GetMapping("/users")
24-
public ResponseEntity<UserPageResponse> getUsers(Pageable pageable) {
25-
UserPageResponse response = adminService.getAllUsers(pageable);
26+
public ResponseEntity<UserPageResponse> getUsers(
27+
@RequestParam(required = false) String nickname, Pageable pageable) {
28+
UserPageResponse response;
29+
30+
if (StringUtils.isBlank(nickname)) {
31+
response = adminService.getAllUsers(pageable);
32+
} else {
33+
response = adminService.searchUsersByNickname(nickname, pageable);
34+
}
2635
return ResponseEntity.ok().body(response);
2736
}
2837
}

โ€Žbackend/src/main/java/io/f1/backend/domain/admin/app/AdminService.javaโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ public UserPageResponse getAllUsers(Pageable pageable) {
2424
Page<UserResponse> users = userRepository.findAllUsersWithPaging(pageable);
2525
return toUserListPageResponse(users);
2626
}
27+
28+
@Transactional(readOnly = true)
29+
public UserPageResponse searchUsersByNickname(String nickname, Pageable pageable) {
30+
Page<UserResponse> users = userRepository.findUsersByNicknameContaining(nickname, pageable);
31+
return toUserListPageResponse(users);
32+
}
2733
}

โ€Žbackend/src/main/java/io/f1/backend/domain/auth/dto/CurrentUserAndAdminResponse.javaโ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import io.f1.backend.domain.admin.dto.AdminPrincipal;
44
import io.f1.backend.domain.user.dto.UserPrincipal;
55

6-
public record CurrentUserAndAdminResponse(Long id, String name, String role) {
6+
public record CurrentUserAndAdminResponse(Long id, String name, String role, String providerId) {
77

88
public static CurrentUserAndAdminResponse from(UserPrincipal userPrincipal) {
99
return new CurrentUserAndAdminResponse(
1010
userPrincipal.getUserId(),
1111
userPrincipal.getUserNickname(),
12-
UserPrincipal.ROLE_USER);
12+
UserPrincipal.ROLE_USER,
13+
userPrincipal.getName());
1314
}
1415

1516
public static CurrentUserAndAdminResponse from(AdminPrincipal adminPrincipal) {
1617
return new CurrentUserAndAdminResponse(
1718
adminPrincipal.getAuthenticationAdmin().adminId(),
1819
adminPrincipal.getUsername(),
19-
AdminPrincipal.ROLE_ADMIN);
20+
AdminPrincipal.ROLE_ADMIN,
21+
null);
2022
}
2123
}
Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package io.f1.backend.domain.game.app;
22

3+
import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse;
4+
import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination;
35
import static io.f1.backend.domain.quiz.mapper.QuizMapper.toGameStartResponse;
46

5-
import io.f1.backend.domain.game.dto.request.GameStartRequest;
6-
import io.f1.backend.domain.game.dto.response.GameStartResponse;
7+
import io.f1.backend.domain.game.dto.MessageType;
78
import io.f1.backend.domain.game.event.RoomUpdatedEvent;
8-
import io.f1.backend.domain.game.model.GameSetting;
99
import io.f1.backend.domain.game.model.Player;
1010
import io.f1.backend.domain.game.model.Room;
1111
import io.f1.backend.domain.game.model.RoomState;
1212
import io.f1.backend.domain.game.store.RoomRepository;
13+
import io.f1.backend.domain.game.websocket.MessageSender;
1314
import io.f1.backend.domain.question.entity.Question;
1415
import io.f1.backend.domain.quiz.app.QuizService;
1516
import io.f1.backend.domain.quiz.entity.Quiz;
17+
import io.f1.backend.domain.user.dto.UserPrincipal;
1618
import io.f1.backend.global.exception.CustomException;
1719
import io.f1.backend.global.exception.errorcode.GameErrorCode;
1820
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
@@ -24,56 +26,48 @@
2426

2527
import java.util.List;
2628
import java.util.Map;
29+
import java.util.Objects;
2730

2831
@Service
2932
@RequiredArgsConstructor
3033
public class GameService {
3134

35+
public static final int START_DELAY = 5;
36+
37+
private final MessageSender messageSender;
38+
private final TimerService timerService;
3239
private final QuizService quizService;
3340
private final RoomRepository roomRepository;
3441
private final ApplicationEventPublisher eventPublisher;
3542

36-
public GameStartResponse gameStart(Long roomId, GameStartRequest gameStartRequest) {
43+
public void gameStart(Long roomId, UserPrincipal principal) {
3744

38-
Long quizId = gameStartRequest.quizId();
45+
String destination = getDestination(roomId);
3946

4047
Room room =
4148
roomRepository
4249
.findRoom(roomId)
4350
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
4451

45-
if (!validateReadyStatus(room)) {
46-
throw new CustomException(RoomErrorCode.PLAYER_NOT_READY);
47-
}
48-
49-
// ๋ฐฉ์˜ gameSetting์— ์„ค์ •๋œ ํ€ด์ฆˆ๋ž‘ ์š”์ฒญ ํ€ด์ฆˆ๋ž‘ ๊ฐ™์€์ง€ ์ฒดํฌ ํ›„ GameSetting์—์„œ ๋ผ์šด๋“œ ๊ฐ€์ ธ์˜ค๊ธฐ
50-
Integer round = checkGameSetting(room, quizId);
52+
validateRoomStart(room, principal);
5153

54+
Long quizId = room.getGameSetting().getQuizId();
5255
Quiz quiz = quizService.getQuizWithQuestionsById(quizId);
56+
List<Question> questions = prepareQuestions(room, quiz);
5357

54-
// ๋ผ์šด๋“œ ์ˆ˜๋งŒํผ ๋žœ๋ค Question ์ถ”์ถœ
55-
List<Question> questions = quizService.getRandomQuestionsWithoutAnswer(quizId, round);
5658
room.updateQuestions(questions);
57-
58-
GameStartResponse gameStartResponse = toGameStartResponse(questions);
59-
60-
// ๋ฐฉ ์ •๋ณด ๊ฒŒ์ž„ ์ค‘์œผ๋กœ ๋ณ€๊ฒฝ
59+
room.increaseCurrentRound();
6160
room.updateRoomState(RoomState.PLAYING);
6261

6362
eventPublisher.publishEvent(new RoomUpdatedEvent(room, quiz));
6463

65-
return gameStartResponse;
66-
}
67-
68-
private Integer checkGameSetting(Room room, Long quizId) {
69-
70-
GameSetting gameSetting = room.getGameSetting();
71-
72-
if (!gameSetting.validateQuizId(quizId)) {
73-
throw new CustomException(GameErrorCode.GAME_SETTING_CONFLICT);
74-
}
64+
timerService.startTimer(room, START_DELAY);
7565

76-
return gameSetting.getRound();
66+
messageSender.send(destination, MessageType.GAME_START, toGameStartResponse(questions));
67+
messageSender.send(
68+
destination,
69+
MessageType.QUESTION_START,
70+
toQuestionStartResponse(room, START_DELAY));
7771
}
7872

7973
private boolean validateReadyStatus(Room room) {
@@ -82,4 +76,25 @@ private boolean validateReadyStatus(Room room) {
8276

8377
return playerSessionMap.values().stream().allMatch(Player::isReady);
8478
}
79+
80+
private void validateRoomStart(Room room, UserPrincipal principal) {
81+
if (!Objects.equals(principal.getUserId(), room.getHost().getId())) {
82+
throw new CustomException(RoomErrorCode.NOT_ROOM_OWNER);
83+
}
84+
85+
if (!validateReadyStatus(room)) {
86+
throw new CustomException(GameErrorCode.PLAYER_NOT_READY);
87+
}
88+
89+
if (room.getState() == RoomState.PLAYING) {
90+
throw new CustomException(RoomErrorCode.GAME_ALREADY_PLAYING);
91+
}
92+
}
93+
94+
// ๋ผ์šด๋“œ ์ˆ˜๋งŒํผ ๋žœ๋ค Question ์ถ”์ถœ
95+
private List<Question> prepareQuestions(Room room, Quiz quiz) {
96+
Long quizId = quiz.getId();
97+
Integer round = room.getGameSetting().getRound();
98+
return quizService.getRandomQuestionsWithoutAnswer(quizId, round);
99+
}
85100
}

0 commit comments

Comments
ย (0)