refactor: add color parameter to saveMiningArea and updateMiningArea functions; implement write-file and refresh-inventory endpoints for Turtle

This commit is contained in:
MayaTheShy
2026-02-20 04:20:06 -05:00
parent 12fc109a30
commit bfae87287a

View File

@@ -1073,7 +1073,7 @@ function formatMiningArea(area) {
// Save a mining area
app.post('/api/mining-areas', (req, res) => {
try {
const { turtleId, turtleID, bounds, startX, startY, startZ, endX, endY, endZ, areaName, status } = req.body;
const { turtleId, turtleID, bounds, startX, startY, startZ, endX, endY, endZ, areaName, status, color } = req.body;
const tid = turtleId || turtleID;
// Support both {turtleId, bounds} and flat {startX,startY,...} formats
@@ -1086,7 +1086,7 @@ app.post('/api/mining-areas', (req, res) => {
maxZ: Math.max(Number(startZ), Number(endZ))
};
db.saveMiningArea(tid, areaBounds, areaName || null, status || 'planned');
db.saveMiningArea(tid, areaBounds, areaName || null, status || 'planned', color || '#4a8c2a');
const areas = db.getMiningAreas();
broadcastToClients({
@@ -1110,12 +1110,21 @@ app.get('/api/mining-areas', (req, res) => {
}
});
// Update mining area status
// Update mining area
app.put('/api/mining-areas/:areaId', (req, res) => {
try {
const areaId = parseInt(req.params.areaId);
const { status } = req.body;
db.updateMiningAreaStatus(areaId, status);
const { status, name, color } = req.body;
// Build updates object with only provided fields
const updates = {};
if (status !== undefined) updates.status = status;
if (name !== undefined) updates.name = name;
if (color !== undefined) updates.color = color;
if (Object.keys(updates).length > 0) {
db.updateMiningArea(areaId, updates);
}
const areas = db.getMiningAreas();
broadcastToClients({
@@ -1790,6 +1799,36 @@ app.post('/api/turtle/:id/gps', async (req, res) => {
}
});
// Write a file to turtle filesystem
app.post('/api/turtle/:id/write-file', async (req, res) => {
try {
const turtleID = parseInt(req.params.id);
const { path, content, append } = req.body;
const turtle = turtles.get(turtleID);
if (!turtle) return res.status(404).json({ error: 'Turtle not found' });
if (!path || content === undefined) return res.status(400).json({ error: 'Missing path or content' });
const result = await turtle.writeToFile(path, content, append || false);
res.json({ success: result === true, result });
} catch (error) {
res.json({ success: false, error: error.message });
}
});
// Refresh detailed inventory state
app.post('/api/turtle/:id/refresh-inventory', async (req, res) => {
try {
const turtleID = parseInt(req.params.id);
const turtle = turtles.get(turtleID);
if (!turtle) return res.status(404).json({ error: 'Turtle not found' });
const inventory = await turtle.refreshInventoryState();
res.json({ success: true, inventory });
} catch (error) {
res.json({ success: false, error: error.message });
}
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n🛑 Shutting down server...');