-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: save GameContext on localStorage (no UI)
- Loading branch information
1 parent
8b9988d
commit cce19cf
Showing
7 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.menu { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
width: 32px; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { logRender } from "utils/console"; | ||
import { savegame } from "services/db"; | ||
import MenuIcon from "./menu-icon.svg"; | ||
import { useGameContext } from "contexts"; | ||
|
||
import "./menu.scss"; | ||
|
||
export function Menu(): JSX.Element { | ||
logRender("Menu"); | ||
|
||
const gameContext = useGameContext(); | ||
|
||
const saveHandler = () => { | ||
savegame(gameContext); | ||
}; | ||
|
||
const loadHandler = () => { | ||
// TODO | ||
}; | ||
|
||
return ( | ||
<div className="menu"> | ||
<img | ||
src={MenuIcon} | ||
alt="Menu Icon" | ||
onClick={() => { | ||
console.log("clicked"); | ||
}} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* using local storage for now (up to 5MB) | ||
* check if i need to use Indexed DB instead | ||
*/ | ||
|
||
import { empireSize } from "game-logic/empire-size"; | ||
import { warnInconsistentState } from "utils/console"; | ||
|
||
export function savegame(gameContext: GameContext) { | ||
const date = Date.now().toString(); | ||
const playerEmpireSize = empireSize(gameContext.board).player; | ||
const item: Savegame = { | ||
date, | ||
empireSize: playerEmpireSize, | ||
gameContext, | ||
}; | ||
try { | ||
window.localStorage.setItem(date, JSON.stringify(item)); | ||
} catch (e) { | ||
warnInconsistentState(`error while saving game context: ${e}`); | ||
return undefined; | ||
} | ||
return date; | ||
} | ||
|
||
export function listSavegames() { | ||
return Object.keys(window.localStorage); | ||
} | ||
|
||
export function loadSavegame(date: string): Savegame | undefined { | ||
const raw = window.localStorage.getItem(date); | ||
if (!raw) { | ||
warnInconsistentState( | ||
`trying to load game context with key ${key} but no data found` | ||
); | ||
return undefined; | ||
} | ||
return JSON.parse(raw); | ||
} |