Move save states to a browser console interface
This commit is contained in:
parent
5eda5c158b
commit
26f4a6120b
59
src/store/saves.ts
Normal file
59
src/store/saves.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -109,8 +109,6 @@ export const useGameStore = create<
|
||||||
setActionCooldown: (cooldown: number) => void
|
setActionCooldown: (cooldown: number) => void
|
||||||
buyItem: (itemId: string) => void
|
buyItem: (itemId: string) => void
|
||||||
sellItem: (itemId: string) => void
|
sellItem: (itemId: string) => void
|
||||||
saveToSlot: (slot: number) => void
|
|
||||||
loadFromSlot: (slot: number) => void
|
|
||||||
purchaseUpgrade: (upgradeId: string) => void
|
purchaseUpgrade: (upgradeId: string) => void
|
||||||
pray: () => void
|
pray: () => void
|
||||||
buyEquipment: (equipmentId: string) => void
|
buyEquipment: (equipmentId: string) => void
|
||||||
|
|
@ -118,7 +116,14 @@ export const useGameStore = create<
|
||||||
useEquipment: (equipmentId: string) => void
|
useEquipment: (equipmentId: string) => void
|
||||||
addCash: (amount: number) => void
|
addCash: (amount: number) => void
|
||||||
}
|
}
|
||||||
>((set, get) => ({
|
>((set, get) => {
|
||||||
|
// Expose store methods to window for the save system
|
||||||
|
;(window as any).gameStore = {
|
||||||
|
getState: get,
|
||||||
|
setState: set
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
cash: INITIAL_CASH,
|
cash: INITIAL_CASH,
|
||||||
inventory: INITIAL_INVENTORY,
|
inventory: INITIAL_INVENTORY,
|
||||||
fieldSize: INITIAL_FIELD_SIZE,
|
fieldSize: INITIAL_FIELD_SIZE,
|
||||||
|
|
@ -503,19 +508,6 @@ export const useGameStore = create<
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
saveToSlot: (slot: number) => {
|
|
||||||
const state = get()
|
|
||||||
const { ...gameState } = state
|
|
||||||
saveGame(slot, gameState)
|
|
||||||
},
|
|
||||||
|
|
||||||
loadFromSlot: (slot: number) => {
|
|
||||||
const savedState = loadGame(slot)
|
|
||||||
if (savedState) {
|
|
||||||
set(savedState)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
purchaseUpgrade: (upgradeId) => {
|
purchaseUpgrade: (upgradeId) => {
|
||||||
const { inventory, purchasedUpgrades } = get()
|
const { inventory, purchasedUpgrades } = get()
|
||||||
const upgrade = UPGRADES[upgradeId]
|
const upgrade = UPGRADES[upgradeId]
|
||||||
|
|
@ -674,4 +666,5 @@ export const useGameStore = create<
|
||||||
produce((state) => { state.cash += amount }),
|
produce((state) => { state.cash += amount }),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}))
|
}
|
||||||
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,51 +1,23 @@
|
||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
import { hasSaveInSlot, useGameStore } from './useGameStore'
|
import { useGameStore } from './useGameStore'
|
||||||
|
import './saves' // Import the saves module to initialize the global saves object
|
||||||
|
|
||||||
export const useSaveSystem = () => {
|
export const useSaveSystem = () => {
|
||||||
const { saveToSlot, loadFromSlot } = useGameStore()
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
// Auto-save every 30 seconds
|
||||||
if (e.key === 'r' && e.ctrlKey) {
|
|
||||||
localStorage.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
const slot =
|
|
||||||
{
|
|
||||||
'1': 1,
|
|
||||||
'2': 2,
|
|
||||||
'3': 3,
|
|
||||||
'!': 1,
|
|
||||||
'@': 2,
|
|
||||||
'#': 3,
|
|
||||||
}[e.key] ?? null
|
|
||||||
|
|
||||||
if (slot !== null) {
|
|
||||||
if (e.shiftKey) {
|
|
||||||
saveToSlot(slot)
|
|
||||||
console.log(`Game saved to slot ${slot}`)
|
|
||||||
} else if (hasSaveInSlot(slot)) {
|
|
||||||
loadFromSlot(slot)
|
|
||||||
console.log(`Game loaded from slot ${slot}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('keydown', handleKeyDown)
|
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
saveToSlot(0)
|
window.saves.save(0) // Save to autosave slot
|
||||||
}, 30000)
|
}, 30000)
|
||||||
|
|
||||||
console.log('Initalized save system')
|
console.log('Initialized save system')
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('keydown', handleKeyDown)
|
|
||||||
clearInterval(interval)
|
clearInterval(interval)
|
||||||
}
|
}
|
||||||
}, [saveToSlot, loadFromSlot])
|
}, [])
|
||||||
|
|
||||||
// When starting the game, load from the autosave slot
|
// When starting the game, load from the autosave slot
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadFromSlot(0)
|
window.saves.load(0)
|
||||||
}, [])
|
}, [])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue