themes.json: Use a exercice list instead of hash

This commit is contained in:
nemunaire 2023-01-24 14:15:48 +01:00
parent 3fcf705dcf
commit ef1eafb789
11 changed files with 82 additions and 82 deletions

View File

@ -57,9 +57,9 @@
<tr ng-repeat="lvl in [1,2,3,4,5]"> <tr ng-repeat="lvl in [1,2,3,4,5]">
<th class="text-center"><nobr>Challenge {{ lvl }}</nobr></th> <th class="text-center"><nobr>Challenge {{ lvl }}</nobr></th>
<td ng-repeat="(tid,theme) in themes" class="text-center text-bold"> <td ng-repeat="(tid,theme) in themes" class="text-center text-bold">
<a ng-href="exercices/{{ exercices[Utils.keys(theme.exercices)[lvl-1]].id_exercice }}" ng-if="exercices[Utils.keys(theme.exercices)[lvl-1]].team_tries || lvl == 1" ng-class="{'text-primary': exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count == 0, 'text-success': exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count >= 1, 'text-bold': exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count >= 1, 'text-warning': exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count == 0 && exercices[Utils.keys(theme.exercices)[lvl-1]].team_tries}"> <a ng-href="exercices/{{ exercices[theme.exercices[lvl-1].id].id_exercice }}" ng-if="exercices[theme.exercices[lvl-1].id].team_tries || lvl == 1" ng-class="{'text-primary': exercices[theme.exercices[lvl-1].id].solved_count == 0, 'text-success': exercices[theme.exercices[lvl-1].id].solved_count >= 1, 'text-bold': exercices[theme.exercices[lvl-1].id].solved_count >= 1, 'text-warning': exercices[theme.exercices[lvl-1].id].solved_count == 0 && exercices[theme.exercices[lvl-1].id].team_tries}">
<span ng-if="exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count">{{ exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count }}</span> <span ng-if="exercices[theme.exercices[lvl-1].id].solved_count">{{ exercices[theme.exercices[lvl-1].id].solved_count }}</span>
<span ng-if="!exercices[Utils.keys(theme.exercices)[lvl-1]].solved_count">{{ exercices[Utils.keys(theme.exercices)[lvl-1]].team_tries }}</span> <span ng-if="!exercices[theme.exercices[lvl-1].id].solved_count">{{ exercices[theme.exercices[lvl-1].id].team_tries }}</span>
</a> </a>
</td> </td>
</tr> </tr>

View File

@ -36,7 +36,7 @@
<hr> <hr>
{/if} {/if}
{/if} {/if}
{#if theme.exercices[exercice.id].next} {#if theme.exercices[exercice.id] && theme.exercices[exercice.id].next}
<a href="{theme.urlid}/{theme.exercices[theme.exercices[exercice.id].next].urlid}" class="btn btn-success">Passer au défi suivant</a> <a href="{theme.urlid}/{theme.exercices[theme.exercices[exercice.id].next].urlid}" class="btn btn-success">Passer au défi suivant</a>
{/if} {/if}
</CardBody> </CardBody>

View File

@ -20,21 +20,21 @@
</DropdownToggle> </DropdownToggle>
<DropdownMenu class="niceborder"> <DropdownMenu class="niceborder">
<div> <div>
{#each Object.keys($themes) as th, index} {#each $themes as th, index}
<DropdownItem href="{$themes[th].urlid}"> <DropdownItem href="{th.urlid}">
{$themes[th].name} {th.name}
{#if $max_solved > 1 && $themes[th].solved == $max_solved} {#if $max_solved > 1 && th.solved == $max_solved}
<Badge color="danger"> <Badge color="danger">
<Icon name="heart-fill" /> <Icon name="heart-fill" />
</Badge> </Badge>
{/if} {/if}
{#if $themes[th].exercice_coeff_max > 1} {#if th.exercice_coeff_max > 1}
<Badge color="success"> <Badge color="success">
<Icon name="gift-fill" /> <Icon name="gift-fill" />
</Badge> </Badge>
{/if} {/if}
<Badge> <Badge>
{#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}
</Badge> </Badge>
</DropdownItem> </DropdownItem>
{/each} {/each}

View File

@ -10,7 +10,7 @@
import DateFormat from '$lib/components/DateFormat.svelte'; import DateFormat from '$lib/components/DateFormat.svelte';
import { my } from '$lib/stores/my.js'; 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; let req = null;
function refresh_scores() { function refresh_scores() {
@ -55,9 +55,9 @@
<Badge color="primary"><Icon name="question" /></Badge> <Badge color="primary"><Icon name="question" /></Badge>
{row.reason} {row.reason}
{/if} {/if}
{#if row.id_exercice && $my.exercices[row.id_exercice]} {#if row.id_exercice && $exercices_idx[row.id_exercice]}
sur <a href="/{$themes[$my.exercices[row.id_exercice].theme_id].urlid}/{$themes[$my.exercices[row.id_exercice].theme_id].exercices[row.id_exercice].urlid}"> sur <a href="/{$themes[$exercices_idx[row.id_exercice].id_theme].urlid}/{$exercices_idx[row.id_exercice].urlid}">
{$themes[$my.exercices[row.id_exercice].theme_id].exercices[row.id_exercice].title} {$exercices_idx[row.id_exercice].title}
</a> </a>
{/if} {/if}
</Column> </Column>

View File

@ -13,32 +13,32 @@
</script> </script>
<Breadcrumb listClassName="mb-0 px-3 py-2"> <Breadcrumb listClassName="mb-0 px-3 py-2">
{#each Object.keys(theme.exercices) as k, index} {#each theme.exercices as ex, index}
<BreadcrumbItem active={k == exercice.id}> <BreadcrumbItem active={ex.id == exercice.id}>
{#if k == exercice.id} {#if ex.id == exercice.id}
<strong class="text-info"> <strong class="text-info">
{theme.exercices[k].title} {ex.title}
{#if theme.exercices[k].curcoeff > 1.0} {#if ex.curcoeff > 1.0}
<Icon name="gift" aria-hidden="true" /> <Icon name="gift" aria-hidden="true" />
{/if} {/if}
{#if $my && $my.team_id && $my.exercices[k] && $my.exercices[k].solved} {#if $my && $my.team_id && $my.exercices[ex.id] && $my.exercices[ex.id].solved}
<Icon name="check" class="text-success" aria-hidden="true" /> <Icon name="check" class="text-success" aria-hidden="true" />
{/if} {/if}
</strong> </strong>
{:else if $my && $my.exercices[k]} {:else if $my && $my.exercices[ex.id]}
<a href="{theme.urlid}/{theme.exercices[k].urlid}" class:text-success={$my.exercices[k].solved}> <a href="{theme.urlid}/{ex.urlid}" class:text-success={$my.exercices[ex.id].solved}>
{theme.exercices[k].title} {ex.title}
{#if theme.exercices[k].curcoeff > 1.0} {#if ex.curcoeff > 1.0}
<Icon name="gift" aria-hidden="true" /> <Icon name="gift" aria-hidden="true" />
{/if} {/if}
{#if $my.team_id && $my.exercices[k].solved} {#if $my.team_id && $my.exercices[ex.id].solved}
<Icon name="check" class="text-success" aria-hidden="true" /> <Icon name="check" class="text-success" aria-hidden="true" />
{/if} {/if}
</a> </a>
{:else} {:else}
<span class="text-muted"> <span class="text-muted">
{theme.exercices[k].title} {ex.title}
{#if theme.exercices[k].curcoeff > 1.0} {#if ex.curcoeff > 1.0}
<Icon name="gift" aria-hidden="true" /> <Icon name="gift" aria-hidden="true" />
{/if} {/if}
</span> </span>

View File

@ -12,8 +12,8 @@ export const myThemes = derived([my, themesStore], ([$my, $themesStore]) => {
mythemes[key] = {exercice_solved: 0}; mythemes[key] = {exercice_solved: 0};
if ($my && $my.exercices) { if ($my && $my.exercices) {
for (let k in $themesStore[key].exercices) { for (const exercice of $themesStore[key].exercices) {
if ($my.exercices[k] && $my.exercices[k].solved_rank) { if ($my.exercices[exercice.id] && $my.exercices[exercice.id].solved_rank) {
mythemes[key].exercice_solved++; mythemes[key].exercice_solved++;
} }
} }
@ -51,15 +51,15 @@ export const themes = derived(
export const tags = derived([my, themesStore], ([$my, $themesStore]) => { export const tags = derived([my, themesStore], ([$my, $themesStore]) => {
const tags = {}; const tags = {};
for (let key in $themesStore) { for (const key in $themesStore) {
for (let k in $themesStore[key].exercices) { for (const exercice of $themesStore[key].exercices) {
$themesStore[key].exercices[k].tags.forEach((tag) => { exercice.tags.forEach((tag) => {
if (!tags[tag]) if (!tags[tag])
tags[tag] = {count: 1, solved: 0}; tags[tag] = {count: 1, solved: 0};
else else
tags[tag].count += 1; 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; tags[tag].solved += 1;
}); });
} }

View File

@ -53,12 +53,11 @@ export const themes = derived(
const theme = $themesStore[key]; const theme = $themesStore[key];
themes[key] = theme 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].exercice_coeff_max = 0;
themes[key].max_gain = 0; themes[key].max_gain = 0;
let last_exercice = null; for (const k in theme.exercices) {
for (let k in theme.exercices) {
const exercice = theme.exercices[k]; const exercice = theme.exercices[k];
themes[key].max_gain += exercice.gain; themes[key].max_gain += exercice.gain;
@ -66,11 +65,8 @@ export const themes = derived(
themes[key].exercice_coeff_max = exercice.curcoeff; themes[key].exercice_coeff_max = exercice.curcoeff;
} }
if (last_exercice != null) if (k > 0)
themes[key].exercices[last_exercice].next = k; themes[key].exercices[k-1].next = k;
last_exercice = k;
exercice.id = k;
} }
} }
@ -101,8 +97,9 @@ export const exercices_idx = derived(
for (const key in $themesStore) { for (const key in $themesStore) {
const theme = $themesStore[key]; const theme = $themesStore[key];
for (let k in theme.exercices) { for (let exercice of theme.exercices) {
ret[k] = theme.exercices[k]; 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) { for (const key in $themesStore) {
const theme = $themesStore[key]; const theme = $themesStore[key];
for (let k in theme.exercices) { for (let exercice of theme.exercices) {
ret[theme.exercices[k].urlid] = theme.exercices[k]; ret[exercice.urlid] = exercice;
} }
} }

View File

@ -49,12 +49,12 @@
{/if} {/if}
<Row cols={{ lg: 3, md: 2, sm: 1 }}> <Row cols={{ lg: 3, md: 2, sm: 1 }}>
{#each Object.keys($themes) as th, index} {#each $themes as th, index}
<Col class="mb-3"> <Col class="mb-3">
<CardTheme <CardTheme
class="{$my && $my.team_id && $myThemes[$themes[th].id].exercice_solved > 0?'border-light ':''}{$themes[th].exercice_coeff_max > 1?'border-success ':''}" class="{$my && $my.team_id && $myThemes[th.id].exercice_solved > 0?'border-light ':''}{th.exercice_coeff_max > 1?'border-success ':''}"
theme={$themes[th]} theme={th}
on:click={goto(`${$themes[th].urlid}`)} on:click={goto(`${th.urlid}`)}
/> />
</Col> </Col>
{/each} {/each}

View File

@ -50,56 +50,56 @@
{#if $current_theme.exercices} {#if $current_theme.exercices}
<ul class="list-group"> <ul class="list-group">
{#each Object.keys($current_theme.exercices) as k, index} {#each $current_theme.exercices as exercice, index}
<li <li
class="list-group-item" class="list-group-item"
class:list-group-item-action={$my && $my.exercices[k]} class:list-group-item-action={$my && $my.exercices[exercice.id]}
on:click={goto(`${$current_theme.urlid}/${$current_theme.exercices[k].urlid}`)} on:click={goto(`${$current_theme.urlid}/${exercice.urlid}`)}
on:keypress={goto(`${$current_theme.urlid}/${$current_theme.exercices[k].urlid}`)} on:keypress={goto(`${$current_theme.urlid}/${exercice.urlid}`)}
> >
<div class="row"> <div class="row">
<div class="col-1" style="margin-top: -0.5rem; margin-bottom: -0.5rem; text-align: right; border-right: 5px solid #{$my && $my.exercices[k] && $my.exercices[k].solved_rank ? '62c462' : 'bbb'}"> <div class="col-1" style="margin-top: -0.5rem; margin-bottom: -0.5rem; text-align: right; border-right: 5px solid #{$my && $my.exercices[exercice.id] && $my.exercices[exercice.id].solved_rank ? '62c462' : 'bbb'}">
</div> </div>
<div class="col-10"> <div class="col-10">
<div style="position: absolute; margin-left: calc(var(--bs-gutter-x) * -.5 - 15px); margin-top: -0.5rem;"> <div style="position: absolute; margin-left: calc(var(--bs-gutter-x) * -.5 - 15px); margin-top: -0.5rem;">
<svg style="height: 50px; width: 23px;"> <svg style="height: 50px; width: 23px;">
<rect <rect
style="fill:#{$my && $my.exercices[k] && (index < 1 || ($my.exercices[Object.keys($current_theme.exercices)[index-1]] && $my.exercices[Object.keys($current_theme.exercices)[index-1]].solved_rank)) ? '62c462' : 'bbb'}" style="fill:#{$my && $my.exercices[exercice.id] && (index < 1 || ($my.exercices[$current_theme.exercices[index-1].id] && $my.exercices[$current_theme.exercices[index-1].id].solved_rank)) ? '62c462' : 'bbb'}"
width="5" width="5"
height="30" height="30"
x="10" x="10"
y="0" /> y="0" />
<path <path
style="fill:#{$my && $my.exercices[k] ? ($my.exercices[k].solved_rank ? '62c462' : ($current_theme.exercices[k].curcoeff > 1.0 ? 'f89406' : '5bc0de')) : '555'}" style="fill:#{$my && $my.exercices[exercice.id] ? ($my.exercices[exercice.id].solved_rank ? '62c462' : (exercice.curcoeff > 1.0 ? 'f89406' : '5bc0de')) : '555'}"
d="m 22,20 a 9.5700617,9.5700617 0 0 1 -9.5690181,9.57006 9.5700617,9.5700617 0 0 1 -9.57110534,-9.56797 9.5700617,9.5700617 0 0 1 9.56692984,-9.57215 9.5700617,9.5700617 0 0 1 9.5731926,9.56588" /> d="m 22,20 a 9.5700617,9.5700617 0 0 1 -9.5690181,9.57006 9.5700617,9.5700617 0 0 1 -9.57110534,-9.56797 9.5700617,9.5700617 0 0 1 9.56692984,-9.57215 9.5700617,9.5700617 0 0 1 9.5731926,9.56588" />
</svg> </svg>
</div> </div>
{#each $current_theme.exercices[k].tags as tag, idx} {#each exercice.tags as tag, idx}
<Badge href="tags/{tag}" pill color="secondary" class="mx-1 float-end">#{tag}</Badge> <Badge href="tags/{tag}" pill color="secondary" class="mx-1 float-end">#{tag}</Badge>
{/each} {/each}
<h5 class="fw-bold"> <h5 class="fw-bold">
{#if $my && $my.exercices[k]} {#if $my && $my.exercices[exercice.id]}
<span style="white-space: nowrap"> <span style="white-space: nowrap">
{#if $my.exercices[k].wip} {#if $my.exercices[exercice.id].wip}
<Icon name="cone-striped" aria-hidden="true" title="Cette étape est encore en construction." /> <Icon name="cone-striped" aria-hidden="true" title="Cette étape est encore en construction." />
{/if} {/if}
{$current_theme.exercices[k].title} {exercice.title}
</span> </span>
{:else} {:else}
<span style="white-space: nowrap"> <span style="white-space: nowrap">
<Icon name="lock-fill" aria-hidden="true" title="Vous n'avez pas encore accès à ce défi" /> <Icon name="lock-fill" aria-hidden="true" title="Vous n'avez pas encore accès à ce défi" />
{$current_theme.exercices[k].title} {exercice.title}
</span> </span>
{/if} {/if}
{#if $current_theme.exercices[k].curcoeff > 1.0} {#if exercice.curcoeff > 1.0}
<Icon name="gift" aria-hidden="true" title="Un bonus est actuellement appliqué lors de la résolution de ce défi" /> <Icon name="gift" aria-hidden="true" title="Un bonus est actuellement appliqué lors de la résolution de ce défi" />
{/if} {/if}
</h5> </h5>
<p>{@html $current_theme.exercices[k].headline}</p> <p>{@html exercice.headline}</p>
</div> </div>
<div class="d-none d-md-block col-1"> <div class="d-none d-md-block col-1">
{#if $my && $my.exercices[k]} {#if $my && $my.exercices[exercice.id]}
<a class="float-right" href="{$current_theme.urlid}/{$current_theme.exercices[k].urlid}" style="font-size: 3rem"> <a class="float-right" href="{$current_theme.urlid}/{exercice.urlid}" style="font-size: 3rem">
<Icon name="chevron-right" aria-hidden="true" /> <Icon name="chevron-right" aria-hidden="true" />
</a> </a>
{:else} {:else}

View File

@ -22,10 +22,11 @@
$: { $: {
let tmp_exercices = []; let tmp_exercices = [];
for (let th in $themes) { for (let k in $themes) {
for (let ex in $themes[th].exercices) { const th = $themes[k];
if ($themes[th].exercices[ex].tags.indexOf(data.tag) >= 0) { for (const ex of th.exercices) {
tmp_exercices.push({theme: $themes[th], exercice: $themes[th].exercices[ex], index: th + "," + ex}); if (ex.tags.indexOf(data.tag) >= 0) {
tmp_exercices.push({theme: th, exercice: ex, index: k + "," + ex});
} }
} }
} }

View File

@ -10,6 +10,7 @@ var GlobalScoreCoefficient float64 = 1
// exportedExercice is a structure representing a challenge, as exposed to players. // exportedExercice is a structure representing a challenge, as exposed to players.
type exportedExercice struct { type exportedExercice struct {
Id int64 `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Headline string `json:"headline,omitempty"` Headline string `json:"headline,omitempty"`
URLId string `json:"urlid"` URLId string `json:"urlid"`
@ -31,7 +32,7 @@ type exportedTheme struct {
PartnerImage string `json:"partner_img,omitempty"` PartnerImage string `json:"partner_img,omitempty"`
PartnerLink string `json:"partner_href,omitempty"` PartnerLink string `json:"partner_href,omitempty"`
PartnerText string `json:"partner_txt,omitempty"` PartnerText string `json:"partner_txt,omitempty"`
Exercices map[string]exportedExercice `json:"exercices"` Exercices []exportedExercice `json:"exercices"`
} }
// Exportedthemes exports themes from the database, to be displayed to players. // 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 { if exercices, err := theme.GetExercices(); err != nil {
return nil, err return nil, err
} else { } else {
exos := map[string]exportedExercice{} exos := []exportedExercice{}
for _, exercice := range exercices { for _, exercice := range exercices {
tags, _ := exercice.GetTags() tags, _ := exercice.GetTags()
exos[fmt.Sprintf("%d", exercice.Id)] = exportedExercice{ exos = append(exos, exportedExercice{
exercice.Id,
exercice.Title, exercice.Title,
exercice.Headline, exercice.Headline,
exercice.URLId, exercice.URLId,
@ -56,7 +58,7 @@ func ExportThemes() (interface{}, error) {
exercice.Coefficient, exercice.Coefficient,
exercice.SolvedCount(), exercice.SolvedCount(),
exercice.TriedTeamCount(), exercice.TriedTeamCount(),
} })
} }
imgpath := "" imgpath := ""