File size: 7,324 Bytes
c9a41bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Dataset browser (adapted from anon-submission-2026/docs/viewer.js)
// Wrapped in lazy-init so it only loads when the Dataset Browser tab is opened.

let _viewerInitialized = false;

// Base URL for images hosted in the HF dataset repo
const IMAGE_BASE_URL = "https://huggingface.co/datasets/jpeper/LudoBench_test/resolve/main/images";

// -----------------------
// Manifest loader
// -----------------------
async function loadManifest() {
  const res = await fetch("manifest.json");
  if (!res.ok) throw new Error("Failed to load manifest: " + res.status);
  const manifest = await res.json();
  return manifest.files;
}

// -----------------------
// Folder handling
// -----------------------
function buildFolderIndex(files) {
  const set = new Set();
  for (const f of files) set.add(f.folder);
  return Array.from(set).sort();
}

function populateFolderSelect(folders) {
  const sel = document.getElementById("folderSelect");
  sel.innerHTML = "";
  for (const f of folders) {
    const opt = document.createElement("option");
    opt.textContent = f;
    opt.value = f;
    sel.appendChild(opt);
  }
}

function filterFiles(files, folder) {
  return files.filter(f => f.folder === folder);
}

function populateFileSelect(files) {
  const sel = document.getElementById("fileSelect");
  sel.innerHTML = "";
  for (const f of files) {
    const opt = document.createElement("option");
    opt.value = f.json_path;
    opt.textContent = f.name;
    sel.appendChild(opt);
  }
}

// -----------------------
// Load one JSON annotation
// -----------------------
async function loadJson(path) {
  const res = await fetch(path);
  if (!res.ok) throw new Error("Error loading " + path);
  return await res.json();
}

// -----------------------
// Rendering question + answer
// -----------------------
function normalizedAnswers(raw) {
  return new Set(
    String(raw || "")
      .split(/,|\/|\bor\b/i)
      .map(s => s.trim().toLowerCase())
      .filter(Boolean)
  );
}

function isNumber(s) {
  return s !== "" && !Number.isNaN(Number(s));
}

function renderQuestion(data) {
  const card = document.getElementById("questionCard");
  card.innerHTML = `
    <h2>${data.Game} <span class="id-tag">(ID ${data.ID})</span></h2>
    <p class="question-label">Question:</p>
    <p class="question-text">${data.Question}</p>
  `;

  const info = document.getElementById("answerInfo");
  info.textContent = "";
  info.className = "answer-info";
}

function attachAnswerLogic(data) {
  const raw = String(data.Answer || "").trim();
  const accepted = normalizedAnswers(raw);

  const input = document.getElementById("answerInput");
  const button = document.getElementById("checkButton");
  const info = document.getElementById("answerInfo");

  const sol = document.getElementById("solutionText");
  sol.innerHTML = `<strong>Expected:</strong> ${raw || "\u2014"}`;

  button.onclick = () => {
    const user = input.value.trim().toLowerCase();
    let ok = false;

    if (accepted.has(user)) ok = true;
    else if (isNumber(user)) {
      for (const a of accepted)
        if (isNumber(a) && Math.abs(Number(a) - Number(user)) < 1e-9)
          ok = true;
    }

    info.textContent = ok ? "Correct!" : "Not quite. Try again.";
    info.className = "answer-info " + (ok ? "correct" : "wrong");
  };
}

// -----------------------
// Render images (local paths)
// -----------------------
function renderImage(data) {
  const container = document.getElementById("imageContainer");
  const multi = document.getElementById("multiImages");

  multi.innerHTML = "";
  container.classList.add("hidden");

  let urls = data.game_state_url;
  if (!urls) return;
  if (!Array.isArray(urls)) urls = [urls];

  const folder = data.Game.toLowerCase().replace(/\s+/g, "_");

  urls.forEach(url => {
    const file = url.split("/").pop();
    const localPath = `${IMAGE_BASE_URL}/${folder}/${file}`;

    const block = document.createElement("div");
    block.className = "multi-img-block";

    const spinner = document.createElement("div");
    spinner.className = "spinner";
    block.appendChild(spinner);

    const img = document.createElement("img");
    img.style.display = "none";
    img.src = localPath;

    img.onload = () => {
      spinner.style.display = "none";
      img.style.display = "block";
    };

    img.onerror = () => {
      spinner.style.display = "none";
      const err = document.createElement("div");
      err.textContent = "Failed to load " + localPath;
      err.style.color = "#d44";
      block.appendChild(err);
    };

    const link = document.createElement("a");
    link.href = localPath;
    link.target = "_blank";
    link.rel = "noopener noreferrer";
    link.appendChild(img);
    block.appendChild(link);

    const caption = document.createElement("div");
    caption.className = "multi-img-caption";
    caption.textContent = file;
    block.appendChild(caption);

    const full = document.createElement("a");
    full.href = localPath;
    full.target = "_blank";
    full.rel = "noopener noreferrer";
    full.className = "full-img-link";
    full.textContent = "View full image";
    block.appendChild(full);

    multi.appendChild(block);
  });

  container.classList.remove("hidden");
}

// -----------------------
// Navigation (prev / next)
// -----------------------
let GLOBAL_FILES = [];
let GLOBAL_CURRENT_FOLDER = "";
let loadAndRenderRef = null;

function goRelative(offset) {
  const fileSelect = document.getElementById("fileSelect");
  const options = Array.from(fileSelect.options);
  if (options.length === 0) return;

  const values = options.map(o => o.value);
  const current = fileSelect.value;
  let idx = values.indexOf(current);
  if (idx === -1) return;

  let next = idx + offset;
  if (next < 0) next = values.length - 1;
  if (next >= values.length) next = 0;

  fileSelect.value = values[next];
  if (loadAndRenderRef) loadAndRenderRef(values[next]);
}

// -----------------------
// Lazy initialization
// -----------------------
async function initViewer() {
  if (_viewerInitialized) return;
  _viewerInitialized = true;

  document.getElementById("prevBtn").onclick = () => goRelative(-1);
  document.getElementById("nextBtn").onclick = () => goRelative(1);

  const questionCard = document.getElementById("questionCard");

  try {
    const files = await loadManifest();
    GLOBAL_FILES = files;

    const folders = buildFolderIndex(files);
    populateFolderSelect(folders);

    const folderSel = document.getElementById("folderSelect");
    const fileSel = document.getElementById("fileSelect");

    async function loadAndRender(path) {
      const data = await loadJson(path);
      renderQuestion(data);
      attachAnswerLogic(data);
      renderImage(data);
    }

    loadAndRenderRef = loadAndRender;

    function refresh() {
      GLOBAL_CURRENT_FOLDER = folderSel.value;
      const filtered = filterFiles(files, GLOBAL_CURRENT_FOLDER);
      populateFileSelect(filtered);
      if (filtered.length > 0)
        loadAndRender(filtered[0].json_path);
    }

    folderSel.onchange = refresh;
    fileSel.onchange = () => loadAndRender(fileSel.value);

    folderSel.value = "kingdomino_tier1";
    refresh();

  } catch (err) {
    console.error(err);
    questionCard.innerHTML = `<p style="color:#f55;">Init error: ${err}</p>`;
  }
}