Skip to content

Commit

Permalink
Merge pull request #12 from urdego/feature/#11
Browse files Browse the repository at this point in the history
Feature/#11 : 타 서비스 Feign 연결
  • Loading branch information
oo-ni authored Jan 22, 2025
2 parents 36c4315 + 5bb4754 commit 1f6d1e0
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@

import java.util.List;

@FeignClient(name = "content-service")
@FeignClient(name = "content-service", url = "${feign.client.config.content-service.url}")
public interface ContentServiceClient {
@GetMapping("/random")
List<ContentRes> getRandomContents(@RequestParam List<String> players);
@GetMapping("/api/content-service/content")
ContentRes getContent(@RequestParam Long contentId);

@GetMapping("/content")
ContentRes getContent(@RequestParam String contentId);

@GetMapping("/urdego-content")
@GetMapping("/api/content-service/urdego-content")
List<ContentRes> getUrdegoContents(@RequestParam int counts);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.urdego.urdego_game_service.common.client.dto;

public record ContentRes(
String contentId,
Long contentId,
String contentName,
String address,
double latitude,
double longitude,
String hint
) {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.urdego.urdego_game_service.controller.game;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.urdego.urdego_game_service.controller.game.dto.request.GameCreateReq;
import io.urdego.urdego_game_service.controller.game.dto.response.GameCreateRes;
import io.urdego.urdego_game_service.domain.game.service.GameService;
Expand All @@ -19,6 +21,8 @@ public class GameController {
private final GameService gameService;

// 게임 생성
@Tag(name = "게임 API")
@Operation(summary = "게임 생성", description = "roomId로 게임 생성")
@PostMapping("/game/start")
public ResponseEntity<GameCreateRes> startGame(@RequestBody GameCreateReq request) {
GameCreateRes response = gameService.createGame(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.urdego.urdego_game_service.controller.room;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.urdego.urdego_game_service.controller.room.dto.request.RoomCreateReq;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomCreateRes;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomInfoRes;
import io.urdego.urdego_game_service.domain.room.service.RoomService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -19,10 +21,21 @@ public class RoomController {
private final RoomService roomService;

// 대기방 생성
@Tag(name = "대기방 API")
@Operation(summary = "대기방 생성", description = "방장이 입력한 정보로 대기방 생성")
@PostMapping("/room/create")
public ResponseEntity<RoomCreateRes> createRoom(@RequestBody RoomCreateReq request) {
RoomCreateRes response = roomService.createRoom(request);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

// 대기방 목록 조회
@Tag(name = "대기방 API")
@Operation(summary = "대기방 목록 조회", description = "현재 생성되어 있는 대기방 목록 전체 조회")
@GetMapping("/room/list")
public ResponseEntity<List<RoomInfoRes>> viewRooms() {
List<RoomInfoRes> response = roomService.getRoomList();
return new ResponseEntity<>(response, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.urdego.urdego_game_service.controller.room.dto.request.ContentSelectReq;
import io.urdego.urdego_game_service.controller.room.dto.request.PlayerReq;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomInfoRes;
import io.urdego.urdego_game_service.controller.room.dto.response.PlayerRes;
import io.urdego.urdego_game_service.domain.room.service.RoomService;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
Expand All @@ -19,7 +19,7 @@ public class RoomSocketController {
// 친구 초대
@MessageMapping("/room/player/invite")
public void invitePlayer(PlayerReq request) {
RoomInfoRes response = roomService.joinRoom(request);
PlayerRes response = roomService.joinRoom(request);
messagingTemplate.convertAndSend("/game-service/sub/" + response.roomId(), response);
}

Expand All @@ -32,7 +32,7 @@ public void selectContent(ContentSelectReq request) {
// 플레이어 삭제
@MessageMapping("/room/player/remove")
public void removePlayer(PlayerReq request) {
RoomInfoRes response = roomService.removePlayer(request);
PlayerRes response = roomService.removePlayer(request);
messagingTemplate.convertAndSend("/game-service/sub/" + response.roomId(), response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public record RoomCreateReq(
String userId,
String roomName,
int maxPlayers,
int totalRounds,
int timer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.urdego.urdego_game_service.controller.room.dto.response;

import io.urdego.urdego_game_service.common.enums.Status;
import io.urdego.urdego_game_service.domain.room.entity.Room;

import java.util.List;

public record PlayerRes(
String roomId,
Status status,
List<String> currentPlayers
) {
public static PlayerRes from(Room room) {
return new PlayerRes(
room.getRoomId(),
room.getStatus(),
room.getCurrentPlayers()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import io.urdego.urdego_game_service.common.enums.Status;
import io.urdego.urdego_game_service.domain.room.entity.Room;

import java.util.List;

public record RoomInfoRes(
String roomId,
Status status,
List<String> currentPlayers
String roomName,
int maxPlayers,
int currentPlayersCount,
int totalRounds
) {
public static RoomInfoRes from(Room room) {
return new RoomInfoRes(
room.getRoomId(),
room.getStatus(),
room.getCurrentPlayers()
room.getRoomName(),
room.getMaxPlayers(),
room.getCurrentPlayers().size(),
room.getTotalRounds()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RoundSocketController {
@MessageMapping("/round/question")
public void giveQuestion(QuestionReq request) {
QuestionRes response = roundService.getQuestion(request);
messagingTemplate.convertAndSend("game-service/sub/" + response.roomId(), response);
messagingTemplate.convertAndSend("/game-service/sub/" + response.roomId(), response);
}

// 플레이어 정답 제출
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public GameCreateRes createGame(GameCreateReq request) {

// Question 생성
for (int roundNum = 1; roundNum <= room.getTotalRounds(); roundNum++) {
Question question = roundService.createQuestion(game.getRoomId());
Question question = roundService.createQuestion(game.getRoomId(), roundNum);
game.getQuestionIds().add(question.getQuestionId());
}

Expand Down Expand Up @@ -109,8 +109,17 @@ public Game updateGameStatusById(String gameId, Status status) {
@Transactional(readOnly = true)
@Override
public Game findGameById(String gameId) {
return gameRepository.findById(gameId)
Game game = gameRepository.findById(gameId)
.orElseThrow(() -> new GameException(ExceptionMessage.GAME_NOT_FOUND));

if (game.getRoundScores() == null) {
game.setRoundScores(new HashMap<>());
}
if (game.getTotalScores() == null) {
game.setTotalScores(new HashMap<>());
}

return game;
}

// 라운드 점수 업뎃
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Room {

@Id
private String roomId;
private String roomName;
private Status status;
private int maxPlayers;
private int totalRounds;
Expand All @@ -25,8 +26,9 @@ public class Room {
// private String gameType;

@Builder
public Room(String roomId, Status status, int maxPlayers, int totalRounds, int timer, List<String> currentPlayers, Map<String, List<String>> playerContents) {
public Room(String roomId, String roomName, Status status, int maxPlayers, int totalRounds, int timer, List<String> currentPlayers, Map<String, List<String>> playerContents) {
this.roomId = roomId;
this.roomName = roomName;
this.status = status;
this.maxPlayers = maxPlayers;
this.totalRounds = totalRounds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
import io.urdego.urdego_game_service.controller.room.dto.request.ContentSelectReq;
import io.urdego.urdego_game_service.controller.room.dto.request.PlayerReq;
import io.urdego.urdego_game_service.controller.room.dto.request.RoomCreateReq;
import io.urdego.urdego_game_service.controller.room.dto.response.PlayerRes;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomCreateRes;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomInfoRes;
import io.urdego.urdego_game_service.common.enums.Status;
import io.urdego.urdego_game_service.domain.room.entity.Room;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface RoomService {

// 방 생성
RoomCreateRes createRoom(RoomCreateReq request);

// 대기방 리스트 조회
List<RoomInfoRes> getRoomList();

// 방 참가
RoomInfoRes joinRoom(PlayerReq request);
PlayerRes joinRoom(PlayerReq request);

// 컨텐츠 등록
void registerContents(ContentSelectReq request);

// 플레이어 삭제
RoomInfoRes removePlayer(PlayerReq request);
PlayerRes removePlayer(PlayerReq request);

// 방 상태 변경
Room updateRoomStatusById(String roomId, Status status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.urdego.urdego_game_service.controller.room.dto.request.ContentSelectReq;
import io.urdego.urdego_game_service.controller.room.dto.request.PlayerReq;
import io.urdego.urdego_game_service.controller.room.dto.request.RoomCreateReq;
import io.urdego.urdego_game_service.controller.room.dto.response.PlayerRes;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomCreateRes;
import io.urdego.urdego_game_service.controller.room.dto.response.RoomInfoRes;
import io.urdego.urdego_game_service.common.enums.Status;
Expand All @@ -11,29 +12,33 @@
import io.urdego.urdego_game_service.domain.room.entity.Room;
import io.urdego.urdego_game_service.domain.room.repository.RoomRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.*;

@Service
@Transactional
@RequiredArgsConstructor
public class RoomServiceImpl implements RoomService {

private final RoomRepository roomRepository;
private final RedisTemplate<String, Object> redisTemplate;


// 대기방 생성
@Override
public RoomCreateRes createRoom(RoomCreateReq request) {
List<String> currentPlayers = new ArrayList<>();
currentPlayers.add(request.userId());

String roomName = (request.roomName() == null || request.roomName().isBlank())
? "어데고 게임방" : request.roomName();

Room room = Room.builder()
.roomId(UUID.randomUUID().toString())
.roomName(roomName)
.status(Status.WAITING)
.maxPlayers(request.maxPlayers())
.totalRounds(request.totalRounds())
Expand All @@ -47,9 +52,24 @@ public RoomCreateRes createRoom(RoomCreateReq request) {
return RoomCreateRes.from(room);
}

// 대기방 리스트 조회
@Override
public List<RoomInfoRes> getRoomList() {
Iterable<Room> rooms = roomRepository.findAll();
List<RoomInfoRes> roomList = new ArrayList<>();

for (Room room : rooms) {
if (room != null) {
roomList.add(RoomInfoRes.from(room));
}
}

return roomList;
}

// 대기방 참여
@Override
public RoomInfoRes joinRoom(PlayerReq request) {
public PlayerRes joinRoom(PlayerReq request) {
Room room = findRoomById(request.roomId());
if (room.getCurrentPlayers().size() >= room.getMaxPlayers()) {
throw new RoomException(ExceptionMessage.ROOM_FULL);
Expand All @@ -58,7 +78,7 @@ public RoomInfoRes joinRoom(PlayerReq request) {

roomRepository.save(room);

return RoomInfoRes.from(room);
return PlayerRes.from(room);
}

// 컨텐츠 등록
Expand All @@ -75,7 +95,7 @@ public void registerContents(ContentSelectReq request) {

// 플레이어 삭제
@Override
public RoomInfoRes removePlayer(PlayerReq request) {
public PlayerRes removePlayer(PlayerReq request) {
Room room = findRoomById(request.roomId());

if (!room.getCurrentPlayers().contains(request.userId().toString())) {
Expand All @@ -87,7 +107,7 @@ public RoomInfoRes removePlayer(PlayerReq request) {

roomRepository.save(room);

return RoomInfoRes.from(room);
return PlayerRes.from(room);
}

// 대기방 상태 변경
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class Question {
private List<String> contents;

@Builder
public Question(String questionId, String roomId, int roundNum, double latitude, double longitude, String hint, List<String> contents) {
this.questionId = questionId;
public Question(String roomId, int roundNum, double latitude, double longitude, String hint, List<String> contents) {
this.questionId = roomId + ":" + roundNum;
this.roomId = roomId;
this.roundNum = roundNum;
this.latitude = latitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Repository
public interface QuestionRepository extends CrudRepository<Question, String> {
Optional<Question> findByRoomId(String roomId);
Optional<Question> findByRoomIdAndRoundNum(String roomId, int roundNum);

List<Question> findAllByRoomId(String roomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public interface RoundService {
// 문제 생성
Question createQuestion(String roomId);
Question createQuestion(String roomId, int roundNum);

// 문제 출제
QuestionRes getQuestion(QuestionReq request);
Expand Down
Loading

0 comments on commit 1f6d1e0

Please sign in to comment.