diff --git a/src/store/saves.ts b/src/store/saves.ts index 8d5136c..0a43d59 100644 --- a/src/store/saves.ts +++ b/src/store/saves.ts @@ -1,11 +1,20 @@ import { GameState } from '../types' const SAVE_SLOT_PREFIX = 'dionysian_idle_save_' +const CURRENT_SAVE_VERSION = 1 + +interface SaveData { + version: number + state: GameState +} const saveGame = (slot: number, state: GameState) => { try { - const saveData = JSON.stringify(state) - localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, saveData) + const saveData: SaveData = { + version: CURRENT_SAVE_VERSION, + state + } + localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, JSON.stringify(saveData)) return true } catch (error) { console.error('Failed to save game:', error) @@ -17,7 +26,22 @@ const loadGame = (slot: number): GameState | null => { try { const saveData = localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`) if (!saveData) return null - return JSON.parse(saveData) + + const parsedData = JSON.parse(saveData) + + // Handle legacy saves (no version field) + if (!parsedData.version) { + console.warn('Loading legacy save without version information') + return parsedData as GameState + } + + // Check version compatibility + if (parsedData.version !== CURRENT_SAVE_VERSION) { + console.warn(`Save version mismatch: found ${parsedData.version}, expected ${CURRENT_SAVE_VERSION}`) + return null + } + + return parsedData.state } catch (error) { console.error('Failed to load game:', error) return null diff --git a/src/store/useGameStore.ts b/src/store/useGameStore.ts index 9a1c0d6..484a1f9 100644 --- a/src/store/useGameStore.ts +++ b/src/store/useGameStore.ts @@ -91,6 +91,7 @@ export const useGameStore = create< configureEquipment: (equipmentId: string, recipeId: string) => void useEquipment: (equipmentId: string) => void addCash: (amount: number) => void + addConsoleMessage: (text: string) => void } >((set, get) => { // Expose store methods to window for the save system @@ -635,5 +636,9 @@ export const useGameStore = create< produce((state) => { state.cash += amount }), ) }, + + addConsoleMessage: (text: string) => { + addConsoleMessage(get(), text) + }, } }) diff --git a/src/store/useSaveSystem.ts b/src/store/useSaveSystem.ts index 56a5858..93d0604 100644 --- a/src/store/useSaveSystem.ts +++ b/src/store/useSaveSystem.ts @@ -3,6 +3,8 @@ import { useGameStore } from './useGameStore' import './saves' // Import the saves module to initialize the global saves object export const useSaveSystem = () => { + const addConsoleMessage = useGameStore((state) => state.addConsoleMessage) + useEffect(() => { // Auto-save every 30 seconds const interval = setInterval(() => { @@ -18,6 +20,9 @@ export const useSaveSystem = () => { // When starting the game, load from the autosave slot useEffect(() => { - window.saves.load(0) - }, []) + const savedState = window.saves.load(0) + if (savedState === null) { + addConsoleMessage('No compatible save found. Starting new game.') + } + }, [addConsoleMessage]) }