Search in sources :

Example 1 with SzCategory

use of com.faendir.zachtronics.bot.sz.model.SzCategory in project zachtronics-leaderboard-bot by F43nd1r.

the class SzSolution method unmarshal.

@NotNull
public static SzSolution unmarshal(@NotNull String[] fields) {
    assert fields.length == 3;
    SzScore score = Objects.requireNonNull(SzScore.parseScore(fields[0]));
    String author = fields[1];
    String categories = fields[2];
    SzSolution solution = new SzSolution(score, author);
    if (categories != null)
        Pattern.compile(",").splitAsStream(categories).map(SzCategory::valueOf).forEach(solution.categories::add);
    return solution;
}
Also used : SzCategory(com.faendir.zachtronics.bot.sz.model.SzCategory) SzScore(com.faendir.zachtronics.bot.sz.model.SzScore) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SzCategory

use of com.faendir.zachtronics.bot.sz.model.SzCategory in project zachtronics-leaderboard-bot by F43nd1r.

the class SzSolutionRepository method archiveOne.

/**
 * @param solutions the list is modified with the updated state
 */
@Override
@NotNull
protected SubmitResult<SzRecord, SzCategory> archiveOne(@NotNull GitRepository.ReadWriteAccess access, @NotNull List<SzSolution> solutions, @NotNull SzSubmission submission) {
    SzPuzzle puzzle = submission.getPuzzle();
    Path puzzlePath = getPuzzlePath(access, puzzle);
    List<CategoryRecord<SzRecord, SzCategory>> beatenCategoryRecords = new ArrayList<>();
    SzSolution candidate = new SzSolution(submission.getScore(), submission.getAuthor());
    try {
        for (ListIterator<SzSolution> it = solutions.listIterator(); it.hasNext(); ) {
            SzSolution solution = it.next();
            int r = dominanceCompare(candidate.getScore(), solution.getScore());
            if (r > 0) {
                // TODO actually return all of the beating sols
                CategoryRecord<SzRecord, SzCategory> categoryRecord = solution.extendToCategoryRecord(puzzle, makeArchiveLink(puzzle, solution.getScore()), makeArchivePath(puzzlePath, solution.getScore()));
                return new SubmitResult.NothingBeaten<>(Collections.singletonList(categoryRecord));
            } else if (r < 0) {
                // allow same-score solution changes only if you are the original author
                if (candidate.getScore().equals(solution.getScore()) && !candidate.getAuthor().equals(solution.getAuthor())) {
                    return new SubmitResult.AlreadyPresent<>();
                }
                // remove beaten score and get categories
                candidate.getCategories().addAll(solution.getCategories());
                Files.delete(makeArchivePath(puzzlePath, solution.getScore()));
                // the beaten record has no data anymore
                beatenCategoryRecords.add(solution.extendToCategoryRecord(puzzle, null, null));
                it.remove();
            }
        }
        // the new record may have gained categories of records it didn't pareto-beat, do the transfers
        for (SzSolution solution : solutions) {
            EnumSet<SzCategory> lostCategories = EnumSet.noneOf(SzCategory.class);
            for (SzCategory category : solution.getCategories()) {
                if (category.supportsScore(candidate.getScore()) && category.getScoreComparator().compare(candidate.getScore(), solution.getScore()) < 0) {
                    lostCategories.add(category);
                }
            }
            if (!lostCategories.isEmpty()) {
                // add a CR holding the lost categories, then correct the solutions
                CategoryRecord<SzRecord, SzCategory> beatenCR = new CategoryRecord<>(solution.extendToRecord(puzzle, makeArchiveLink(puzzle, solution.getScore()), makeArchivePath(puzzlePath, solution.getScore())), lostCategories);
                beatenCategoryRecords.add(beatenCR);
                solution.getCategories().removeAll(lostCategories);
                candidate.getCategories().addAll(lostCategories);
            }
        }
        int index = Collections.binarySearch(solutions, candidate, COMPARATOR);
        if (index < 0) {
            index = -index - 1;
        }
        solutions.add(index, candidate);
        String filename = makeFilename(submission.getPuzzle().getId(), candidate.getScore());
        Path solutionPath = puzzlePath.resolve(filename);
        // ensure there is one and only one newline at the end
        String data = submission.getData().replaceFirst("\\s*$", "\n");
        Files.writeString(solutionPath, data, StandardOpenOption.CREATE_NEW);
        marshalSolutions(solutions, puzzlePath);
    } catch (IOException e) {
        // failures could happen after we dirtied the repo, so we call reset&clean on the puzzle dir
        access.resetAndClean(puzzlePath.toFile());
        return new SubmitResult.Failure<>(e.toString());
    }
    if (access.status().isClean()) {
        // the same exact sol was already archived,
        return new SubmitResult.AlreadyPresent<>();
    }
    String result = commit(access, submission, puzzlePath);
    return new SubmitResult.Success<>(result, beatenCategoryRecords);
}
Also used : Path(java.nio.file.Path) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) SubmitResult(com.faendir.zachtronics.bot.repository.SubmitResult) IOException(java.io.IOException) SzCategory(com.faendir.zachtronics.bot.sz.model.SzCategory) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with SzCategory

use of com.faendir.zachtronics.bot.sz.model.SzCategory in project zachtronics-leaderboard-bot by F43nd1r.

the class SzController method getRecord.

@Override
public SzRecordDTO getRecord(@NotNull String puzzleId, @NotNull String categoryId) {
    SzPuzzle puzzle = findPuzzle(puzzleId);
    SzCategory category = findCategory(categoryId);
    SzRecord record = repository.find(puzzle, category);
    if (record != null)
        return SzRecordDTO.fromCategoryRecord(new CategoryRecord<>(record, EnumSet.of(category)));
    else
        return null;
}
Also used : SzPuzzle(com.faendir.zachtronics.bot.sz.model.SzPuzzle) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) SzCategory(com.faendir.zachtronics.bot.sz.model.SzCategory) SzRecord(com.faendir.zachtronics.bot.sz.model.SzRecord)

Example 4 with SzCategory

use of com.faendir.zachtronics.bot.sz.model.SzCategory in project zachtronics-leaderboard-bot by F43nd1r.

the class SzShowCommand method findPuzzleAndCategory.

@NotNull
@Override
public Pair<SzPuzzle, SzCategory> findPuzzleAndCategory(@NotNull ShowData parameters) {
    SzPuzzle puzzle = parameters.puzzle;
    SzCategory category = parameters.category;
    if (!parameters.puzzle.getSupportedCategories().contains(category))
        throw new IllegalArgumentException("Category " + category.getDisplayName() + " does not support " + puzzle.getDisplayName());
    return new Pair<>(puzzle, category);
}
Also used : SzPuzzle(com.faendir.zachtronics.bot.sz.model.SzPuzzle) SzCategory(com.faendir.zachtronics.bot.sz.model.SzCategory) Pair(kotlin.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with SzCategory

use of com.faendir.zachtronics.bot.sz.model.SzCategory in project zachtronics-leaderboard-bot by F43nd1r.

the class SzSolutionRepository method writeToRedditLeaderboard.

@Override
protected void writeToRedditLeaderboard(SzPuzzle puzzle, Path puzzlePath, @NotNull List<SzSolution> solutions, String updateMessage) {
    Map<SzCategory, SzRecord> recordMap = new EnumMap<>(SzCategory.class);
    for (SzSolution solution : solutions) {
        SzRecord record = solution.extendToRecord(puzzle, makeArchiveLink(puzzle, solution.getScore()), makeArchivePath(puzzlePath, solution.getScore()));
        for (SzCategory category : solution.getCategories()) {
            recordMap.put(category, record);
        }
    }
    List<String> lines = Pattern.compile("\\r?\\n").splitAsStream(redditService.getWikiPage(Subreddit.SHENZHEN_IO, "index")).collect(// mutable list
    Collectors.toList());
    Pattern puzzleRegex = Pattern.compile("^\\| \\[" + Pattern.quote(puzzle.getDisplayName()));
    ListIterator<String> it = lines.listIterator();
    // |                        | [(**c**/pp/l)](https://cl.txt) |                                | [(c/pp/**l**)](https://lp.txt)
    while (it.hasNext()) {
        String line = it.next();
        if (puzzleRegex.matcher(line).find()) {
            it.remove();
            break;
        }
    }
    while (it.hasNext()) {
        String line = it.next();
        if (line.equals("|") || line.isBlank()) {
            it.previous();
            break;
        } else {
            it.remove();
        }
    }
    for (int rowIdx = 0; rowIdx < 2; rowIdx++) {
        StringBuilder row = new StringBuilder("| ");
        if (rowIdx == 0)
            row.append(Markdown.linkOrText(puzzle.getDisplayName(), puzzle.getLink()));
        SzCategory[] blockCategories = CATEGORIES[rowIdx];
        boolean usefulLine = false;
        for (int i = 0; i < 3; i++) {
            SzCategory thisCategory = blockCategories[i];
            row.append(" | ");
            SzRecord thisRecord = recordMap.get(thisCategory);
            if (rowIdx == 0 || thisRecord != recordMap.get(CATEGORIES[0][i])) {
                DisplayContext<SzCategory> displayContext = new DisplayContext<>(StringFormat.REDDIT, thisCategory);
                String cell = thisRecord.toDisplayString(displayContext);
                row.append(cell);
                usefulLine = true;
            }
        }
        if (usefulLine)
            it.add(row.toString());
    }
    redditService.updateWikiPage(Subreddit.SHENZHEN_IO, "index", String.join("\n", lines), updateMessage);
}
Also used : Pattern(java.util.regex.Pattern) DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) SzCategory(com.faendir.zachtronics.bot.sz.model.SzCategory)

Aggregations

SzCategory (com.faendir.zachtronics.bot.sz.model.SzCategory)6 NotNull (org.jetbrains.annotations.NotNull)4 CategoryRecord (com.faendir.zachtronics.bot.repository.CategoryRecord)2 SzPuzzle (com.faendir.zachtronics.bot.sz.model.SzPuzzle)2 SzRecord (com.faendir.zachtronics.bot.sz.model.SzRecord)2 DisplayContext (com.faendir.zachtronics.bot.model.DisplayContext)1 SubmitResult (com.faendir.zachtronics.bot.repository.SubmitResult)1 SzScore (com.faendir.zachtronics.bot.sz.model.SzScore)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Pattern (java.util.regex.Pattern)1 Pair (kotlin.Pair)1