diff --git a/src/app/(frontend)/personnel/contacts/[id]/actions.ts b/src/app/(frontend)/personnel/contacts/[id]/actions.ts index 095fd188..8ac1e234 100644 --- a/src/app/(frontend)/personnel/contacts/[id]/actions.ts +++ b/src/app/(frontend)/personnel/contacts/[id]/actions.ts @@ -15,6 +15,7 @@ import { canSeeTestingDialogues, loadPlayerDialogue } from "@/lib/npc-dialogue/v import { resolveAttachments, resolveVoiceClip } from "@/lib/npc-dialogue/attachments"; import { transcodeToMp3, MP3_MIMETYPE } from "@/lib/voiceover/transcode"; import { hasPermission } from "@/utils/access-control/hasPermission"; +import { launchDialoguePoll } from "@/lib/polls/service"; import { generateDialogueDraft, rewriteDialogueLine } from "@/lib/npc-dialogue/generation"; import { loadDialogueSettings } from "@/lib/npc-dialogue/generationSettings"; import { @@ -72,7 +73,7 @@ export async function triggerNpcDialogueEvent( dialogueId: number, npcId: number, nodeId: string, -): Promise { +): Promise> { try { const { payload, user } = await authenticate(); @@ -97,9 +98,15 @@ export async function triggerNpcDialogueEvent( } const effect: NpcDialogueEffect = node.data.effect; - await runEffect(payload, user.id as number, npcId, effect); + const result = await runEffect(payload, { + userId: user.id as number, + npcId, + dialogueId, + nodeId, + effect, + }); - return { success: true }; + return { success: true, data: result }; } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; if (message === "Unauthorized") { @@ -111,10 +118,16 @@ export async function triggerNpcDialogueEvent( async function runEffect( payload: Awaited>, - userId: number, - npcId: number, - effect: NpcDialogueEffect, -): Promise { + context: { + userId: number; + npcId: number; + dialogueId: number; + nodeId: string; + effect: NpcDialogueEffect; + }, +): Promise<{ pollId: number } | undefined> { + const { userId, npcId, dialogueId, nodeId, effect } = context; + switch (effect.type) { case "notify": { await notifyUser(payload, { @@ -183,6 +196,30 @@ async function runEffect( }); break; } + case "poll": { + // Idempotent: the node raises the poll once, and every later player who + // reaches it joins the same poll instead of opening a second one. + const { poll, created } = await launchDialoguePoll(payload, { + dialogueId, + nodeId, + initiatorId: userId, + config: effect.poll, + }); + + // The conversation hands the player a durable way back to the poll, and + // says whether they are the one who opened it. + await notifyUser(payload, { + userId, + type: "poll:open", + title: poll.title, + message: created + ? "The unit is being polled. Cast your vote." + : "This poll is already open. Cast your vote.", + link: `/polls/${poll.id}`, + }); + + return { pollId: poll.id }; + } } } diff --git a/src/collections/game/GameEventLogs.ts b/src/collections/game/GameEventLogs.ts index f12a1fb3..182ebfbc 100644 --- a/src/collections/game/GameEventLogs.ts +++ b/src/collections/game/GameEventLogs.ts @@ -55,6 +55,7 @@ const TARGET_COLLECTIONS = [ { label: "Leadership Transfers", value: "leadership-transfers" }, { label: "Player Flags", value: "player-flags" }, { label: "Reinforcement Pools", value: "reinforcement-pools" }, + { label: "Polls", value: "polls" }, ] as const; export const GameEventLogs: CollectionConfig = { diff --git a/src/components/admin/npc-dialogue/NodeEditDialog.tsx b/src/components/admin/npc-dialogue/NodeEditDialog.tsx index be68d532..99f073f3 100644 --- a/src/components/admin/npc-dialogue/NodeEditDialog.tsx +++ b/src/components/admin/npc-dialogue/NodeEditDialog.tsx @@ -27,6 +27,9 @@ import type { DialogueVoiceClip, NpcDialogueEffect, NpcDialogueNodeType, + NpcDialoguePollConfig, + NpcDialoguePollOption, + NpcDialoguePollWeightTier, } from "@/lib/npc-dialogue/dialogue"; import { conditionSummary, @@ -63,8 +66,43 @@ const EFFECT_LABELS: Record = { emit: "Log game event", reward: "Reward (unconfigured)", flag: "Set player flag", + poll: "Open a unit poll", }; +/** Compact checkbox list for role ids. Native inputs: the admin has no preflight. */ +function RoleChecklist({ + roles, + value, + onChange, +}: { + readonly roles: { id: number; name: string }[]; + readonly value: number[]; + readonly onChange: (next: number[]) => void; +}) { + if (roles.length === 0) { + return

No roles loaded.

; + } + return ( +
+ {roles.map((role) => ( + + ))} +
+ ); +} + const FLAG_OP_LABELS: Record = { set_true: "Set to true", set_false: "Set to false", @@ -193,6 +231,30 @@ export function NodeEditDialog({ }; }, [open]); + // Roles for poll audience and voting-weight tiers. Same one-shot fetch. + const [roles, setRoles] = useState<{ id: number; name: string }[]>([]); + useEffect(() => { + let active = true; + if (!open) return; + fetch("/api/roles?limit=200&depth=0") + .then((res) => (res.ok ? res.json() : null)) + .then((data) => { + if (!active) return; + const list = Array.isArray(data?.docs) ? data.docs : []; + setRoles( + list + .filter((r: { name?: unknown }) => Boolean(r?.name)) + .map((r: { id: number; name: string }) => ({ id: r.id, name: r.name })), + ); + }) + .catch(() => { + if (active) setRoles([]); + }); + return () => { + active = false; + }; + }, [open]); + useEffect(() => { setDraft(node?.data ?? null); }, [node?.id, open]); @@ -224,6 +286,28 @@ export function NodeEditDialog({ const patchEffect = (partial: Partial) => patch({ effect: { type: "notify", ...draft.effect, ...partial } }); + // Poll authoring helpers. The poll config lives under `effect.poll` so the + // effect keeps its own flat fields (message, visibility, discord) untouched. + const patchPoll = (partial: Partial) => + patchEffect({ poll: { ...draft.effect?.poll, ...partial } }); + const pollOptions = draft.effect?.poll?.options ?? []; + const setPollOption = (index: number, partial: Partial) => + patchPoll({ + options: pollOptions.map((option, i) => (i === index ? { ...option, ...partial } : option)), + }); + const addPollOption = () => patchPoll({ options: [...pollOptions, { label: "" }] }); + const removePollOption = (index: number) => + patchPoll({ options: pollOptions.filter((_, i) => i !== index) }); + const pollTiers = draft.effect?.poll?.weightTiers ?? []; + const setPollTier = (index: number, partial: Partial) => + patchPoll({ + weightTiers: pollTiers.map((tier, i) => (i === index ? { ...tier, ...partial } : tier)), + }); + const addPollTier = () => + patchPoll({ weightTiers: [...pollTiers, { label: "", roleIds: [], multiplier: 1.5 }] }); + const removePollTier = (index: number) => + patchPoll({ weightTiers: pollTiers.filter((_, i) => i !== index) }); + const setChoice = (index: number, next: DialogueNext) => @@ -737,7 +821,241 @@ export function NodeEditDialog({

)} - {draft.effect && ( + {draft.effect?.type === "poll" && ( +
+
+ + patchPoll({ title: e.target.value })} + placeholder="Should the Task Force work with the Vanguard?" + className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30" + /> +
+
+ +