Add a research pipeline over the existing technology tree: Research Facility blueprint fields copied to structure instances, tech-researches/ tech-ballots/tech-ballot-votes collections, Game Rules research config, pure rules lib (capacity, duration math, eligibility, intel-weighted tally with tie cascade), a timestamp-derived tech-tick bin that opens, resolves, advances and completes research with notifications, vote casting server actions, and a read-only member-facing tech tree page at /intelligence/tech-tree with veil blackout and dossier-style panels. Includes migration 20261019_184409_add_tech_research_system, permission groups, event and notification types, sidebar entry, and integration plus e2e coverage.
327 lines
9.9 KiB
TypeScript
327 lines
9.9 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
import type {
|
|
CollectionBeforeChangeHook,
|
|
CollectionBeforeDeleteHook,
|
|
} from "payload";
|
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
|
import {
|
|
approvalStatusFieldAccess,
|
|
requireApprovalPermission,
|
|
requireIntelligencePermission,
|
|
resetApprovalStatusOnEdit,
|
|
} from "@/utils/access-control/divisionAccess";
|
|
import { ifUnmodifiedAtGuard } from "@/utils/access-control/optimisticConcurrency";
|
|
|
|
const approvalStatusFieldAccessConfig = approvalStatusFieldAccess("technologies");
|
|
|
|
/**
|
|
* Guard hook: a technology with an in-flight research (queued or active
|
|
* tech-researches doc) must stay approved until the research resolves, so the
|
|
* research engine's eligibility assumptions cannot be pulled out from under it.
|
|
* Runs AFTER resetApprovalStatusOnEdit so it sees the final outgoing value.
|
|
*/
|
|
const blockUnapproveWhileResearching: CollectionBeforeChangeHook = async ({
|
|
req,
|
|
data,
|
|
originalDoc,
|
|
operation,
|
|
}) => {
|
|
if (operation !== "update" || !data) return data;
|
|
const wasApproved = (originalDoc as { approvalStatus?: string } | undefined)?.approvalStatus;
|
|
const incoming = data.approvalStatus as string | undefined;
|
|
if (wasApproved !== "approved" || !incoming || incoming === "approved") return data;
|
|
|
|
const inFlight = await req.payload.find({
|
|
collection: "tech-researches",
|
|
where: {
|
|
and: [
|
|
{ technology: { equals: (originalDoc as { id: number }).id } },
|
|
{ status: { in: ["queued", "active"] } },
|
|
],
|
|
},
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
if (inFlight.docs.length > 0) {
|
|
throw new Error(
|
|
"This technology is currently being researched (queued or active). It cannot leave the Approved state until the research completes.",
|
|
);
|
|
}
|
|
return data;
|
|
};
|
|
|
|
/**
|
|
* Guard hook: researched / researching / queued technologies are part of the
|
|
* unit's progression history and the research engine's working set; deleting
|
|
* them would corrupt both.
|
|
*/
|
|
const blockDeleteResearchedTech: CollectionBeforeDeleteHook = async ({ req, id }) => {
|
|
const tech = await req.payload
|
|
.findByID({ collection: "technologies", id, depth: 0, overrideAccess: true })
|
|
.catch(() => null);
|
|
if (tech?.researchedAt) {
|
|
throw new Error(
|
|
`"${tech.name}" has been researched and cannot be deleted. Un-approve it instead only if it was never live.`,
|
|
);
|
|
}
|
|
const inFlight = await req.payload.find({
|
|
collection: "tech-researches",
|
|
where: {
|
|
and: [{ technology: { equals: id } }, { status: { in: ["queued", "active"] } }],
|
|
},
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
if (inFlight.docs.length > 0) {
|
|
throw new Error(
|
|
"This technology is queued or actively being researched and cannot be deleted until the research resolves.",
|
|
);
|
|
}
|
|
};
|
|
|
|
export const Technologies: CollectionConfig = {
|
|
slug: "technologies",
|
|
admin: {
|
|
group: "Intelligence",
|
|
useAsTitle: "name",
|
|
},
|
|
access: {
|
|
create: requireIntelligencePermission("technologies:create"),
|
|
update: requireIntelligencePermission("technologies:update"),
|
|
delete: requireIntelligencePermission("technologies:delete"),
|
|
read: requirePermission("technologies:read"),
|
|
},
|
|
hooks: {
|
|
// Guard first: a stale write must abort before the approval-reset hook
|
|
// rewrites data (see optimisticConcurrency.ts for the contract). The
|
|
// research guard runs last so it sees the final outgoing approvalStatus
|
|
// (including any rewrite by resetApprovalStatusOnEdit).
|
|
beforeChange: [
|
|
ifUnmodifiedAtGuard(),
|
|
resetApprovalStatusOnEdit(),
|
|
blockUnapproveWhileResearching,
|
|
],
|
|
beforeDelete: [blockDeleteResearchedTech],
|
|
},
|
|
fields: [
|
|
{
|
|
name: "name",
|
|
type: "text",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "category",
|
|
type: "relationship",
|
|
relationTo: "tech-categories",
|
|
admin: {
|
|
description:
|
|
"Tech Tree column this technology belongs to. Uncategorized technologies are auto-placed on the Tech Tree by prerequisite depth.",
|
|
},
|
|
},
|
|
{
|
|
name: "treePosition",
|
|
type: "group",
|
|
label: "Tech Tree Position",
|
|
admin: {
|
|
description:
|
|
"Manual position on the Tech Tree canvas (set by dragging a node). Leave empty to use the automatic layout.",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "x",
|
|
type: "number",
|
|
},
|
|
{
|
|
name: "y",
|
|
type: "number",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "approvalStatus",
|
|
type: "select",
|
|
label: "Approval Status",
|
|
access: approvalStatusFieldAccessConfig,
|
|
options: [
|
|
{ label: "In Progress", value: "in_progress" },
|
|
{ label: "Ready for Review", value: "ready_for_review" },
|
|
{ label: "Approved", value: "approved" },
|
|
{ label: "Rejected", value: "rejected" },
|
|
{ label: "Revision Requested", value: "revision_requested" },
|
|
],
|
|
defaultValue: "in_progress",
|
|
required: true,
|
|
admin: {
|
|
description:
|
|
"Mark whether this technology is ready for developer approval and implementation into the live environment.",
|
|
},
|
|
},
|
|
{
|
|
name: "researchedAt",
|
|
type: "date",
|
|
label: "Researched At",
|
|
admin: {
|
|
readOnly: true,
|
|
description:
|
|
"Set automatically by the tech tick when this technology finishes researching. Empty = not yet researched.",
|
|
},
|
|
},
|
|
{
|
|
name: "type",
|
|
type: "select",
|
|
options: [
|
|
{
|
|
label: "Asset",
|
|
value: "asset",
|
|
},
|
|
{
|
|
label: "Upgrade",
|
|
value: "upgrade",
|
|
},
|
|
],
|
|
admin: {
|
|
description:
|
|
'An "Asset" technology will unlock a new item, vehicle, gear piece, structure, etc. An "Upgrade" technology will unlock an improvement to an Asset, like faster build speed, more storage capacity, etc.',
|
|
},
|
|
required: true,
|
|
defaultValue: "upgrade",
|
|
},
|
|
{
|
|
name: "summary",
|
|
type: "textarea",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "description",
|
|
type: "richText",
|
|
},
|
|
{
|
|
name: "requirements",
|
|
type: "group",
|
|
fields: [
|
|
{
|
|
name: "prerequisiteTechnologies",
|
|
type: "array",
|
|
fields: [
|
|
{
|
|
type: "row",
|
|
fields: [
|
|
{
|
|
name: "requiresAnd",
|
|
type: "group",
|
|
label: "Requires (AND)",
|
|
admin: {
|
|
description:
|
|
"ALL of these technologies MUST be unlocked before this one can be.",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "technology",
|
|
type: "relationship",
|
|
relationTo: "technologies",
|
|
hasMany: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "requiresOr",
|
|
type: "group",
|
|
label: "Requires (OR)",
|
|
admin: {
|
|
description:
|
|
"One or more of these technologies MUST be unlocked before this one can be.",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "technology",
|
|
type: "relationship",
|
|
relationTo: "technologies",
|
|
hasMany: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "researchCosts",
|
|
type: "group",
|
|
fields: [
|
|
{
|
|
name: "resources",
|
|
type: "array",
|
|
admin: {
|
|
description:
|
|
"These resources are consumed when this technology is researched. If a resource is not listed, you can add a new one, but creating new resources requires the Logistics qualification. Without it, the save will fail.",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "resource",
|
|
type: "relationship",
|
|
relationTo: "resources",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "amount",
|
|
type: "number",
|
|
defaultValue: 1,
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
type: "row",
|
|
fields: [
|
|
{
|
|
name: "minimumResearchDuration",
|
|
type: "number",
|
|
admin: {
|
|
components: {
|
|
Description: {
|
|
path: "@/components/static/TickDurationDescription",
|
|
clientProps: {
|
|
task: "research this technology",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
min: 1,
|
|
max: 10000,
|
|
required: true,
|
|
defaultValue: 0,
|
|
},
|
|
{
|
|
name: "maximumResearchDuration",
|
|
type: "number",
|
|
admin: {
|
|
components: {
|
|
Description: {
|
|
path: "@/components/static/TickDurationDescription",
|
|
clientProps: {
|
|
task: "research this technology",
|
|
field: "maximumResearchDuration",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
min: 1,
|
|
max: 10000,
|
|
/*validate: (value: number, { data, siblingData }: { data: any; siblingData: any }) => {
|
|
const minDuration = siblingData?.minimumResearchDuration;
|
|
if (value !== undefined && minDuration !== undefined && value < minDuration) {
|
|
return `Maximum research duration must be at least ${minDuration} (the minimum research duration)`;
|
|
}
|
|
return true;
|
|
},*/
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|