feat: Implement block discovery endpoint and update worldBlocks state on discovery
This commit is contained in:
@@ -77,6 +77,15 @@ export const useTurtleStore = create((set, get) => ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
} else if (data.type === 'block_discovered') {
|
||||||
|
if (data.block) {
|
||||||
|
set(state => {
|
||||||
|
const blockMap = new Map(state.worldBlocks.map(b => [`${b.x},${b.y},${b.z}`, b]));
|
||||||
|
const key = `${data.block.x},${data.block.y},${data.block.z}`;
|
||||||
|
blockMap.set(key, data.block);
|
||||||
|
return { worldBlocks: Array.from(blockMap.values()) };
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing message:', error);
|
console.error('Error processing message:', error);
|
||||||
|
|||||||
@@ -364,6 +364,31 @@ app.get('/api/world/blocks', (req, res) => {
|
|||||||
res.json({ blocks });
|
res.json({ blocks });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Store a discovered block
|
||||||
|
app.post('/api/world/blocks', (req, res) => {
|
||||||
|
try {
|
||||||
|
const { x, y, z, blockName, metadata, discoveredBy } = req.body;
|
||||||
|
|
||||||
|
if (x === undefined || y === undefined || z === undefined || !blockName) {
|
||||||
|
return res.status(400).json({ error: 'Missing required fields: x, y, z, blockName' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const blockData = { name: blockName, metadata: metadata || 0 };
|
||||||
|
storeBlock(x, y, z, blockData, discoveredBy);
|
||||||
|
|
||||||
|
// Broadcast to web clients
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'block_discovered',
|
||||||
|
block: { x, y, z, name: blockName, metadata: metadata || 0, discoveredBy, timestamp: Date.now() }
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error storing block:', error);
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Send command to turtle
|
// Send command to turtle
|
||||||
app.post('/api/turtle/:id/command', (req, res) => {
|
app.post('/api/turtle/:id/command', (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -390,24 +415,6 @@ app.post('/api/turtle/:id/command', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Cleanup stale turtles (haven't updated in 30 seconds)
|
|
||||||
setInterval(() => {
|
|
||||||
const now = Date.now();
|
|
||||||
const timeout = 30000; // 30 seconds
|
|
||||||
|
|
||||||
for (const [turtleID, turtle] of turtleData.entries()) {
|
|
||||||
if (now - turtle.lastUpdate > timeout) {
|
|
||||||
console.log(`🗑️ Removing stale turtle: ${turtleID}`);
|
|
||||||
turtleData.delete(turtleID);
|
|
||||||
|
|
||||||
broadcastToClients({
|
|
||||||
type: 'turtle_disconnected',
|
|
||||||
turtleID
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 10000); // Check every 10 seconds
|
|
||||||
|
|
||||||
// ========== PATH RECORDING ENDPOINTS ==========
|
// ========== PATH RECORDING ENDPOINTS ==========
|
||||||
|
|
||||||
// Save a recorded path
|
// Save a recorded path
|
||||||
@@ -575,7 +582,9 @@ app.get('/api/stats', (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const stats = {
|
const stats = {
|
||||||
activeTurtles: turtleData.size,
|
activeTurtles: turtleData.size,
|
||||||
|
turtles: turtleData.size,
|
||||||
totalBlocks: worldBlocks.size,
|
totalBlocks: worldBlocks.size,
|
||||||
|
blocks: worldBlocks.size,
|
||||||
savedHomes: turtleHomes.size,
|
savedHomes: turtleHomes.size,
|
||||||
connectedClients: webClients.size,
|
connectedClients: webClients.size,
|
||||||
tasks: db.getAllTasks().length,
|
tasks: db.getAllTasks().length,
|
||||||
|
|||||||
Reference in New Issue
Block a user