diff --git a/src/lib/components/Card.svelte b/src/lib/components/Card.svelte index 0c1856d..0640fb5 100644 --- a/src/lib/components/Card.svelte +++ b/src/lib/components/Card.svelte @@ -1,13 +1,22 @@ -
+
{ const cards = await getCards(); const movies = await getNextMovie(); + const selected_cliches = { + baseCards: JSON.parse(event.cookies.get('selected_cliches') ?? '[]'), + bonusCards: JSON.parse(event.cookies.get('selected_bonus_cliches') ?? '[]') + }; return { movie: movies, cards: cards, - user: event.locals.user + user: event.locals.user, + selected_cliches: selected_cliches }; -}; \ No newline at end of file +}; + +export const actions: Actions = { + /** + * Starts a new game. Sets the selected cliche and bonus cliche cookies for the user. + * @param {RequestEvent} event - The request event. + * @param {Cookie | undefined} cookies - The cookies object. + * @returns {Promise} - The response object. + */ + startGame: async ({ request, cookies }) => { + const formData = await request.formData(); + const selected_cliches = formData.get('selected_cliches')?.toString() ?? '[]'; + const selected_bonus_cliches = formData.get('selected_bonus_cliches')?.toString() ?? '[]'; + + cookies.set('selected_cliches', selected_cliches, { + path: '/', + sameSite: 'strict', + secure: false, + maxAge: 60 * 60 * 24 + }); + cookies.set('selected_bonus_cliches', selected_bonus_cliches, { + path: '/', + sameSite: 'strict', + secure: false, + maxAge: 60 * 60 * 24 + }); + }, + /** + * End game action. Calculates the total score for the user and sends it to the + * server to be saved. Redirects to the game over page. + * @param {RequestEvent} event - The request event. + * @param {Cookie | undefined} cookies - The cookies object. + * @returns {Promise} - The response object. + */ + endGame: async ({ request, cookies }) => { + const formData = await request.formData(); + console.log(formData); + + const movieId = formData.get('movieId'); + const userId = formData.get('userId'); + + const resultCards = JSON.parse(formData.get('result_cards')?.toString() ?? '[]'); + const resultBonusCards = JSON.parse(formData.get('result_bonus_cards')?.toString() ?? '[]'); + const selectedBonusCards = JSON.parse(cookies.get('selected_bonus_cliches') ?? '[]'); + + let totalScore: number = 0; + + for (let i = 0; i < resultCards.length; i++) { + console.log('resultCards[i].point_value', resultCards[i].point_value); + totalScore += resultCards[i].point_value; + } + + for (let i = 0; i < selectedBonusCards.length; i++) { + if (resultBonusCards.some((card: Cliche) => card.id === selectedBonusCards[i].id)) { + totalScore += selectedBonusCards[i].point_value*2; + } else { + totalScore -= selectedBonusCards[i].point_value*2; + } + } + + const response = await fetch(`${api_url}/score`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + user_id: userId, + movie_id: movieId, + score: totalScore + }) + }); + + console.log('response', await response); + // console.log(baseCards, bonusCards); + // cookies.delete('selected_cliches', { + // path: '/', + // sameSite: 'strict', + // secure: false, + // }); + // cookies.delete('selected_bonus_cliches', { + // path: '/', + // sameSite: 'strict', + // secure: false, + // }); + throw redirect(302, `/gameroom/${totalScore}`); + } +} \ No newline at end of file diff --git a/src/routes/gameroom/+page.svelte b/src/routes/gameroom/+page.svelte index e14ea29..13d848b 100644 --- a/src/routes/gameroom/+page.svelte +++ b/src/routes/gameroom/+page.svelte @@ -1,11 +1,54 @@
@@ -19,6 +62,95 @@
-

Valitse kortit

- + {#if roundNumber == 1 && data.selected_cliches.baseCards.length == 0} +

Valitse kortit

+
+ {#each cards as card} + + {/each} +
+

Kortteja valittu: {selected_cliches.length} / 5

+ + {#if selected_cliches.length == 5} + + {:else} + + {/if} + {/if} + + {#if roundNumber == 2 && data.selected_cliches.baseCards.length == 0} +

Bonus kortit

+
+ {#each cards as card} + {#if !selected_cliches.some((c) => c.id === card.id)} + + + + {/if} + {/each} +
+

Kortteja valittu: {selected_bonus_cliches.length} / 3

+ + {#if selected_bonus_cliches.length < 4} + +
+ + + +
+ {:else} + + {/if} + {/if} + + {#if data.selected_cliches.baseCards.length > 0} +

Peli alkaa

+
+
+

Valitut kortit

+ {#each data.selected_cliches.baseCards as cliche} + + {/each} +
+ +
+

Valitut bonus kortit

+ {#each data.selected_cliches.bonusCards as cliche} + + {/each} +
+ + + + + + +
+ {/if}
+ + diff --git a/src/routes/logout/+page.server.ts b/src/routes/logout/+page.server.ts index 35ca755..d161c39 100644 --- a/src/routes/logout/+page.server.ts +++ b/src/routes/logout/+page.server.ts @@ -6,5 +6,15 @@ export const load: PageServerLoad = async (event) => { sameSite: 'strict', secure: false, }); + event.cookies.delete('selected_cliches', { + path: '/', + sameSite: 'strict', + secure: false, + }); + event.cookies.delete('selected_bonus_cliches', { + path: '/', + sameSite: 'strict', + secure: false, + }); throw redirect(302, '/login'); }; \ No newline at end of file