Skip to content

Commit

Permalink
Merge pull request #39 from lsn5963/feat/quest
Browse files Browse the repository at this point in the history
퀘스트 완료 로직 수정
  • Loading branch information
lsn5963 authored Aug 16, 2024
2 parents 2bfb913 + 2ce5783 commit 12a24c1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 27 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/noplanb/NoPlanBApplication.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.noplanb;

import com.noplanb.global.config.YamlPropertySourceFactory;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.TimeZone;

@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling

@PropertySource(value = { "classpath:database/application-database.yml" }, factory = YamlPropertySourceFactory.class)
@PropertySource(value = { "classpath:oauth2/application-oauth2.yml" }, factory = YamlPropertySourceFactory.class)
Expand All @@ -18,5 +23,11 @@ public class NoPlanBApplication {
public static void main(String[] args) {
SpringApplication.run(NoPlanBApplication.class, args);
}
@PostConstruct
public void init() {
System.out.println("true = " + true);
// timezone 설정
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/noplanb/domain/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.noplanb.domain;

import io.jsonwebtoken.Clock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
public class HomeController {

@GetMapping("/")
public LocalDateTime home() {
LocalDateTime now = LocalDateTime.now();
return now;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ public void updateExp(Long exp){
this.todayExp += exp;
this.totalExp += exp;
}

public void updateTotalQuest() {
this.totalQuest += 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.noplanb.domain.character.domain.Character;
import com.noplanb.domain.character.repository.CharacterRepository;
import com.noplanb.domain.item.domain.Item;
import com.noplanb.domain.item_image.domain.ItemImage;
import com.noplanb.domain.item_image.domain.repository.ItemImageRepository;
import com.noplanb.domain.quest.domain.DailyExperience;
import com.noplanb.domain.quest.domain.Quest;
import com.noplanb.domain.quest.dto.req.CreateQuestReq;
import com.noplanb.domain.quest.dto.req.ModifyQuestReq;
import com.noplanb.domain.quest.dto.res.RetrieveLevelAndTodayExpRes;
import com.noplanb.domain.quest.dto.res.RetrieveLevelUpItemImage;
import com.noplanb.domain.quest.dto.res.RetrieveCompleteQuest;
import com.noplanb.domain.quest.dto.res.RetrieveQuestRes;
import com.noplanb.domain.quest.repository.DailyExperienceRepository;
import com.noplanb.domain.quest.repository.QuestRepository;
Expand All @@ -21,25 +20,26 @@
import com.noplanb.global.payload.exception.CharacterNotFoundException;
import com.noplanb.global.payload.exception.QuestNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class QuestService {
private final CharacterRepository characterRepository;
private final QuestRepository questRepository;
private final DailyExperienceRepository dailyExperienceRepository;
private final ItemImageRepository itemImageRepository;
private final UserRepository userRepository;

@Transactional
public ResponseEntity<?> createQuest(CreateQuestReq createQuestReq, UserPrincipal userPrincipal) {
Expand Down Expand Up @@ -105,28 +105,39 @@ public ResponseEntity<?> completeQuest(UserPrincipal userPrincipal, Long id) {

//퀘스트 완료처리
quest.updateIsComplete(Boolean.TRUE);
//총 퀘스트 개수 1개 추가
character.updateTotalQuest();
//오늘얻은 경험치 update
character.updateExp(quest.getExp());

List<String> itemImageUrls = new ArrayList<>();
String questType = "완료";

// 레벨업 확인
if (character.getTotalExp()>((characterLevel*(characterLevel+1)/2)*10)){
questType = "레벨업";
// 레벨업
character.updateLevel();
// 레벌업된 레벨이 해금대상인지 확인
if (character.getLevel() % 5 == 0) {
questType = "해금";
//아이템 해금
List<Item> unLockItems = unLockItem(character.getLevel(), character);
//해금된 아이템들 이미지 반환
List<RetrieveLevelUpItemImage> unLockImages = unLockItems.stream()
.map(item -> RetrieveLevelUpItemImage.builder()
.itemImageUrl(item.getItemImage().getItemImageUrl())
.build())
itemImageUrls = unLockItems.stream()
//장착하지 않은 아이템들중
.filter(item -> !item.isEquipped())
//이미지를 가져온다.
.map(item -> item.getItemImage().getItemImageUrl())
.collect(Collectors.toList());
return createApiResponse(unLockImages);
}
return createApiResponse(Message.builder().message("레벨업을 했습니다!").build());
}
return createApiResponse(Message.builder().message("퀘스트를 완료했습니다!").build());

RetrieveCompleteQuest retrieveCompleteQuest = RetrieveCompleteQuest.builder()
.questType(questType)
.itemImageUrls(itemImageUrls)
.build();
return createApiResponse(retrieveCompleteQuest);
}

private List<Item> unLockItem(Long level, Character character) {
Expand Down Expand Up @@ -163,9 +174,9 @@ public ResponseEntity<?> deleteQuest(UserPrincipal userPrincipal, Long id) {
return createApiResponse(Message.builder().message("퀘스트를 삭제했습니다.").build());
}
@Transactional
@Scheduled(cron = "0 0 0 * * *")
@Scheduled(cron = "0 0 0 * * *",zone = "Asia/Seoul")
public void resetDailyExperience() {
System.out.println(" 하루 경험치 초기화 함수실행");
log.info(" 하루 경험치 초기화 함수실행");
List<Character> characters = characterRepository.findAll();
for (Character character : characters) {
// 어제 날짜로 DailyExperience 엔티티에 저장
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.noplanb.global.config.security.token.CurrentUser;
import com.noplanb.global.config.security.token.UserPrincipal;
import com.noplanb.global.payload.ErrorResponse;
import com.noplanb.global.payload.Message;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -31,7 +32,7 @@ public class CalendarController {
@GetMapping("/{date}")
@Operation(summary = "달력 경험치 조회", description = "달력경험치를 조회할 때 사용하는 API")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "달력 조회 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = RetrieveCalendarRes.class))}),
@ApiResponse(responseCode = "200", description = "달력 조회 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = Message.class))}),
@ApiResponse(responseCode = "400", description = "달력 조회 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}),
})
public ResponseEntity<?> retrieveCalendar(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.noplanb.domain.quest.controller;


import com.noplanb.domain.quest.application.QuestService;
import com.noplanb.domain.quest.dto.req.CreateQuestReq;
import com.noplanb.domain.quest.dto.req.ModifyQuestReq;
import com.noplanb.domain.quest.dto.res.RetrieveCompleteQuest;
import com.noplanb.domain.quest.dto.res.RetrieveLevelAndTodayExpRes;
import com.noplanb.domain.quest.dto.res.RetrieveQuestRes;
import com.noplanb.global.config.security.token.CurrentUser;
Expand Down Expand Up @@ -67,7 +69,7 @@ public ResponseEntity<?> retrieveLevelAndTodayExp(
@PostMapping("/{id}")
@Operation(summary = "퀘스트 완료", description = "퀘스트 완료할 때 사용하는 API")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "퀘스트 완료 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = Message.class))}),
@ApiResponse(responseCode = "200", description = "퀘스트 완료 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = RetrieveCompleteQuest.class))}),
@ApiResponse(responseCode = "400", description = "퀘스트 완료 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}),
})
public ResponseEntity<?> completeQuest(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.noplanb.domain.quest.dto.res;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
public class RetrieveCompleteQuest {
@Schema(type = "String", example = "완료", description = "완료한 퀘스트의 상태")
private String questType;
@Schema(type = "List", example = "uuid", description = "이미지 url")
private List<String> itemImageUrls;
}

This file was deleted.

0 comments on commit 12a24c1

Please sign in to comment.