feat: add API endpoint for player data management with insert and update functionality

This commit is contained in:
Lars Behrends
2025-12-28 23:27:40 +01:00
parent 81f1e90b85
commit abebf8f7a2

View File

@@ -1585,6 +1585,85 @@ app.post('/api/admin/cities/:cityId/gallery/upload', upload.single('gallery'), (
});
});
app.post('/api/data', (req, res) => { const { eventType, player, uuid, timestamp, char, statistics, advancements } = req.body;
if (!uuid || !player) {
return res.status(400).json({ error: 'UUID und Player-Name erforderlich' });
}
// Prepare player data
const playerData = {
uuid,
username: player,
isOnline: eventType === 'JOIN' ? 1 : 0,
isNpc: 0,
isAdmin: 0,
tags: JSON.stringify([]),
stats: JSON.stringify({
char,
statistics,
advancements,
lastSync: timestamp
}),
inventory: JSON.stringify([]),
storyMarkdown: '',
discordId: null
};
// Check if player exists
db.get("SELECT uuid FROM players WHERE uuid = ?", [uuid], (err, row) => {
if (err) {
console.error('Error checking player existence:', err);
return res.status(500).json({ error: 'Datenbankfehler beim Überprüfen des Spielers' });
}
if (row) {
// Update existing player
db.run(`UPDATE players SET username = ?, isOnline = ?, stats = ?, storyMarkdown = ? WHERE uuid = ?`,
[playerData.username, playerData.isOnline, playerData.stats, playerData.storyMarkdown, uuid],
function(err) {
if (err) {
console.error('Error updating player:', err);
return res.status(500).json({ error: 'Fehler beim Aktualisieren des Spielers' });
}
res.json({
success: true,
message: 'Spieler erfolgreich aktualisiert',
action: 'updated',
uuid
});
}
);
} else {
// Insert new player
db.run(`INSERT INTO players (uuid, username, isOnline, isNpc, isAdmin, tags, stats, inventory, storyMarkdown, discordId)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[playerData.uuid, playerData.username, playerData.isOnline, playerData.isNpc, playerData.isAdmin,
playerData.tags, playerData.stats, playerData.inventory, playerData.storyMarkdown, playerData.discordId],
function(err) {
if (err) {
console.error('Error inserting player:', err);
return res.status(500).json({ error: 'Fehler beim Erstellen des Spielers' });
}
res.json({
success: true,
message: 'Spieler erfolgreich erstellt',
action: 'created',
uuid
});
}
);
}
});
});
// Serve uploaded files statically
app.use('/uploads', express.static(UPLOAD_DIR));