diff --git a/backend/database.db b/backend/database.db index 6e123de..98c3653 100644 Binary files a/backend/database.db and b/backend/database.db differ diff --git a/backend/server.js b/backend/server.js index c43c754..40d6087 100644 --- a/backend/server.js +++ b/backend/server.js @@ -166,12 +166,15 @@ app.post('/games', (req, res) => { app.post('/createGame', (req, res) => { const { name, description, gameMasterId, participants } = req.body; + const gameId = crypto.randomBytes(4).toString('hex'); - const gameId = crypto.randomBytes(4).toString('hex'); + // Convert gameMasterId to integer + const parsedGameMasterId = parseInt(gameMasterId); 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) { + stmt.run([gameId, name, description, parsedGameMasterId, JSON.stringify(participants)], function (err) { if (err) { + console.error('Database error:', err); return res.status(400).json({ error: 'Failed to create game.' }); } res.status(201).json({ message: 'Game created successfully!', gameId: gameId }); diff --git a/frontend/src/pages/gameMasterPage.jsx b/frontend/src/pages/gameMasterPage.jsx index a694e99..afad57e 100644 --- a/frontend/src/pages/gameMasterPage.jsx +++ b/frontend/src/pages/gameMasterPage.jsx @@ -212,6 +212,14 @@ const GameMasterPage = () => { } }; + // Add new handler for form changes + const handleFormChange = (key, value) => { + setFormData(prev => ({ + ...prev, + [key]: value + })); + }; + // Update the EditModal component const EditModal = () => { const nonEditableFields = [ @@ -227,10 +235,7 @@ const GameMasterPage = () => { setFormData({ - ...formData, - [key]: e.target.value - })} + onChange={(e) => handleFormChange(key, e.target.value)} > Male Female @@ -267,10 +269,7 @@ const GameMasterPage = () => { setFormData({ - ...formData, - [key]: e.target.value - })} + onChange={(e) => handleFormChange(key, e.target.value)} > Weapon Armor @@ -313,10 +309,7 @@ const GameMasterPage = () => { setFormData({ - ...formData, - [key]: e.target.value - })} + onChange={(e) => handleFormChange(key, e.target.value)} sx={{ color: '#fff', '& .MuiOutlinedInput-notchedOutline': { borderColor: '#444' }, @@ -382,10 +372,7 @@ const GameMasterPage = () => { fullWidth label={key} value={formData[key] || ''} - onChange={(e) => setFormData({ - ...formData, - [key]: e.target.value - })} + onChange={(e) => handleFormChange(key, e.target.value)} sx={{ ...commonStyles.input, mb: 2 }} /> ); diff --git a/frontend/src/pages/startGame.jsx b/frontend/src/pages/startGame.jsx index 216331c..982991c 100644 --- a/frontend/src/pages/startGame.jsx +++ b/frontend/src/pages/startGame.jsx @@ -1,8 +1,10 @@ -import React, { useState } from 'react'; +import React, { useState, useContext } from 'react'; import { useNavigate } from 'react-router-dom'; +import { UserContext } from '../context/UserContext'; import './startGame.css'; -const startGame = () => { +const StartGame = () => { + const { userId } = useContext(UserContext); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [error, setError] = useState(''); @@ -14,6 +16,14 @@ const startGame = () => { setError(''); setSuccess(''); + // Parse userId to ensure it's a number + const parsedUserId = parseInt(userId); + + if (!parsedUserId) { + setError('You must be logged in to create a game.'); + return; + } + if (!name || !description) { setError('Game name and description are required.'); return; @@ -28,7 +38,7 @@ const startGame = () => { body: JSON.stringify({ name, description, - gameMasterId: 1, + gameMasterId: parsedUserId, // Use parsed ID participants: [], }), }); @@ -41,7 +51,7 @@ const startGame = () => { const data = await response.json(); setSuccess('Game created successfully!'); - navigate(`/games/${data.gameId}`); // Redirect to the game details page + navigate(`/games/${data.gameId}/master`); // Navigate to game master page directly } catch (err) { setError('Something went wrong. Please try again.'); } @@ -80,4 +90,4 @@ const startGame = () => { ); }; -export default startGame; +export default StartGame;