From cb666a6a459fffc39650fe9b1b6e407f81937815 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 04:24:15 -0500 Subject: [PATCH] refactor: synchronize turtle facing state with global variable on turn commands --- server/Turtle.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/Turtle.js b/server/Turtle.js index 6b7a798..e0ee7da 100644 --- a/server/Turtle.js +++ b/server/Turtle.js @@ -406,15 +406,15 @@ export class Turtle extends EventEmitter { const turn = (direction - this._facing + 4) % 4; if (turn === 1) { - await this.exec('return turtle.turnRight()'); + await this.exec(`turtle.turnRight(); _G._turtleFacing = ${direction}`); this._facing = direction; this._emitUpdate(); } else if (turn === 2) { - await this.exec('turtle.turnLeft(); return turtle.turnLeft()'); + await this.exec(`turtle.turnLeft(); turtle.turnLeft(); _G._turtleFacing = ${direction}`); this._facing = direction; this._emitUpdate(); } else if (turn === 3) { - await this.exec('return turtle.turnLeft()'); + await this.exec(`turtle.turnLeft(); _G._turtleFacing = ${direction}`); this._facing = direction; this._emitUpdate(); } @@ -427,6 +427,8 @@ export class Turtle extends EventEmitter { const result = await this.exec('return turtle.turnLeft()'); if (result === true || (Array.isArray(result) && result[0] === true)) { this._facing = (this._facing + 3) % 4; + // Sync facing on turtle side + this.exec(`_G._turtleFacing = ${this._facing}`).catch(() => {}); this._emitUpdate(); } return result; @@ -439,6 +441,8 @@ export class Turtle extends EventEmitter { const result = await this.exec('return turtle.turnRight()'); if (result === true || (Array.isArray(result) && result[0] === true)) { this._facing = (this._facing + 1) % 4; + // Sync facing on turtle side + this.exec(`_G._turtleFacing = ${this._facing}`).catch(() => {}); this._emitUpdate(); } return result;