From ef1eafb789f4465fd63acd50b9490f84d82059b4 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 24 Jan 2023 14:15:48 +0100 Subject: [PATCH] themes.json: Use a exercice list instead of hash --- admin/static/views/home.html | 6 ++-- .../src/lib/components/ExerciceSolved.svelte | 2 +- .../ui/src/lib/components/NavThemes.svelte | 12 +++---- .../ui/src/lib/components/ScoreGrid.svelte | 8 ++--- .../ui/src/lib/components/ThemeNav.svelte | 26 +++++++-------- frontend/ui/src/lib/stores/mythemes.js | 12 +++---- frontend/ui/src/lib/stores/themes.js | 21 ++++++------ frontend/ui/src/routes/+page.svelte | 8 ++--- frontend/ui/src/routes/[theme]/+page.svelte | 32 +++++++++---------- .../ui/src/routes/tags/[tag]/+page.svelte | 9 +++--- libfic/theme_export.go | 28 ++++++++-------- 11 files changed, 82 insertions(+), 82 deletions(-) diff --git a/admin/static/views/home.html b/admin/static/views/home.html index 6312622b..3105513b 100644 --- a/admin/static/views/home.html +++ b/admin/static/views/home.html @@ -57,9 +57,9 @@ Challenge {{ lvl }} - - {{ exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count }} - {{ exercices[Utils.keys(theme.exercices)[lvl-1]].team_tries }} + + {{ exercices[theme.exercices[lvl-1].id].solved_count }} + {{ exercices[theme.exercices[lvl-1].id].team_tries }} diff --git a/frontend/ui/src/lib/components/ExerciceSolved.svelte b/frontend/ui/src/lib/components/ExerciceSolved.svelte index 6676ec7d..383fae10 100644 --- a/frontend/ui/src/lib/components/ExerciceSolved.svelte +++ b/frontend/ui/src/lib/components/ExerciceSolved.svelte @@ -36,7 +36,7 @@
{/if} {/if} - {#if theme.exercices[exercice.id].next} + {#if theme.exercices[exercice.id] && theme.exercices[exercice.id].next} Passer au défi suivant {/if} diff --git a/frontend/ui/src/lib/components/NavThemes.svelte b/frontend/ui/src/lib/components/NavThemes.svelte index dce00389..a866f54a 100644 --- a/frontend/ui/src/lib/components/NavThemes.svelte +++ b/frontend/ui/src/lib/components/NavThemes.svelte @@ -20,21 +20,21 @@
- {#each Object.keys($themes) as th, index} - - {$themes[th].name} - {#if $max_solved > 1 && $themes[th].solved == $max_solved} + {#each $themes as th, index} + + {th.name} + {#if $max_solved > 1 && th.solved == $max_solved} {/if} - {#if $themes[th].exercice_coeff_max > 1} + {#if th.exercice_coeff_max > 1} {/if} - {#if $my && $my.team_id}{$myThemes[$themes[th].id].exercice_solved}/{/if}{$themes[th].exercice_count} + {#if $my && $my.team_id}{$myThemes[th.id].exercice_solved}/{/if}{th.exercice_count} {/each} diff --git a/frontend/ui/src/lib/components/ScoreGrid.svelte b/frontend/ui/src/lib/components/ScoreGrid.svelte index 4d714ef1..57dbd1f0 100644 --- a/frontend/ui/src/lib/components/ScoreGrid.svelte +++ b/frontend/ui/src/lib/components/ScoreGrid.svelte @@ -10,7 +10,7 @@ import DateFormat from '$lib/components/DateFormat.svelte'; import { my } from '$lib/stores/my.js'; - import { themes } from '$lib/stores/themes.js'; + import { themes, exercices_idx } from '$lib/stores/themes.js'; let req = null; function refresh_scores() { @@ -55,9 +55,9 @@ {row.reason} {/if} - {#if row.id_exercice && $my.exercices[row.id_exercice]} - sur - {$themes[$my.exercices[row.id_exercice].theme_id].exercices[row.id_exercice].title} + {#if row.id_exercice && $exercices_idx[row.id_exercice]} + sur + {$exercices_idx[row.id_exercice].title} {/if} diff --git a/frontend/ui/src/lib/components/ThemeNav.svelte b/frontend/ui/src/lib/components/ThemeNav.svelte index 2f38e2c4..10143504 100644 --- a/frontend/ui/src/lib/components/ThemeNav.svelte +++ b/frontend/ui/src/lib/components/ThemeNav.svelte @@ -13,32 +13,32 @@ - {#each Object.keys(theme.exercices) as k, index} - - {#if k == exercice.id} + {#each theme.exercices as ex, index} + + {#if ex.id == exercice.id} - {theme.exercices[k].title} - {#if theme.exercices[k].curcoeff > 1.0} + {ex.title} + {#if ex.curcoeff > 1.0} - {:else if $my && $my.exercices[k]} - - {theme.exercices[k].title} - {#if theme.exercices[k].curcoeff > 1.0} + {:else if $my && $my.exercices[ex.id]} + + {ex.title} + {#if ex.curcoeff > 1.0} {:else} - {theme.exercices[k].title} - {#if theme.exercices[k].curcoeff > 1.0} + {ex.title} + {#if ex.curcoeff > 1.0} diff --git a/frontend/ui/src/lib/stores/mythemes.js b/frontend/ui/src/lib/stores/mythemes.js index 94a6fd08..08325a15 100644 --- a/frontend/ui/src/lib/stores/mythemes.js +++ b/frontend/ui/src/lib/stores/mythemes.js @@ -12,8 +12,8 @@ export const myThemes = derived([my, themesStore], ([$my, $themesStore]) => { mythemes[key] = {exercice_solved: 0}; if ($my && $my.exercices) { - for (let k in $themesStore[key].exercices) { - if ($my.exercices[k] && $my.exercices[k].solved_rank) { + for (const exercice of $themesStore[key].exercices) { + if ($my.exercices[exercice.id] && $my.exercices[exercice.id].solved_rank) { mythemes[key].exercice_solved++; } } @@ -51,15 +51,15 @@ export const themes = derived( export const tags = derived([my, themesStore], ([$my, $themesStore]) => { const tags = {}; - for (let key in $themesStore) { - for (let k in $themesStore[key].exercices) { - $themesStore[key].exercices[k].tags.forEach((tag) => { + for (const key in $themesStore) { + for (const exercice of $themesStore[key].exercices) { + exercice.tags.forEach((tag) => { if (!tags[tag]) tags[tag] = {count: 1, solved: 0}; else tags[tag].count += 1; - if ($my && $my.exercices && $my.exercices[k] && $my.exercices[k].solved_rank) + if ($my && $my.exercices && $my.exercices[exercice.id] && $my.exercices[exercice.id].solved_rank) tags[tag].solved += 1; }); } diff --git a/frontend/ui/src/lib/stores/themes.js b/frontend/ui/src/lib/stores/themes.js index 247d2c12..eb3d9906 100644 --- a/frontend/ui/src/lib/stores/themes.js +++ b/frontend/ui/src/lib/stores/themes.js @@ -53,12 +53,11 @@ export const themes = derived( const theme = $themesStore[key]; themes[key] = theme - themes[key].exercice_count = Object.keys(theme.exercices).length; + themes[key].exercice_count = theme.exercices.length; themes[key].exercice_coeff_max = 0; themes[key].max_gain = 0; - let last_exercice = null; - for (let k in theme.exercices) { + for (const k in theme.exercices) { const exercice = theme.exercices[k]; themes[key].max_gain += exercice.gain; @@ -66,11 +65,8 @@ export const themes = derived( themes[key].exercice_coeff_max = exercice.curcoeff; } - if (last_exercice != null) - themes[key].exercices[last_exercice].next = k; - last_exercice = k; - - exercice.id = k; + if (k > 0) + themes[key].exercices[k-1].next = k; } } @@ -101,8 +97,9 @@ export const exercices_idx = derived( for (const key in $themesStore) { const theme = $themesStore[key]; - for (let k in theme.exercices) { - ret[k] = theme.exercices[k]; + for (let exercice of theme.exercices) { + ret[exercice.id] = exercice; + ret[exercice.id].id_theme = key; } } @@ -117,8 +114,8 @@ export const exercices_idx_urlid = derived( for (const key in $themesStore) { const theme = $themesStore[key]; - for (let k in theme.exercices) { - ret[theme.exercices[k].urlid] = theme.exercices[k]; + for (let exercice of theme.exercices) { + ret[exercice.urlid] = exercice; } } diff --git a/frontend/ui/src/routes/+page.svelte b/frontend/ui/src/routes/+page.svelte index 808a406a..c9ce7b2c 100644 --- a/frontend/ui/src/routes/+page.svelte +++ b/frontend/ui/src/routes/+page.svelte @@ -49,12 +49,12 @@ {/if} - {#each Object.keys($themes) as th, index} + {#each $themes as th, index} {/each} diff --git a/frontend/ui/src/routes/[theme]/+page.svelte b/frontend/ui/src/routes/[theme]/+page.svelte index 7b8219d1..d7f1c2a6 100644 --- a/frontend/ui/src/routes/[theme]/+page.svelte +++ b/frontend/ui/src/routes/[theme]/+page.svelte @@ -50,56 +50,56 @@ {#if $current_theme.exercices}
    - {#each Object.keys($current_theme.exercices) as k, index} + {#each $current_theme.exercices as exercice, index}
  • -
    +
    - {#each $current_theme.exercices[k].tags as tag, idx} + {#each exercice.tags as tag, idx} #{tag} {/each}
    - {#if $my && $my.exercices[k]} + {#if $my && $my.exercices[exercice.id]} - {#if $my.exercices[k].wip} + {#if $my.exercices[exercice.id].wip} {:else} {/if} - {#if $current_theme.exercices[k].curcoeff > 1.0} + {#if exercice.curcoeff > 1.0}
    -

    {@html $current_theme.exercices[k].headline}

    +

    {@html exercice.headline}

    - {#if $my && $my.exercices[k]} - + {#if $my && $my.exercices[exercice.id]} + {:else} diff --git a/frontend/ui/src/routes/tags/[tag]/+page.svelte b/frontend/ui/src/routes/tags/[tag]/+page.svelte index 87cfc52b..bc530a68 100644 --- a/frontend/ui/src/routes/tags/[tag]/+page.svelte +++ b/frontend/ui/src/routes/tags/[tag]/+page.svelte @@ -22,10 +22,11 @@ $: { let tmp_exercices = []; - for (let th in $themes) { - for (let ex in $themes[th].exercices) { - if ($themes[th].exercices[ex].tags.indexOf(data.tag) >= 0) { - tmp_exercices.push({theme: $themes[th], exercice: $themes[th].exercices[ex], index: th + "," + ex}); + for (let k in $themes) { + const th = $themes[k]; + for (const ex of th.exercices) { + if (ex.tags.indexOf(data.tag) >= 0) { + tmp_exercices.push({theme: th, exercice: ex, index: k + "," + ex}); } } } diff --git a/libfic/theme_export.go b/libfic/theme_export.go index cdfd0eb6..2b301d5b 100644 --- a/libfic/theme_export.go +++ b/libfic/theme_export.go @@ -10,6 +10,7 @@ var GlobalScoreCoefficient float64 = 1 // exportedExercice is a structure representing a challenge, as exposed to players. type exportedExercice struct { + Id int64 `json:"id"` Title string `json:"title"` Headline string `json:"headline,omitempty"` URLId string `json:"urlid"` @@ -22,16 +23,16 @@ type exportedExercice struct { // exportedTheme is a structure representing a Theme, as exposed to players. type exportedTheme struct { - Name string `json:"name"` - URLId string `json:"urlid"` - Authors string `json:"authors"` - Headline string `json:"headline,omitempty"` - Intro string `json:"intro"` - Image string `json:"image,omitempty"` - PartnerImage string `json:"partner_img,omitempty"` - PartnerLink string `json:"partner_href,omitempty"` - PartnerText string `json:"partner_txt,omitempty"` - Exercices map[string]exportedExercice `json:"exercices"` + Name string `json:"name"` + URLId string `json:"urlid"` + Authors string `json:"authors"` + Headline string `json:"headline,omitempty"` + Intro string `json:"intro"` + Image string `json:"image,omitempty"` + PartnerImage string `json:"partner_img,omitempty"` + PartnerLink string `json:"partner_href,omitempty"` + PartnerText string `json:"partner_txt,omitempty"` + Exercices []exportedExercice `json:"exercices"` } // Exportedthemes exports themes from the database, to be displayed to players. @@ -44,10 +45,11 @@ func ExportThemes() (interface{}, error) { if exercices, err := theme.GetExercices(); err != nil { return nil, err } else { - exos := map[string]exportedExercice{} + exos := []exportedExercice{} for _, exercice := range exercices { tags, _ := exercice.GetTags() - exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{ + exos = append(exos, exportedExercice{ + exercice.Id, exercice.Title, exercice.Headline, exercice.URLId, @@ -56,7 +58,7 @@ func ExportThemes() (interface{}, error) { exercice.Coefficient, exercice.SolvedCount(), exercice.TriedTeamCount(), - } + }) } imgpath := ""