Normalize campaign inventory labels

This commit is contained in:
2026-07-05 10:46:07 +09:00
parent 788e2ce21a
commit a0ced53e8d
2 changed files with 10 additions and 4 deletions

View File

@@ -199,7 +199,7 @@ try {
],
completedCampDialogues: ['dialogue-a', 17, 'dialogue-a', ''],
completedCampVisits: 'visit-a',
inventory: { bean: '3', wine: -2, empty: 0 },
inventory: { bean: '3', ' bean ': '4', ['\uCF69']: '200', [' \uCF69 ']: '20', wine: -2, empty: 0 },
selectedSortieUnitIds: { corrupted: true },
sortieFormationAssignments: 'front',
sortieItemAssignments: {
@@ -230,7 +230,10 @@ try {
`Expected malformed completion lists to keep unique strings only: ${JSON.stringify(malformed)}`
);
assert(
malformed.inventory.bean === 3 &&
malformed.inventory.bean === 7 &&
malformed.inventory['\uCF69'] === 120 &&
malformed.inventory[' bean '] === undefined &&
malformed.inventory[' \uCF69 '] === undefined &&
malformed.inventory.wine === undefined &&
malformed.sortieItemAssignments['liu-bei']?.bean === 2 &&
malformed.sortieItemAssignments['liu-bei']?.salve === 1 &&

View File

@@ -901,9 +901,12 @@ function normalizeNonNegativeInteger(value: unknown) {
function normalizeInventory(value: unknown) {
return Object.entries(recordOrEmpty(value)).reduce<Record<string, number>>((inventory, [itemId, amount]) => {
const normalizedItemId = itemId.replace(/\s+/g, ' ').trim();
const normalizedAmount = normalizeNonNegativeInteger(amount);
if (itemId && normalizedAmount > 0) {
inventory[itemId] = normalizedAmount;
if (normalizedItemId && normalizedAmount > 0) {
const nextAmount = (inventory[normalizedItemId] ?? 0) + normalizedAmount;
const cap = campaignInventoryCaps[normalizedItemId];
inventory[normalizedItemId] = cap ? Math.min(nextAmount, cap) : nextAmount;
}
return inventory;
}, {});