feat: Add real-time event handling and chunk analysis endpoints for turtles
This commit is contained in:
@@ -384,6 +384,28 @@ app.post('/api/turtle/:id/commands/ack', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Receive real-time events from turtles (inventory, peripherals)
|
||||||
|
app.post('/api/turtle/event', (req, res) => {
|
||||||
|
try {
|
||||||
|
const { turtleID, type, message } = req.body;
|
||||||
|
|
||||||
|
if (!turtleID || !type) {
|
||||||
|
return res.status(400).json({ error: 'Missing turtleID or type' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const turtle = turtles.get(turtleID);
|
||||||
|
if (turtle) {
|
||||||
|
turtle.handleEvent(type, message);
|
||||||
|
console.log(`📡 Event from T#${turtleID}: ${type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error processing turtle event:', error);
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Get all turtles
|
// Get all turtles
|
||||||
app.get('/api/turtles', (req, res) => {
|
app.get('/api/turtles', (req, res) => {
|
||||||
res.json({
|
res.json({
|
||||||
@@ -970,6 +992,72 @@ app.post('/api/mining-areas/:areaId/close', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ========== CHUNK ANALYSIS ENDPOINTS ==========
|
||||||
|
|
||||||
|
// Get all chunk analyses
|
||||||
|
app.get('/api/chunks', (req, res) => {
|
||||||
|
try {
|
||||||
|
const chunks = db.getAllChunkAnalyses();
|
||||||
|
res.json(chunks);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get specific chunk analysis
|
||||||
|
app.get('/api/chunks/:x/:z', (req, res) => {
|
||||||
|
try {
|
||||||
|
const x = parseInt(req.params.x);
|
||||||
|
const z = parseInt(req.params.z);
|
||||||
|
const chunk = db.getChunkAnalysis(x, z);
|
||||||
|
res.json(chunk || { x, z, analysis: {} });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Save chunk analysis
|
||||||
|
app.post('/api/chunks', (req, res) => {
|
||||||
|
try {
|
||||||
|
const { x, z, analysis } = req.body;
|
||||||
|
db.saveChunkAnalysis(x, z, analysis || {});
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Search blocks by name pattern in area
|
||||||
|
app.get('/api/world/blocks/search', (req, res) => {
|
||||||
|
try {
|
||||||
|
const { fromX, fromY, fromZ, toX, toY, toZ, pattern } = req.query;
|
||||||
|
if (!pattern) {
|
||||||
|
return res.status(400).json({ error: 'Missing pattern parameter' });
|
||||||
|
}
|
||||||
|
const blocks = db.getBlocksWithNameLike(
|
||||||
|
parseInt(fromX) || -1000, parseInt(fromY) || -64, parseInt(fromZ) || -1000,
|
||||||
|
parseInt(toX) || 1000, parseInt(toY) || 320, parseInt(toZ) || 1000,
|
||||||
|
pattern
|
||||||
|
);
|
||||||
|
res.json({ blocks });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get single block info
|
||||||
|
app.get('/api/world/block/:x/:y/:z', (req, res) => {
|
||||||
|
try {
|
||||||
|
const x = parseInt(req.params.x);
|
||||||
|
const y = parseInt(req.params.y);
|
||||||
|
const z = parseInt(req.params.z);
|
||||||
|
const block = db.getBlock(x, y, z);
|
||||||
|
res.json(block || null);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// ========== STATISTICS ENDPOINTS ==========
|
// ========== STATISTICS ENDPOINTS ==========
|
||||||
|
|
||||||
// Get server statistics
|
// Get server statistics
|
||||||
|
|||||||
Reference in New Issue
Block a user