Skip to content

Commit

Permalink
feat : scheduler 추가, puzzle 생성 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixtar committed Mar 19, 2024
1 parent 7da65af commit cc2e44c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableMongoRepositories
@EnableScheduling
public class CrossPuzzleServerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -120,7 +121,7 @@ public String testGen() {
List<ObjectId> categories = new ArrayList<>();
categories.add(categoryRepository.findAll().get(0).getId());
System.out.println("@@@" + categories.get(0));
// wordPuzzle.generateCrossWord(categories);
wordPuzzle.generateCrossWord(categories);

if (puzzleGenService.generatePuzzle()) {
return "success";
Expand All @@ -129,4 +130,12 @@ public String testGen() {
}


@GetMapping("/mig")
public void mig() {
List<ObjectId> categories = new ArrayList<>();
categories.add(categoryRepository.findAll().get(0).getId());
System.out.println("@@@" + categories.get(0));
puzzleGenService.generatePuzzle();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public interface CrossWordsRepository extends MongoRepository<CrossWords, String
long countByCategories(Category category);

@Query("SELECT cw FROM CrossWords cw WHERE cw.categoryId IN :categoryIds")
List<CrossWords> getCrossWordsByCategoryIds(@Param("categoryIds") List<ObjectId> categoryIds);
Optional<List<CrossWords>> getCrossWordsByCategoryIds(@Param("categoryIds") List<ObjectId> categoryIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

public interface WordsRepository extends MongoRepository<Words, ObjectId> {

// Optional<List<Words>> findWordsByCategory(String category);

Optional<List<Words>> findWordsByCategory(String objectId);
Optional<List<Words>> findWordsByCategory(ObjectId objectId);

Optional<Words> findById(ObjectId id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
Expand All @@ -28,6 +29,7 @@ public class PuzzleGenImpl implements PuzzleGenService {
private final CrossWordsRepository crossWordsRepository;

@Override
@Scheduled(cron = "0/10 * * * * *")
public boolean generatePuzzle() {

List<ObjectId> randomCategoryId = getRandomCategoryIds();
Expand All @@ -39,23 +41,24 @@ public boolean generatePuzzle() {
log.info("duplicated puzzle");
return false;
}
log.info("create puzzle success");
answersInfoRepository.saveAll(crossWords.getAnswersInfo());
questionInfoRepository.save(crossWords.getQuestionInfos());
crossWordsRepository.save(crossWords);
return true;
}

private boolean isDuplicated(CrossWords crossWords) {

List<ObjectId> categoryIds = new ArrayList<>();
for (Category c : crossWords.getCategories()) {
categoryIds.add(c.getId());
}
//카테고리 아이디를 통해 전부 가져와서
List<CrossWords> crossWordsList = crossWordsRepository.getCrossWordsByCategoryIds(categoryIds);
List<CrossWords> crossWordsList = crossWordsRepository.getCrossWordsByCategoryIds(categoryIds)
.orElse(new ArrayList<>());
//중복체크
for (CrossWords cw : crossWordsList) {
if (checkDuplicated(cw, crossWords)) {
if (isDuplicatedWords(cw, crossWords)) {
return true;
}
}
Expand All @@ -72,11 +75,11 @@ private List<ObjectId> getRandomCategoryIds() {
return categoryIds;
}

private boolean checkDuplicated(CrossWords cw, CrossWords origin) {
private boolean isDuplicatedWords(CrossWords cw, CrossWords origin) {
Set<String> cwWordsSet = new HashSet<>(getWordsValueList(cw.getAnswersInfo()));
Set<String> originWordsSet = new HashSet<>(getWordsValueList(origin.getAnswersInfo()));

return cwWordsSet.size() != originWordsSet.size();
return cwWordsSet.size() == originWordsSet.size();
}

private List<String> getWordsValueList(List<AnswersInfo> answersInfoList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
import com.example.crosspuzzleserver.domain.CrossWords;
import com.example.crosspuzzleserver.domain.QuestionInfos;
import com.example.crosspuzzleserver.domain.Words;
import com.example.crosspuzzleserver.repository.AnswersInfoRepository;
import com.example.crosspuzzleserver.repository.CategoryRepository;
import com.example.crosspuzzleserver.repository.CrossWordsRepository;
import com.example.crosspuzzleserver.repository.QuestionInfoRepository;
import com.example.crosspuzzleserver.repository.WordsRepository;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class WordPuzzle {
Expand Down Expand Up @@ -79,27 +78,13 @@ static class AnswersInfoDAO {

public CrossWords generateCrossWord(List<ObjectId> categoryIds) {

//String 이 아니라 Words 오브젝트를 리스트로 넘겨야함.
// List<Words> wordsList = new ArrayList<>();
// for (int i = 0; i < words.length; i++) {
// wordsList.add(
// Words.builder()
// .value(words[i])
// .build()
// );
// }

List<Words> wordsList = new ArrayList<>();
for (ObjectId categoryId : categoryIds) {
List<Words> tmpList = wordsRepository.findWordsByCategory(categoryId.toString())
List<Words> tmpList = wordsRepository.findWordsByCategory(categoryId)
.orElse(new ArrayList<>());
wordsList.addAll(tmpList);
}

// System.out.println("@@@ before " + wordsList.size());
// for (Words words : wordsList) {
// System.out.println(words.getValue());
// }
CrossWords createdCrossWords = createPuzzle(wordsList,
categoryIds.stream().map(categoryRepository::findById).toList());

Expand All @@ -115,7 +100,6 @@ public CrossWords generateCrossWord(List<ObjectId> categoryIds) {
}

System.out.println("\n");

//createPuzzle 에 정답 리스트, 판을 자르기 위한 왼쪽위, 오른쪽 아래 좌표를 반환해야함
return createdCrossWords;
}
Expand Down Expand Up @@ -222,6 +206,8 @@ private CrossWords createPuzzle(List<Words> words, List<Category> categories) {
int randInt = (int) ((Math.random() * (10 - 0)) + 0);
int x = BOARD_SIZE / 2 - 5 + randInt;
int y = BOARD_SIZE / 2 - 5 + (10 - randInt);

// 시작 단어가 Index 벗어나면 오류터짐 수정 필요 추가로 X표시 할 때 인덱스 안벗어나게 해야함.
for (int i = 0; i < words.get(0).getValue().length(); i++) {
board[x][y + i] = words.get(0).getValue().charAt(i);
usedWords.add(words.get(0));
Expand Down Expand Up @@ -298,6 +284,7 @@ private PuzzleResult create(char[][] board, List<Words> usedWords, List<Words> u
usedWords.contains(wordInfo.word)) {
continue;
}

usedWords.add(wordInfo.word);
AnswersInfoDAO additionalAnswersInfo = AnswersInfoDAO.builder()
.words(wordInfo.word)
Expand Down Expand Up @@ -373,11 +360,11 @@ private PuzzleResult create(char[][] board, List<Words> usedWords, List<Words> u
}

//삽입후 단어 앞뒤막기
if (prevX >= 0 && prevY >= 0 && nextX < BOARD_SIZE && nextY < BOARD_SIZE) {
if (!isCellOccupied(board, prevX, prevY) && !isCellOccupied(board, nextX, nextY)) {
board[prevX][prevY] = 'X';
board[nextX][nextY] = 'X';
}
if (prevX >= 0 && prevY >= 0 && !isCellOccupied(board, prevX, prevY)) {
board[prevX][prevY] = 'X';
}
if (nextX < BOARD_SIZE && nextY < BOARD_SIZE && !isCellOccupied(board, nextX, nextY)) {
board[nextX][nextY] = 'X';
}

List<WordInfo> additionalConnectionInfo = findOverlaps(
Expand Down

0 comments on commit cc2e44c

Please sign in to comment.