Skip to content

Commit de85a35

Browse files
committed
Persist game state in localstorage
1 parent 2c4114e commit de85a35

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"ky": "^1.5.0",
4747
"lightningcss": "^1.25.1",
4848
"svelte": "^4.2.19",
49+
"svelte-persisted-store": "^0.11.0",
4950
"typescript": "^5.5.4"
5051
},
5152
"devDependencies": {

src/components/qwixx/PlayerSheet.svelte

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { writable } from 'svelte/store';
2+
import { persisted } from 'svelte-persisted-store';
33
44
import { Direction } from '../../lib/qwixx/types.ts';
55
import { createColorConfig } from '../../lib/qwixx/game.ts';
@@ -14,9 +14,21 @@
1414
createColorConfig('Green', [140, 90, 35], Direction.DESCENDING),
1515
createColorConfig('Blue', [215, 100, 50], Direction.DESCENDING),
1616
];
17-
const penalties = writable(0);
17+
const penalties = persisted('qwixx-penalties', 0);
18+
19+
function reset() {
20+
penalties.reset();
21+
colors.forEach((color) => {
22+
color.reset();
23+
});
24+
}
1825
</script>
1926

27+
<header>
28+
<h1><span aria-hidden>🎲</span> Qwixx</h1>
29+
<button on:click={reset} class="button">Reset</button>
30+
</header>
31+
2032
<section>
2133
<h2>Numbers</h2>
2234

@@ -37,6 +49,18 @@
3749
</div>
3850

3951
<style>
52+
header {
53+
display: flex;
54+
align-items: center;
55+
justify-content: space-between;
56+
margin-bottom: var(--layout-gutter);
57+
}
58+
59+
h1 {
60+
font-family: var(--font-family-display);
61+
font-size: 2rem;
62+
}
63+
4064
.numbers {
4165
display: flex;
4266
flex-wrap: wrap;

src/lib/qwixx/game.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { derived, writable } from 'svelte/store';
1+
import { derived } from 'svelte/store';
2+
import { persisted } from 'svelte-persisted-store';
23

34
import { Direction, type ColorConfig, type HSLColor } from './types';
45
import { POINTS } from './constants';
@@ -8,7 +9,7 @@ export function createColorConfig(
89
color: HSLColor,
910
direction: Direction,
1011
): ColorConfig {
11-
const numbers = writable<number[]>([]);
12+
const numbers = persisted<number[]>(`qwixx-numbers-${label}`, []);
1213
const points = derived(numbers, ($numbers) => {
1314
let crosses = $numbers.length;
1415

@@ -27,7 +28,7 @@ export function createColorConfig(
2728

2829
return POINTS[crosses] || 0;
2930
});
30-
const isLocked = writable(false);
31+
const isLocked = persisted(`qwixx-locked-${label}`, false);
3132

3233
const [hue, saturation, lightness] = color;
3334
const style = `--hue: ${hue}; --saturation: ${saturation}%; --lightness: ${lightness}%;`;
@@ -82,6 +83,11 @@ export function createColorConfig(
8283
});
8384
};
8485

86+
const reset = () => {
87+
numbers.reset();
88+
isLocked.reset();
89+
};
90+
8591
return {
8692
label,
8793
direction,
@@ -90,5 +96,6 @@ export function createColorConfig(
9096
points,
9197
isLocked,
9298
style,
99+
reset,
93100
};
94101
}

src/lib/qwixx/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export type ColorConfig = {
1515
points: Readable<number>;
1616
isLocked: Writable<boolean>;
1717
style: string;
18+
reset: () => void;
1819
};

0 commit comments

Comments
 (0)