Phase 6 of the minigame periods feature: - Muteable `minigame:achievement` notification type; the close pipeline now notifies each winner in-app, but only when the achievement row is newly written, so a re-run or a concurrent closer never double-sends. - New bot service `achievementAnnouncements` polls achievements with no announcedAt, groups them by window, posts ONE embed per window to DISCORD_ANNOUNCE_CHANNEL_ID, then stamps announcedAt + discordMessageId. A failed post leaves the rows unannounced so the next tick retries them. - Pure, testable renderer (bot/lib/achievementRender.ts) for grouping and the embed shape; window labels reuse the period/calendar helpers. - Register the service in the bot registry and add DISCORD_ACHIEVEMENT_POLL_MS (default 60s). - Tests: renderer grouping/ordering/fallbacks and the in-app notification path (including idempotency).
116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import { formatPeriodLabel } from "@/lib/minigames/periods/derive";
|
|
import { formatMonthLabel, formatYearLabel } from "@/lib/minigames/periods/calendar";
|
|
import { MINIGAME_ACHIEVEMENT_TYPE_LABELS } from "@/lib/minigames/registry";
|
|
|
|
/**
|
|
* Pure helpers that turn achievement rows into Discord announcement content.
|
|
* No Discord or Payload dependencies, so grouping and rendering are testable in
|
|
* isolation. The service layer converts the rendered shape into an EmbedBuilder.
|
|
*/
|
|
|
|
export interface AchievementAnnouncementRow {
|
|
id: number;
|
|
type: string;
|
|
window: string;
|
|
windowStart: string;
|
|
windowEnd: string;
|
|
user:
|
|
| number
|
|
| {
|
|
id: number;
|
|
displayName?: string | null;
|
|
payloadDisplayName?: string | null;
|
|
username?: string | null;
|
|
}
|
|
| null;
|
|
value: number;
|
|
detail?: string | null;
|
|
}
|
|
|
|
export interface AchievementAnnouncementGroup {
|
|
window: string;
|
|
windowStart: string;
|
|
windowEnd: string;
|
|
label: string;
|
|
achievements: AchievementAnnouncementRow[];
|
|
}
|
|
|
|
export interface RenderedAchievementAnnouncement {
|
|
title: string;
|
|
description: string;
|
|
color: number;
|
|
fields: { name: string; value: string }[];
|
|
}
|
|
|
|
/** Display order for awards inside one announcement. */
|
|
const AWARD_ORDER = [
|
|
"period-champion",
|
|
"player-of-year",
|
|
"player-of-month",
|
|
"most-improved",
|
|
"diligence",
|
|
"dedication",
|
|
"precision",
|
|
];
|
|
|
|
const GOLD = 0xf59e0b;
|
|
|
|
const nameOf = (user: AchievementAnnouncementRow["user"]): string => {
|
|
if (!user || typeof user !== "object") return "Unknown";
|
|
return user.displayName || user.payloadDisplayName || user.username || `Operator #${user.id}`;
|
|
};
|
|
|
|
/** Human label for a window, e.g. "Oct 12 to Oct 25, 2026", "October 2026", "2026". */
|
|
export function achievementWindowLabel(window: string, windowStart: string, windowEnd: string): string {
|
|
const start = new Date(windowStart);
|
|
if (window === "month") return formatMonthLabel(start);
|
|
if (window === "year") return formatYearLabel(start);
|
|
return formatPeriodLabel(start.getTime(), new Date(windowEnd).getTime());
|
|
}
|
|
|
|
/** Group achievement rows by window (one Discord message per window). */
|
|
export function groupAchievementsForAnnouncement(
|
|
rows: AchievementAnnouncementRow[],
|
|
): AchievementAnnouncementGroup[] {
|
|
const byKey = new Map<string, AchievementAnnouncementGroup>();
|
|
for (const row of rows) {
|
|
const key = `${row.window}|${row.windowStart}`;
|
|
const group =
|
|
byKey.get(key) ??
|
|
({
|
|
window: row.window,
|
|
windowStart: row.windowStart,
|
|
windowEnd: row.windowEnd,
|
|
label: achievementWindowLabel(row.window, row.windowStart, row.windowEnd),
|
|
achievements: [],
|
|
} satisfies AchievementAnnouncementGroup);
|
|
group.achievements.push(row);
|
|
byKey.set(key, group);
|
|
}
|
|
|
|
const groups = [...byKey.values()];
|
|
for (const group of groups) {
|
|
group.achievements.sort(
|
|
(a, b) => AWARD_ORDER.indexOf(a.type) - AWARD_ORDER.indexOf(b.type) || a.id - b.id,
|
|
);
|
|
}
|
|
return groups.sort((a, b) => new Date(a.windowStart).getTime() - new Date(b.windowStart).getTime());
|
|
}
|
|
|
|
/** Render one window group into embed-ready content (one message per close). */
|
|
export function renderAchievementAnnouncement(
|
|
group: AchievementAnnouncementGroup,
|
|
): RenderedAchievementAnnouncement {
|
|
return {
|
|
title: `Minigame results: ${group.label}`,
|
|
description:
|
|
group.window === "period"
|
|
? "Anchored period closed. Placement points across the five Core boards."
|
|
: `Calendar ${group.window} closed.`,
|
|
color: GOLD,
|
|
fields: group.achievements.map((achievement) => ({
|
|
name: MINIGAME_ACHIEVEMENT_TYPE_LABELS[achievement.type as keyof typeof MINIGAME_ACHIEVEMENT_TYPE_LABELS] ?? achievement.type,
|
|
value: `${nameOf(achievement.user)} · ${achievement.detail ?? `${achievement.value}`}`,
|
|
})),
|
|
};
|
|
}
|