From 2316e14b9c5dcd52589be043f00e9f3985b8e26b Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 03:41:42 -0500 Subject: [PATCH] style: Add server-side endpoints for turtle movement and actions --- server/server.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/server/server.js b/server/server.js index ff69532..e01e84a 100644 --- a/server/server.js +++ b/server/server.js @@ -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)