Ignore malformed campaign reward labels
This commit is contained in:
@@ -1399,7 +1399,12 @@ function createBattleSettlement(report: FirstBattleReport, reserveTraining: Camp
|
||||
|
||||
function applyRewardDelta(inventory: Record<string, number>, rewards: string[], direction: 1 | -1) {
|
||||
return rewards.reduce<Record<string, number>>((nextInventory, reward) => {
|
||||
const { label, amount } = parseRewardLabel(reward);
|
||||
const parsed = parseRewardLabel(reward);
|
||||
if (!parsed) {
|
||||
return nextInventory;
|
||||
}
|
||||
|
||||
const { label, amount } = parsed;
|
||||
const nextAmount = Math.max(0, (nextInventory[label] ?? 0) + amount * direction);
|
||||
const cap = direction === 1 ? campaignInventoryCaps[label] : undefined;
|
||||
nextInventory[label] = cap ? Math.min(nextAmount, cap) : nextAmount;
|
||||
@@ -1488,16 +1493,23 @@ function advanceReserveEquipment(unit: UnitData, amount: number) {
|
||||
|
||||
function parseRewardLabel(reward: string) {
|
||||
const normalized = reward.replace(/\s+/g, ' ').trim();
|
||||
const match = normalized.match(/^(.*?)(?:\s*[+xX]?\s*(\d+))$/);
|
||||
if (!normalized) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const match = normalized.match(/^(.*?)(?:\s*([+xX-]?)\s*(\d+))$/);
|
||||
|
||||
if (!match) {
|
||||
return { label: normalized, amount: 1 };
|
||||
}
|
||||
|
||||
return {
|
||||
label: match[1].trim() || normalized,
|
||||
amount: Number(match[2] ?? 1)
|
||||
};
|
||||
const amount = Number(match[3] ?? 1) * (match[2] === '-' ? -1 : 1);
|
||||
const label = match[1].trim();
|
||||
if (!label || !Number.isSafeInteger(amount) || amount <= 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return { label, amount };
|
||||
}
|
||||
|
||||
function cloneCampaignState(state: CampaignState): CampaignState {
|
||||
|
||||
Reference in New Issue
Block a user