Skip to content

Commit

Permalink
Merge pull request #374 from SWM-NM/feat/#371
Browse files Browse the repository at this point in the history
✏️ [FIX] 문제 추출 코드 수정
  • Loading branch information
aj4941 authored Sep 25, 2023
2 parents c4ec202 + 2fcbf5c commit 1696563
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void getProblemsByTestType(TestType testType, List<BojProblem> bojProblem
List<DifficultyRange> difficultyRanges = testType.getDifficultyRanges();
long index = 1;
for (DifficultyRange difficultyRange : difficultyRanges) {
if (bojProblems.size() >= testType.getProblemCount()) break;
int start = DifficultyLevel.getLevelByValue(difficultyRange.getStart());
int end = DifficultyLevel.getLevelByValue(difficultyRange.getEnd());
boolean flag = false;
Expand Down Expand Up @@ -68,7 +67,6 @@ public void getProblemsByApi(TestType testType, String bojId, List<BojProblem> b
List<DifficultyRange> difficultyRanges = testType.getDifficultyRanges();
long index = 1;
for (DifficultyRange difficultyRange : difficultyRanges) {
if (bojProblems.size() >= testType.getProblemCount()) break;
if (bojProblems.get((int) (index - 1)).getProblemId() != 0) {
index++;
continue;
Expand All @@ -77,9 +75,7 @@ public void getProblemsByApi(TestType testType, String bojId, List<BojProblem> b
String end = difficultyRange.getEnd().getShortName();
String apiUrl = "https://solved.ac/api/v3/search/problem";
while (true) {
// PK 7번 : 삼성 테스트의 경우 시뮬레이션 우선
String query = testType.getTestTypeId() == 7 ? String.format("tier:%s..%s ~solved_by:%s tag:simulation ~tag:ad_hoc ~tag:constructive ~tag:geometry ~tag:number_theory ~tag:math solved:200..", start, end, bojId) :
String.format("tier:%s..%s ~solved_by:%s ~tag:ad_hoc ~tag:constructive ~tag:geometry ~tag:number_theory ~tag:simulation ~tag:math solved:200.. solved:..5000", start, end, bojId);
String query = getString(testType, bojId, start, end);
WebClient webClient = WebClient.builder().build();
String jsonString = webClient.get()
.uri(apiUrl + "?query=" + query + "&page=1" + "&sort=random")
Expand All @@ -92,21 +88,8 @@ public void getProblemsByApi(TestType testType, String bojId, List<BojProblem> b
JsonNode rootNode = mapper.readTree(jsonString);
JsonNode itemsArray = rootNode.get("items");
if (itemsArray != null && itemsArray.isArray() && itemsArray.size() > 0) {
JsonNode firstItemNode = itemsArray.get(0);
String title = firstItemNode.get("titleKo").asText();
int alpha = (int) IntStream.range(0, 2).filter(i -> ('a' <= title.charAt(i) && title.charAt(i) <= 'z')
|| ('A' <= title.charAt(i) && title.charAt(i) <= 'Z')).count();

if (alpha == 2)
continue;

JsonNode firstProblem = itemsArray.get(0);
BojProblem apiProblem = mapper.treeToValue(firstProblem, BojProblem.class);
BojProblem bojProblem = bojProblems.get((int) (index - 1));
bojProblem.setProblemId(apiProblem.getProblemId());
bojProblem.setLevel(apiProblem.getLevel());
bojProblem.setTestProblemId(index++);
bojProblem.setLevelToString(DifficultyLevel.getValueByLevel(bojProblem.getLevel()));
if (isAlpha(itemsArray)) continue;
index = getProblem(bojProblems, index, mapper, itemsArray);
break;
}
} catch (JsonProcessingException e) {
Expand All @@ -116,4 +99,36 @@ public void getProblemsByApi(TestType testType, String bojId, List<BojProblem> b
}
}
}

private static String getString(TestType testType, String bojId, String start, String end) {
String query = testType.getTestTypeId() == 7 ?
String.format("tier:%s..%s ~solved_by:%s tag:simulation ~tag:ad_hoc ~tag:constructive ~tag:geometry" +
" ~tag:number_theory ~tag:math solved:200..", start, end, bojId) :
String.format("tier:%s..%s ~solved_by:%s ~tag:ad_hoc ~tag:constructive ~tag:geometry" +
" ~tag:number_theory ~tag:simulation ~tag:math solved:200.. solved:..5000", start, end, bojId);
return query;
}

private static long getProblem(List<BojProblem> bojProblems, long index, ObjectMapper mapper, JsonNode itemsArray)
throws JsonProcessingException {
JsonNode firstProblem = itemsArray.get(0);
BojProblem apiProblem = mapper.treeToValue(firstProblem, BojProblem.class);
BojProblem bojProblem = bojProblems.get((int) (index - 1));
bojProblem.setProblemId(apiProblem.getProblemId());
bojProblem.setLevel(apiProblem.getLevel());
bojProblem.setTestProblemId(index++);
bojProblem.setLevelToString(DifficultyLevel.getValueByLevel(bojProblem.getLevel()));
return index;
}

private static boolean isAlpha(JsonNode itemsArray) {
JsonNode firstItemNode = itemsArray.get(0);
String title = firstItemNode.get("titleKo").asText();
int alpha = (int) IntStream.range(0, 2).filter(i -> ('a' <= title.charAt(i) && title.charAt(i) <= 'z')
|| ('A' <= title.charAt(i) && title.charAt(i) <= 'Z')).count();

if (alpha == 2)
return true;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public List<Long> saveAttemptProblems(Member member, Tests test, List<BojProblem
.test(test)
.problem(problem)
.build();

System.out.println("attemptProblem = " + attemptProblem);
attemptProblemRepository.save(attemptProblem);
}
return bojProblemIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Tests isTestinProgress(Member member){
member.setCurrentTestId(-1L);

// 현재 테스트가 진행중이라면
if (member.getCurrentTestId() != -1) {
if (member.getCurrentTestId() != -1L) {
Long currentTestId = member.getCurrentTestId();
Tests test = testRepository.findById(currentTestId).orElseThrow(() -> new MorandiException(TestErrorCode.TEST_NOT_FOUND));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import swm_nm.morandi.global.exception.errorcode.*;
import swm_nm.morandi.global.utils.SecurityUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -41,8 +42,7 @@ public class TestStartUseCase {
private final AttemptProblemRepository attemptProblemRepository;

//이미 테스트 중인지 확인


@Transactional
public TestStartResponseDto getTestStartsData(Long testTypeId) {
Long memberId = SecurityUtils.getCurrentMemberId();
Member member = memberRepository.findById(memberId).orElseThrow(
Expand All @@ -55,7 +55,8 @@ public TestStartResponseDto getTestStartsData(Long testTypeId) {
if (test != null) {
return getTestStartResponseDto(member.getCurrentTestId(), test);
}
TestType testType = testTypeRepository.findById(testTypeId).orElseThrow(() -> new MorandiException(TestTypeErrorCode.TEST_TYPE_NOT_FOUND));
TestType testType = testTypeRepository.findById(testTypeId)
.orElseThrow(() -> new MorandiException(TestTypeErrorCode.TEST_TYPE_NOT_FOUND));
// 현재 진행중인 테스트가 없을 경우 테스트 타입에 맞는 테스트 시작
test = addTestService.startTestByTestTypeId(testType, member);

Expand All @@ -74,8 +75,6 @@ public TestStartResponseDto getTestStartsData(Long testTypeId) {
// 테스트 시작시 코드 캐시 초기화
tempCodeInitializer.initTempCodeCacheWhenTestStart(test);



return getTestStartResponseDto(test, bojProblems);
}

Expand Down

0 comments on commit 1696563

Please sign in to comment.