mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Add LessonEngine, LevelSession and vendored js-yaml
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
// Charge le manifeste et les niveaux (YAML) ; fournit des LevelSession localisées.
|
||||||
|
|
||||||
|
import yaml from '../lib/js-yaml/js-yaml.mjs';
|
||||||
|
import { LevelSession } from './LevelSession.js';
|
||||||
|
|
||||||
|
export class LessonEngine {
|
||||||
|
#levelsUrl;
|
||||||
|
#fetch;
|
||||||
|
#parseYaml;
|
||||||
|
#manifest = null;
|
||||||
|
#levels = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} [options]
|
||||||
|
* @param {string} [options.levelsUrl] Racine des niveaux, avec slash final.
|
||||||
|
* @param {typeof fetch} [options.fetchFn] Remplaçable pour les tests.
|
||||||
|
* @param {(text: string) => unknown} [options.parseYaml]
|
||||||
|
*/
|
||||||
|
constructor({ levelsUrl = '/levels/', fetchFn = (...args) => fetch(...args), parseYaml = (text) => yaml.load(text) } = {}) {
|
||||||
|
this.#levelsUrl = levelsUrl;
|
||||||
|
this.#fetch = fetchFn;
|
||||||
|
this.#parseYaml = parseYaml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Liste ordonnée des niveaux : [{ id, requires }]. */
|
||||||
|
async listLevels() {
|
||||||
|
if (!this.#manifest) {
|
||||||
|
const response = await this.#get('manifest.json');
|
||||||
|
this.#manifest = JSON.parse(await response.text());
|
||||||
|
}
|
||||||
|
return this.#manifest.levels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Niveau brut, mis en cache. */
|
||||||
|
async loadLevel(id) {
|
||||||
|
if (!this.#levels.has(id)) {
|
||||||
|
const known = (await this.listLevels()).some((entry) => entry.id === id);
|
||||||
|
if (!known) {
|
||||||
|
throw new Error(`Unknown level: ${id}`);
|
||||||
|
}
|
||||||
|
const response = await this.#get(`${id}/level.yaml`);
|
||||||
|
const level = this.#parseYaml(await response.text());
|
||||||
|
if (level?.id !== id) {
|
||||||
|
throw new Error(`Level file for ${id} declares id ${level?.id}`);
|
||||||
|
}
|
||||||
|
this.#levels.set(id, level);
|
||||||
|
}
|
||||||
|
return this.#levels.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ouvre une session de parcours dans la langue donnée. */
|
||||||
|
async startLevel(id, lang = 'fr') {
|
||||||
|
const level = await this.loadLevel(id);
|
||||||
|
return new LevelSession(level, lang, new URL(`${id}/`, new URL(this.#levelsUrl, globalThis.location?.href ?? 'http://localhost/')).href);
|
||||||
|
}
|
||||||
|
|
||||||
|
async #get(path) {
|
||||||
|
const response = await this.#fetch(`${this.#levelsUrl}${path}`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Cannot load ${path}: HTTP ${response.status}`);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
// Parcours d'un niveau par l'apprenant : étapes localisées puis mission finale.
|
||||||
|
|
||||||
|
export class LevelSession {
|
||||||
|
#level;
|
||||||
|
#lang;
|
||||||
|
#baseUrl;
|
||||||
|
#index = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} level Niveau brut (conforme à level.schema.json).
|
||||||
|
* @param {string} lang Langue d'affichage ('fr' ou 'en').
|
||||||
|
* @param {string} baseUrl URL du dossier du niveau, avec slash final (pour les images).
|
||||||
|
*/
|
||||||
|
constructor(level, lang, baseUrl) {
|
||||||
|
this.#level = level;
|
||||||
|
this.#lang = lang;
|
||||||
|
this.#baseUrl = baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
get id() {
|
||||||
|
return this.#level.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
get title() {
|
||||||
|
return this.#localize(this.#level.title);
|
||||||
|
}
|
||||||
|
|
||||||
|
get xp() {
|
||||||
|
return this.#level.xp;
|
||||||
|
}
|
||||||
|
|
||||||
|
get stepCount() {
|
||||||
|
return this.#level.steps.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Index de l'étape courante ; égal à stepCount quand on est sur la mission. */
|
||||||
|
get index() {
|
||||||
|
return this.#index;
|
||||||
|
}
|
||||||
|
|
||||||
|
get onMission() {
|
||||||
|
return this.#index >= this.stepCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Étape courante localisée, ou null sur la mission. */
|
||||||
|
get step() {
|
||||||
|
return this.onMission ? null : this.#viewStep(this.#level.steps[this.#index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mission finale localisée. */
|
||||||
|
get mission() {
|
||||||
|
const { mission } = this.#level;
|
||||||
|
return {
|
||||||
|
title: this.#localize(mission.title),
|
||||||
|
goal: this.#localize(mission.goal),
|
||||||
|
starter: mission.starter ?? '',
|
||||||
|
tests: this.#viewTests(mission.tests ?? []),
|
||||||
|
rewardSound: mission.reward_sound ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Avance d'une étape (puis vers la mission). Renvoie false si déjà sur la mission. */
|
||||||
|
next() {
|
||||||
|
if (this.onMission) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.#index += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Revient d'une étape. Renvoie false si on est déjà à la première. */
|
||||||
|
previous() {
|
||||||
|
if (this.#index === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.#index -= 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#localize(value) {
|
||||||
|
return value?.[this.#lang] ?? value?.fr ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
#viewTests(tests) {
|
||||||
|
return tests.map((test) => ({
|
||||||
|
...test,
|
||||||
|
message: test.message ? this.#localize(test.message) : null,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
#viewStep(step) {
|
||||||
|
if (step.type === 'explain') {
|
||||||
|
return {
|
||||||
|
type: 'explain',
|
||||||
|
text: this.#localize(step.text),
|
||||||
|
voice: step.voice ?? false,
|
||||||
|
image: step.image ? new URL(step.image, this.#baseUrl).href : null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: 'code',
|
||||||
|
starter: step.starter,
|
||||||
|
instruction: this.#localize(step.instruction),
|
||||||
|
tests: this.#viewTests(step.tests),
|
||||||
|
// Indices dans la langue demandée, sinon repli sur le français.
|
||||||
|
hints: step.hints?.[this.#lang] ?? step.hints?.fr ?? [],
|
||||||
|
solution: step.solution ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
(The MIT License)
|
||||||
|
|
||||||
|
Copyright (C) 2011-2015 by Vitaly Puzrin
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# js-yaml 4.3.2 (MIT)
|
||||||
|
|
||||||
|
Copie vendorisée de `dist/js-yaml.mjs` (npm `js-yaml@4.3.2`), servie depuis le même domaine
|
||||||
|
(COEP). Utilisée uniquement pour lire les `level.yaml`. Licence : voir `LICENSE`.
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user