feat: Enhance mining area API; add formatting helper, support for multiple input formats, and new endpoints for updating and deleting areas
This commit is contained in:
@@ -625,16 +625,45 @@ app.post('/api/tasks/:taskId/complete', (req, res) => {
|
|||||||
|
|
||||||
// ========== MINING AREA ENDPOINTS ==========
|
// ========== MINING AREA ENDPOINTS ==========
|
||||||
|
|
||||||
// Save a mining area claim
|
// Helper to format mining area for API response
|
||||||
|
function formatMiningArea(area) {
|
||||||
|
return {
|
||||||
|
areaID: area.id,
|
||||||
|
turtleID: area.turtle_id,
|
||||||
|
startX: area.min_x,
|
||||||
|
startY: area.min_y,
|
||||||
|
startZ: area.min_z,
|
||||||
|
endX: area.max_x,
|
||||||
|
endY: area.max_y,
|
||||||
|
endZ: area.max_z,
|
||||||
|
status: area.status,
|
||||||
|
createdAt: area.created_at,
|
||||||
|
updatedAt: area.updated_at
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save a mining area
|
||||||
app.post('/api/mining-areas', (req, res) => {
|
app.post('/api/mining-areas', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { turtleId, bounds } = req.body;
|
const { turtleId, turtleID, bounds, startX, startY, startZ, endX, endY, endZ, areaName, status } = req.body;
|
||||||
db.saveMiningArea(turtleId, bounds);
|
const tid = turtleId || turtleID;
|
||||||
|
|
||||||
|
// Support both {turtleId, bounds} and flat {startX,startY,...} formats
|
||||||
|
const areaBounds = bounds || {
|
||||||
|
minX: Math.min(Number(startX), Number(endX)),
|
||||||
|
minY: Math.min(Number(startY), Number(endY)),
|
||||||
|
minZ: Math.min(Number(startZ), Number(endZ)),
|
||||||
|
maxX: Math.max(Number(startX), Number(endX)),
|
||||||
|
maxY: Math.max(Number(startY), Number(endY)),
|
||||||
|
maxZ: Math.max(Number(startZ), Number(endZ))
|
||||||
|
};
|
||||||
|
|
||||||
|
db.saveMiningArea(tid, areaBounds, areaName || null, status || 'planned');
|
||||||
|
|
||||||
const areas = db.getMiningAreas();
|
const areas = db.getMiningAreas();
|
||||||
broadcastToClients({
|
broadcastToClients({
|
||||||
type: 'mining_areas_updated',
|
type: 'mining_areas_updated',
|
||||||
areas
|
areas: areas.map(formatMiningArea)
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
@@ -643,17 +672,54 @@ app.post('/api/mining-areas', (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get all active mining areas
|
// Get all mining areas
|
||||||
app.get('/api/mining-areas', (req, res) => {
|
app.get('/api/mining-areas', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const areas = db.getMiningAreas();
|
const areas = db.getMiningAreas();
|
||||||
res.json({ areas });
|
res.json(areas.map(formatMiningArea));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Close a mining area
|
// Update mining area status
|
||||||
|
app.put('/api/mining-areas/:areaId', (req, res) => {
|
||||||
|
try {
|
||||||
|
const areaId = parseInt(req.params.areaId);
|
||||||
|
const { status } = req.body;
|
||||||
|
db.updateMiningAreaStatus(areaId, status);
|
||||||
|
|
||||||
|
const areas = db.getMiningAreas();
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'mining_areas_updated',
|
||||||
|
areas: areas.map(formatMiningArea)
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete a mining area
|
||||||
|
app.delete('/api/mining-areas/:areaId', (req, res) => {
|
||||||
|
try {
|
||||||
|
const areaId = parseInt(req.params.areaId);
|
||||||
|
db.deleteMiningArea(areaId);
|
||||||
|
|
||||||
|
const areas = db.getMiningAreas();
|
||||||
|
broadcastToClients({
|
||||||
|
type: 'mining_areas_updated',
|
||||||
|
areas: areas.map(formatMiningArea)
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Close a mining area (legacy endpoint)
|
||||||
app.post('/api/mining-areas/:areaId/close', (req, res) => {
|
app.post('/api/mining-areas/:areaId/close', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const areaId = parseInt(req.params.areaId);
|
const areaId = parseInt(req.params.areaId);
|
||||||
@@ -662,7 +728,7 @@ app.post('/api/mining-areas/:areaId/close', (req, res) => {
|
|||||||
const areas = db.getMiningAreas();
|
const areas = db.getMiningAreas();
|
||||||
broadcastToClients({
|
broadcastToClients({
|
||||||
type: 'mining_areas_updated',
|
type: 'mining_areas_updated',
|
||||||
areas
|
areas: areas.map(formatMiningArea)
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user