fix: support mixed SCUM gift catalog entries

This commit is contained in:
npc0-hue
2026-09-20 16:46:42 +08:00
parent 4abdf75c77
commit 5f2bac3280
4 changed files with 48 additions and 13 deletions
@@ -1,3 +1,5 @@
import { scumCatalogEntryFor } from "./scum-catalog.js";
export type RecordMap = Record<string, unknown>;
export type PluginDataMutation = { operation: "put" | "delete"; key: string; value?: RecordMap };
@@ -160,7 +162,7 @@ export async function queueGiftDelivery(actions: SCUMWorkspaceActions, gift: Rec
if (!items.length && !operations.length) throw new Error("礼包必须包含物品或命令。");
const now = Date.now();
const grantId = safeCommandId(`gift:${giftCode}:${playerId}:${now}`);
const commands = [...items.map((item) => `#SpawnItem ${item.catalogCode} ${item.quantity}`), ...operations].map(normalizeRCONCommand);
const commands = [...giftCatalogCommands(items), ...operations].map(normalizeRCONCommand);
const results = [];
for (const [index, command] of commands.entries()) {
results.push(await dispatchSCUMRCONCommand(actions, command, `${grantId}:${index + 1}`));
@@ -216,6 +218,14 @@ export function parseGiftItems(input: string): Array<{ catalogCode: string; quan
return items;
}
function giftCatalogCommands(items: Array<{ catalogCode: string; quantity: number }>): string[] {
return items.flatMap((item) => {
const entry = scumCatalogEntryFor(item.catalogCode);
if (!entry || entry.kind === "item") return [`#SpawnItem ${item.catalogCode} ${item.quantity}`];
return Array.from({ length: item.quantity }, () => entry.command);
});
}
export function parseGiftCommands(input: string): Array<{ command: string }> {
return input.split(/\r?\n/).map((command) => command.trim()).filter(Boolean).map((command) => ({ command }));
}