Search in sources :

Example 1 with DisplayContext

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

the class ScSolutionRepository method writeToRedditLeaderboard.

@Override
protected void writeToRedditLeaderboard(@NotNull ScPuzzle puzzle, Path puzzlePath, @NotNull List<ScSolution> solutions, String updateMessage) {
    Map<ScCategory, ScRecord> recordMap = new EnumMap<>(ScCategory.class);
    Map<ScCategory, ScRecord> videoRecordMap = new EnumMap<>(ScCategory.class);
    List<ScRecord> videoRecords = solutions.stream().filter(s -> s.getDisplayLink() != null).map(// no export needed
    s -> s.extendToRecord(puzzle, null, null)).toList();
    for (ScSolution solution : solutions) {
        ScRecord record = solution.extendToRecord(puzzle, makeArchiveLink(puzzle, solution.getScore()), makeArchivePath(puzzlePath, solution.getScore()));
        for (ScCategory category : solution.getCategories()) {
            recordMap.put(category, record);
            if (record.getDisplayLink() == null) {
                videoRecords.stream().filter(s -> category.supportsScore(s.getScore())).min(Comparator.comparing(ScRecord::getScore, category.getScoreComparator())).ifPresent(// bosses have no videos at all
                videoRecord -> videoRecordMap.put(category, videoRecord));
            }
        }
    }
    String[] lines = redditService.getWikiPage(Subreddit.SPACECHEM, puzzle.getGroup().getWikiPage()).split("\\r?\\n");
    Pattern puzzleRegex = Pattern.compile("^\\| \\[" + Pattern.quote(puzzle.getDisplayName()));
    int rowIdx = 0;
    // | [Puzzle - 1 Reactor](https://zlbb) | [(**ccc**/**r**/ss) author](https://li.nk) | ← | [(ccc/**r**/**ss**) author](https://li.nk) | ←
    for (int lineIdx = 0; lineIdx < lines.length; lineIdx++) {
        String line = lines[lineIdx];
        if (puzzleRegex.matcher(line).find()) {
            String[] prevElems = line.trim().split("\\s*\\|\\s*", -1);
            int halfSize = (prevElems.length - 2) / 2;
            StringBuilder row = new StringBuilder("| ");
            int minReactors = Integer.MAX_VALUE;
            String rowTitle = puzzle.getDisplayName();
            if (rowIdx == 1) {
                minReactors = recordMap.get(ScCategory.RC).getScore().getReactors();
                rowTitle += " - " + minReactors + " Reactor" + (minReactors == 1 ? "" : "s");
            }
            row.append(Markdown.linkOrText(rowTitle, puzzle.getLink()));
            for (int block = 0; block < 2; block++) {
                ScCategory[] blockCategories = CATEGORIES[2 * rowIdx + block];
                ScRecord[] blockRecords = Arrays.stream(blockCategories).map(recordMap::get).toArray(ScRecord[]::new);
                ScRecord[] blockVideoRecords = Arrays.stream(blockCategories).map(videoRecordMap::get).toArray(ScRecord[]::new);
                for (int i = 0; i < halfSize; i++) {
                    ScCategory thisCategory = blockCategories[i];
                    row.append(" | ");
                    if (blockRecords[i] != null) {
                        DisplayContext<ScCategory> displayContext = new DisplayContext<>(StringFormat.REDDIT, thisCategory);
                        String cell = makeLeaderboardCell(blockRecords, i, minReactors, displayContext);
                        row.append(cell);
                        if (blockVideoRecords[i] != null) {
                            String videoCell = makeLeaderboardCell(blockVideoRecords, i, Integer.MAX_VALUE, displayContext);
                            if (!cell.equals(videoCell))
                                row.append(". Top&nbsp;video&nbsp;").append(videoCell);
                        }
                    } else
                        row.append(prevElems[2 + block * halfSize + i]);
                }
            }
            lines[lineIdx] = row.toString();
            rowIdx++;
        } else if (rowIdx != 0) {
            // we've already found the point and now we're past it, we're done
            break;
        }
    }
    redditService.updateWikiPage(Subreddit.SPACECHEM, puzzle.getGroup().getWikiPage(), String.join("\n", lines), updateMessage);
}
Also used : DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) Subreddit(com.faendir.zachtronics.bot.reddit.Subreddit) java.util(java.util) Getter(lombok.Getter) GitRepository(com.faendir.zachtronics.bot.git.GitRepository) AbstractSolutionRepository(com.faendir.zachtronics.bot.repository.AbstractSolutionRepository) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Function(java.util.function.Function) AccessLevel(lombok.AccessLevel) Qualifier(org.springframework.beans.factory.annotation.Qualifier) BiConsumer(java.util.function.BiConsumer) RedditService(com.faendir.zachtronics.bot.reddit.RedditService) Path(java.nio.file.Path) Markdown(com.faendir.zachtronics.bot.utils.Markdown) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) com.faendir.zachtronics.bot.sc.model(com.faendir.zachtronics.bot.sc.model) IOException(java.io.IOException) StringFormat(com.faendir.zachtronics.bot.model.StringFormat) Component(org.springframework.stereotype.Component) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) Paths(java.nio.file.Paths) SubmitResult(com.faendir.zachtronics.bot.repository.SubmitResult) ScCategory(com.faendir.zachtronics.bot.sc.model.ScCategory) Pattern(java.util.regex.Pattern) ValidationResult(com.faendir.zachtronics.bot.validation.ValidationResult) NotNull(org.jetbrains.annotations.NotNull) Pattern(java.util.regex.Pattern) DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) ScCategory(com.faendir.zachtronics.bot.sc.model.ScCategory)

Example 2 with DisplayContext

use of com.faendir.zachtronics.bot.model.DisplayContext 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)

Example 3 with DisplayContext

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

the class IfSolutionRepository method writeToRedditLeaderboard.

@Override
protected void writeToRedditLeaderboard(IfPuzzle puzzle, Path puzzlePath, @NotNull List<IfSolution> solutions, String updateMessage) {
    if (// TODO
    true)
        return;
    Map<IfCategory, IfRecord> recordMap = new EnumMap<>(IfCategory.class);
    for (IfSolution solution : solutions) {
        IfRecord record = solution.extendToRecord(puzzle, makeArchiveLink(puzzle, solution.getScore()), makeArchivePath(puzzlePath, solution.getScore()));
        for (IfCategory category : solution.getCategories()) {
            recordMap.put(category, record);
        }
    }
    List<String> lines = Pattern.compile("\\r?\\n").splitAsStream(redditService.getWikiPage(Subreddit.INFINIFACTORY, "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()));
        IfCategory[] blockCategories = CATEGORIES[rowIdx];
        boolean usefulLine = false;
        for (int i = 0; i < 3; i++) {
            IfCategory thisCategory = blockCategories[i];
            row.append(" | ");
            IfRecord thisRecord = recordMap.get(thisCategory);
            if (rowIdx == 0 || thisRecord != recordMap.get(CATEGORIES[0][i])) {
                DisplayContext<IfCategory> 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.INFINIFACTORY, "index", String.join("\n", lines), updateMessage);
}
Also used : Pattern(java.util.regex.Pattern) DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) IfCategory(com.faendir.zachtronics.bot.inf.model.IfCategory)

Aggregations

DisplayContext (com.faendir.zachtronics.bot.model.DisplayContext)3 Pattern (java.util.regex.Pattern)3 GitRepository (com.faendir.zachtronics.bot.git.GitRepository)1 IfCategory (com.faendir.zachtronics.bot.inf.model.IfCategory)1 StringFormat (com.faendir.zachtronics.bot.model.StringFormat)1 RedditService (com.faendir.zachtronics.bot.reddit.RedditService)1 Subreddit (com.faendir.zachtronics.bot.reddit.Subreddit)1 AbstractSolutionRepository (com.faendir.zachtronics.bot.repository.AbstractSolutionRepository)1 CategoryRecord (com.faendir.zachtronics.bot.repository.CategoryRecord)1 SubmitResult (com.faendir.zachtronics.bot.repository.SubmitResult)1 com.faendir.zachtronics.bot.sc.model (com.faendir.zachtronics.bot.sc.model)1 ScCategory (com.faendir.zachtronics.bot.sc.model.ScCategory)1 SzCategory (com.faendir.zachtronics.bot.sz.model.SzCategory)1 Markdown (com.faendir.zachtronics.bot.utils.Markdown)1 ValidationResult (com.faendir.zachtronics.bot.validation.ValidationResult)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 StandardOpenOption (java.nio.file.StandardOpenOption)1