Skip to content

Commit 6afe267

Browse files
authored
Merge pull request #187 from DguFarmSystem/feat/#175-minigame
Feat/#175 minigame
2 parents c9051eb + 95dd40f commit 6afe267

14 files changed

Lines changed: 119 additions & 82 deletions

File tree

src/main/java/org/farmsystem/homepage/domain/minigame/player/controller/BadgeController.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.farmsystem.homepage.domain.minigame.player.dto.request.BadgeUpdateRequestDTO;
55
import org.farmsystem.homepage.domain.minigame.player.dto.response.BadgeResponseDTO;
66
import org.farmsystem.homepage.domain.minigame.player.service.BadgeService;
7+
import org.farmsystem.homepage.global.common.SuccessResponse;
8+
import org.springframework.http.ResponseEntity;
79
import org.springframework.security.core.annotation.AuthenticationPrincipal;
810
import org.springframework.web.bind.annotation.*;
911

@@ -18,16 +20,18 @@ public class BadgeController {
1820

1921
// 전체 칭호 조회
2022
@GetMapping
21-
public List<BadgeResponseDTO> getBadges(@AuthenticationPrincipal Long userId) {
22-
return badgeService.getBadges(userId);
23+
public ResponseEntity<SuccessResponse<?>> getBadges(@AuthenticationPrincipal Long userId) {
24+
List<BadgeResponseDTO> response = badgeService.getBadges(userId);
25+
return SuccessResponse.ok(response);
2326
}
2427

2528
// 칭호 추가
2629
@PostMapping
27-
public BadgeResponseDTO addBadge(
30+
public ResponseEntity<SuccessResponse<?>> addBadge(
2831
@AuthenticationPrincipal Long userId,
2932
@RequestBody BadgeUpdateRequestDTO request
3033
) {
31-
return badgeService.addBadge(userId, request);
34+
BadgeResponseDTO response = badgeService.addBadge(userId, request);
35+
return SuccessResponse.created(response);
3236
}
33-
}
37+
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/controller/DailyGameController.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.farmsystem.homepage.domain.minigame.player.dto.request.GameTypeRequestDTO;
55
import org.farmsystem.homepage.domain.minigame.player.dto.response.GameCountResponseDTO;
66
import org.farmsystem.homepage.domain.minigame.player.service.DailyGameService;
7+
import org.farmsystem.homepage.global.common.SuccessResponse;
8+
import org.springframework.http.ResponseEntity;
79
import org.springframework.security.core.annotation.AuthenticationPrincipal;
810
import org.springframework.web.bind.annotation.*;
911

@@ -16,19 +18,21 @@ public class DailyGameController {
1618

1719
// 게임 횟수 조회
1820
@GetMapping("/{gameType}")
19-
public GameCountResponseDTO getGameCount(
21+
public ResponseEntity<SuccessResponse<?>> getGameCount(
2022
@AuthenticationPrincipal Long userId,
2123
@PathVariable String gameType
2224
) {
23-
return dailyGameService.getGameCount(userId, gameType);
25+
GameCountResponseDTO response = dailyGameService.getGameCount(userId, gameType);
26+
return SuccessResponse.ok(response);
2427
}
2528

26-
// 게임 횟수 증가
27-
@PatchMapping("/increment")
28-
public GameCountResponseDTO incrementGameCount(
29+
// 게임 횟수 1회 소모
30+
@PatchMapping("/use")
31+
public ResponseEntity<SuccessResponse<?>> useGame(
2932
@AuthenticationPrincipal Long userId,
3033
@RequestBody GameTypeRequestDTO request
3134
) {
32-
return dailyGameService.incrementGameCount(userId, request);
35+
GameCountResponseDTO response = dailyGameService.useGame(userId, request);
36+
return SuccessResponse.ok(response);
3337
}
3438
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.farmsystem.homepage.domain.minigame.player.dto.request;
22

3+
import jakarta.validation.constraints.NotNull;
4+
35
public record BadgeUpdateRequestDTO(
4-
Integer badgeType
6+
@NotNull Integer badgeType
57
) {
68
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.farmsystem.homepage.domain.minigame.player.dto.request;
22

3+
import jakarta.validation.constraints.NotBlank;
4+
35
public record GameTypeRequestDTO(
4-
String gameType
6+
@NotBlank String gameType
57
) {
68
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/dto/response/GameCountResponseDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public record GameCountResponseDTO(
77
int count
88
) {
99
public static GameCountResponseDTO from(DailyGame dailyGame, String gameType) {
10-
return new GameCountResponseDTO(gameType, dailyGame.getGameCount(gameType));
10+
return new GameCountResponseDTO(gameType, dailyGame.getRemainingCount(gameType));
1111
}
1212
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/entity/Badge.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
@Getter
99
@NoArgsConstructor
1010
@AllArgsConstructor
11-
@Builder
1211
public class Badge { //칭호
1312
@Id
1413
@GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -22,10 +21,13 @@ public class Badge { //칭호
2221
@JoinColumn(name = "player_id")
2322
private Player player;
2423

25-
public static Badge create(Player player, Integer badgeType) {
26-
return Badge.builder()
27-
.badgeType(badgeType)
28-
.player(player)
29-
.build();
24+
// 플레이어가 새로운 칭호를 획득할 때 사용하는 생성 메서드
25+
public static Badge createBadge(Player player, Integer badgeType) {
26+
return new Badge(player, badgeType);
27+
}
28+
// 생성은 createBadge만 사용
29+
private Badge(Player player, Integer badgeType) {
30+
this.player = player;
31+
this.badgeType = badgeType;
3032
}
3133
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/entity/DailyGame.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public class DailyGame {
1919
private Long gameId;
2020

2121
@Column(name = "rock_scissors_game", nullable = false)
22-
private Integer rockScissors;
22+
private Integer rockScissors; // 남은 횟수
2323

2424
@Column(name = "carrot_game", nullable = false)
25-
private Integer carrotGame;
25+
private Integer carrotGame; // 남은 횟수
2626

2727
@Column(name = "sunlight_game", nullable = false)
28-
private Integer sunlightGame;
28+
private Integer sunlightGame; // 남은 횟수
2929

3030
@Column(name = "last_reset_date", nullable = false)
3131
private LocalDate lastResetDate;
@@ -34,44 +34,56 @@ public class DailyGame {
3434
@JoinColumn(name = "player_id", nullable = false)
3535
private Player player;
3636

37-
public static DailyGame createNew(Player player) {
38-
return DailyGame.builder()
39-
.player(player)
40-
.rockScissors(0)
41-
.carrotGame(0)
42-
.sunlightGame(0)
43-
.lastResetDate(LocalDate.now())
44-
.build();
37+
public static DailyGame createDailyGame(Player player) {
38+
return new DailyGame(player, 3, 3, 3, LocalDate.now());
4539
}
4640

41+
// 날짜가 바뀌었으면 횟수를 초기화한다 (다시 3회로 초기화)
4742
public void resetIfNeeded() {
4843
if (!LocalDate.now().equals(this.lastResetDate)) {
4944
resetCounts();
5045
}
5146
}
52-
5347
public void resetCounts() {
54-
this.rockScissors = 0;
55-
this.carrotGame = 0;
56-
this.sunlightGame = 0;
48+
this.rockScissors = 3;
49+
this.carrotGame = 3;
50+
this.sunlightGame = 3;
5751
this.lastResetDate = LocalDate.now();
5852
}
5953

60-
public void incrementGame(String gameType) {
54+
// 게임 횟수를 1회 차감(0보다 작아질 수 없음)
55+
public void useGame(String gameType) {
6156
switch (gameType.toLowerCase()) {
62-
case "rock" -> this.rockScissors++;
63-
case "carrot" -> this.carrotGame++;
64-
case "sunlight" -> this.sunlightGame++;
57+
case "rock" -> {
58+
if (rockScissors <= 0) throw new IllegalStateException("rock game 횟수 모두 소진됨");
59+
this.rockScissors--;
60+
}
61+
case "carrot" -> {
62+
if (carrotGame <= 0) throw new IllegalStateException("carrot game 횟수 모두 소진됨");
63+
this.carrotGame--;
64+
}
65+
case "sunlight" -> {
66+
if (sunlightGame <= 0) throw new IllegalStateException("sunlight game 횟수 모두 소진됨");
67+
this.sunlightGame--;
68+
}
6569
default -> throw new IllegalArgumentException("Invalid game type: " + gameType);
6670
}
6771
}
68-
69-
public int getGameCount(String gameType) {
72+
// 현재 게임의 남은 횟수 반환
73+
public int getRemainingCount(String gameType) {
7074
return switch (gameType.toLowerCase()) {
7175
case "rock" -> this.rockScissors;
7276
case "carrot" -> this.carrotGame;
7377
case "sunlight" -> this.sunlightGame;
7478
default -> throw new IllegalArgumentException("Invalid game type: " + gameType);
7579
};
7680
}
81+
82+
private DailyGame(Player player, int rock, int carrot, int sunlight, LocalDate resetDate) {
83+
this.player = player;
84+
this.rockScissors = rock;
85+
this.carrotGame = carrot;
86+
this.sunlightGame = sunlight;
87+
this.lastResetDate = resetDate;
88+
}
7789
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/service/BadgeService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public class BadgeService {
2121
private final BadgeRepository badgeRepository;
2222
private final PlayerRepository playerRepository;
2323

24+
// 플레이어 조회
2425
private Player findPlayerOrThrow(Long userId) {
2526
return playerRepository.findByUser_UserId(userId)
2627
.orElseThrow(() -> new BusinessException(ErrorCode.PLAYER_NOT_FOUND));
2728
}
28-
29+
// 플레이어의 전체 칭호 조회
2930
@Transactional(readOnly = true)
3031
public List<BadgeResponseDTO> getBadges(Long userId) {
3132
Player player = findPlayerOrThrow(userId);
@@ -35,7 +36,7 @@ public List<BadgeResponseDTO> getBadges(Long userId) {
3536
.map(BadgeResponseDTO::from)
3637
.toList();
3738
}
38-
39+
// 새로운 칭호 등록
3940
@Transactional
4041
public BadgeResponseDTO addBadge(Long userId, BadgeUpdateRequestDTO request) {
4142
Player player = findPlayerOrThrow(userId);
@@ -44,7 +45,7 @@ public BadgeResponseDTO addBadge(Long userId, BadgeUpdateRequestDTO request) {
4445
throw new BusinessException(ErrorCode.BADGE_ALREADY_EXISTS);
4546
}
4647

47-
Badge badge = badgeRepository.save(Badge.create(player, request.badgeType()));
48+
Badge badge = badgeRepository.save(Badge.createBadge(player, request.badgeType()));
4849
return BadgeResponseDTO.from(badge);
4950
}
5051
}

src/main/java/org/farmsystem/homepage/domain/minigame/player/service/DailyGameService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ private Player findPlayerOrThrow(Long userId) {
2727
}
2828

2929
private DailyGame getOrCreateDailyGame(Player player) {
30-
Optional<DailyGame> optional = dailyGameRepository.findByPlayer(player);
31-
return optional.orElseGet(() -> dailyGameRepository.save(DailyGame.createNew(player)));
30+
return dailyGameRepository.findByPlayer(player)
31+
.orElseGet(() -> dailyGameRepository.save(DailyGame.createDailyGame(player)));
3232
}
3333

34-
@Transactional
34+
// 게임의 남은 횟수 조회
35+
@Transactional(readOnly = true)
3536
public GameCountResponseDTO getGameCount(Long userId, String gameType) {
3637
Player player = findPlayerOrThrow(userId);
3738
DailyGame dailyGame = getOrCreateDailyGame(player);
@@ -40,18 +41,19 @@ public GameCountResponseDTO getGameCount(Long userId, String gameType) {
4041
return GameCountResponseDTO.from(dailyGame, gameType);
4142
}
4243

44+
// 게임 횟수 1회 소모
4345
@Transactional
44-
public GameCountResponseDTO incrementGameCount(Long userId, GameTypeRequestDTO request) {
46+
public GameCountResponseDTO useGame(Long userId, GameTypeRequestDTO request) {
4547
Player player = findPlayerOrThrow(userId);
4648
DailyGame dailyGame = getOrCreateDailyGame(player);
4749

4850
dailyGame.resetIfNeeded();
4951

50-
if (dailyGame.getGameCount(request.gameType()) >= 3) {
52+
if (dailyGame.getRemainingCount(request.gameType()) <= 0) {
5153
throw new BusinessException(ErrorCode.DAILY_GAME_LIMIT_EXCEEDED);
5254
}
5355

54-
dailyGame.incrementGame(request.gameType());
56+
dailyGame.useGame(request.gameType());
5557
return GameCountResponseDTO.from(dailyGame, request.gameType());
5658
}
5759
}

src/main/java/org/farmsystem/homepage/domain/minigame/solarstation/controller/SolarController.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import lombok.RequiredArgsConstructor;
44
import org.farmsystem.homepage.domain.minigame.solarstation.dto.SolarDTO;
55
import org.farmsystem.homepage.domain.minigame.solarstation.service.SolarService;
6+
import org.farmsystem.homepage.global.common.SuccessResponse;
7+
import org.springframework.http.ResponseEntity;
68
import org.springframework.security.core.annotation.AuthenticationPrincipal;
79
import org.springframework.web.bind.annotation.*;
810

@@ -12,17 +14,20 @@
1214
public class SolarController {
1315

1416
private final SolarService solarService;
15-
17+
// 플레이어의 태양광 발전소 상태 조회
1618
@GetMapping
17-
public SolarDTO getSolarStation(@AuthenticationPrincipal Long userId) {
18-
return solarService.getSolarStation(userId);
19+
public ResponseEntity<SuccessResponse<?>> getSolarStation(@AuthenticationPrincipal Long userId) {
20+
SolarDTO response = solarService.getSolarStation(userId);
21+
return SuccessResponse.ok(response);
1922
}
2023

24+
// 플레이어의 태양광 발전소 상태 업데이트
2125
@PatchMapping("/chargetime")
22-
public SolarDTO updateChargeTime(
26+
public ResponseEntity<SuccessResponse<?>> updateChargeTime(
2327
@AuthenticationPrincipal Long userId,
2428
@RequestBody SolarDTO request
2529
) {
26-
return solarService.updateChargeTime(userId, request);
30+
SolarDTO response = solarService.updateChargeTime(userId, request);
31+
return SuccessResponse.ok(response);
2732
}
2833
}

0 commit comments

Comments
 (0)