Skip to content

Commit

Permalink
Delete excavator kmails (#77)
Browse files Browse the repository at this point in the history
* Delete excavator kmails

Only check all pages once a day in case the player has a ton of sent kmails that aren't excavator

* Update utils.ts

* js is a bad langauge
  • Loading branch information
Rinn committed Jun 1, 2024
1 parent a11d09f commit 27614a4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/excavator-script/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
inMultiFight,
print,
printHtml,
sessionStorage,
todayToString,
visitUrl,
} from "kolmafia";
import { get, Kmail, set } from "libram";

Expand Down Expand Up @@ -111,6 +114,7 @@ export function sendSpadingData(projectName: string, data: object) {

if (success) {
flushSpadingData();
deleteSpadingKmail(recipient);
return;
}

Expand All @@ -126,3 +130,43 @@ export function sendSpadingData(projectName: string, data: object) {
`Excavator's project to spade ${projectName}`,
);
}

function deleteSpadingKmail(sentTo: string): void {
// Check all Outbox pages once a day, then only the first page
const maxPage =
(sessionStorage.getItem("LastOutboxPurge") ?? "") !== todayToString()
? Infinity
: 1;

let currentPage = 1;
while (currentPage <= maxPage) {
const buffer = visitUrl(
`messages.php?box=Outbox&begin=${currentPage}&per_page=10`,
).toLowerCase();
if (!buffer.includes("toid")) break;
const messageIds: string[] = buffer
.split("td valign=top")
.filter((s) =>
s.match(
`<a href="showplayer.php\\?who=(\\d+)">${sentTo.toLowerCase()}</a>`,
),
)
.map((s) => {
const match = s.match('checkbox name="sel(\\d+)"');
return match ? match[1] : "";
})
.filter((s) => s.length > 0);

if (messageIds.length === 0) {
currentPage += 1;
continue;
}

const del = `messages.php?the_action=delete&box=Outbox&pwd${messageIds.map((id) => `&sel${id}=on`).join("")}`;
visitUrl(del);
}

if (maxPage === Infinity) {
sessionStorage.setItem("LastOutboxPurge", todayToString());
}
}

0 comments on commit 27614a4

Please sign in to comment.