This commit is contained in:
Marces Zastrow
2025-01-17 08:10:15 +01:00
parent d0dd69e9b5
commit a3bf8064d9
7 changed files with 681 additions and 10 deletions
+70 -9
View File
@@ -95,6 +95,21 @@ app.get('/games/:userId', (req, res) => {
});
// Fetch game details including game master
app.get('/games/:gameId/master', (req, res) => {
const gameId = req.params.gameId;
db.get('SELECT * FROM games WHERE game_id = ?', [gameId], (err, row) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
if (!row) {
return res.status(404).json({ error: 'Game not found' });
}
// Convert game_master_id to number for consistent comparison
res.json(row);
});
});
// Join Game
app.post('/joinGame/:gameId', (req, res) => {
const gameId = req.params.gameId;
@@ -162,18 +177,25 @@ app.post('/createGame', (req, res) => {
// Fetch game Participant Characters
app.get('/games/:gameId/playerchars', (req, res) => {
const gameId = req.params.gameId;
db.get('SELECT * FROM PlayerCharacter WHERE GameID = ?', [gameId], (err, row) => {
db.all('SELECT * FROM PlayerCharacter WHERE GameID = ?', [gameId], (err, rows) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(row);
// Process each row and convert BLOB to base64 if image exists
const processedRows = rows.map(row => {
if (row.Img) {
row.Img = `data:image/jpeg;base64,${row.Img.toString('base64')}`;
}
return row;
});
res.json(processedRows);
});
})
});
// Fetch game NPCs
app.get('/games/:gameId/npcs', (req, res) => {
const gameId = req.params.gameId;
db.get('SELECT * FROM NPC WHERE GameID = ?', [gameId], (err, row) => {
db.all('SELECT * FROM NPC WHERE GameID = ?', [gameId], (err, row) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
@@ -184,12 +206,19 @@ app.get('/games/:gameId/npcs', (req, res) => {
// Fetch game Items
app.get('/games/:gameId/items', (req, res) => {
const gameId = req.params.gameId;
db.get('SELECT * FROM Item WHERE GameID = ?', [gameId], (err, row) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
db.all(`
SELECT Item.*, PlayerCharacter.CharName as OwnerName
FROM Item
LEFT JOIN PlayerCharacter ON Item.OwnerID = PlayerCharacter.CharID
WHERE Item.GameID = ?`,
[gameId],
(err, rows) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(rows);
}
res.json(row);
});
);
})
@@ -324,6 +353,38 @@ app.get('/games/:gameId/:charId/items', (req, res) => {
});
// Item Part
// Create Item
app.post('/games/item/create', upload.single('image'), (req, res) => {
const {
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
GoldValue, Abilities, GameID, AP
} = req.body;
const imageBuffer = req.file ? req.file.buffer : null;
const stmt = db.prepare(`
INSERT INTO Item (
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
GoldValue, Abilities, GameID, OwnerID, img, AP
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?)
`);
stmt.run([
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
GoldValue, Abilities, GameID, imageBuffer, AP
], function(err) {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
res.status(201).json({
message: 'Item created successfully!',
itemId: this.lastID
});
});
stmt.finalize();
});
// Set Item Owner
app.post('/games/:charId/:itemId/owner', (req, res) => {
const gameId = req.params.gameId;