style: Add server-side endpoints for turtle movement and actions

This commit is contained in:
MayaTheShy
2026-02-20 03:41:42 -05:00
parent 45a4b4e7ae
commit 2316e14b9c

View File

@@ -549,6 +549,46 @@ app.post('/api/turtle/:id/command', (req, res) => {
}
});
// ========== SERVER-SIDE MOVEMENT & ACTION ENDPOINTS ==========
// Generic helper for turtle action routes
function turtleAction(routePath, actionFn) {
app.post(`/api/turtle/:id/${routePath}`, async (req, res) => {
try {
const turtleID = parseInt(req.params.id);
const turtle = turtles.get(turtleID);
if (!turtle) return res.status(404).json({ error: 'Turtle not found' });
const result = await actionFn(turtle, req.body);
res.json({ success: true, result });
} catch (error) {
console.error(`❌ Error in /${routePath} for turtle ${req.params.id}:`, error.message);
res.json({ success: false, error: error.message });
}
});
}
// Movement
turtleAction('forward', (t) => t.forward());
turtleAction('back', (t) => t.back());
turtleAction('up', (t) => t.up());
turtleAction('down', (t) => t.down());
turtleAction('turnLeft', (t) => t.turnLeft());
turtleAction('turnRight', (t) => t.turnRight());
// Digging
turtleAction('dig', (t) => t.dig());
turtleAction('digUp', (t) => t.digUp());
turtleAction('digDown', (t) => t.digDown());
// Placing
turtleAction('place', (t, body) => t.place(body?.text));
turtleAction('placeUp', (t, body) => t.placeUp(body?.text));
turtleAction('placeDown', (t, body) => t.placeDown(body?.text));
// Refuel (via server)
turtleAction('refuel-action', (t, body) => t.refuel(body?.count));
// ========== EVAL PROTOCOL ENDPOINTS ==========
// Receive eval response from webbridge (turtle -> webbridge -> server)