Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/ocr'
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jul 31, 2023
2 parents 4859aac + 9269a01 commit 02e5031
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public CommandManager2 registerDefaults() {
this.commands.registerMethod(new PlayerSettingCommands(), List.of("announcement"), "viewAnnouncement", "view");

this.commands.registerMethod(new TestCommands(), List.of("announcement"), "ocr", "ocr");
this.commands.registerMethod(new TestCommands(), List.of("announcement"), "find_announcement", "find");

StringBuilder output = new StringBuilder();
this.commands.generatePojo("", output, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,49 @@ public String archiveAnnouncement(@Me GuildDB db, int announcementId, @Default B
return (archive ? "Archived" : "Unarchived") + " announcement with id: #" + announcementId;
}

@Command(desc = "Find the announcement closest matching a message")
@RolePermission(Roles.ADMIN)
@NoFormat
public String find_announcement(@Me GuildDB db, int announcementId, String message) throws IOException {
// Announcement announcement = db.getAnnouncement(announcementId);
List<Announcement.PlayerAnnouncement> announcements = db.getPlayerAnnouncementsByAnnId(announcementId);
if (announcements.isEmpty()) {
return "No announcements found with id: #" + announcementId;
}
long diffMin = Long.MAX_VALUE;
List<Announcement.PlayerAnnouncement> matches = new ArrayList<>();
for (Announcement.PlayerAnnouncement announcement : announcements) {
String content = announcement.getContent();
if (message.equalsIgnoreCase(content)) {
return "Announcement sent to nation id: " + announcement.receiverNation;
}
byte[] diff = StringMan.getDiffBytes(message, content);
if (diff.length < diffMin) {
diffMin = diff.length;
matches.clear();
matches.add(announcement);
} else if (diff.length == diffMin) {
matches.add(announcement);
}
}

if (matches.isEmpty()) {
return "No announcements found with id: #" + announcementId;
} else if (matches.size() == 1) {
Announcement.PlayerAnnouncement match = matches.get(0);
return "Closest match: " + match.receiverNation + " with " + diffMin + " differences:\n```\n" + match.getContent() + "\n```";
} else {
StringBuilder response = new StringBuilder();
response.append(matches.size() + " matches with " + diffMin + " differences:\n");
for (Announcement.PlayerAnnouncement match : matches) {
response.append("- " + match.receiverNation + "\n");
// content in ```
response.append("```\n" + match.getContent() + "\n```\n");
}
return response.toString();
}
}

@Command(desc = "Send an announcement to multiple nations, with random variations for each receiver\n")
@RolePermission(Roles.ADMIN)
@HasApi
Expand Down

0 comments on commit 02e5031

Please sign in to comment.