mirror of
https://github.com/ceratic/project_vollidioten_website.git
synced 2026-05-14 00:16:47 +02:00
feat: add API endpoint for player data management with insert and update functionality
This commit is contained in:
@@ -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));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user