Skip to content

Commit

Permalink
fix : #5 반환값 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
leestana01 committed Aug 6, 2024
1 parent ae8f89b commit 50a2dce
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public StartCheckResDto checkStart(@DestinationVariable String roomId, UserPosit
}

@MessageMapping("/update_position/{roomId}")
@SendTo("/room/{roomId}")
public PositionUpdateResDto updatePosition(@DestinationVariable String roomId, UserPosition userPosition, Authentication authentication) {
// @SendTo("/room/{roomId}")
public void updatePosition(@DestinationVariable String roomId, UserPosition userPosition, Authentication authentication) {
String userId = authentication.getName();
return gameService.updatePosition(roomId, userId, userPosition);
gameService.updatePosition(roomId, userId, userPosition);
}

@MessageMapping("/surrender/{roomId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package likelion.hufsglobal.lgtu.runwithmate.domain.game;

import jakarta.persistence.*;
import likelion.hufsglobal.lgtu.runwithmate.domain.game.type.FinishType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -15,8 +16,12 @@ public class GameInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String type="game_finished";
private FinishType finishType;
private String roomId;
private Long betPoint;
private String winnerId;
private String winnerName;
@OneToMany(cascade = CascadeType.ALL)
private List<GameInfoForUser> usersInfo = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GameInfoForUser {
private Long id;

private String userId;
private String userName;
private Long dopamine;
private Long point;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
public class GameFinishResDto {
private final String type = "game_finished";
private FinishType finishType; // 두 개로 해야하는디 흠
private String winner;
private String roomId;
private Long betPoint;
private String winnerId;
private String winnerName;
private List<GameFinishInfoForUser> usersInfo;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package likelion.hufsglobal.lgtu.runwithmate.domain.game.type;

public enum FinishType {
TIME_EXCEED, PLAYER_SURRENDER
TIME_OVER, PLAYER_SURRENDER
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
public class GameRoomService {

private final RedisTemplate<String, Object> redisTemplate;
private static final long DEFAULT_BET_POINT = 1000L;
private static final long DEFAULT_TIME_LIMIT = 60L;
private static final int DEFAULT_BET_POINT = 1000;
private static final int DEFAULT_TIME_LIMIT = 60;

private final UserRepository userRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,26 @@ private BoxInfo createBox(BoxType boxType, Double range, Double division, long i
}

private Long calcRunTime(String roomId){
ObjectMapper mapper = new ObjectMapper();
Object startTimeObject = redisTemplate.opsForHash().get("game_rooms:" + roomId, "start_time");

if (startTimeObject instanceof String) {
// 저장된 문자열을 LocalDateTime으로 변환
LocalDateTime startTime = LocalDateTime.parse((String) startTimeObject, formatter);
LocalDateTime currentTime = LocalDateTime.now();
Duration duration = Duration.between(startTime, currentTime);
return duration.getSeconds();
} else {
throw new IllegalStateException("게임 시작 시간을 인식할 수 없습니다. String 타입이 아닙니다.");
}
LocalDateTime startTime = LocalDateTime.parse(mapper.convertValue(startTimeObject, String.class), formatter);
LocalDateTime currentTime = LocalDateTime.now();
Duration duration = Duration.between(startTime, currentTime);
return duration.getSeconds();
}

private Long calcTimeLeft(String roomId){
Long runTime = calcRunTime(roomId);
Long timeLimit = Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "time_limit"));;
return timeLimit - runTime;
ObjectMapper mapper = new ObjectMapper();

Object object = redisTemplate.opsForHash().get("game_rooms:" + roomId, "time_limit");;
return mapper.convertValue(object, Long.class) - runTime;
}

// -------------------------------------------------

public PositionUpdateResDto updatePosition(String roomId, String userId, UserPosition position) {
public void updatePosition(String roomId, String userId, UserPosition position) {
/**
* 1. 해당 플레이어 위치를 `player_position:방번호`방번호 에서 찾음
* 2. (선택) 현재 위치와 이전 위치의 오차를 계산하고, 비정상적인 변동인지 파악함
Expand All @@ -204,7 +202,11 @@ public PositionUpdateResDto updatePosition(String roomId, String userId, UserPos
positionUpdateResDto.setPosition(position);
positionUpdateResDto.setTimeLeft(calcTimeLeft(roomId));

return positionUpdateResDto;
messagingTemplate.convertAndSend("/room/" + roomId, positionUpdateResDto);
// 시간 종료 체크
if (calcTimeLeft(roomId) <= 0) {
messagingTemplate.convertAndSend("/room/" + roomId, finishGame(roomId, FinishType.TIME_OVER, ""));
}
}

// -------------------------------------------------
Expand Down Expand Up @@ -327,24 +329,35 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String
String userTwoId = (String) redisTemplate.opsForHash().get("game_rooms:"+roomId,"user2_id");

// user1 인게임 도파민/포인트 빼오기
Map<String, Long> userOnePoints = (Map<String, Long>) redisTemplate.opsForHash().get("player_points:" + roomId, userOneId);
Long userOneDopamine = userOnePoints.get("dopamine");
Long userOneGamePoint = userOnePoints.get("point");
ObjectMapper mapper = new ObjectMapper();
Object object;
object = redisTemplate.opsForHash().get("player_points:" + roomId, userOneId);
Map<String, Integer> userOnePoints = mapper.convertValue(object, Map.class);
Long userOneDopamine = Long.valueOf((Integer)userOnePoints.get("dopamine"));
Long userOneGamePoint = Long.valueOf((Integer)userOnePoints.get("point"));

// user2 인게임 도파민/포인트 빼오기
Map<String, Long> userTwoPoints = (Map<String, Long>) redisTemplate.opsForHash().get("player_points:" + roomId, userTwoId);
Long userTwoDopamine = userTwoPoints.get("dopamine");
Long userTwoGamePoint = userTwoPoints.get("point");
object = redisTemplate.opsForHash().get("player_points:" + roomId, userTwoId);
Map<String, Integer> userTwoPoints = mapper.convertValue(object, Map.class);
Long userTwoDopamine = Long.valueOf((Integer)userTwoPoints.get("dopamine"));
Long userTwoGamePoint = Long.valueOf((Integer)userTwoPoints.get("point"));


boolean isUserOneWin = userOneDopamine >= userTwoDopamine;
if (finishType.equals(FinishType.PLAYER_SURRENDER)){
isUserOneWin = !surrenderId.equals(userOneId);
}

// --- Response DTO 만들기 ---

GameFinishResDto gameFinishResDto = new GameFinishResDto();
gameFinishResDto.setFinishType(finishType);
gameFinishResDto.setWinner(isUserOneWin ? userOneId : userTwoId);
gameFinishResDto.setRoomId(roomId);
gameFinishResDto.setBetPoint(Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point")));

String winnerId = isUserOneWin ? userOneId : userTwoId;
gameFinishResDto.setWinnerId(isUserOneWin ? userOneId : userTwoId);
gameFinishResDto.setWinnerName(userRepository.findByUserId(winnerId).orElseThrow(() -> new IllegalArgumentException("User not found")).getNickname());

User userOne = userRepository.findByUserId(userOneId).orElseThrow(() -> new IllegalArgumentException("User not found"));
String userOneName = userOne.getNickname();
Expand All @@ -371,12 +384,14 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String
// user1 GameInfoForUser에 저장
GameInfoForUser newGameInfoForUser1 = new GameInfoForUser();
newGameInfoForUser1.setUserId(userOneId);
newGameInfoForUser1.setUserName(userOneName);
newGameInfoForUser1.setDopamine(userOneDopamine);
newGameInfoForUser1.setPoint(userOneGamePoint);

// user2 GameInfoForUser에 저장
GameInfoForUser newGameInfoForUser2 = new GameInfoForUser();
newGameInfoForUser2.setUserId(userTwoId);
newGameInfoForUser2.setUserName(userTwoName);
newGameInfoForUser2.setDopamine(userTwoDopamine);
newGameInfoForUser2.setPoint(userTwoGamePoint);

Expand All @@ -385,12 +400,13 @@ public GameFinishResDto finishGame(String roomId, FinishType finishType, String
newUsersInfo.add(newGameInfoForUser1);
newUsersInfo.add(newGameInfoForUser2);

// 배팅 포인트 가져오기
Long betPoint = (Long) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point");

Long betPoint = Long.valueOf((Integer) redisTemplate.opsForHash().get("game_rooms:" + roomId, "bet_point"));
GameInfo newGameInfo = new GameInfo();
newGameInfo.setFinishType(finishType);
newGameInfo.setRoomId(roomId);
newGameInfo.setBetPoint(betPoint);
newGameInfo.setWinnerId(winnerId);
newGameInfo.setWinnerName(userRepository.findByUserId(winnerId).orElseThrow(() -> new IllegalArgumentException("User not found")).getNickname());
newGameInfo.setUsersInfo(newUsersInfo);

// mysql에 데이터 저장하기
Expand Down

0 comments on commit 50a2dce

Please sign in to comment.