feat: Enhance turtle management by broadcasting removal notifications and cleaning up stale turtles
This commit is contained in:
@@ -49,11 +49,21 @@ export const useTurtleStore = create((set, get) => ({
|
|||||||
if (data.turtle.surroundings && data.turtle.position && data.turtle.facing !== undefined) {
|
if (data.turtle.surroundings && data.turtle.position && data.turtle.facing !== undefined) {
|
||||||
get().updateBlocksFromSurroundings(data.turtle);
|
get().updateBlocksFromSurroundings(data.turtle);
|
||||||
}
|
}
|
||||||
} else if (data.type === 'turtle_disconnected') {
|
} else if (data.type === 'turtle_removed' || data.type === 'turtle_disconnected') {
|
||||||
|
console.log(`🔌 Turtle ${data.turtleID} removed`);
|
||||||
set(state => {
|
set(state => {
|
||||||
const newTurtles = { ...state.turtles };
|
const newTurtles = { ...state.turtles };
|
||||||
delete newTurtles[data.turtleID];
|
delete newTurtles[data.turtleID];
|
||||||
return { turtles: newTurtles };
|
|
||||||
|
// If the removed turtle was selected, deselect it
|
||||||
|
const newSelectedId = state.selectedTurtleId === data.turtleID
|
||||||
|
? null
|
||||||
|
: state.selectedTurtleId;
|
||||||
|
|
||||||
|
return {
|
||||||
|
turtles: newTurtles,
|
||||||
|
selectedTurtleId: newSelectedId
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -15,6 +15,43 @@ const webClients = new Set();
|
|||||||
const turtleData = new Map(); // turtleID -> turtle state
|
const turtleData = new Map(); // turtleID -> turtle state
|
||||||
const worldBlocks = new Map(); // "x,y,z" -> {name, metadata, discoveredBy, timestamp}
|
const worldBlocks = new Map(); // "x,y,z" -> {name, metadata, discoveredBy, timestamp}
|
||||||
|
|
||||||
|
// Timeout for considering turtles offline (30 seconds)
|
||||||
|
const TURTLE_TIMEOUT = 30000;
|
||||||
|
|
||||||
|
// Broadcast to all web clients
|
||||||
|
function broadcastToClients(data) {
|
||||||
|
const message = JSON.stringify(data);
|
||||||
|
webClients.forEach((client) => {
|
||||||
|
if (client.readyState === 1) { // OPEN
|
||||||
|
client.send(message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup stale turtles periodically
|
||||||
|
setInterval(() => {
|
||||||
|
const now = Date.now();
|
||||||
|
let removedCount = 0;
|
||||||
|
|
||||||
|
for (const [turtleID, turtle] of turtleData.entries()) {
|
||||||
|
if (now - turtle.lastUpdate > TURTLE_TIMEOUT) {
|
||||||
|
console.log(`🔌 Turtle ${turtleID} timed out (last seen ${Math.floor((now - turtle.lastUpdate) / 1000)}s ago)`);
|
||||||
|
turtleData.delete(turtleID);
|
||||||
|
removedCount++;
|
||||||
|
|
||||||
|
// Notify web clients
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'turtle_removed',
|
||||||
|
turtleID: turtleID
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (removedCount > 0) {
|
||||||
|
console.log(`🧹 Cleaned up ${removedCount} offline turtle(s)`);
|
||||||
|
}
|
||||||
|
}, 10000); // Check every 10 seconds
|
||||||
|
|
||||||
// Helper to store discovered blocks
|
// Helper to store discovered blocks
|
||||||
function storeBlock(x, y, z, blockData, turtleID) {
|
function storeBlock(x, y, z, blockData, turtleID) {
|
||||||
const key = `${x},${y},${z}`;
|
const key = `${x},${y},${z}`;
|
||||||
@@ -113,16 +150,6 @@ wss.on('connection', (ws) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Broadcast to all web clients
|
|
||||||
function broadcastToClients(data) {
|
|
||||||
const message = JSON.stringify(data);
|
|
||||||
webClients.forEach((client) => {
|
|
||||||
if (client.readyState === 1) { // OPEN
|
|
||||||
client.send(message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// REST API endpoint for turtles to update their status
|
// REST API endpoint for turtles to update their status
|
||||||
app.post('/api/turtle/update', (req, res) => {
|
app.post('/api/turtle/update', (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user