Search in sources :

Example 6 with CategoryRecord

use of com.faendir.zachtronics.bot.repository.CategoryRecord in project zachtronics-leaderboard-bot by F43nd1r.

the class SzManualTest method bootstrapPsv.

@Test
public void bootstrapPsv() throws IOException {
    Path repoPath = Paths.get("../shenzhenIO/leaderboard");
    for (SzPuzzle puzzle : SzPuzzle.values()) {
        if (puzzle.getType() != SzType.STANDARD)
            continue;
        Path indexPath = repoPath.resolve(puzzle.getGroup().getRepoFolder()).resolve(puzzle.getId()).resolve("solutions.psv");
        try (ICSVWriter writer = new CSVWriterBuilder(Files.newBufferedWriter(indexPath)).withSeparator('|').build()) {
            Map<SzScore, CategoryRecord<SzRecord, SzCategory>> scoreMap = repository.findCategoryHolders(puzzle, true).stream().collect(Collectors.toMap(cr -> cr.getRecord().getScore(), Function.identity(), (cr1, cr2) -> {
                cr1.getCategories().addAll(cr2.getCategories());
                return cr1;
            }, () -> new TreeMap<>(SzCategory.CP.getScoreComparator())));
            for (CategoryRecord<SzRecord, SzCategory> cr : scoreMap.values()) {
                SzRecord record = cr.getRecord();
                String author = record.getAuthor();
                String categories = cr.getCategories().stream().map(SzCategory::name).sorted().collect(Collectors.joining(","));
                String[] csvRecord = new String[] { record.getScore().toDisplayString(), author, categories };
                writer.writeNext(csvRecord, false);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DisplayContext(com.faendir.zachtronics.bot.model.DisplayContext) Subreddit(com.faendir.zachtronics.bot.reddit.Subreddit) Files(java.nio.file.Files) BotTest(com.faendir.zachtronics.bot.BotTest) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) com.faendir.zachtronics.bot.sz.model(com.faendir.zachtronics.bot.sz.model) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) TreeMap(java.util.TreeMap) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) Paths(java.nio.file.Paths) Map(java.util.Map) RedditService(com.faendir.zachtronics.bot.reddit.RedditService) ICSVWriter(com.opencsv.ICSVWriter) Path(java.nio.file.Path) CSVWriterBuilder(com.opencsv.CSVWriterBuilder) CSVWriterBuilder(com.opencsv.CSVWriterBuilder) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) TreeMap(java.util.TreeMap) ICSVWriter(com.opencsv.ICSVWriter) BotTest(com.faendir.zachtronics.bot.BotTest) Test(org.junit.jupiter.api.Test)

Example 7 with CategoryRecord

use of com.faendir.zachtronics.bot.repository.CategoryRecord in project zachtronics-leaderboard-bot by F43nd1r.

the class IfSolutionRepository method archiveOne.

/**
 * @param solutions the list is modified with the updated state
 */
@Override
@NotNull
protected SubmitResult<IfRecord, IfCategory> archiveOne(@NotNull GitRepository.ReadWriteAccess access, @NotNull List<IfSolution> solutions, @NotNull IfSubmission submission) {
    IfPuzzle puzzle = submission.getPuzzle();
    Path puzzlePath = getPuzzlePath(access, puzzle);
    List<CategoryRecord<IfRecord, IfCategory>> beatenCategoryRecords = new ArrayList<>();
    IfSolution candidate = new IfSolution(submission.getScore(), submission.getAuthor(), submission.getDisplayLinks());
    try {
        for (ListIterator<IfSolution> it = solutions.listIterator(); it.hasNext(); ) {
            IfSolution solution = it.next();
            int r = dominanceCompare(candidate.getScore(), solution.getScore());
            if (r > 0) {
                // TODO actually return all of the beating sols
                CategoryRecord<IfRecord, IfCategory> 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 (IfSolution solution : solutions) {
            EnumSet<IfCategory> lostCategories = EnumSet.noneOf(IfCategory.class);
            for (IfCategory 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<IfRecord, IfCategory> 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 = makeScoreFilename(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) IfCategory(com.faendir.zachtronics.bot.inf.model.IfCategory) SubmitResult(com.faendir.zachtronics.bot.repository.SubmitResult) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CategoryRecord (com.faendir.zachtronics.bot.repository.CategoryRecord)7 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 SubmitResult (com.faendir.zachtronics.bot.repository.SubmitResult)3 NotNull (org.jetbrains.annotations.NotNull)3 BotTest (com.faendir.zachtronics.bot.BotTest)2 IfCategory (com.faendir.zachtronics.bot.inf.model.IfCategory)2 ScCategory (com.faendir.zachtronics.bot.sc.model.ScCategory)2 SzCategory (com.faendir.zachtronics.bot.sz.model.SzCategory)2 Test (org.junit.jupiter.api.Test)2 IfPuzzle (com.faendir.zachtronics.bot.inf.model.IfPuzzle)1 IfRecord (com.faendir.zachtronics.bot.inf.model.IfRecord)1 DisplayContext (com.faendir.zachtronics.bot.model.DisplayContext)1 RedditService (com.faendir.zachtronics.bot.reddit.RedditService)1 Subreddit (com.faendir.zachtronics.bot.reddit.Subreddit)1 ScPuzzle (com.faendir.zachtronics.bot.sc.model.ScPuzzle)1 com.faendir.zachtronics.bot.sz.model (com.faendir.zachtronics.bot.sz.model)1 SzPuzzle (com.faendir.zachtronics.bot.sz.model.SzPuzzle)1 SzRecord (com.faendir.zachtronics.bot.sz.model.SzRecord)1 CSVWriterBuilder (com.opencsv.CSVWriterBuilder)1