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);
}
}
}
}
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);
}
Aggregations