feat(contributions): award XP for published journal entries
This commit is contained in:
parent
2cc7629690
commit
9409f31dcf
8 changed files with 40760 additions and 3 deletions
|
|
@ -7,6 +7,8 @@ import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
||||||
import { hasPermission } from "@/utils/access-control/hasPermission";
|
import { hasPermission } from "@/utils/access-control/hasPermission";
|
||||||
import { emitGameEvent } from "@/utils/event-log/emit";
|
import { emitGameEvent } from "@/utils/event-log/emit";
|
||||||
import { EventTypes } from "@/utils/event-log/eventTypes";
|
import { EventTypes } from "@/utils/event-log/eventTypes";
|
||||||
|
import { recordContribution, revokeEntriesForSource } from "@/lib/contributions";
|
||||||
|
import { journalBodyLength } from "@/lib/journals/excerpt";
|
||||||
import type { Journal } from "@/payload-types";
|
import type { Journal } from "@/payload-types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -23,6 +25,12 @@ const MIN_OCCURRENCE_DATE = Date.parse("2156-03-19T00:00:00.000Z");
|
||||||
*/
|
*/
|
||||||
const MAX_OCCURRENCE_OFFSET_DAYS = 47_326;
|
const MAX_OCCURRENCE_OFFSET_DAYS = 47_326;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum visible body length (chars) for a published entry to earn
|
||||||
|
* contribution XP; shorter entries still publish, they just do not count.
|
||||||
|
*/
|
||||||
|
const MIN_JOURNAL_BODY_CHARS = 200;
|
||||||
|
|
||||||
/** Latest allowed in-world date as an instant: real now + the game's offset. */
|
/** Latest allowed in-world date as an instant: real now + the game's offset. */
|
||||||
function maxOccurrenceInstant(): number {
|
function maxOccurrenceInstant(): number {
|
||||||
return Date.now() + MAX_OCCURRENCE_OFFSET_DAYS * 86_400_000;
|
return Date.now() + MAX_OCCURRENCE_OFFSET_DAYS * 86_400_000;
|
||||||
|
|
@ -110,6 +118,18 @@ export async function createJournal(input: JournalInput): Promise<ActionResult<n
|
||||||
targetCollection: "journals",
|
targetCollection: "journals",
|
||||||
targetId: doc.id,
|
targetId: doc.id,
|
||||||
});
|
});
|
||||||
|
if (journalBodyLength(input.body) >= MIN_JOURNAL_BODY_CHARS) {
|
||||||
|
await recordContribution(payload, {
|
||||||
|
userId: user.id,
|
||||||
|
type: "journal-publish",
|
||||||
|
sourceCollection: "journals",
|
||||||
|
sourceId: doc.id,
|
||||||
|
summary: `Published a journal entry: "${doc.title}"`,
|
||||||
|
link: `/journals/${doc.id}`,
|
||||||
|
actorId: user.id,
|
||||||
|
actorName: who,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/journals");
|
revalidatePath("/journals");
|
||||||
|
|
@ -169,6 +189,18 @@ export async function updateJournal(
|
||||||
targetCollection: "journals",
|
targetCollection: "journals",
|
||||||
targetId: id,
|
targetId: id,
|
||||||
});
|
});
|
||||||
|
if (journalBodyLength(input.body) >= MIN_JOURNAL_BODY_CHARS) {
|
||||||
|
await recordContribution(payload, {
|
||||||
|
userId: user.id,
|
||||||
|
type: "journal-publish",
|
||||||
|
sourceCollection: "journals",
|
||||||
|
sourceId: id,
|
||||||
|
summary: `Published a journal entry: "${input.title.trim()}"`,
|
||||||
|
link: `/journals/${id}`,
|
||||||
|
actorId: user.id,
|
||||||
|
actorName: who,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/journals");
|
revalidatePath("/journals");
|
||||||
|
|
@ -198,6 +230,15 @@ export async function deleteJournal(id: number): Promise<ActionResult> {
|
||||||
|
|
||||||
await payload.delete({ collection: "journals", id, overrideAccess: true });
|
await payload.delete({ collection: "journals", id, overrideAccess: true });
|
||||||
|
|
||||||
|
// Deleting an entry (e.g. spam) pulls back any XP it earned.
|
||||||
|
await revokeEntriesForSource(
|
||||||
|
payload,
|
||||||
|
"journals",
|
||||||
|
id,
|
||||||
|
`The journal entry "${existing.title}" was deleted.`,
|
||||||
|
Number(user.id),
|
||||||
|
);
|
||||||
|
|
||||||
const who = user.displayName || user.username || "Someone";
|
const who = user.displayName || user.username || "Someone";
|
||||||
await emitGameEvent(payload, {
|
await emitGameEvent(payload, {
|
||||||
type: EventTypes.JournalDeleted,
|
type: EventTypes.JournalDeleted,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ const SOURCE_LABELS: Record<string, string> = {
|
||||||
"wiki-pages": "Wiki Page",
|
"wiki-pages": "Wiki Page",
|
||||||
"mission-map-markers": "Planning Marker",
|
"mission-map-markers": "Planning Marker",
|
||||||
"form-submissions": "Survey Submission",
|
"form-submissions": "Survey Submission",
|
||||||
|
journals: "Journal Entry",
|
||||||
manual: "Manual",
|
manual: "Manual",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ export const CONTRIBUTION_TYPES = [
|
||||||
"wiki-page-edit",
|
"wiki-page-edit",
|
||||||
"mission-map-marker",
|
"mission-map-marker",
|
||||||
"survey-completion",
|
"survey-completion",
|
||||||
|
"journal-publish",
|
||||||
"manual",
|
"manual",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
|
@ -29,6 +30,7 @@ export const CONTRIBUTION_SOURCE_COLLECTIONS = [
|
||||||
"wiki-pages",
|
"wiki-pages",
|
||||||
"mission-map-markers",
|
"mission-map-markers",
|
||||||
"form-submissions",
|
"form-submissions",
|
||||||
|
"journals",
|
||||||
"manual",
|
"manual",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
|
@ -93,6 +95,16 @@ export const CONTRIBUTION_RULES: Record<ContributionType, ContributionRule> = {
|
||||||
maxPerMission: null,
|
maxPerMission: null,
|
||||||
minEditDeltaChars: null,
|
minEditDeltaChars: null,
|
||||||
},
|
},
|
||||||
|
"journal-publish": {
|
||||||
|
type: "journal-publish",
|
||||||
|
label: "Journal entry published",
|
||||||
|
points: 5,
|
||||||
|
mode: "instant",
|
||||||
|
description: "Published a journal entry to the Chronicle with a substantive body.",
|
||||||
|
dailyCap: 25,
|
||||||
|
maxPerMission: null,
|
||||||
|
minEditDeltaChars: null,
|
||||||
|
},
|
||||||
manual: {
|
manual: {
|
||||||
type: "manual",
|
type: "manual",
|
||||||
label: "Manual award",
|
label: "Manual award",
|
||||||
|
|
|
||||||
|
|
@ -34,3 +34,15 @@ export function extractExcerpt(body: unknown, maxLength = 180): string {
|
||||||
if (text.length <= maxLength) return text;
|
if (text.length <= maxLength) return text;
|
||||||
return `${text.slice(0, maxLength - 1).trimEnd()}…`;
|
return `${text.slice(0, maxLength - 1).trimEnd()}…`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Total visible-text length of a Lexical body (whitespace collapsed), with no
|
||||||
|
* truncation. Used to gate contribution XP so one-word entries do not earn.
|
||||||
|
*/
|
||||||
|
export function journalBodyLength(body: unknown): number {
|
||||||
|
const root = (body as { root?: LexicalNodeLike } | null)?.root;
|
||||||
|
if (!root || !Array.isArray(root.children)) return 0;
|
||||||
|
const parts: string[] = [];
|
||||||
|
collectText(root, parts, Number.MAX_SAFE_INTEGER);
|
||||||
|
return parts.join("").replace(/\s+/g, " ").trim().length;
|
||||||
|
}
|
||||||
|
|
|
||||||
40660
src/migrations/20261013_183041_add_journal_contribution_type.json
Normal file
40660
src/migrations/20261013_183041_add_journal_contribution_type.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
|
||||||
|
|
||||||
|
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TYPE "public"."enum_contribution_entries_type" ADD VALUE 'journal-publish' BEFORE 'manual';
|
||||||
|
ALTER TYPE "public"."enum_contribution_entries_source_collection" ADD VALUE 'journals' BEFORE 'manual';`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
|
||||||
|
await db.execute(sql`
|
||||||
|
ALTER TABLE "contribution_entries" ALTER COLUMN "type" SET DATA TYPE text;
|
||||||
|
DROP TYPE "public"."enum_contribution_entries_type";
|
||||||
|
CREATE TYPE "public"."enum_contribution_entries_type" AS ENUM('wiki-page-create', 'wiki-page-edit', 'mission-map-marker', 'survey-completion', 'manual');
|
||||||
|
ALTER TABLE "contribution_entries" ALTER COLUMN "type" SET DATA TYPE "public"."enum_contribution_entries_type" USING "type"::"public"."enum_contribution_entries_type";
|
||||||
|
ALTER TABLE "contribution_entries" ALTER COLUMN "source_collection" SET DATA TYPE text;
|
||||||
|
DROP TYPE "public"."enum_contribution_entries_source_collection";
|
||||||
|
CREATE TYPE "public"."enum_contribution_entries_source_collection" AS ENUM('wiki-pages', 'mission-map-markers', 'form-submissions', 'manual');
|
||||||
|
ALTER TABLE "contribution_entries" ALTER COLUMN "source_collection" SET DATA TYPE "public"."enum_contribution_entries_source_collection" USING "source_collection"::"public"."enum_contribution_entries_source_collection";`)
|
||||||
|
}
|
||||||
|
|
@ -132,6 +132,7 @@ import * as migration_20261012_173511_add_sidebar_order_preference from './20261
|
||||||
import * as migration_20261012_181310_add_journals_collection from './20261012_181310_add_journals_collection';
|
import * as migration_20261012_181310_add_journals_collection from './20261012_181310_add_journals_collection';
|
||||||
import * as migration_20261012_182016_add_journals_event_target from './20261012_182016_add_journals_event_target';
|
import * as migration_20261012_182016_add_journals_event_target from './20261012_182016_add_journals_event_target';
|
||||||
import * as migration_20261012_185409_add_award_rules_automation from './20261012_185409_add_award_rules_automation';
|
import * as migration_20261012_185409_add_award_rules_automation from './20261012_185409_add_award_rules_automation';
|
||||||
|
import * as migration_20261013_183041_add_journal_contribution_type from './20261013_183041_add_journal_contribution_type';
|
||||||
|
|
||||||
export const migrations = [
|
export const migrations = [
|
||||||
{
|
{
|
||||||
|
|
@ -802,6 +803,11 @@ export const migrations = [
|
||||||
{
|
{
|
||||||
up: migration_20261012_185409_add_award_rules_automation.up,
|
up: migration_20261012_185409_add_award_rules_automation.up,
|
||||||
down: migration_20261012_185409_add_award_rules_automation.down,
|
down: migration_20261012_185409_add_award_rules_automation.down,
|
||||||
name: '20261012_185409_add_award_rules_automation'
|
name: '20261012_185409_add_award_rules_automation',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
up: migration_20261013_183041_add_journal_contribution_type.up,
|
||||||
|
down: migration_20261013_183041_add_journal_contribution_type.down,
|
||||||
|
name: '20261013_183041_add_journal_contribution_type'
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -3729,10 +3729,16 @@ export interface Experience {
|
||||||
export interface ContributionEntry {
|
export interface ContributionEntry {
|
||||||
id: number;
|
id: number;
|
||||||
user: number | User;
|
user: number | User;
|
||||||
type: 'wiki-page-create' | 'wiki-page-edit' | 'mission-map-marker' | 'survey-completion' | 'manual';
|
type:
|
||||||
|
| 'wiki-page-create'
|
||||||
|
| 'wiki-page-edit'
|
||||||
|
| 'mission-map-marker'
|
||||||
|
| 'survey-completion'
|
||||||
|
| 'journal-publish'
|
||||||
|
| 'manual';
|
||||||
status: 'queued' | 'granted' | 'revoked' | 'expired';
|
status: 'queued' | 'granted' | 'revoked' | 'expired';
|
||||||
points: number;
|
points: number;
|
||||||
sourceCollection: 'wiki-pages' | 'mission-map-markers' | 'form-submissions' | 'manual';
|
sourceCollection: 'wiki-pages' | 'mission-map-markers' | 'form-submissions' | 'journals' | 'manual';
|
||||||
/**
|
/**
|
||||||
* Id of the source document (polymorphic).
|
* Id of the source document (polymorphic).
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue