Files
2026-08-18 20:07:41 +02:00

200 lines
3.9 KiB
JavaScript

const {
getCollections,
getUnixTime
} = require(
"../utils/cookieEconomy"
);
const {
activateCrumbleStrike,
failCrumbleStrike,
cancelCrumbleStrikeAndRefund
} = require(
"../utils/crumbleStrike"
);
const CHECK_INTERVAL =
60 * 1000;
module.exports = client => {
setTimeout(
() => {
processCrumbleStrikeEvents(
client
);
},
5000
);
setInterval(
() => {
processCrumbleStrikeEvents(
client
);
},
CHECK_INTERVAL
);
console.log(
"[CrumbleStrike] Event watcher initialized"
);
};
async function processCrumbleStrikeEvents(
client
) {
try {
const {
db
} =
getCollections();
const events =
db.collection(
"items_events"
);
const now =
getUnixTime();
/*
* Recruitment expired.
*/
const joiningEvents =
await events
.find({
eventType:
"CrumbleStrike",
state:
"Joining",
joinUntil: {
$lte:
now
}
})
.toArray();
for (
const event
of joiningEvents
) {
const participantCount =
Object.keys(
event.participants ??
{}
).length;
console.log(
`[CrumbleStrike] Recruitment expired for ${event.eventId} ` +
`(${participantCount}/${event.minimumParticipants})`
);
if (
participantCount >=
event.minimumParticipants
) {
const started =
await activateCrumbleStrike(
client,
event.eventId
);
if (started) {
console.log(
`[CrumbleStrike] ${event.eventId} started automatically`
);
}
continue;
}
const cancelled =
await cancelCrumbleStrikeAndRefund(
client,
event.eventId
);
if (cancelled) {
console.log(
`[CrumbleStrike] ${event.eventId} cancelled and refunded`
);
}
}
/*
* Battle expired.
*/
const activeEvents =
await events
.find({
eventType:
"CrumbleStrike",
state:
"Active",
battleEndsAt: {
$lte:
now
},
"boss.health": {
$gt:
0
}
})
.toArray();
for (
const event
of activeEvents
) {
console.log(
`[CrumbleStrike] ${event.eventId} expired with ` +
`${event.boss.health}/${event.boss.maxHealth} HP remaining`
);
const failed =
await failCrumbleStrike(
client,
event.eventId
);
if (failed) {
console.log(
`[CrumbleStrike] ${event.eventId} failed`
);
}
}
} catch (error) {
console.error(
"[CrumbleStrike] Watcher error:",
error
);
}
}