Spaces:
Running
Running
Add 24h local session (auto-save/restore answers) to both assignments
Browse files- assignment1.html +24 -0
- assignment2.html +72 -5
assignment1.html
CHANGED
|
@@ -277,6 +277,30 @@ problemSel.onchange = async () => {
|
|
| 277 |
|
| 278 |
applyMode();
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
// ===== Grader Web Worker =====
|
| 281 |
// The answer key (oracle / bug definitions / reasons) lives in a SEPARATE Pyodide
|
| 282 |
// running inside grader.worker.js, on its own thread with its own memory. Student
|
|
|
|
| 277 |
|
| 278 |
applyMode();
|
| 279 |
|
| 280 |
+
// ===== 24h local session: keep the code you typed across reloads =====
|
| 281 |
+
const A1_KEY = "st_a1_session_v1", A1_TTL = 24 * 3600 * 1000;
|
| 282 |
+
let __a1timer = null;
|
| 283 |
+
function persistA1(){
|
| 284 |
+
clearTimeout(__a1timer);
|
| 285 |
+
__a1timer = setTimeout(() => {
|
| 286 |
+
try { saveEditor(); localStorage.setItem(A1_KEY, JSON.stringify({ ts: Date.now(), mode, prob, work })); } catch(_){}
|
| 287 |
+
}, 250);
|
| 288 |
+
}
|
| 289 |
+
function restoreA1(){
|
| 290 |
+
let d; try { d = JSON.parse(localStorage.getItem(A1_KEY)); } catch(_){ return; }
|
| 291 |
+
if (!d || !d.ts || (Date.now() - d.ts) > A1_TTL){ localStorage.removeItem(A1_KEY); return; }
|
| 292 |
+
if (d.work){
|
| 293 |
+
if (typeof d.work.run === "string") work.run = d.work.run;
|
| 294 |
+
if (d.work.test){ if (d.work.test.Q1 != null) work.test.Q1 = d.work.test.Q1; if (d.work.test.Q2 != null) work.test.Q2 = d.work.test.Q2; }
|
| 295 |
+
}
|
| 296 |
+
if (d.mode) mode = d.mode;
|
| 297 |
+
if (d.prob){ prob = d.prob; problemSel.value = prob; }
|
| 298 |
+
}
|
| 299 |
+
restoreA1();
|
| 300 |
+
applyMode();
|
| 301 |
+
loadEditor();
|
| 302 |
+
editor.on("change", persistA1);
|
| 303 |
+
|
| 304 |
// ===== Grader Web Worker =====
|
| 305 |
// The answer key (oracle / bug definitions / reasons) lives in a SEPARATE Pyodide
|
| 306 |
// running inside grader.worker.js, on its own thread with its own memory. Student
|
assignment2.html
CHANGED
|
@@ -165,6 +165,7 @@
|
|
| 165 |
<div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div>
|
| 166 |
<div class="spacer"></div>
|
| 167 |
<span class="toast" id="toast"></span>
|
|
|
|
| 168 |
<button class="ghost" id="copyBtn">Copy JSON</button>
|
| 169 |
<button id="exportBtn">⤓ Export JSON</button>
|
| 170 |
</div>
|
|
@@ -228,12 +229,12 @@ function buildChoice(host, name, items, onSel){
|
|
| 228 |
}
|
| 229 |
buildChoice(document.getElementById("q1opts"), "q1",
|
| 230 |
[1,2,3,4,5,6].map(n=>({val:String(n), label:"Option "+n, body:dtTable(Q1[n])})),
|
| 231 |
-
v => answers.q1 = v);
|
| 232 |
|
| 233 |
// ============ Q2: four CFG images ============
|
| 234 |
buildChoice(document.getElementById("q2opts"), "q2",
|
| 235 |
[1,2,3,4].map(n=>({val:String(n), label:"Option "+n, body:`<img src="a2/assets/img${n-1}.png" alt="CFG option ${n}">`})),
|
| 236 |
-
v => answers.q2 = v);
|
| 237 |
|
| 238 |
// ============ Q3: fill-in trace with 12 blanks ============
|
| 239 |
const Q3_TEXT =
|
|
@@ -267,7 +268,7 @@ Test 3 loop runs 2 times
|
|
| 267 |
const inp = document.createElement("input");
|
| 268 |
inp.className = "blank"; inp.dataset.n = n; inp.title = "blank "+n;
|
| 269 |
inp.placeholder = n;
|
| 270 |
-
inp.oninput = () => answers.q3[n] = inp.value;
|
| 271 |
host.appendChild(inp);
|
| 272 |
}
|
| 273 |
}
|
|
@@ -290,7 +291,7 @@ return result`;
|
|
| 290 |
buildChoice(document.getElementById("q5opts"), "q5",
|
| 291 |
[["a","(a) 2729.72"],["b","(b) 2827.21"],["c","(c) 2882.64"],["d","(d) 2938.08"]]
|
| 292 |
.map(([v,l])=>({val:v, label:l, body:""})),
|
| 293 |
-
v => answers.q5 = v);
|
| 294 |
|
| 295 |
// ============ Q4 playground (Pyodide) ============
|
| 296 |
const q4status = document.getElementById("q4status");
|
|
@@ -306,6 +307,7 @@ const editor = CodeMirror.fromTextArea(document.getElementById("q4code"), {
|
|
| 306 |
});
|
| 307 |
editor.setValue(REF_SRC);
|
| 308 |
document.getElementById("q4reset").onclick = () => editor.setValue(REF_SRC);
|
|
|
|
| 309 |
|
| 310 |
function q4append(t, cls){ const s=document.createElement("span"); if(cls)s.className=cls; s.textContent=t; q4out.appendChild(s); }
|
| 311 |
|
|
@@ -385,7 +387,7 @@ async function runQ4(){
|
|
| 385 |
else q4append(" ✗ Same output on both — this test does NOT distinguish them yet.\n","warn");
|
| 386 |
}
|
| 387 |
} catch(e){ q4append("Runtime error: "+e+"\n","err"); }
|
| 388 |
-
finally { q4run.disabled = false; }
|
| 389 |
}
|
| 390 |
q4run.onclick = runQ4;
|
| 391 |
(async () => {
|
|
@@ -430,6 +432,71 @@ document.getElementById("copyBtn").onclick = async () => {
|
|
| 430 |
try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied ✓"); }
|
| 431 |
catch(_){ toast("Copy failed"); }
|
| 432 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
</script>
|
| 434 |
</body>
|
| 435 |
</html>
|
|
|
|
| 165 |
<div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div>
|
| 166 |
<div class="spacer"></div>
|
| 167 |
<span class="toast" id="toast"></span>
|
| 168 |
+
<button class="ghost" id="clearBtn">Clear saved</button>
|
| 169 |
<button class="ghost" id="copyBtn">Copy JSON</button>
|
| 170 |
<button id="exportBtn">⤓ Export JSON</button>
|
| 171 |
</div>
|
|
|
|
| 229 |
}
|
| 230 |
buildChoice(document.getElementById("q1opts"), "q1",
|
| 231 |
[1,2,3,4,5,6].map(n=>({val:String(n), label:"Option "+n, body:dtTable(Q1[n])})),
|
| 232 |
+
v => { answers.q1 = v; saveSession(); });
|
| 233 |
|
| 234 |
// ============ Q2: four CFG images ============
|
| 235 |
buildChoice(document.getElementById("q2opts"), "q2",
|
| 236 |
[1,2,3,4].map(n=>({val:String(n), label:"Option "+n, body:`<img src="a2/assets/img${n-1}.png" alt="CFG option ${n}">`})),
|
| 237 |
+
v => { answers.q2 = v; saveSession(); });
|
| 238 |
|
| 239 |
// ============ Q3: fill-in trace with 12 blanks ============
|
| 240 |
const Q3_TEXT =
|
|
|
|
| 268 |
const inp = document.createElement("input");
|
| 269 |
inp.className = "blank"; inp.dataset.n = n; inp.title = "blank "+n;
|
| 270 |
inp.placeholder = n;
|
| 271 |
+
inp.oninput = () => { answers.q3[n] = inp.value; saveSession(); };
|
| 272 |
host.appendChild(inp);
|
| 273 |
}
|
| 274 |
}
|
|
|
|
| 291 |
buildChoice(document.getElementById("q5opts"), "q5",
|
| 292 |
[["a","(a) 2729.72"],["b","(b) 2827.21"],["c","(c) 2882.64"],["d","(d) 2938.08"]]
|
| 293 |
.map(([v,l])=>({val:v, label:l, body:""})),
|
| 294 |
+
v => { answers.q5 = v; saveSession(); });
|
| 295 |
|
| 296 |
// ============ Q4 playground (Pyodide) ============
|
| 297 |
const q4status = document.getElementById("q4status");
|
|
|
|
| 307 |
});
|
| 308 |
editor.setValue(REF_SRC);
|
| 309 |
document.getElementById("q4reset").onclick = () => editor.setValue(REF_SRC);
|
| 310 |
+
editor.on("change", () => saveSession());
|
| 311 |
|
| 312 |
function q4append(t, cls){ const s=document.createElement("span"); if(cls)s.className=cls; s.textContent=t; q4out.appendChild(s); }
|
| 313 |
|
|
|
|
| 387 |
else q4append(" ✗ Same output on both — this test does NOT distinguish them yet.\n","warn");
|
| 388 |
}
|
| 389 |
} catch(e){ q4append("Runtime error: "+e+"\n","err"); }
|
| 390 |
+
finally { q4run.disabled = false; saveSession(); }
|
| 391 |
}
|
| 392 |
q4run.onclick = runQ4;
|
| 393 |
(async () => {
|
|
|
|
| 432 |
try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied ✓"); }
|
| 433 |
catch(_){ toast("Copy failed"); }
|
| 434 |
};
|
| 435 |
+
|
| 436 |
+
// ============ 24h local session (auto-save / restore) ============
|
| 437 |
+
const SESSION_KEY = "st_a2_session_v1";
|
| 438 |
+
const SESSION_TTL = 24 * 3600 * 1000; // 24 hours
|
| 439 |
+
let __saveTimer = null;
|
| 440 |
+
function saveSession(){
|
| 441 |
+
// debounce rapid edits (typing in the editor / blanks)
|
| 442 |
+
clearTimeout(__saveTimer);
|
| 443 |
+
__saveTimer = setTimeout(() => {
|
| 444 |
+
try {
|
| 445 |
+
const d = {
|
| 446 |
+
ts: Date.now(),
|
| 447 |
+
studentId: document.getElementById("studentId").value,
|
| 448 |
+
q1: answers.q1, q2: answers.q2, q5: answers.q5,
|
| 449 |
+
q3: answers.q3,
|
| 450 |
+
q4: {
|
| 451 |
+
mutation: document.getElementById("q4mutation").value,
|
| 452 |
+
code: editor.getValue(),
|
| 453 |
+
target: q4target.value, key: q4key.value,
|
| 454 |
+
original_output: lastQ4.original_output, mutant_output: lastQ4.mutant_output
|
| 455 |
+
}
|
| 456 |
+
};
|
| 457 |
+
localStorage.setItem(SESSION_KEY, JSON.stringify(d));
|
| 458 |
+
} catch(_){}
|
| 459 |
+
}, 250);
|
| 460 |
+
}
|
| 461 |
+
function restoreRadio(name, val){
|
| 462 |
+
if (!val) return;
|
| 463 |
+
const r = document.querySelector(`input[name="${name}"][value="${val}"]`);
|
| 464 |
+
if (r){ r.checked = true; const opt = r.closest(".opt"); if (opt) opt.classList.add("sel"); answers[name] = val; }
|
| 465 |
+
}
|
| 466 |
+
function loadSession(){
|
| 467 |
+
let d;
|
| 468 |
+
try { d = JSON.parse(localStorage.getItem(SESSION_KEY)); } catch(_){ return; }
|
| 469 |
+
if (!d || !d.ts || (Date.now() - d.ts) > SESSION_TTL){ localStorage.removeItem(SESSION_KEY); return; }
|
| 470 |
+
document.getElementById("studentId").value = d.studentId || "";
|
| 471 |
+
restoreRadio("q1", d.q1); restoreRadio("q2", d.q2); restoreRadio("q5", d.q5);
|
| 472 |
+
if (d.q3){ answers.q3 = Object.assign({}, d.q3);
|
| 473 |
+
document.querySelectorAll("input.blank").forEach(inp => { const v = d.q3[inp.dataset.n]; if (v != null) inp.value = v; }); }
|
| 474 |
+
if (d.q4){
|
| 475 |
+
document.getElementById("q4mutation").value = d.q4.mutation || "";
|
| 476 |
+
if (d.q4.code) editor.setValue(d.q4.code);
|
| 477 |
+
q4target.value = d.q4.target || ""; q4key.value = d.q4.key || "";
|
| 478 |
+
lastQ4 = { original_output: d.q4.original_output || "", mutant_output: d.q4.mutant_output || "" };
|
| 479 |
+
}
|
| 480 |
+
// banner
|
| 481 |
+
const mins = Math.round((Date.now() - d.ts) / 60000);
|
| 482 |
+
const ago = mins < 60 ? mins + " min ago" : Math.round(mins/60) + " h ago";
|
| 483 |
+
const b = document.createElement("div");
|
| 484 |
+
b.style.cssText = "margin:10px 0;padding:8px 12px;border:1px solid var(--border);border-radius:8px;background:#262a3f;font-size:12px;color:var(--muted)";
|
| 485 |
+
b.innerHTML = `↺ Restored your answers from this browser (saved ${ago}). They are kept for 24 hours. `;
|
| 486 |
+
const x = document.createElement("button"); x.textContent = "Clear & start fresh"; x.className = "ghost";
|
| 487 |
+
x.style.cssText = "padding:3px 10px;font-size:11px;margin-left:6px";
|
| 488 |
+
x.onclick = clearSession;
|
| 489 |
+
b.appendChild(x);
|
| 490 |
+
const wrap = document.querySelector(".wrap");
|
| 491 |
+
wrap.insertBefore(b, wrap.children[2]);
|
| 492 |
+
}
|
| 493 |
+
function clearSession(){ localStorage.removeItem(SESSION_KEY); location.reload(); }
|
| 494 |
+
// save on the free-text fields too
|
| 495 |
+
["studentId","q4mutation"].forEach(id => document.getElementById(id).addEventListener("input", saveSession));
|
| 496 |
+
[q4target, q4key].forEach(el => el.addEventListener("input", saveSession));
|
| 497 |
+
document.getElementById("clearBtn").onclick = clearSession;
|
| 498 |
+
// restore once everything above is defined
|
| 499 |
+
loadSession();
|
| 500 |
</script>
|
| 501 |
</body>
|
| 502 |
</html>
|