Game id changed
This commit is contained in:
Marces Zastrow
2024-12-18 11:35:53 +01:00
parent 0c4af623bc
commit 6c6feb0185
4 changed files with 79 additions and 2 deletions
+19
View File
@@ -3,6 +3,7 @@ const sqlite3 = require('sqlite3').verbose();
const bcrypt = require('bcrypt');
const cors = require('cors');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const app = express();
const port = 5000;
@@ -31,6 +32,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,
name TEXT,
description TEXT,
game_master_id INTEGER,
@@ -99,6 +101,23 @@ app.post('/games', (req, res) => {
stmt.finalize();
});
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 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) {
if (err) {
return res.status(400).json({ error: 'Failed to create game.' });
}
res.status(201).json({ message: 'Game created successfully!', gameId: gameId });
});
stmt.finalize();
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);