- Can now join games
- Can continue games using table
- Can create accounts
- Can create Games
This commit is contained in:
Marces Zastrow
2025-01-08 08:25:00 +01:00
parent 5a46333bfd
commit 1cbe8b9d94
14 changed files with 252 additions and 77 deletions
Binary file not shown.
+29 -10
View File
@@ -68,7 +68,7 @@ app.post('/login', (req, res) => {
if (!row || !(await bcrypt.compare(password, row.password))) {
return res.status(400).json({ error: 'Invalid username or password.' });
}
res.json({ message: 'Login successful!', userId: row.id });
res.json({ success: true, message: 'Login successful!', userId: row.id });
});
});
@@ -87,19 +87,39 @@ app.get('/games/:userId', (req, res) => {
);
});
//Join Game
// 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) {
db.get('SELECT * FROM games WHERE game_id = ?', [gameId], (err, game) => {
if (err) {
return res.status(400).json({ error: 'Failed to create game.' });
return res.status(500).json({ error: 'Internal server error' });
}
res.status(201).json({message: "User joined Game!", gameId: this.gameId})
})
stmt.finalize();
if (!game) {
return res.status(404).json({ error: 'Game not found' });
}
const participants = JSON.parse(game.participants);
const gameMasterId = game.game_master_id;
// Check if the user is already a participant or the game master
if (gameMasterId === userId || participants.includes(userId)) {
return res.status(400).json({ error: 'User is already a participant or the game master.' });
}
participants.push(userId);
const stmt = db.prepare('UPDATE games SET participants = ? WHERE game_id = ?');
stmt.run([JSON.stringify(participants), gameId], function (err) {
if (err) {
return res.status(400).json({ error: 'Failed to join game.' });
}
res.status(201).json({ message: 'User joined game successfully!', gameId: gameId });
});
stmt.finalize();
});
});
@@ -119,8 +139,7 @@ app.post('/games', (req, res) => {
app.post('/createGame', (req, res) => {
const { name, description, gameMasterId, participants } = req.body;
// Generate an 8-character alphanumeric ID
const gameId = crypto.randomBytes(4).toString('hex'); // 8 characters (4 bytes * 2 hex chars per byte)
const gameId = crypto.randomBytes(4).toString('hex');
const stmt = db.prepare('INSERT INTO games (game_id, name, description, game_master_id, participants) VALUES (?, ?, ?, ?, ?)');
stmt.run([gameId, name, description, gameMasterId, JSON.stringify(participants)], function (err) {