feat(npc-dialogue): add global flag support (nullable user on player-flags)
This commit is contained in:
parent
4c2fc86ecb
commit
035dd49149
7 changed files with 37439 additions and 17 deletions
|
|
@ -138,13 +138,17 @@ async function runEffect(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Effective value of a player flag: missing flags read false / 0. */
|
/**
|
||||||
|
* Effective value of a flag: player-specific entry takes priority; if absent,
|
||||||
|
* fall back to the global entry (user is null). Missing flags read false / 0.
|
||||||
|
*/
|
||||||
async function readFlag(
|
async function readFlag(
|
||||||
payload: Awaited<ReturnType<typeof getPayload>>,
|
payload: Awaited<ReturnType<typeof getPayload>>,
|
||||||
userId: number,
|
userId: number,
|
||||||
key: string,
|
key: string,
|
||||||
): Promise<{ bool: boolean; num: number }> {
|
): Promise<{ bool: boolean; num: number }> {
|
||||||
const res = await payload.find({
|
// Player-specific flag (priority).
|
||||||
|
const playerRes = await payload.find({
|
||||||
collection: "player-flags",
|
collection: "player-flags",
|
||||||
where: {
|
where: {
|
||||||
user: { equals: userId },
|
user: { equals: userId },
|
||||||
|
|
@ -154,13 +158,34 @@ async function readFlag(
|
||||||
depth: 0,
|
depth: 0,
|
||||||
overrideAccess: true,
|
overrideAccess: true,
|
||||||
});
|
});
|
||||||
const doc = res.docs[0];
|
const playerDoc = playerRes.docs[0];
|
||||||
if (!doc) return { bool: false, num: 0 };
|
if (playerDoc) {
|
||||||
if (doc.flagType === "number") {
|
if (playerDoc.flagType === "number") {
|
||||||
const num = doc.numberValue ?? 0;
|
const num = playerDoc.numberValue ?? 0;
|
||||||
|
return { bool: num !== 0, num };
|
||||||
|
}
|
||||||
|
const bool = playerDoc.boolValue === true;
|
||||||
|
return { bool, num: bool ? 1 : 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Global fallback (user is null).
|
||||||
|
const globalRes = await payload.find({
|
||||||
|
collection: "player-flags",
|
||||||
|
where: {
|
||||||
|
user: { equals: null },
|
||||||
|
key: { equals: key },
|
||||||
|
},
|
||||||
|
limit: 1,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
const globalDoc = globalRes.docs[0];
|
||||||
|
if (!globalDoc) return { bool: false, num: 0 };
|
||||||
|
if (globalDoc.flagType === "number") {
|
||||||
|
const num = globalDoc.numberValue ?? 0;
|
||||||
return { bool: num !== 0, num };
|
return { bool: num !== 0, num };
|
||||||
}
|
}
|
||||||
const bool = doc.boolValue === true;
|
const bool = globalDoc.boolValue === true;
|
||||||
return { bool, num: bool ? 1 : 0 };
|
return { bool, num: bool ? 1 : 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ import { CollectionConfig } from "payload";
|
||||||
import { requirePermission } from "@/utils/access-control/hasPermission";
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic per-player flags: the lightweight state layer conversations branch
|
* Generic flags — per-player (when `user` is set) or global (when null).
|
||||||
* on. Event nodes write them (set / add), condition nodes read them. GMs can
|
* The lightweight state layer conversations branch on. Event nodes write
|
||||||
* also seed or correct them here in the admin panel.
|
* player flags; condition nodes read them, falling back to global if the
|
||||||
|
* player-specific flag is absent. GMs can seed or correct both here in the
|
||||||
|
* admin panel.
|
||||||
*/
|
*/
|
||||||
export const PlayerFlags: CollectionConfig = {
|
export const PlayerFlags: CollectionConfig = {
|
||||||
slug: "player-flags",
|
slug: "player-flags",
|
||||||
|
|
@ -26,9 +28,8 @@ export const PlayerFlags: CollectionConfig = {
|
||||||
name: "user",
|
name: "user",
|
||||||
type: "relationship",
|
type: "relationship",
|
||||||
relationTo: "users",
|
relationTo: "users",
|
||||||
required: true,
|
|
||||||
admin: {
|
admin: {
|
||||||
description: "The player this flag belongs to.",
|
description: "The player this flag belongs to. Leave empty for a global flag.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -411,8 +411,8 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
|
||||||
placeholder="met_informant"
|
placeholder="met_informant"
|
||||||
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30"
|
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30"
|
||||||
/>
|
/>
|
||||||
<p className="text-[10px] text-white/40">
|
<p className="text-[10px] text-zinc-400">
|
||||||
Checked against the player's flags. A missing flag counts as false / 0.
|
Player-specific flag takes priority; falls back to global (no user). Missing = false / 0.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
37379
src/migrations/20261006_155244_add_global_flag_support.json
Normal file
37379
src/migrations/20261006_155244_add_global_flag_support.json
Normal file
File diff suppressed because it is too large
Load diff
11
src/migrations/20261006_155244_add_global_flag_support.ts
Normal file
11
src/migrations/20261006_155244_add_global_flag_support.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
|
||||||
|
|
||||||
|
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "player_flags" ALTER COLUMN "user_id" DROP NOT NULL;`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "player_flags" ALTER COLUMN "user_id" SET NOT NULL;`)
|
||||||
|
}
|
||||||
|
|
@ -108,6 +108,7 @@ import * as migration_20261005_054132_add_game_npc_bio_richtext from './20261005
|
||||||
import * as migration_20261005_063613_add_game_npc_image from './20261005_063613_add_game_npc_image';
|
import * as migration_20261005_063613_add_game_npc_image from './20261005_063613_add_game_npc_image';
|
||||||
import * as migration_20261005_185014_add_player_flags from './20261005_185014_add_player_flags';
|
import * as migration_20261005_185014_add_player_flags from './20261005_185014_add_player_flags';
|
||||||
import * as migration_20261006_000000_add_npc_dialogue_and_activity_fields from './20261006_000000_add_npc_dialogue_and_activity_fields';
|
import * as migration_20261006_000000_add_npc_dialogue_and_activity_fields from './20261006_000000_add_npc_dialogue_and_activity_fields';
|
||||||
|
import * as migration_20261006_155244_add_global_flag_support from './20261006_155244_add_global_flag_support';
|
||||||
|
|
||||||
export const migrations = [
|
export const migrations = [
|
||||||
{
|
{
|
||||||
|
|
@ -658,6 +659,11 @@ export const migrations = [
|
||||||
{
|
{
|
||||||
up: migration_20261006_000000_add_npc_dialogue_and_activity_fields.up,
|
up: migration_20261006_000000_add_npc_dialogue_and_activity_fields.up,
|
||||||
down: migration_20261006_000000_add_npc_dialogue_and_activity_fields.down,
|
down: migration_20261006_000000_add_npc_dialogue_and_activity_fields.down,
|
||||||
name: '20261006_000000_add_npc_dialogue_and_activity_fields'
|
name: '20261006_000000_add_npc_dialogue_and_activity_fields',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
up: migration_20261006_155244_add_global_flag_support.up,
|
||||||
|
down: migration_20261006_155244_add_global_flag_support.down,
|
||||||
|
name: '20261006_155244_add_global_flag_support'
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -2545,9 +2545,9 @@ export interface NpcDialogue {
|
||||||
export interface PlayerFlag {
|
export interface PlayerFlag {
|
||||||
id: number;
|
id: number;
|
||||||
/**
|
/**
|
||||||
* The player this flag belongs to.
|
* The player this flag belongs to. Leave empty for a global flag.
|
||||||
*/
|
*/
|
||||||
user: number | User;
|
user?: (number | null) | User;
|
||||||
/**
|
/**
|
||||||
* Stable key referenced by conversation nodes (e.g. 'met_informant', 'rep_merchants'). Lowercase with underscores.
|
* Stable key referenced by conversation nodes (e.g. 'met_informant', 'rep_merchants'). Lowercase with underscores.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue