1
0
Fork 0
polaris-task-force/src/collections/logistics/Resources.ts
Z8MB1E 159755af8d feat(collections): add grid fields to game-structures and resources schemas
GameStructures:
- Add gridX, gridY, rotated fields to storedResources rows
- Remove beforeValidate hook that merged duplicate resources
- Add read access (was missing)
- Add min: 1 constraint on amount

Resources:
- Add gridWidth, gridHeight, stackable, maxStackSize, rotatable fields
- Add read access (was missing)
- Wrap game-rules afterRead hook in try/catch to handle missing global
2026-07-28 14:02:35 -04:00

345 lines
9.4 KiB
TypeScript

import { CollectionConfig, PayloadRequest } from "payload";
import { isDeveloper } from "@/utils/access-control/isRole";
const onlyIfNotPrimaryCurrency = async ({ req }: { req: PayloadRequest }) => {
if (isDeveloper({ req })) {
return true;
}
try {
const gameRules = await req.payload.findGlobal({
slug: "game-rules",
depth: 0,
});
const primaryCurrencyId =
typeof gameRules.mainCurrency === "object"
? gameRules.mainCurrency?.id
: gameRules.mainCurrency;
if (primaryCurrencyId) {
return {
id: {
not_equals: primaryCurrencyId,
},
};
}
} catch (_) {
// Fail silently and allow update if global rules cannot be fetched
}
return true;
};
export const Resources: CollectionConfig = {
slug: "resources",
admin: {
useAsTitle: "name",
group: "Logistics",
},
access: {
read: ({ req }) => !!req.user,
update: onlyIfNotPrimaryCurrency,
delete: onlyIfNotPrimaryCurrency,
},
fields: [
{
name: "NoEditWarning",
type: "ui",
admin: {
condition: (data) => data.showWarning,
components: {
Field: {
path: "@/components/static/WarningMessage",
serverProps: {
title: "This item is locked.",
message:
"This item is considered an essential application component and can only be edited by a developer.",
},
},
},
disableListColumn: true,
},
},
{
label: "Identity",
type: "group",
fields: [
{
name: "showWarning",
type: "checkbox",
virtual: true,
admin: {
hidden: true,
readOnly: true,
disableListColumn: true,
},
hooks: {
afterRead: [
async ({ data, req }) => {
try {
const gameRules = await req.payload.findGlobal({
slug: "game-rules",
depth: 0,
});
const primaryCurrencyId =
typeof gameRules.mainCurrency === "object"
? gameRules.mainCurrency?.id
: gameRules.mainCurrency;
if (primaryCurrencyId) {
return data?.id === primaryCurrencyId;
}
} catch (_) {
// Silently fail — game-rules global may not exist yet
}
},
],
},
},
{
name: "name",
type: "text",
required: true,
admin: {
description: 'Human-readable name for resources (e.g., "Fuel").',
},
},
{
name: "codeName",
type: "text",
required: true,
admin: {
description: 'Internal ID for scripts (e.g., "res_fuel").',
},
},
{
name: "approvalStatus",
type: "select",
label: "Approval Status",
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",
filterOptions: ({ options, data, req }) => {
return !isDeveloper({ req })
? options.filter((option) =>
typeof option === "string"
? options
: ["in_progress", "ready_for_review"].includes(option.value),
)
: options;
},
required: true,
admin: {
description:
"Mark whether this resource is ready for developer approval and implementation into the live environment.",
},
},
{
name: "type",
type: "select",
required: true,
defaultValue: "physical",
options: [
{
label: "Physical",
value: "physical",
},
{
label: "Fluid",
value: "fluid",
},
{
label: "Energy",
value: "energy",
},
{
label: "Abstract",
value: "abstract",
},
],
},
{
name: "description",
type: "richText",
admin: {
description:
"A description of the resource, including where it comes from, how it is used, and any lore-related details.",
},
},
{
name: "icon",
type: "upload",
relationTo: "media",
admin: {
description:
"While not required, an icon is absolutely recommended and will affect the way this resource is displayed in the application UI.",
},
},
{
name: "color",
type: "text",
admin: {
description:
"A HEX color code used by this resource in the application UI (e.g., progress bars, text highlighting, etc.)",
},
},
],
},
{
label: "Economy",
type: "group",
fields: [
{
name: "baseValue",
type: "number",
required: true,
defaultValue: 1,
admin: {
description:
'The inherent "gold standard" value of one (1) unit of this resource. Used for calculating exchange rates and total asset worth.',
},
},
{
name: "tradeable",
type: "checkbox",
label: "Is Tradeable?",
admin: {
description: "Can this be bought and/or sold on the market?",
},
},
{
name: "rarity",
type: "select",
required: true,
defaultValue: "common",
options: [
{
label: "Common",
value: "common",
},
{
label: "Uncommon",
value: "uncommon",
},
{
label: "Rare",
value: "rare",
},
{
label: "Epic",
value: "epic",
},
{
label: "Legendary",
value: "legendary",
},
{
label: "Exotic",
value: "exotic",
},
{
label: "Unique",
value: "unique",
},
],
},
],
},
{
label: "Measurement & Storage",
type: "group",
fields: [
{
name: "unitOfMeasure",
type: "text",
required: true,
admin: {
description:
"The unit of measure for this resource in singular form (no plurals, e.g., liter, kg, point, unit, megawatt, person, etc.)",
},
},
{
name: "massPerUnit",
type: "number",
required: true,
defaultValue: 0,
admin: {
description:
'The weight of one (1) unit (can be "0" for abstract resources like "Intel", "Labor", etc.) Critical for logistics transport calculations.',
},
},
{
name: "stackSize",
type: "number",
defaultValue: 1,
admin: {
description:
"Maximum amount that fits in a single standard cargo slot (if applicable.)",
},
},
{
label: "Grid Dimensions",
type: "group",
fields: [
{
name: "gridWidth",
type: "number",
required: true,
defaultValue: 1,
min: 1,
admin: {
description: "Width of this resource in grid cells.",
},
},
{
name: "gridHeight",
type: "number",
required: true,
defaultValue: 1,
min: 1,
admin: {
description: "Height of this resource in grid cells.",
},
},
{
name: "stackable",
type: "checkbox",
defaultValue: true,
admin: {
description:
"Can multiple units of this resource stack in a single grid cell? If checked, the amount displayed on the grid reflects the stack count.",
},
},
{
name: "maxStackSize",
type: "number",
admin: {
description:
"Maximum number of units that can exist in a single grid stack. Leave empty for unlimited. Only applies if stackable is checked.",
condition: (_, siblingData) => siblingData?.stackable === true,
},
},
{
name: "rotatable",
type: "checkbox",
defaultValue: true,
admin: {
description:
"Can this resource be rotated 90 degrees in the grid? When rotated, width and height are swapped.",
},
},
],
},
],
},
],
};