From 00d7c9604b5f64b4413b503c6fc19fe8ceda13ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Mon, 2 Oct 2023 01:14:48 +0200 Subject: [PATCH] feat: leaderboard event --- src/lib/components/Chapter.svelte | 15 ++-- src/lib/components/Puzzle.svelte | 10 +-- src/routes/dashboard/+page.svelte | 50 +++++++------ src/routes/dashboard/chapters/+page.svelte | 2 +- .../chapters/[chapterId]/+page.svelte | 26 ++++--- .../leaderboard/[chapterId]/+page.server.ts | 37 ++++++++++ .../leaderboard/[chapterId]/+page.svelte | 71 +++++++++++++++++++ 7 files changed, 167 insertions(+), 44 deletions(-) create mode 100644 src/routes/dashboard/leaderboard/[chapterId]/+page.server.ts create mode 100644 src/routes/dashboard/leaderboard/[chapterId]/+page.svelte diff --git a/src/lib/components/Chapter.svelte b/src/lib/components/Chapter.svelte index f285767..ce7a6f7 100644 --- a/src/lib/components/Chapter.svelte +++ b/src/lib/components/Chapter.svelte @@ -1,15 +1,17 @@
  • {#if chapter.show} -
    -

    +
    + {chapter.name} -

    + + {#if chapter.id === 1} + + {/if}
    diff --git a/src/lib/components/Puzzle.svelte b/src/lib/components/Puzzle.svelte index 3acf636..ef90fa0 100644 --- a/src/lib/components/Puzzle.svelte +++ b/src/lib/components/Puzzle.svelte @@ -13,7 +13,7 @@
  • tag.name.toLowerCase() === 'easy'), 'border-yellow-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'medium'), @@ -36,8 +36,8 @@ ({puzzle.score ? `${puzzle.score}` : '?'}/{puzzle.scoreMax} points) -
    - {#if puzzle.tags?.length} + {#if puzzle.tags?.length} +
    {#each puzzle.tags as tag} {/each}
    - {/if} -
    +
    + {/if} diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index 69acc45..c250510 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -10,28 +10,6 @@
    - {#if data.daily && data.daily.puzzle} -
    -
    - {data.daily.chapter.name} - - {data.daily.puzzle.name} ({data.daily.puzzle.score ?? '?'}/{data.daily.puzzle.scoreMax}) - -
    -
    - -
    -
    - {/if}

    Tableau de bord

    Ceci est la page d'accueil du dashboard

    @@ -44,6 +22,34 @@ + {#if data.daily && data.daily.puzzle} +
    +

    Puzzle du jour

    +

    + Essayer de résoudre le puzzle du jour pour gagner des points supplémentaires +

    +
    +
    +
    + {data.daily.chapter.name} + + {data.daily.puzzle.name} ({data.daily.puzzle.score ?? '?'}/{data.daily.puzzle.scoreMax}) + +
    +
    + +
    +
    + {/if}
    diff --git a/src/routes/dashboard/chapters/+page.svelte b/src/routes/dashboard/chapters/+page.svelte index 899edf2..c8541bc 100644 --- a/src/routes/dashboard/chapters/+page.svelte +++ b/src/routes/dashboard/chapters/+page.svelte @@ -23,7 +23,7 @@

    Chapitres

    Les chapitres sont les différentes parties du jeu. Chaque chapitre est composé de plusieurs - puzzles. + puzzles

    diff --git a/src/routes/dashboard/chapters/[chapterId]/+page.svelte b/src/routes/dashboard/chapters/[chapterId]/+page.svelte index 90c3d32..79b8983 100644 --- a/src/routes/dashboard/chapters/[chapterId]/+page.svelte +++ b/src/routes/dashboard/chapters/[chapterId]/+page.svelte @@ -2,23 +2,27 @@ import type { PageData } from './$types'; import Puzzle from '$lib/components/Puzzle.svelte'; + import Button from '$lib/components/ui/Button.svelte'; export let data: PageData; data.chapter.puzzles = data.chapter.puzzles.sort((a, b) => a.scoreMax - b.scoreMax); -
    -
    -
    -
    -

    {data.chapter.name}

    -
    +
    +
    +
    +

    {data.chapter.name}

    -
      - {#each data.chapter.puzzles as puzzle} - - {/each} -
    + {#if data.chapter.id === 1} + + {/if}
    +
      + {#each data.chapter.puzzles as puzzle} + + {/each} +
    diff --git a/src/routes/dashboard/leaderboard/[chapterId]/+page.server.ts b/src/routes/dashboard/leaderboard/[chapterId]/+page.server.ts new file mode 100644 index 0000000..a9127aa --- /dev/null +++ b/src/routes/dashboard/leaderboard/[chapterId]/+page.server.ts @@ -0,0 +1,37 @@ +import { API_URL } from '$env/static/private'; + +import { redirect } from '@sveltejs/kit'; + +import type { LeaderboardEvent } from '$lib/types'; + +import type { PageServerLoad } from './$types'; + +export const load = (async ({ parent, fetch, cookies, params: { chapterId } }) => { + await parent(); + + if (!chapterId) { + throw redirect(303, '/dashboard'); + } + + const session = cookies.get('session'); + + const res = await fetch(`${API_URL}/leaderboard/${chapterId}`, { + headers: { + Authorization: `Bearer ${session}` + } + }); + + if (!res.ok) { + throw redirect(303, '/dashboard'); + } + + const leaderboard = (await res.json()) as LeaderboardEvent; + + if (!leaderboard || !leaderboard.groups.length) { + throw redirect(303, '/dashboard'); + } + + return { + leaderboard + }; +}) satisfies PageServerLoad; diff --git a/src/routes/dashboard/leaderboard/[chapterId]/+page.svelte b/src/routes/dashboard/leaderboard/[chapterId]/+page.svelte new file mode 100644 index 0000000..1a45a87 --- /dev/null +++ b/src/routes/dashboard/leaderboard/[chapterId]/+page.svelte @@ -0,0 +1,71 @@ + + +
    +
    +
    +

    Tableau des scores

    +

    Progression des équipes et joueurs dans le chapitre.

    +
    +
    + +
    + + +
      + {#each leaderboard.groups.filter( (g) => g.players.reduce((a, b) => a + b.score, 0) ) as group, i} + {@const players = group.players.sort((a, b) => b.score - a.score)} + {@const last = players[players.length - 1]} +
    • +
      + + {i + 1} + +
      + + + {#if group.players.length > 1} + {#each group.players as player} + {player.pseudo || 'Anonyme'}{#if player !== last}, {/if} + {' '} + {/each} + {:else} + {players[0].pseudo} + {/if} + +
      +
      +
      +
      + + + Essai{group.players.reduce((a, b) => a + b.tries, 0) || 0 ? 's' : ''} + {group.players.reduce((a, b) => a + b.tries, 0) || 0} +
      +
      + Score + + + {group.players.reduce((a, b) => a + b.score, 0)} + +
      +
      +
    • + {/each} +
    +
    +