mirror of
https://github.com/ceratic/project_vollidioten_website.git
synced 2026-05-14 00:16:47 +02:00
feat: enhance player model with organizationId and update related API handling
This commit is contained in:
@@ -22,10 +22,11 @@ const SEED_PLAYERS = [
|
||||
isNpc: 0,
|
||||
isAdmin: 0,
|
||||
tags: JSON.stringify(['#Bürger', '#Händler']),
|
||||
stats: JSON.stringify({ playtimeHours: 482, level: 45, role: 'Bürger', organizationId: 'org-3' }),
|
||||
stats: JSON.stringify({ playtimeHours: 482, level: 45, role: 'Bürger' }),
|
||||
minecraftStats: null,
|
||||
inventory: JSON.stringify([]),
|
||||
storyMarkdown: '# Der Bauplan von V\n\n> "Stein erinnert sich..."'
|
||||
storyMarkdown: '# Der Bauplan von V\n\n> "Stein erinnert sich..."',
|
||||
organizationId: 'org-3'
|
||||
},
|
||||
{
|
||||
uuid: '8984c0b5-d912-4462-b189-c864fba4a1af',
|
||||
@@ -34,10 +35,11 @@ const SEED_PLAYERS = [
|
||||
isNpc: 0,
|
||||
isAdmin: 1, // DrKButz is admin for testing
|
||||
tags: JSON.stringify(['#Bauunternehmer']),
|
||||
stats: JSON.stringify({ playtimeHours: 120, level: 12, role: 'Unternehmer', organizationId: 'org-4' }),
|
||||
stats: JSON.stringify({ playtimeHours: 120, level: 12, role: 'Unternehmer' }),
|
||||
minecraftStats: null,
|
||||
inventory: JSON.stringify([]),
|
||||
storyMarkdown: '# Forschungslogbuch:\n\nSpezialisiert auf...'
|
||||
storyMarkdown: '# Forschungslogbuch:\n\nSpezialisiert auf...',
|
||||
organizationId: 'org-4'
|
||||
}
|
||||
];
|
||||
|
||||
@@ -122,7 +124,8 @@ function setupTables() {
|
||||
minecraftStats JSON,
|
||||
inventory JSON,
|
||||
storyMarkdown TEXT,
|
||||
discordId VARCHAR(255)
|
||||
discordId VARCHAR(255),
|
||||
organizationId VARCHAR(50)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS orgs (
|
||||
id VARCHAR(50) PRIMARY KEY,
|
||||
@@ -184,8 +187,8 @@ function seedData() {
|
||||
if (!err && rows[0].count === 0) {
|
||||
console.log("Seeding Players...");
|
||||
SEED_PLAYERS.forEach(p => {
|
||||
pool.query("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[p.uuid, p.username, p.isOnline, p.isNpc, p.isAdmin, p.tags, p.stats, p.inventory, p.storyMarkdown, null]);
|
||||
pool.query("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
[p.uuid, p.username, p.isOnline, p.isNpc, p.isAdmin, p.tags, p.stats, p.minecraftStats, p.inventory, p.storyMarkdown, null, p.organizationId]);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -153,11 +153,11 @@ app.get('/api/players', (req, res) => {
|
||||
const parsed = rows.map(r => ({
|
||||
...r,
|
||||
tags: JSON.parse(r.tags || '[]'),
|
||||
stats: JSON.parse(r.minecraftStats || '{}'),
|
||||
//stats: JSON.parse(r.stats || '{}'),
|
||||
//minecraftStats: r.minecraftStats ? JSON.parse(r.minecraftStats) : undefined,
|
||||
stats: JSON.parse(r.stats || '{}'),
|
||||
minecraftStats: r.minecraftStats ? JSON.parse(r.minecraftStats) : undefined,
|
||||
inventory: JSON.parse(r.inventory || '[]'),
|
||||
isOnline: !!r.isOnline
|
||||
isOnline: !!r.isOnline,
|
||||
organizationId: r.organizationId || undefined
|
||||
}));
|
||||
res.json(parsed);
|
||||
});
|
||||
@@ -189,60 +189,11 @@ app.put('/api/players/:uuid', (req, res) => {
|
||||
}
|
||||
|
||||
const updates = req.body;
|
||||
const allowedFields = ['storyMarkdown', 'tags'];
|
||||
const allowedFields = ['storyMarkdown', 'tags', 'organizationId'];
|
||||
const updateFields = [];
|
||||
const values = [];
|
||||
|
||||
// Handle organizationId specially - it goes into stats JSON
|
||||
if (updates.organizationId !== undefined) {
|
||||
// First get current player data
|
||||
db.get("SELECT stats FROM players WHERE uuid = ?", [req.params.uuid], (err, row) => {
|
||||
if (err) return res.status(500).json({error: err.message});
|
||||
if (!row) return res.status(404).json({error: 'Spieler nicht gefunden'});
|
||||
|
||||
try {
|
||||
const currentStats = JSON.parse(row.stats || '{}');
|
||||
const updatedStats = { ...currentStats, organizationId: updates.organizationId };
|
||||
|
||||
updateFields.push('stats = ?');
|
||||
values.push(JSON.stringify(updatedStats));
|
||||
} catch (e) {
|
||||
return res.status(500).json({error: 'Fehler beim Verarbeiten der Stats'});
|
||||
}
|
||||
|
||||
// Continue with other fields
|
||||
for (const field of allowedFields) {
|
||||
if (updates[field] !== undefined) {
|
||||
if (field === 'tags') {
|
||||
// Tags are stored as JSON
|
||||
updateFields.push(`${field} = ?`);
|
||||
values.push(JSON.stringify(updates[field]));
|
||||
} else {
|
||||
updateFields.push(`${field} = ?`);
|
||||
values.push(updates[field]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (updateFields.length === 0) {
|
||||
return res.status(400).json({error: 'Keine gültigen Felder zum Aktualisieren'});
|
||||
}
|
||||
|
||||
const query = `UPDATE players SET ${updateFields.join(', ')} WHERE uuid = ?`;
|
||||
values.push(req.params.uuid);
|
||||
|
||||
db.run(query, values, function(err) {
|
||||
if (err) {
|
||||
console.error('Error updating player:', err);
|
||||
return res.status(500).json({error: 'Fehler beim Aktualisieren'});
|
||||
}
|
||||
res.json({success: true, message: 'Spieler erfolgreich aktualisiert'});
|
||||
});
|
||||
});
|
||||
return; // Exit early since we're handling this asynchronously
|
||||
}
|
||||
|
||||
// Handle other fields normally
|
||||
// Handle fields
|
||||
for (const field of allowedFields) {
|
||||
if (updates[field] !== undefined) {
|
||||
if (field === 'tags') {
|
||||
|
||||
Reference in New Issue
Block a user