Added Websocket stuff

This commit is contained in:
2026-05-02 18:26:02 -04:00
parent 7f49abd5e1
commit f34125d67b
10 changed files with 140 additions and 102 deletions

View File

@@ -1 +0,0 @@
{"type": "two_sides", "problem": "-4*x - 3 = x + 32", "solution": -7, "steps": [{"before": "-4*x - 3 = x + 32", "after": "-5*x - 3 = 32", "step": "Subtract x from both sides", "rule": "Subtraction Property of Equality"}, {"before": "-5*x - 3 = 32", "after": "-5*x = 35", "step": "Add 3 to both sides", "rule": "Addition Property of Equality"}, {"before": "-5*x = 35", "after": "x = -7", "step": "Divide both sides by -5", "rule": "Division Property of Equality"}]}

View File

@@ -2,7 +2,7 @@
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="style/styles.css">
</head>
<body>
<canvas id="waveCanvas"></canvas>
@@ -10,7 +10,8 @@
<div id="problem">Loading...</div>
<div id="steps"></div>
<script src="background.js"></script>
<script src="problem.js"></script>
<script src="scripts/background.js"></script>
<script src="scripts/problem.js"></script>
<script src="scripts/web_sockets.js"></script>
</body>
</html>

View File

@@ -1,79 +0,0 @@
let state = "problem"; // problem | steps | done
let stepIndex = 0;
let currentData = null;
// Load the data to draw
async function loadData() {
const res = await fetch("data/data.json");
currentData = await res.json();
state = "problem";
stepIndex = 0;
render();
}
function render() {
if (!currentData) return;
const problemEl = document.getElementById("problem");
const stepsEl = document.getElementById("steps");
if (state === "problem") {
problemEl.innerHTML = `\\(${currentData.problem}\\)`;
stepsEl.innerHTML = "";
}
if (state === "step") {
problemEl.innerHTML = `\\(${currentData.problem}\\)`;
let html = "";
for (let i = 0; i <= stepIndex; i++) {
const s = currentData.steps[i];
html += `
<div class="step">
<div>\\(${s.before}\\)</div>
<div>${s.step}</div>
<div>\\(${s.after}\\)</div>
</div>
<hr/>
`;
}
stepsEl.innerHTML = html;
}
MathJax.typesetPromise();
}
function startSequence() {
// 1. show problem
state = "problem";
render();
setTimeout(() => {
state = "step";
stepIndex = 0;
render();
let interval = setInterval(() => {
stepIndex++;
render();
if (stepIndex >= currentData.steps.length - 1) {
clearInterval(interval);
setTimeout(() => {
loadData(); // next problem
}, 3000);
}
}, 2000); // time between steps
}, 4000); // thinking time before reveal
}
loadData().then(() => {
startSequence();
});

50
www/scripts/problem.js Normal file
View File

@@ -0,0 +1,50 @@
let state = "problem"; // problem | steps | done
function showProblem() {
const problemEl = document.getElementById("problem");
const stepsEl = document.getElementById("steps");
problemEl.innerHTML = `\\(${problemData.problem}\\)`;
stepsEl.innerHTML = "";
MathJax.typesetPromise();
}
function startSequence() {
setTimeout(() => {
showNextStep();
}, 3000); // thinking time
}
function showNextStep() {
const stepsEl = document.getElementById("steps");
if (stepIndex >= problemData.steps.length) {
// finished → request next problem
setTimeout(() => {
requestNextProblem();
}, 3000);
return;
}
const s = problemData.steps[stepIndex];
const html = `
<div class="step">
<div>\\(${s.before}\\)</div>
<div>${s.step}</div>
<div>\\(${s.after}\\)</div>
</div>
<hr/>
`;
stepsEl.innerHTML += html;
MathJax.typesetPromise();
stepIndex++;
setTimeout(showNextStep, 2000); // time between steps
}

View File

@@ -0,0 +1,31 @@
const socket = new WebSocket("ws://localhost:8000/ws");
let problemData = null;
let stepIndex = 0;
socket.onopen = () => {
console.log("Connected to server");
socket.send(JSON.stringify({
type: "query_problem"
}));
};
socket.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "generated_problem") {
problemData = msg.data;
stepIndex = 0;
showProblem();
// start animation sequence
startSequence();
}
};
function requestNextProblem() {
socket.send(JSON.stringify({
type: "query_problem"
}));
}