forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveGame.js
17 lines (15 loc) 路 642 Bytes
/
saveGame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export function saveGame(state) {
const string = state.codemirror.state.doc.toString();
const match = string.match(/@title:\s+([^\n]+)/);
const name = (match !== null) ? match[1] : "DRAFT";
const gameDict = Object.fromEntries(state.savedGames);
if (name in gameDict && gameDict[name] === string) return;
const newSave = [ name, string ];
const currentGames = state.savedGames
.filter( x => x[0] !== name)
// .slice(0, 4);
const toSave = [ newSave, ...currentGames ]
state.savedGames = toSave;
window.localStorage.setItem("puzzle-lab", JSON.stringify(toSave) );
window.localStorage.setItem("last-game", name);
}