tally.ts is pure (no Payload, no clock, no ambient randomness) so the vote rules are testable on their own: - Weights take the HIGHEST matching role tier, never a sum, so a member in two weighted roles cannot outvote the unit. Equal multipliers resolve to the earlier tier for determinism. - Weighted scores accumulate as integer thousandths, so 1.1 + 1.1 ties 2.2 exactly instead of drifting apart on float addition; tie detection is an equality test, so that matters. - Duplicate keys in one ballot are deduped, answers naming options the poll no longer carries are ignored, and a ballot naming only dead options is not turnout. service.ts is the single write path for both surfaces: launch (idempotent on sourceKey, racing players join the winner), cast (replace or retract), close, superuser tie decision, deadline fallback, and advancePoll as the one reconcile entry point the bot tick and the poll page share. Weight is resolved at close and frozen into the outcome, so the tie set a superuser is asked about cannot shift while they decide, and a second close cannot rewrite the result. surface.ts is the read model: it reads ballots with overrideAccess and includes voter identities only for a public poll or a superuser.
20 lines
690 B
TypeScript
20 lines
690 B
TypeScript
import type { Payload } from "payload";
|
|
|
|
export const POLL_CHANGED_CHANNEL = "ptf_poll_changed";
|
|
|
|
/**
|
|
* Tell the standalone Discord bot that a poll changed, so it re-renders
|
|
* immediately instead of waiting for its next poll tick. Mirrors
|
|
* `notifyAttendanceChanged`; the bot also reconciles on a timer, so a dropped
|
|
* notification only costs latency.
|
|
*/
|
|
export async function notifyPollChanged(payload: Payload, pollId: number): Promise<void> {
|
|
try {
|
|
await payload.db.pool.query("SELECT pg_notify($1, $2)", [
|
|
POLL_CHANGED_CHANNEL,
|
|
JSON.stringify({ pollId }),
|
|
]);
|
|
} catch {
|
|
// The bot's timer is the fallback; a notify failure must never fail a vote.
|
|
}
|
|
}
|