From f4ff01e917171dc7d21d00f9449e54292ece7f4e Mon Sep 17 00:00:00 2001 From: Wickedness Date: Sun, 5 Jul 2026 10:38:30 +0900 Subject: [PATCH] Prevent duplicate camp rewards --- .../verify-campaign-save-normalization.mjs | 56 ++++++++++++++++++- src/game/scenes/CampScene.ts | 5 ++ src/game/state/campaignState.ts | 41 ++++++++++---- 3 files changed, 91 insertions(+), 11 deletions(-) diff --git a/scripts/verify-campaign-save-normalization.mjs b/scripts/verify-campaign-save-normalization.mjs index de48762..eed2da1 100644 --- a/scripts/verify-campaign-save-normalization.mjs +++ b/scripts/verify-campaign-save-normalization.mjs @@ -27,10 +27,13 @@ const server = await createServer({ try { const { campaignStorageKey, + applyCampBondExp, + applyCampVisitReward, getCampaignState, loadCampaignState, normalizeCampaignSortieItemAssignments, resetCampaignState, + setCampaignState, setFirstBattleReport } = await server.ssrLoadModule('/src/game/state/campaignState.ts'); const { battleScenarios } = await server.ssrLoadModule('/src/game/data/battles.ts'); @@ -99,6 +102,57 @@ try { revisedSettlement.inventory.Wine === 1, `Expected revised battle report settlement to replace previous rewards instead of stacking: ${JSON.stringify(revisedSettlement)}` ); + applyCampVisitReward('repeat-visit', { gold: 10, itemRewards: ['Bean x2'] }); + applyCampVisitReward('repeat-visit', { gold: 10, itemRewards: ['Bean x2'] }); + const repeatedVisit = getCampaignState(); + assert( + repeatedVisit.gold === 150 && + repeatedVisit.inventory.Bean === 5 && + repeatedVisit.completedCampVisits.includes('repeat-visit') && + repeatedVisit.firstBattleReport?.completedCampVisits.includes('repeat-visit'), + `Expected repeated camp visit rewards to be applied once: ${JSON.stringify(repeatedVisit)}` + ); + const visitDesynced = getCampaignState(); + visitDesynced.completedCampVisits = []; + visitDesynced.firstBattleReport.completedCampVisits = ['repeat-visit']; + setCampaignState(visitDesynced); + applyCampVisitReward('repeat-visit', { gold: 10, itemRewards: ['Bean x2'] }); + const visitResynced = getCampaignState(); + assert( + visitResynced.gold === 150 && + visitResynced.inventory.Bean === 5 && + visitResynced.completedCampVisits.includes('repeat-visit') && + visitResynced.firstBattleReport?.completedCampVisits.includes('repeat-visit'), + `Expected report-only camp visit completion to resync without duplicate rewards: ${JSON.stringify(visitResynced)}` + ); + const repeatBondId = firstScenario.bonds[0].id; + applyCampBondExp('repeat-dialogue', repeatBondId, 10); + const repeatedDialogueOnce = getCampaignState(); + applyCampBondExp('repeat-dialogue', repeatBondId, 10); + const repeatedDialogueTwice = getCampaignState(); + const onceBond = repeatedDialogueOnce.bonds.find((bond) => bond.id === repeatBondId); + const twiceBond = repeatedDialogueTwice.bonds.find((bond) => bond.id === repeatBondId); + assert( + onceBond?.exp === twiceBond?.exp && + onceBond?.battleExp === twiceBond?.battleExp && + repeatedDialogueTwice.completedCampDialogues.includes('repeat-dialogue') && + repeatedDialogueTwice.firstBattleReport?.completedCampDialogues.includes('repeat-dialogue'), + `Expected repeated camp dialogue rewards to be applied once: ${JSON.stringify(repeatedDialogueTwice)}` + ); + const dialogueDesynced = getCampaignState(); + dialogueDesynced.completedCampDialogues = []; + dialogueDesynced.firstBattleReport.completedCampDialogues = ['repeat-dialogue']; + setCampaignState(dialogueDesynced); + applyCampBondExp('repeat-dialogue', repeatBondId, 10); + const dialogueResynced = getCampaignState(); + const resyncedBond = dialogueResynced.bonds.find((bond) => bond.id === repeatBondId); + assert( + resyncedBond?.exp === twiceBond?.exp && + resyncedBond?.battleExp === twiceBond?.battleExp && + dialogueResynced.completedCampDialogues.includes('repeat-dialogue') && + dialogueResynced.firstBattleReport?.completedCampDialogues.includes('repeat-dialogue'), + `Expected report-only camp dialogue completion to resync without duplicate bond exp: ${JSON.stringify(dialogueResynced)}` + ); storage.clear(); storage.set( @@ -553,7 +607,7 @@ try { const fallback = loadCampaignState(); assert(fallback.step === 'second-camp' && fallback.activeSaveSlot === 2, `Expected valid slotted save fallback: ${JSON.stringify(fallback)}`); - console.log('Verified campaign save normalization, reward resettlement, malformed-field/roster-equipment/bond-roster/report-reference/sortie-roster/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.'); + console.log('Verified campaign save normalization, reward resettlement, camp reward idempotence, malformed-field/roster-equipment/bond-roster/report-reference/sortie-roster/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.'); } finally { await server.close(); } diff --git a/src/game/scenes/CampScene.ts b/src/game/scenes/CampScene.ts index fb9b953..677ceaf 100644 --- a/src/game/scenes/CampScene.ts +++ b/src/game/scenes/CampScene.ts @@ -14949,6 +14949,11 @@ export class CampScene extends Phaser.Scene { } private completeDialogue(dialogue: CampDialogue, choice: CampDialogueChoice) { + if (this.completedCampDialogues().includes(dialogue.id)) { + this.showCampNotice('이미 완료된 대화입니다.'); + return; + } + const rewardExp = dialogue.rewardExp + choice.rewardExp; const updated = applyCampBondExp(dialogue.id, dialogue.bondId, rewardExp); if (updated) { diff --git a/src/game/state/campaignState.ts b/src/game/state/campaignState.ts index 2cad5d0..6061eec 100644 --- a/src/game/state/campaignState.ts +++ b/src/game/state/campaignState.ts @@ -677,11 +677,12 @@ export function applyCampBondExp(dialogueId: string, bondId: string, amount: num return undefined; } - if (!state.completedCampDialogues.includes(dialogueId)) { - state.completedCampDialogues.push(dialogueId); - } - if (!state.firstBattleReport.completedCampDialogues.includes(dialogueId)) { - state.firstBattleReport.completedCampDialogues.push(dialogueId); + if (hasCompletionFlag(state.completedCampDialogues, state.firstBattleReport.completedCampDialogues, dialogueId)) { + if (syncCompletionFlag(state.completedCampDialogues, state.firstBattleReport.completedCampDialogues, dialogueId)) { + state.updatedAt = new Date().toISOString(); + persistCampaignState(state); + } + return cloneReport(state.firstBattleReport); } const campaignBond = state.bonds.find((candidate) => candidate.id === bondId); @@ -690,6 +691,7 @@ export function applyCampBondExp(dialogueId: string, bondId: string, amount: num return undefined; } + syncCompletionFlag(state.completedCampDialogues, state.firstBattleReport.completedCampDialogues, dialogueId); advanceBond(campaignBond, amount); reportBond.level = campaignBond.level; reportBond.exp = campaignBond.exp; @@ -709,14 +711,15 @@ export function applyCampVisitReward( return undefined; } - if (state.completedCampVisits.includes(visitId)) { + if (hasCompletionFlag(state.completedCampVisits, state.firstBattleReport.completedCampVisits, visitId)) { + if (syncCompletionFlag(state.completedCampVisits, state.firstBattleReport.completedCampVisits, visitId)) { + state.updatedAt = new Date().toISOString(); + persistCampaignState(state); + } return cloneCampaignState(state); } - state.completedCampVisits.push(visitId); - if (!state.firstBattleReport.completedCampVisits.includes(visitId)) { - state.firstBattleReport.completedCampVisits.push(visitId); - } + syncCompletionFlag(state.completedCampVisits, state.firstBattleReport.completedCampVisits, visitId); if (reward.bondId && reward.bondExp && reward.bondExp > 0) { const campaignBond = state.bonds.find((candidate) => candidate.id === reward.bondId); @@ -744,6 +747,23 @@ export function applyCampVisitReward( return cloneCampaignState(state); } +function hasCompletionFlag(stateFlags: string[], reportFlags: string[], flagId: string) { + return stateFlags.includes(flagId) || reportFlags.includes(flagId); +} + +function syncCompletionFlag(stateFlags: string[], reportFlags: string[], flagId: string) { + let changed = false; + if (!stateFlags.includes(flagId)) { + stateFlags.push(flagId); + changed = true; + } + if (!reportFlags.includes(flagId)) { + reportFlags.push(flagId); + changed = true; + } + return changed; +} + export function ensureCampaignRosterUnits(units: UnitData[], bonds: BattleBond[] = []) { const state = ensureCampaignState(); let changed = false; @@ -1451,6 +1471,7 @@ function cloneCampaignState(state: CampaignState): CampaignState { function cloneReport(report: FirstBattleReport): FirstBattleReport { const cloned = JSON.parse(JSON.stringify(report)) as FirstBattleReport; + cloned.completedCampDialogues = [...new Set(cloned.completedCampDialogues ?? [])]; cloned.completedCampVisits = [...new Set(cloned.completedCampVisits ?? [])]; if (cloned.campaignRewards) { cloned.campaignRewards = cloneCampaignRewardSnapshot(cloned.campaignRewards);