fork of hey-api/openapi-ts because I need some additional things
1import { fail } from '@sveltejs/kit';
2
3import type { Actions, PageServerLoad } from './$types';
4import { Game } from './game';
5
6export const load = (({ cookies }) => {
7 const game = new Game(cookies.get('sverdle'));
8
9 return {
10 /**
11 * The correct answer, revealed if the game is over
12 */
13 answer: game.answers.length >= 6 ? game.answer : null,
14
15 /**
16 * An array of strings like '__x_c' corresponding to the guesses, where 'x' means
17 * an exact match, and 'c' means a close match (right letter, wrong place)
18 */
19 answers: game.answers,
20
21 /**
22 * The player's guessed words so far
23 */
24 guesses: game.guesses,
25 };
26}) satisfies PageServerLoad;
27
28export const actions = {
29 /**
30 * Modify game state in reaction to a guessed word. This logic always runs on
31 * the server, so that people can't cheat by peeking at the JavaScript
32 */
33 enter: async ({ cookies, request }) => {
34 const game = new Game(cookies.get('sverdle'));
35
36 const data = await request.formData();
37 const guess = data.getAll('guess') as string[];
38
39 if (!game.enter(guess)) {
40 return fail(400, { badGuess: true });
41 }
42
43 cookies.set('sverdle', game.toString(), { path: '/' });
44 },
45
46 restart: async ({ cookies }) => {
47 cookies.delete('sverdle', { path: '/' });
48 },
49
50 /**
51 * Modify game state in reaction to a keypress. If client-side JavaScript
52 * is available, this will happen in the browser instead of here
53 */
54 update: async ({ cookies, request }) => {
55 const game = new Game(cookies.get('sverdle'));
56
57 const data = await request.formData();
58 const key = data.get('key');
59
60 const i = game.answers.length;
61
62 if (key === 'backspace') {
63 game.guesses[i] = game.guesses[i].slice(0, -1);
64 } else {
65 game.guesses[i] += key;
66 }
67
68 cookies.set('sverdle', game.toString(), { path: '/' });
69 },
70} satisfies Actions;