Added Profile
Updated Backend
This commit is contained in:
Marces Zastrow
2025-01-08 12:07:27 +01:00
parent 1cbe8b9d94
commit 2091ce9e0f
10 changed files with 526 additions and 14 deletions
+93
View File
@@ -38,6 +38,9 @@ db.run(`CREATE TABLE IF NOT EXISTS games (
participants TEXT
)`);
//User Part
// Registration route
app.post('/register', async (req, res) => {
const { username, email, password } = req.body;
@@ -72,6 +75,8 @@ app.post('/login', (req, res) => {
});
});
// Game Part
// Fetch games for a specific user
app.get('/games/:userId', (req, res) => {
const userId = req.params.userId;
@@ -151,6 +156,94 @@ app.post('/createGame', (req, res) => {
stmt.finalize();
});
// Master Part
// 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) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(row);
});
})
// 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) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(row);
});
})
// 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' });
}
res.json(row);
});
})
// Player Part
// Fetch all Player Characters of Player
app.get('/games/:userId/characters', (req, res) => {
const userId = req.params.userId;
db.all('SELECT * FROM PlayerCharacter WHERE PlayerId = ?', [userId], (err, rows) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
// Directly return the array of characters
console.log("Characters: ", rows);
res.json(rows);
});
});
// Fetch Player Character
app.get('/games/:gameId/:playerId/character', (req, res) => {
const gameId = req.params.gameId;
const playerId = req.params.playerId;
db.get('SELECT * FROM PlayerCharacter WHERE GameID = ? AND PlayerID = ?', [gameId, playerId], (err, row) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(row);
});
})
// Fetch Player Items
app.get('/games/:charId/items', (req, res) => {
const gameId = req.params.gameId;
const charId = req.params.charId;
db.get('SELECT * FROM Item WHERE GameID = ? AND OwnerID = ?', [gameId, charId], (err, row) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
res.json(row);
});
})
// Item Part
// Set Item Owner
app.post('/games/:charId/:itemId/owner', (req, res) => {
const gameId = req.params.gameId;
const itemId = req.params.itemId;
const ownerId = req.body.ownerId;
db.run('UPDATE Item SET OwnerID = ? WHERE ItemID = ?', [ownerId, gameId, itemId], function (err) {
if (err) {
return res.status(400).json({ error: 'Failed to update item owner.' });
}
res.json({ message: 'Item owner updated successfully!' });
});
})
// Start the server
app.listen(port, () => {