Skip to content

Commit

Permalink
Merge pull request #444 from mash-up-kr/feature/mashong-level-up
Browse files Browse the repository at this point in the history
매숑이 레벨업 분리
  • Loading branch information
eunjungL authored Jun 29, 2024
2 parents 2df83b0 + 056c089 commit f51c8fb
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public enum ResultCode {
MASHONG_MISSION_LEVEL_NOT_FOUND("매숑 미션 레벨이 존재하지 않습니다."),
PLATFORM_MASHONG_NOT_FOUND("해당 기수에 플랫폼 매숑이가 존재하지 않습니다."),
MASHONG_POPCORN_INSUFFICIENT("보유한 팝콘의 개수가 요청한 개수보다 적습니다."),
PLATFORM_MASHONG_LEVEL_NOT_FOUND("존재하지 않는 매숑이 레벨입니다."),

// JsonUtil
JSON_DESERIALIZE_UNABLE("객체를 json string 을 deserialize 할 수 없습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,28 @@ public class PlatformMashong {

private Long accumulatedPopcorn;

public void feed(Long popcornCount, PlatformMashongLevel nextLevel) {
if (accumulatedPopcorn + popcornCount >= level.getGoalPopcorn()) {
level = nextLevel;
}

public void feed(Long popcornCount) {
accumulatedPopcorn += popcornCount;
}

public boolean isMaxLevel() {
return level.isMaxLevel();
}

public boolean isSameLevel(PlatformMashongLevel level) {
return getCurrentLevel() == level.getLevel();
}

public boolean levelUp(PlatformMashongLevel goalLevel) {
if (accumulatedPopcorn < goalLevel.getGoalPopcorn()) {
return false;
}

level = goalLevel;
accumulatedPopcorn -= goalLevel.getGoalPopcorn();
return true;
}

public int getCurrentLevel() {
return level.getLevel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

import kr.mashup.branding.domain.mashong.PlatformMashongLevel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface PlatformMashongLevelRepository extends JpaRepository<PlatformMashongLevel, Long> {

@Query("SELECT ml FROM PlatformMashongLevel AS ml WHERE ml.level = :level + 1")
Optional<PlatformMashongLevel> findNextLevelByLevel(int level);

@Query("SELECT ml FROM PlatformMashongLevel AS ml WHERE ml.level = (SELECT MAX(level) FROM PlatformMashongLevel)")
PlatformMashongLevel findMaxLevel();
Optional<PlatformMashongLevel> findByLevel(int level);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.mashup.branding.service.mashong;

import kr.mashup.branding.domain.ResultCode;
import kr.mashup.branding.domain.exception.NotFoundException;
import kr.mashup.branding.domain.mashong.PlatformMashongLevel;
import kr.mashup.branding.repository.mashong.PlatformMashongLevelRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PlatformMashongLevelService {

private final PlatformMashongLevelRepository platformMashongLevelRepository;

public PlatformMashongLevel findByLevel(int level) {
return platformMashongLevelRepository.findByLevel(level)
.orElseThrow(() -> new NotFoundException(ResultCode.PLATFORM_MASHONG_LEVEL_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
public class PlatformMashongService {

private final PlatformMashongRepository platformMashongRepository;
private final PlatformMashongLevelRepository platformMashongLevelRepository;

public PlatformMashong findByPlatformAndGeneration(Platform platform, Generation generation) {
return platformMashongRepository.findByPlatformAndGeneration(platform, generation)
.orElseThrow(() -> new NotFoundException(ResultCode.PLATFORM_MASHONG_NOT_FOUND));
}

public void feedPopcorn(PlatformMashong platformMashong, Long popcornCount) {
final PlatformMashongLevel nextLevel = platformMashongLevelRepository.findNextLevelByLevel(
platformMashong.getCurrentLevel()
).orElseGet(platformMashongLevelRepository::findMaxLevel);
platformMashong.feed(popcornCount);
}

public boolean levelUp(Platform platform, Generation generation, PlatformMashongLevel goalLevel) {
PlatformMashong platformMashong = findByPlatformAndGeneration(platform, generation);

platformMashong.feed(popcornCount, nextLevel);
if (platformMashong.isSameLevel(goalLevel)) {
return true;
}
return platformMashong.levelUp(goalLevel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import kr.mashup.branding.service.mashong.*;
import kr.mashup.branding.service.member.MemberService;
import kr.mashup.branding.ui.mashong.response.MashongFeedResponse;
import kr.mashup.branding.ui.mashong.response.MashongLevelUpResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,6 +23,7 @@ public class MashongFacadeService {
private final MashongMissionTeamLogService mashongMissionTeamLogService;
private final MashongMissionLevelService mashongMissionLevelService;
private final PlatformMashongService platformMashongService;
private final PlatformMashongLevelService platformMashongLevelService;
private final MemberService memberService;

@Transactional
Expand Down Expand Up @@ -108,4 +110,20 @@ public MashongFeedResponse feedPopcorn(Long memberGenerationId, Long popcornCoun

return MashongFeedResponse.of(true, platformMashong, mashongPopcorn);
}

@Transactional
public MashongLevelUpResponse levelUp(Long memberGenerationId, int goalLevel) {
PlatformMashongLevel goalPlatformMashongLevel = platformMashongLevelService.findByLevel(goalLevel);
MemberGeneration memberGeneration = memberService.findByMemberGenerationId(memberGenerationId);
Platform platform = memberService.getLatestPlatform(memberGeneration.getMember());
Generation generation = memberGeneration.getGeneration();

boolean isLevelUp = platformMashongService.levelUp(platform, generation, goalPlatformMashongLevel);
if (!isLevelUp) {
PlatformMashong platformMashong = platformMashongService.findByPlatformAndGeneration(platform, generation);
return MashongLevelUpResponse.of(isLevelUp, platformMashong.getLevel());
}

return MashongLevelUpResponse.of(isLevelUp, goalPlatformMashongLevel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import kr.mashup.branding.security.MemberAuth;
import kr.mashup.branding.ui.ApiResponse;
import kr.mashup.branding.ui.mashong.request.MashongFeedRequest;
import kr.mashup.branding.ui.mashong.request.MashongLevelUpRequest;
import kr.mashup.branding.ui.mashong.response.MashongFeedResponse;
import kr.mashup.branding.ui.mashong.response.MashongLevelUpResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -57,6 +59,19 @@ public ApiResponse<MashongFeedResponse> feedPopcorn(
return ApiResponse.success(result);
}

@ApiOperation(value = "매숑이 레벨업")
@PostMapping("/level-up")
public ApiResponse<MashongLevelUpResponse> levelUp(
@ApiIgnore MemberAuth memberAuth,
@RequestBody MashongLevelUpRequest request
) {
MashongLevelUpResponse result = mashongFacadeService.levelUp(
memberAuth.getMemberGenerationId(),
request.getGoalLevel()
);
return ApiResponse.success(result);
}

@ApiOperation(
value = "매숑이 팝콘 조회"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.mashup.branding.ui.mashong.request;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Getter
public class MashongLevelUpRequest {

@NotNull(message = "레벨업 목표 레벨은 비어있을 수 없습니다.")
@Min(value = 1, message = "레벨업 목표 레벨은 1 이상이어야 합니다.")
private int goalLevel;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.mashup.branding.ui.mashong.response;

import kr.mashup.branding.domain.mashong.PlatformMashongLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@Builder
@RequiredArgsConstructor
public class MashongLevelUpResponse {

private final int currentLevel;
private final boolean isLevelUp;

public static MashongLevelUpResponse of(boolean isLevelUp, PlatformMashongLevel level) {
return MashongLevelUpResponse.builder()
.isLevelUp(isLevelUp)
.currentLevel(level.getLevel())
.build();
}
}

0 comments on commit f51c8fb

Please sign in to comment.