This commit is contained in:
Christopher Cookman 2026-01-25 15:30:15 -07:00
parent 9f01d52b31
commit a0bef1245c

View file

@ -3,22 +3,34 @@ const router = express.Router();
const db = global.db;
router.post('/', global.auth, async (req, res) => {
const { serverId, totalPlayers, duration } = req.body;
const { serverId, totalPlayers, duration } = req.body;
if (!serverId || totalPlayers === undefined || duration === undefined) {
return res.status(400).json({ error: 'Missing required fields' });
}
const nextHeartbeat = Date.now() + ((parseInt(process.env.MAX_HEARTBEAT, 10) || 60) * 1000);
db.run(
'UPDATE analytics SET serverDuration = ?, allPlayers = ?, heartbeatCheck = ? WHERE id = ?',
[duration, JSON.stringify(totalPlayers), serverId, nextHeartbeat / 1000],
function(err) {
if (err) {
console.error('Failed to record heartbeat data', err);
return res.status(500).json({ error: 'Database error' });
}
return res.status(200).json({ message: 'Heartbeat data recorded' });
db.get('SELECT * FROM analytics WHERE id = ?', [serverId], (err, row) => {
if (err) {
console.error('Failed to find server for heartbeat', err);
return res.status(500).json({ error: 'Database error' });
}
);
if (!row) {
return res.status(404).json({ error: 'Server not found' });
}
console.log(`Recording heartbeat: ${serverId} with ${JSON.stringify(totalPlayers)} players and duration ${duration}`);
const nextHeartbeat = Date.now() + ((parseInt(process.env.MAX_HEARTBEAT, 10) || 60) * 1000);
db.run(
'UPDATE analytics SET serverDuration = ?, allPlayers = ?, heartbeatCheck = ? WHERE id = ?',
[duration, JSON.stringify(totalPlayers), serverId, nextHeartbeat / 1000],
function (err) {
if (err) {
console.error('Failed to record heartbeat data', err);
return res.status(500).json({ error: 'Database error' });
}
return res.status(200).json({ message: 'Heartbeat data recorded' });
}
);
})
});
module.exports = router;