fix: Update mining stats and top miners endpoints to return structured data for single and multiple turtles

This commit is contained in:
MayaTheShy
2026-02-20 01:32:35 -05:00
parent d2e0deb945
commit 207f6901d6

View File

@@ -765,7 +765,24 @@ app.get('/api/stats/mining/:turtleId?', (req, res) => {
const turtleId = req.params.turtleId ? parseInt(req.params.turtleId) : null;
const days = parseInt(req.query.days) || 7;
const stats = db.getMiningStats(turtleId, days);
res.json({ stats });
if (turtleId) {
// Single turtle: return { turtleId, blocks: [{blockType, count}] }
res.json({
turtleId,
blocks: stats.map(s => ({ blockType: s.block_type, count: s.total_count }))
});
} else {
// All turtles: group by turtle_id, return array of { turtleId, blocks: [...] }
const grouped = {};
stats.forEach(s => {
if (!grouped[s.turtle_id]) {
grouped[s.turtle_id] = { turtleId: s.turtle_id, blocks: [] };
}
grouped[s.turtle_id].blocks.push({ blockType: s.block_type, count: s.total_count });
});
res.json(Object.values(grouped));
}
} catch (error) {
res.status(500).json({ error: error.message });
}
@@ -776,7 +793,11 @@ app.get('/api/stats/top-miners', (req, res) => {
try {
const limit = parseInt(req.query.limit) || 10;
const topMiners = db.getTopMiners(limit);
res.json({ topMiners });
res.json(topMiners.map(m => ({
turtleId: m.turtle_id,
totalBlocks: m.total_blocks,
uniqueTypes: m.unique_types
})));
} catch (error) {
res.status(500).json({ error: error.message });
}
@@ -795,6 +816,18 @@ app.post('/api/stats/block-mined', (req, res) => {
// ========== TURTLE GROUPS/TEAMS ENDPOINTS ==========
// Helper to format group for API response
function formatGroup(group, members) {
return {
groupId: group.id,
groupName: group.group_name,
color: group.color,
memberCount: members.length,
members: members.map(m => ({ turtleId: m.turtle_id, joinedAt: m.joined_at })),
createdAt: group.created_at
};
}
// Create a new group
app.post('/api/groups', (req, res) => {
try {
@@ -818,12 +851,11 @@ app.post('/api/groups', (req, res) => {
app.get('/api/groups', (req, res) => {
try {
const groups = db.getAllGroups();
// Enhance with member count
const groupsWithMembers = groups.map(group => ({
...group,
members: db.getGroupMembers(group.id)
}));
res.json({ groups: groupsWithMembers });
const result = groups.map(group => {
const members = db.getGroupMembers(group.id);
return formatGroup(group, members);
});
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}