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