-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (58 loc) · 2.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import "./main.css";
import { Elm } from "./Main.elm";
import * as serviceWorker from "./serviceWorker";
let app = Elm.Main.init({
node: document.getElementById("root"),
});
const START_DAY = 19147;
const GAME_NUMBER = (() => {
const today = Math.floor(new Date().getTime() / 1000 / 60 / 60 / 24);
return today - START_DAY;
})();
const LOCAL_STORAGE_KEY = "gameHistory";
const SAVE_FORMAT = "v1";
// written like trash but it's 4am and i need to get this done asap
app.ports.copyResultsToClipboard.subscribe(([board, isWin]) => {
navigator.clipboard.writeText(
`Pintordle ${GAME_NUMBER} ${isWin ? board.length : "x"}/6
${board.map((row) => row.join("")).join("\n")}
`
);
});
/**
* When the page loads, read the save data for the current day from local
* storage and send it over to elm to populate the model
*/
window.addEventListener("DOMContentLoaded", () => {
const gameHistory = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY)) ?? [];
const game = gameHistory.find((entry) => entry.gameNumber === GAME_NUMBER);
const board = game?.board ?? [];
const gameState = game?.gameState ?? "playing";
app.ports.load.send([board, gameState]);
});
/**
* When we receive a `save` command from Elm, store the board and the game
* status (win/loss/playing) in localStorage
*/
app.ports.save.subscribe(([board, gameState]) => {
let gameHistory = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY)) ?? [];
const gameIndex = gameHistory.findIndex(
(entry) => entry.gameNumber === GAME_NUMBER
);
const newEntry = {
format: SAVE_FORMAT,
gameNumber: GAME_NUMBER,
board,
gameState,
};
if (gameIndex === -1) {
gameHistory.push(newEntry);
} else {
gameHistory[gameIndex] = newEntry;
}
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(gameHistory));
});
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();