Skip to content

Commit

Permalink
Persist game state in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Sep 28, 2024
1 parent 2c4114e commit de85a35
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"ky": "^1.5.0",
"lightningcss": "^1.25.1",
"svelte": "^4.2.19",
"svelte-persisted-store": "^0.11.0",
"typescript": "^5.5.4"
},
"devDependencies": {
Expand Down
28 changes: 26 additions & 2 deletions src/components/qwixx/PlayerSheet.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { writable } from 'svelte/store';
import { persisted } from 'svelte-persisted-store';
import { Direction } from '../../lib/qwixx/types.ts';
import { createColorConfig } from '../../lib/qwixx/game.ts';
Expand All @@ -14,9 +14,21 @@
createColorConfig('Green', [140, 90, 35], Direction.DESCENDING),
createColorConfig('Blue', [215, 100, 50], Direction.DESCENDING),
];
const penalties = writable(0);
const penalties = persisted('qwixx-penalties', 0);
function reset() {
penalties.reset();
colors.forEach((color) => {
color.reset();
});
}
</script>

<header>
<h1><span aria-hidden>🎲</span> Qwixx</h1>
<button on:click={reset} class="button">Reset</button>
</header>

<section>
<h2>Numbers</h2>

Expand All @@ -37,6 +49,18 @@
</div>

<style>
header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: var(--layout-gutter);
}
h1 {
font-family: var(--font-family-display);
font-size: 2rem;
}
.numbers {
display: flex;
flex-wrap: wrap;
Expand Down
13 changes: 10 additions & 3 deletions src/lib/qwixx/game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { derived, writable } from 'svelte/store';
import { derived } from 'svelte/store';
import { persisted } from 'svelte-persisted-store';

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

Expand All @@ -27,7 +28,7 @@ export function createColorConfig(

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

const [hue, saturation, lightness] = color;
const style = `--hue: ${hue}; --saturation: ${saturation}%; --lightness: ${lightness}%;`;
Expand Down Expand Up @@ -82,6 +83,11 @@ export function createColorConfig(
});
};

const reset = () => {
numbers.reset();
isLocked.reset();
};

return {
label,
direction,
Expand All @@ -90,5 +96,6 @@ export function createColorConfig(
points,
isLocked,
style,
reset,
};
}
1 change: 1 addition & 0 deletions src/lib/qwixx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type ColorConfig = {
points: Readable<number>;
isLocked: Writable<boolean>;
style: string;
reset: () => void;
};

0 comments on commit de85a35

Please sign in to comment.