Added functionallity to start a new Game
This commit is contained in:
Marces Zastrow
2024-12-19 11:08:22 +01:00
parent 6c6feb0185
commit 5a46333bfd
5 changed files with 163 additions and 4 deletions
Binary file not shown.
+17 -2
View File
@@ -31,8 +31,7 @@ db.run(`CREATE TABLE IF NOT EXISTS users (
// Create games table if it doesn't exist
db.run(`CREATE TABLE IF NOT EXISTS games (
id INTEGER PRIMARY KEY AUTOINCREMENT,
game_id TEXT,
game_id TEXT PRIMARY KEY,
name TEXT,
description TEXT,
game_master_id INTEGER,
@@ -88,6 +87,22 @@ app.get('/games/:userId', (req, res) => {
);
});
//Join Game
app.post('/joinGame/:gameId', (req, res) => {
const gameId = req.params.gameId;
const userId = req.body.userId;
const stmt = db.prepare('INSERT INTO games (participants) VALUES (?) WHERE game_id IS (?)');
stmt.run([gameId, userId], function(err) {
if (err) {
return res.status(400).json({ error: 'Failed to create game.' });
}
res.status(201).json({message: "User joined Game!", gameId: this.gameId})
})
stmt.finalize();
});
// Create a new game
app.post('/games', (req, res) => {
const { name, description, gameMasterId, participants } = req.body;