Add compat version with saves.
This commit is contained in:
parent
7c015787dd
commit
1a86c5854e
|
|
@ -1,11 +1,20 @@
|
||||||
import { GameState } from '../types'
|
import { GameState } from '../types'
|
||||||
|
|
||||||
const SAVE_SLOT_PREFIX = 'dionysian_idle_save_'
|
const SAVE_SLOT_PREFIX = 'dionysian_idle_save_'
|
||||||
|
const CURRENT_SAVE_VERSION = 1
|
||||||
|
|
||||||
|
interface SaveData {
|
||||||
|
version: number
|
||||||
|
state: GameState
|
||||||
|
}
|
||||||
|
|
||||||
const saveGame = (slot: number, state: GameState) => {
|
const saveGame = (slot: number, state: GameState) => {
|
||||||
try {
|
try {
|
||||||
const saveData = JSON.stringify(state)
|
const saveData: SaveData = {
|
||||||
localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, saveData)
|
version: CURRENT_SAVE_VERSION,
|
||||||
|
state
|
||||||
|
}
|
||||||
|
localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, JSON.stringify(saveData))
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to save game:', error)
|
console.error('Failed to save game:', error)
|
||||||
|
|
@ -17,7 +26,22 @@ const loadGame = (slot: number): GameState | null => {
|
||||||
try {
|
try {
|
||||||
const saveData = localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`)
|
const saveData = localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`)
|
||||||
if (!saveData) return null
|
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) {
|
} catch (error) {
|
||||||
console.error('Failed to load game:', error)
|
console.error('Failed to load game:', error)
|
||||||
return null
|
return null
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ export const useGameStore = create<
|
||||||
configureEquipment: (equipmentId: string, recipeId: string) => void
|
configureEquipment: (equipmentId: string, recipeId: string) => void
|
||||||
useEquipment: (equipmentId: string) => void
|
useEquipment: (equipmentId: string) => void
|
||||||
addCash: (amount: number) => void
|
addCash: (amount: number) => void
|
||||||
|
addConsoleMessage: (text: string) => void
|
||||||
}
|
}
|
||||||
>((set, get) => {
|
>((set, get) => {
|
||||||
// Expose store methods to window for the save system
|
// Expose store methods to window for the save system
|
||||||
|
|
@ -635,5 +636,9 @@ export const useGameStore = create<
|
||||||
produce((state) => { state.cash += amount }),
|
produce((state) => { state.cash += amount }),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
addConsoleMessage: (text: string) => {
|
||||||
|
addConsoleMessage(get(), text)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ import { useGameStore } from './useGameStore'
|
||||||
import './saves' // Import the saves module to initialize the global saves object
|
import './saves' // Import the saves module to initialize the global saves object
|
||||||
|
|
||||||
export const useSaveSystem = () => {
|
export const useSaveSystem = () => {
|
||||||
|
const addConsoleMessage = useGameStore((state) => state.addConsoleMessage)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Auto-save every 30 seconds
|
// Auto-save every 30 seconds
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
|
|
@ -18,6 +20,9 @@ export const useSaveSystem = () => {
|
||||||
|
|
||||||
// When starting the game, load from the autosave slot
|
// When starting the game, load from the autosave slot
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.saves.load(0)
|
const savedState = window.saves.load(0)
|
||||||
}, [])
|
if (savedState === null) {
|
||||||
|
addConsoleMessage('No compatible save found. Starting new game.')
|
||||||
|
}
|
||||||
|
}, [addConsoleMessage])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue