# Sole Hub – how to make a game that works in the hub

**For the AI:** you get this guide together with an HTML game file. Rewrite the game so it works inside **Sole Hub** (Sole English – Ms. May). Follow every rule in section 2. Keep the game's look, characters, sounds and fun. Change only what you need to make it connect to the hub. Give back **one complete HTML file**.

---

## 1. How the hub runs a game

1. A student chooses a topic, a level and a game.
2. The hub opens the game in a frame as `your-game.html?hub=1`.
3. When the game has loaded, it sends **`ready`** to the hub.
4. The hub sends **`start`**. It holds the student's name and **the list of questions** (`items`) for this level.
5. The game asks **exactly these questions**, one by one. After **every** answer it sends **`answer`**.
6. When all the questions are done, the game sends **`end`**.
7. The hub then saves the result, gives points, shows the teacher, and starts the final writing task.

If step 3 never happens, the student sees "This game did not connect". If step 6 never happens, the result is never saved.

---

## 2. Rules (all of them are required)

1. **Paste the bridge** from section 3, unchanged, into the game's `<script>`.
2. **Questions come from the hub.** In hub mode do **not** use the game's own question list. Use `cfg.items` from `start`. Keep the old list only as `SAMPLE` for playing the file on its own.
3. **The first option is always the correct answer** (`item.options[0]`). Shuffle the options before showing them. Remember where the correct one went.
4. **Any number of questions.** Use `cfg.items.length` (usually 8–20). Don't assume 10. Don't stop early. Don't repeat questions to fill a fixed number.
5. **2, 3 or 4 options.** The number can be different for every question.
6. **Any length of text.** A question can be 1 word or 3 long sentences. It can contain a blank written as `_____`. Text must wrap and never overflow, overlap or be cut off. Use `clamp()` font sizes.
7. **Call `SOLE.answer(ok, score)` once for every question the student answers.** `ok` is `true` or `false`. Call it for the **first** answer to each question only, not for retries.
8. **Call `SOLE.end(score)` once** when the last question is finished. Also call it if the game ends early, for example when the player falls. Then stop the game: no more questions and no restart.
9. **Start straight away.** In hub mode, when `start` arrives, begin the game right away, or after a short "3-2-1".
   - Do **not** show the game's own start menu, name box, level picker, settings, "Play again" or "Home" buttons. Hide them with the `.sole-hub` CSS class (see the example).
   - If the game needs a "Tap to start" (for example to unlock sound), one big button is fine. When `cfg.autostart` is `true` (a challenge between two students), start without it.
10. **One player only, on one screen.** No local two-player mode. No asking for names (use `cfg.name`).
11. **Fit any screen.**
    - The frame can be a phone in landscape (about 640×360), a laptop (1366×768) or a big TV.
    - Use the full width and height (`100vw/100vh` or `100%`). Nothing may be fixed at 1920 px.
    - No page scroll during play.
12. **Controls:** mouse **and** keyboard (number keys 1–4 or arrow keys) **and** touch must all work. Buttons are big (at least 44 px).
13. **Read aloud:** if `cfg.read` is not `false`, read each question with `speechSynthesis` (`en-GB`, rate about 0.9). Read `_____` as "blank". Also keep short sound effects (Web Audio) for right and wrong answers.
14. **Everything in one file.**
    - Pictures go inside the file (inline SVG, CSS, emoji or `data:` images). The file must stay under **5 MB**.
    - Google Fonts are OK. No other outside libraries, no `fetch`, no external images.
15. **Never use** `alert()`, `confirm()`, `prompt()`, `window.open`, `location.reload()`, fullscreen requests or `localStorage` for game data.
16. **English only** on screen. Text is for Cambridge Young Learners to IELTS students, so keep it short and clear.
17. If something goes wrong, the bridge reports the error to the hub on its own. Don't hide errors with empty `try/catch` around the whole game.

---

## 3. The bridge (copy exactly)

```js
/* ============ Sole Hub bridge v2 – do not change ============ */
const SOLE = (() => {
  const on = window.parent !== window && /[?&]hub=1\b/.test(location.search);
  let cb = null, cfg = null; const st = { answered: 0, correct: 0, wrong: 0, score: 0, ended: false };
  const post = (type, extra) => { if (on) parent.postMessage(Object.assign({ sole: type }, extra || {}), '*'); };
  if (on) {
    document.documentElement.classList.add('sole-hub');
    addEventListener('message', e => { const d = e.data || {}; if (d.sole === 'start' && !cfg) { cfg = d; if (cb) cb(d); } });
    addEventListener('error', e => post('error', { message: String(e.message || e.error || 'error').slice(0, 200) }));
  }
  return {
    on, st,
    onStart(f) { cb = f; if (cfg) f(cfg); },          // f(cfg) runs once when the hub says start
    ready() { post('ready', { api: 2 }); },           // call once, when the game has loaded
    answer(ok, score) {                                 // call after EVERY answered question
      if (st.ended) return; st.answered++; ok ? st.correct++ : st.wrong++; st.score = Math.round(score || 0);
      post('answer', { ok: !!ok, answered: st.answered, correct: st.correct, wrong: st.wrong, score: st.score });
    },
    score(score) { st.score = Math.round(score || 0); post('score', { score: st.score }); },   // optional: score changed without an answer
    end(score) {                                        // call ONCE at the end
      if (st.ended) return; st.ended = true; if (score != null) st.score = Math.round(score);
      post('end', { score: st.score, answered: st.answered, correct: st.correct, wrong: st.wrong });
    }
  };
})();
/* ============================================================== */
```

---

## 4. What the hub sends in `start` (`cfg`)

| Field | Example | Meaning |
|---|---|---|
| `items` | `[{q:'I ___ to school.', options:['walk','walks','walking'], kind:'mcq'}, …]` | The questions. **`options[0]` is correct.** `kind` is just information, so treat every item the same way. |
| `n` | `12` | Same as `items.length`. |
| `name` | `'Linh'` | The student's name, for "Well done, Linh!". |
| `read` | `true` | Read the questions aloud. |
| `autostart` | `false` | `true` means start without any button (challenge mode). |
| `touch` | `false` | `true` on phones and tablets: show on-screen buttons if the game needs them. |
| `answerTime` | `0` / `20` / `30` | Seconds per question if the game has a timer (0 = no timer). Optional. |
| `speed` | `'normal'` | `'slow'`, `'normal'` or `'fast'`, for moving games. Optional. |
| `mode` | `'solo'` | `'solo'` or `'duel'`. A normal game can ignore it. |
| `level` | `2` | The level number, for display only. |

The hub counts lives, points and time itself. The game only reports right/wrong with `SOLE.answer`.

---

## 5. Skeleton – how the game should start

```html
<style>
  /* things the game shows only when it is opened on its own */
  .sole-hub .standalone { display: none !important; }
</style>
<script>
/* … bridge from section 3 here … */

const SAMPLE = [ /* the game's own questions, same format: {q, options:[correct, wrong, wrong]} */ ];
const shuffle = a => { a = a.slice(); for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; };
const say = t => { try { speechSynthesis.cancel(); const u = new SpeechSynthesisUtterance(String(t).replace(/_{2,}/g, ' blank ')); u.lang = 'en-GB'; u.rate = .9; speechSynthesis.speak(u); } catch (e) {} };

function startGame(items, opts) {
  // turn every item into: question text, shuffled options, index of the correct one
  const qs = items.map(it => { const o = shuffle(it.options); return { q: it.q, options: o, correct: o.indexOf(it.options[0]) }; });
  /* … build the game with qs … read aloud if opts.read !== false …
     every time the player answers question i:  SOLE.answer(ok, score)
     after the last question (or game over):     SOLE.end(score)            */
}

if (SOLE.on) {
  SOLE.onStart(cfg => startGame(cfg.items, cfg));   // hub mode: questions from the hub
  SOLE.ready();                                      // tell the hub the game is ready
} else {
  /* opened on its own: show the game's normal menu, play with SAMPLE */
}
</script>
```

---

## 6. Check before giving the file back

- [ ] The bridge from section 3 is in the file, unchanged.
- [ ] `SOLE.ready()` runs once after the page loads, and only in hub mode.
- [ ] In hub mode the questions come **only** from `cfg.items`. The correct answer is `options[0]`, shuffled.
- [ ] `SOLE.answer()` runs once per question, so after the last one `answered` equals `items.length`.
- [ ] `SOLE.end()` runs once at the end, then the game stops (no restart button in hub mode).
- [ ] Menus, name input, level select and "Play again" are hidden in hub mode (`.sole-hub`).
- [ ] Questions with 2, 3 or 4 options and long questions with `_____` look fine.
- [ ] It works with mouse, keyboard and touch, on 640×360 and on 1920×1080.
- [ ] There is no `alert/confirm/prompt`, no outside files, and the file is under 5 MB.
- [ ] Opening the file on its own still plays with `SAMPLE`.

---

## 7. Message for the AI (copy and paste with this guide and the game file)

> I attached my HTML game and "Sole Hub – how to make a game that works in the hub". Rewrite the game so it follows the guide exactly: add the bridge, take the questions from the hub, report every answer and the end, hide the menus in hub mode, and make it fit every screen. Keep my graphics, characters, sounds and gameplay. Give me the full HTML file in one piece, then list what you changed.
