Refactor WebSocket command handling to enforce server-side control of turtle movement and remove legacy command support

This commit is contained in:
MayaTheShy
2026-02-20 03:43:45 -05:00
parent c02bb7db68
commit f6b39808aa

View File

@@ -249,20 +249,14 @@ wss.on('connection', (ws) => {
const turtle = turtles.get(turtleID);
if (turtle) {
// Check for state change commands
// All commands are state changes — server controls all turtle movement
if (data.command === 'set_state' || data.command === 'setState') {
const stateName = data.param?.state || data.param;
const stateData = data.param?.data || {};
turtle.setState(stateName, stateData);
console.log(`✅ State set for turtle ${turtleID}: ${stateName}`);
} else {
// Legacy command - queue for webbridge polling
turtle.pendingLegacyCommands.push({
command: data.command,
param: data.param,
timestamp: Date.now()
});
console.log(`✅ Command queued for turtle ${turtleID}: ${data.command}`, data.param ? `(param: ${JSON.stringify(data.param)})` : '');
console.log(`⚠️ Unrecognized command from web client: ${data.command} — use state machine or server-side action routes`);
}
} else {
console.log(`❌ Turtle ${turtleID} not found`);
@@ -524,22 +518,17 @@ app.post('/api/turtle/:id/command', (req, res) => {
const turtle = turtles.get(turtleID);
if (turtle) {
// Check for state change commands
// All commands are state changes — server controls all turtle movement
if (command === 'set_state' || command === 'setState') {
const stateName = param?.state || param;
const stateData = param?.data || {};
turtle.setState(stateName, stateData);
console.log(`📤 State set for turtle ${turtleID}: ${stateName}`);
res.json({ success: true });
} else {
// Legacy command - queue for webbridge
turtle.pendingLegacyCommands.push({
command,
param,
timestamp: Date.now()
});
console.log(`📤 Command queued for turtle ${turtleID}:`, command);
console.log(`⚠️ Legacy command rejected for turtle ${turtleID}: ${command} — use state machine or server-side action routes`);
res.status(400).json({ error: 'Legacy commands are no longer supported. Use state machine or action routes.' });
}
res.json({ success: true });
} else {
res.status(404).json({ error: 'Turtle not found' });
}