Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import lombok.RequiredArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -21,8 +23,15 @@ public class AdminController {

@LimitPageSize
@GetMapping("/users")
public ResponseEntity<UserPageResponse> getUsers(Pageable pageable) {
UserPageResponse response = adminService.getAllUsers(pageable);
public ResponseEntity<UserPageResponse> getUsers(
@RequestParam(required = false) String nickname, Pageable pageable) {
UserPageResponse response;

if (StringUtils.isBlank(nickname)) {
response = adminService.getAllUsers(pageable);
} else {
response = adminService.searchUsersByNickname(nickname, pageable);
}
return ResponseEntity.ok().body(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public UserPageResponse getAllUsers(Pageable pageable) {
Page<UserResponse> users = userRepository.findAllUsersWithPaging(pageable);
return toUserListPageResponse(users);
}

@Transactional(readOnly = true)
public UserPageResponse searchUsersByNickname(String nickname, Pageable pageable) {
Page<UserResponse> users = userRepository.findUsersByNicknameContaining(nickname, pageable);
return toUserListPageResponse(users);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import io.f1.backend.domain.admin.dto.AdminPrincipal;
import io.f1.backend.domain.user.dto.UserPrincipal;

public record CurrentUserAndAdminResponse(Long id, String name, String role) {
public record CurrentUserAndAdminResponse(Long id, String name, String role, String providerId) {

public static CurrentUserAndAdminResponse from(UserPrincipal userPrincipal) {
return new CurrentUserAndAdminResponse(
userPrincipal.getUserId(),
userPrincipal.getUserNickname(),
UserPrincipal.ROLE_USER);
UserPrincipal.ROLE_USER,
userPrincipal.getName());
}

public static CurrentUserAndAdminResponse from(AdminPrincipal adminPrincipal) {
return new CurrentUserAndAdminResponse(
adminPrincipal.getAuthenticationAdmin().adminId(),
adminPrincipal.getUsername(),
AdminPrincipal.ROLE_ADMIN);
AdminPrincipal.ROLE_ADMIN,
null);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.f1.backend.domain.game.app;

import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse;
import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination;
import static io.f1.backend.domain.quiz.mapper.QuizMapper.toGameStartResponse;

import io.f1.backend.domain.game.dto.request.GameStartRequest;
import io.f1.backend.domain.game.dto.response.GameStartResponse;
import io.f1.backend.domain.game.dto.MessageType;
import io.f1.backend.domain.game.event.RoomUpdatedEvent;
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.RoomState;
import io.f1.backend.domain.game.store.RoomRepository;
import io.f1.backend.domain.game.websocket.MessageSender;
import io.f1.backend.domain.question.entity.Question;
import io.f1.backend.domain.quiz.app.QuizService;
import io.f1.backend.domain.quiz.entity.Quiz;
import io.f1.backend.domain.user.dto.UserPrincipal;
import io.f1.backend.global.exception.CustomException;
import io.f1.backend.global.exception.errorcode.GameErrorCode;
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
Expand All @@ -24,56 +26,48 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class GameService {

public static final int START_DELAY = 5;

private final MessageSender messageSender;
private final TimerService timerService;
private final QuizService quizService;
private final RoomRepository roomRepository;
private final ApplicationEventPublisher eventPublisher;

public GameStartResponse gameStart(Long roomId, GameStartRequest gameStartRequest) {
public void gameStart(Long roomId, UserPrincipal principal) {

Long quizId = gameStartRequest.quizId();
String destination = getDestination(roomId);

Room room =
roomRepository
.findRoom(roomId)
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));

if (!validateReadyStatus(room)) {
throw new CustomException(RoomErrorCode.PLAYER_NOT_READY);
}

// 방의 gameSetting에 설정된 퀴즈랑 요청 퀴즈랑 같은지 체크 후 GameSetting에서 라운드 가져오기
Integer round = checkGameSetting(room, quizId);
validateRoomStart(room, principal);

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

// 라운드 수만큼 랜덤 Question 추출
List<Question> questions = quizService.getRandomQuestionsWithoutAnswer(quizId, round);
room.updateQuestions(questions);

GameStartResponse gameStartResponse = toGameStartResponse(questions);

// 방 정보 게임 중으로 변경
room.increaseCurrentRound();
room.updateRoomState(RoomState.PLAYING);

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

return gameStartResponse;
}

private Integer checkGameSetting(Room room, Long quizId) {

GameSetting gameSetting = room.getGameSetting();

if (!gameSetting.validateQuizId(quizId)) {
throw new CustomException(GameErrorCode.GAME_SETTING_CONFLICT);
}
timerService.startTimer(room, START_DELAY);

return gameSetting.getRound();
messageSender.send(destination, MessageType.GAME_START, toGameStartResponse(questions));
messageSender.send(
destination,
MessageType.QUESTION_START,
toQuestionStartResponse(room, START_DELAY));
}

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

return playerSessionMap.values().stream().allMatch(Player::isReady);
}

private void validateRoomStart(Room room, UserPrincipal principal) {
if (!Objects.equals(principal.getUserId(), room.getHost().getId())) {
throw new CustomException(RoomErrorCode.NOT_ROOM_OWNER);
}

if (!validateReadyStatus(room)) {
throw new CustomException(GameErrorCode.PLAYER_NOT_READY);
}

if (room.getState() == RoomState.PLAYING) {
throw new CustomException(RoomErrorCode.GAME_ALREADY_PLAYING);
}
}

// 라운드 수만큼 랜덤 Question 추출
private List<Question> prepareQuestions(Room room, Quiz quiz) {
Long quizId = quiz.getId();
Integer round = room.getGameSetting().getRound();
return quizService.getRandomQuestionsWithoutAnswer(quizId, round);
}
}
Loading