Adds the lib/base service layer: hire/fire staff, set labor headcount, storage consumption helpers, and structure upgrade handling, all with event logging and permission checks. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import type { Payload } from "payload";
|
|
import type { GameStructure, Structure } from "@/payload-types";
|
|
import { emitGameEvent } from "@/utils/event-log/emit";
|
|
import { EventTypes } from "@/utils/event-log/eventTypes";
|
|
import { staffingDefaultsFromBlueprint } from "./staffingDefaults";
|
|
import { consumeStorage, relationshipId, resourceName, storedAmount } from "./storage";
|
|
|
|
async function structureBlueprint(payload: Payload, source: GameStructure): Promise<Structure> {
|
|
if (typeof source.type === "object") return source.type;
|
|
return payload.findByID({
|
|
collection: "structures",
|
|
id: source.type,
|
|
depth: 2,
|
|
overrideAccess: true,
|
|
});
|
|
}
|
|
|
|
export async function upgradeStructure(
|
|
payload: Payload,
|
|
gameStructureId: number,
|
|
): Promise<GameStructure> {
|
|
const gameStructure = await payload.findByID({
|
|
collection: "game-structures",
|
|
id: gameStructureId,
|
|
depth: 2,
|
|
overrideAccess: true,
|
|
});
|
|
const source = await structureBlueprint(payload, gameStructure);
|
|
const targetReference = source.upgradesInto;
|
|
if (!targetReference) throw new Error("This structure has no upgrade path.");
|
|
|
|
const target =
|
|
typeof targetReference === "object"
|
|
? targetReference
|
|
: await payload.findByID({
|
|
collection: "structures",
|
|
id: targetReference,
|
|
depth: 2,
|
|
overrideAccess: true,
|
|
});
|
|
const multiplier = source.upgradeCostMultiplier ?? 1;
|
|
const requirements = new Map<number, { needed: number; name: string }>();
|
|
|
|
for (const material of target.materials) {
|
|
const resourceId = relationshipId(material.resource);
|
|
if (resourceId === null) continue;
|
|
const current = requirements.get(resourceId);
|
|
requirements.set(resourceId, {
|
|
needed: (current?.needed ?? 0) + Math.ceil((material.amount ?? 0) * multiplier),
|
|
name: current?.name ?? (await resourceName(payload, material.resource)),
|
|
});
|
|
}
|
|
|
|
let storedResources = [...(gameStructure.storedResources ?? [])];
|
|
let voidStorage = [...(gameStructure.voidStorage ?? [])];
|
|
const shortages: string[] = [];
|
|
for (const [resourceId, requirement] of requirements) {
|
|
const available =
|
|
storedAmount(storedResources, resourceId) + storedAmount(voidStorage, resourceId);
|
|
if (available < requirement.needed) {
|
|
shortages.push(`${requirement.name} (have ${available}, need ${requirement.needed})`);
|
|
}
|
|
}
|
|
if (shortages.length > 0) throw new Error(`Missing materials: ${shortages.join(", ")}`);
|
|
|
|
for (const [resourceId, requirement] of requirements) {
|
|
const storedResult = consumeStorage(storedResources, resourceId, requirement.needed);
|
|
storedResources = storedResult.entries;
|
|
voidStorage = consumeStorage(voidStorage, resourceId, storedResult.remaining).entries;
|
|
}
|
|
|
|
const updated = await payload.update({
|
|
collection: "game-structures",
|
|
id: gameStructure.id,
|
|
data: {
|
|
type: target.id,
|
|
storedResources,
|
|
voidStorage,
|
|
...staffingDefaultsFromBlueprint(target),
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
await emitGameEvent(payload, {
|
|
type: EventTypes.StructureUpgraded,
|
|
message: `Structure "${gameStructure.name}" upgraded to ${target.name}`,
|
|
structure: gameStructure.id,
|
|
targetCollection: "game-structures",
|
|
targetId: gameStructure.id,
|
|
});
|
|
return updated;
|
|
}
|