1
0
Fork 0
polaris-task-force/src/collections/game/GameRules.ts
Z8MB1E a0f767cc13 perf: cut queries per render and refreshes per realtime event
Under 10-12 concurrent users the app queued behind itself: every SSE
game-event/profile-update made each client run router.refresh(), i.e. a
full layout + page render at ~15-25 queries, and a mission completion
emitted one XP event per attendee row. Measured against production, no
single query is slow and the DB is small, so this targets render count
and per-render query count instead of indexes.

Fewer renders per event:
- GameTickRealtime: 250ms coalescing window -> 1s, hidden tabs skip the
  render and refresh once when shown again.
- Delete OperationsRealtimeRefresher (5 routes) and the duplicate
  useGameTick refreshers in ProductionPanel/SlotsPanel; the global
  subscriber already refreshes those routes.
- Presence heartbeats no longer broadcast (was O(N^2) per channel every
  15s for a lastSeen no surface renders).
- NavCore links opt out of prefetch: the layout is force-dynamic with no
  loading.tsx, so ~35 prefetched links per tab were 35 server renders.

Fewer queries per render:
- Qualification flags go through one cached, single-flight profile read
  instead of one query per auth check; invalidated on profile writes.
- Dedupe DIVISION_ADMIN_QUALIFICATIONS (5 identical lookups -> 1).
- Anchor the permission cache on globalThis so route bundles share it.
- Minigame registry: real TTL cache plus invalidation on write.
- New cachedConfig TTL cache for the shims/game-rules globals, with
  invalidation hooks; bank-account existence check stops hydrating.
- pg pool max 10 -> 20 (node-postgres default throttled concurrent
  renders).

Hot-path per-item work:
- Contribution grant sweep: one status write per batch, one bucket write
  and one name lookup per user, one existence query per source
  collection instead of ~5 queries per entry.
- Notifications bell: coalesced refetch, hidden-tab skip; mark-all-read
  filters by the actor instead of scanning 100 foreign unread rows.
- Polling loops (shipments, ledger, mission reminders, chip bank) pause
  while the tab is hidden.
2026-11-22 22:39:10 -05:00

369 lines
11 KiB
TypeScript

import { GlobalConfig } from "payload";
import { requirePermission } from "@/utils/access-control/hasPermission";
import { invalidateConfigCache } from "@/lib/config-cache";
export const GameRules: GlobalConfig = {
slug: "game-rules",
access: {
read: requirePermission("game-rules:read"),
update: requirePermission("game-rules:update"),
},
hooks: {
// The (frontend) layout caches this global for 60s (lib/config-cache.ts).
afterChange: [() => invalidateConfigCache("global:game-rules")],
},
fields: [
{
name: "mainCurrency",
type: "relationship",
relationTo: "resources",
required: true,
admin: {
description:
'The base currency ("gold standard") used by all factions to pay for goods and services.',
},
},
{
label: "Universal Currency",
type: "collapsible",
admin: {
initCollapsed: true,
description:
"Display names used throughout the app. These do not change the accounting resource selected above.",
},
fields: [
{
name: "currencyName",
type: "text",
admin: {
description:
'General display name for the currency, e.g. "credit". Used when singular or plural names are not set.',
},
},
{
name: "currencySymbol",
type: "upload",
relationTo: "media",
admin: {
description:
"Optional emblem shown inline before amounts. It renders scaled to the surrounding text height (unlike a text symbol).",
},
},
{
name: "currencySingular",
type: "text",
admin: {
description: 'Singular amount label, e.g. "credit".',
},
},
{
name: "currencyPlural",
type: "text",
admin: {
description: 'Plural amount label, e.g. "credits".',
},
},
{
name: "currencyDenomination",
type: "text",
admin: {
description:
'Optional subunit name for future fractional displays, e.g. "cent". This does not affect stored values.',
},
},
],
},
{
name: "commandAssignments",
type: "relationship",
relationTo: "assignments",
hasMany: true,
admin: {
description:
"Assignment(s) considered unit command. Members and leaders of these assignments get elevated access to draft missions and other restricted content.",
},
},
{
name: "intelAssignments",
type: "relationship",
relationTo: "assignments",
hasMany: true,
admin: {
description:
"Assignment(s) considered the intelligence division. Members and leaders of these assignments get elevated access to draft missions and intelligence content.",
},
},
{
name: "infantryAssignment",
type: "relationship",
relationTo: "assignments",
hasMany: false,
admin: {
description:
"Assignment that members are moved to when a division leader removes them from their division (the default destination for leadership-initiated transfers).",
},
},
{
name: "proximityThreshold",
type: "number",
defaultValue: 1000,
admin: {
description:
"Distance threshold in real meters for instant transfers. Structures within this distance can transfer resources instantly. Beyond this, shipments are required.",
},
},
{
name: "gameTickIntervalMinutes",
type: "number",
defaultValue: 10,
admin: {
description:
"How often the game tick runs, in minutes. This processes shipment movement, fuel consumption, and arrivals.",
},
},
{
name: "lockerGridWidth",
type: "number",
defaultValue: 10,
min: 4,
admin: {
description:
"Width of each member's personal locker grid, in cells. Applies to newly provisioned lockers.",
},
},
{
name: "lockerGridHeight",
type: "number",
defaultValue: 6,
min: 4,
admin: {
description:
"Height of each member's personal locker grid, in cells. Applies to newly provisioned lockers.",
},
},
{
name: "staffHiringEnabled",
type: "checkbox",
defaultValue: false,
admin: {
description:
"When off, logistics officers cannot hire staff anywhere (both labor crews and named NPC staff). Back-office labor headcount edits in the admin panel are not affected.",
},
},
{
name: "staffHiringDisabledMessage",
type: "text",
admin: {
description:
"Message shown to logistics officers when hiring is disabled, e.g. explaining the in-universe reason.",
},
},
{
name: "economy",
type: "group",
label: "Market Economy",
admin: {
description:
"Global knobs for the supply-and-demand market simulation run by the economy tick (docs/economy/supply-demand-design.md).",
},
fields: [
{
name: "enabled",
type: "checkbox",
defaultValue: false,
admin: {
description:
"Master gate for the market economy simulation. When off, the economy tick leaves all prices at their resting values.",
},
},
{
name: "defaultPriceBand",
type: "number",
min: 0,
max: 1,
defaultValue: 0.5,
admin: {
description:
"Default price band as a fraction of base price (e.g. 0.5 = price can move ±50%). Used when an item does not set its own band.",
},
},
{
name: "realDemandWindowTicks",
type: "number",
min: 1,
defaultValue: 24,
admin: {
description:
"How many economy ticks of completed-purchase history feed the real-demand signal.",
},
},
{
name: "realDemandDecay",
type: "number",
min: 0,
max: 1,
defaultValue: 0.5,
admin: {
description:
"Fraction of accumulated real demand that survives each tick (0.5 halves it every tick).",
},
},
{
name: "artificialDemandDrift",
type: "number",
min: 0,
max: 1,
defaultValue: 0.05,
admin: {
description:
"How far the NPC (artificial) demand random walk may move per tick, as a fraction of the GM baseline.",
},
},
{
name: "coldStartObservations",
type: "number",
min: 0,
defaultValue: 5,
admin: {
description:
"Completed purchases needed before an item's price starts moving. Until then items stay at their resting price (modifier 1).",
},
},
{
name: "tickIntervalHintMinutes",
type: "number",
min: 1,
defaultValue: 60,
admin: {
description:
"Suggested economy tick cadence in minutes, for cron documentation only. The tick itself is triggered externally.",
},
},
],
},
{
name: "research",
type: "group",
label: "Technology Research",
admin: {
description:
"Global knobs for the member-facing tech research engine run by the tech tick (ballots, facility speed, researcher bonus).",
},
fields: [
{
name: "enabled",
type: "checkbox",
defaultValue: false,
admin: {
description:
"Master gate for the research engine. When off, the tech tick does nothing: no ballots open, no research progresses.",
},
},
{
name: "ballotDurationTicks",
type: "number",
min: 1,
defaultValue: 288,
admin: {
description:
"How long a research ballot stays open, in game ticks (default 288 ticks = 48h at a 10-minute tick).",
},
},
{
name: "researcherSpeedPerHead",
type: "number",
min: 0,
defaultValue: 0.0001,
admin: {
description:
"Research time reduction each researcher adds, as a fraction (0.0001 = 0.01% faster per head). Researchers are active labor staff in the 'researcher' labor classification at operational research facilities.",
},
},
],
},
{
name: "interior",
type: "group",
label: "Superstructure Interior",
admin: {
description: "Global knobs for the Superstructure interior system (interior-tick).",
},
fields: [
{
name: "decayEnabled",
type: "checkbox",
defaultValue: false,
admin: {
description:
"Master gate for interior condition/junk decay. When off, the interior-tick skips decay.",
},
},
{
name: "decayMultiplier",
type: "number",
min: 0,
defaultValue: 1,
admin: {
description: "Multiplier applied to per-room decay while the master gate is on.",
},
},
{
name: "assetOutputsEnabled",
type: "checkbox",
defaultValue: false,
admin: {
description:
"Master switch for crediting asset (Arma item) outputs from interior jobs into structure locker storage. Keep off until locker.apply is verified on a live server.",
},
},
],
},
{
name: "factionRelations",
type: "group",
label: "Faction Relations",
admin: {
description:
"Tunables for the faction relations ledger (faction-relation-entries). Every pairwise score is computed on read from the ledger; nothing is stored on the faction records.",
},
fields: [
{
name: "decayHalfLifeDays",
type: "number",
min: 1,
defaultValue: 30,
admin: {
description:
"Half-life, in days, of a decaying relation entry. A decaying entry keeps full weight until one period elapses, then halves per completed period. Entries without the Decays flag never fade.",
},
},
],
},
{
name: "zonePressureEnabled",
type: "checkbox",
defaultValue: true,
admin: {
description:
"When off, the Zone Pressure feature is hidden from the Operations feed (the overview card and the AAR section).",
},
},
{
name: "statisticsEnabled",
type: "checkbox",
defaultValue: true,
admin: {
description:
"When off, the Personnel > Statistics page and its navigation entry are hidden.",
},
},
{
name: "helpdeskEnabled",
type: "checkbox",
defaultValue: true,
admin: {
description: "When off, the Helpdesk page and its navigation entry are hidden.",
},
},
],
};