59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { GameState } from '../types'
|
|
|
|
const SAVE_SLOT_PREFIX = 'dionysian_idle_save_'
|
|
|
|
const saveGame = (slot: number, state: GameState) => {
|
|
try {
|
|
const saveData = JSON.stringify(state)
|
|
localStorage.setItem(`${SAVE_SLOT_PREFIX}${slot}`, saveData)
|
|
return true
|
|
} catch (error) {
|
|
console.error('Failed to save game:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
const loadGame = (slot: number): GameState | null => {
|
|
try {
|
|
const saveData = localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`)
|
|
if (!saveData) return null
|
|
return JSON.parse(saveData)
|
|
} catch (error) {
|
|
console.error('Failed to load game:', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const hasSaveInSlot = (slot: number): boolean => {
|
|
return !!localStorage.getItem(`${SAVE_SLOT_PREFIX}${slot}`)
|
|
}
|
|
|
|
// Create the global saves object
|
|
declare global {
|
|
interface Window {
|
|
saves: {
|
|
save: (slot: number) => void
|
|
load: (slot: number) => void
|
|
hasSave: (slot: number) => boolean
|
|
}
|
|
}
|
|
}
|
|
|
|
// Initialize the global saves object
|
|
window.saves = {
|
|
save: (slot: number) => {
|
|
const state = (window as any).gameStore?.getState()
|
|
if (state) {
|
|
saveGame(slot, state)
|
|
console.log(`Game saved to slot ${slot}`)
|
|
}
|
|
},
|
|
load: (slot: number) => {
|
|
const savedState = loadGame(slot)
|
|
if (savedState && (window as any).gameStore?.setState) {
|
|
(window as any).gameStore.setState(savedState)
|
|
console.log(`Game loaded from slot ${slot}`)
|
|
}
|
|
},
|
|
hasSave: hasSaveInSlot
|
|
}
|