This commit is contained in:
Marces Zastrow
2025-01-17 14:12:07 +01:00
parent 6f52b6ffb0
commit 5fa7a56a53
4 changed files with 35 additions and 35 deletions
Binary file not shown.
+5 -2
View File
@@ -166,12 +166,15 @@ app.post('/games', (req, res) => {
app.post('/createGame', (req, res) => { app.post('/createGame', (req, res) => {
const { name, description, gameMasterId, participants } = req.body; 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 (?, ?, ?, ?, ?)'); 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) { if (err) {
console.error('Database error:', err);
return res.status(400).json({ error: 'Failed to create game.' }); return res.status(400).json({ error: 'Failed to create game.' });
} }
res.status(201).json({ message: 'Game created successfully!', gameId: gameId }); res.status(201).json({ message: 'Game created successfully!', gameId: gameId });
+15 -28
View File
@@ -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 // Update the EditModal component
const EditModal = () => { const EditModal = () => {
const nonEditableFields = [ const nonEditableFields = [
@@ -227,10 +235,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || 0} value={formData[key] || 0}
label="Status" label="Status"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
> >
<MenuItem value={0}>Allied</MenuItem> <MenuItem value={0}>Allied</MenuItem>
<MenuItem value={1}>Neutral</MenuItem> <MenuItem value={1}>Neutral</MenuItem>
@@ -247,10 +252,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || ''} value={formData[key] || ''}
label="Sex" label="Sex"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
> >
<MenuItem value="Male">Male</MenuItem> <MenuItem value="Male">Male</MenuItem>
<MenuItem value="Female">Female</MenuItem> <MenuItem value="Female">Female</MenuItem>
@@ -267,10 +269,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || 1} value={formData[key] || 1}
label="Rarity" label="Rarity"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
> >
<MenuItem value={1}>Poor</MenuItem> <MenuItem value={1}>Poor</MenuItem>
<MenuItem value={2}>Common</MenuItem> <MenuItem value={2}>Common</MenuItem>
@@ -291,10 +290,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || ''} value={formData[key] || ''}
label="Type" label="Type"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
> >
<MenuItem value="Weapon">Weapon</MenuItem> <MenuItem value="Weapon">Weapon</MenuItem>
<MenuItem value="Armor">Armor</MenuItem> <MenuItem value="Armor">Armor</MenuItem>
@@ -313,10 +309,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || ''} value={formData[key] || ''}
label="Art" label="Art"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
> >
<MenuItem value="Sword">Sword</MenuItem> <MenuItem value="Sword">Sword</MenuItem>
<MenuItem value="Axe">Axe</MenuItem> <MenuItem value="Axe">Axe</MenuItem>
@@ -337,10 +330,7 @@ const GameMasterPage = () => {
<Select <Select
value={formData[key] || ''} value={formData[key] || ''}
label="Owner" label="Owner"
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
sx={{ sx={{
color: '#fff', color: '#fff',
'& .MuiOutlinedInput-notchedOutline': { borderColor: '#444' }, '& .MuiOutlinedInput-notchedOutline': { borderColor: '#444' },
@@ -382,10 +372,7 @@ const GameMasterPage = () => {
fullWidth fullWidth
label={key} label={key}
value={formData[key] || ''} value={formData[key] || ''}
onChange={(e) => setFormData({ onChange={(e) => handleFormChange(key, e.target.value)}
...formData,
[key]: e.target.value
})}
sx={{ ...commonStyles.input, mb: 2 }} sx={{ ...commonStyles.input, mb: 2 }}
/> />
); );
+15 -5
View File
@@ -1,8 +1,10 @@
import React, { useState } from 'react'; import React, { useState, useContext } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { UserContext } from '../context/UserContext';
import './startGame.css'; import './startGame.css';
const startGame = () => { const StartGame = () => {
const { userId } = useContext(UserContext);
const [name, setName] = useState(''); const [name, setName] = useState('');
const [description, setDescription] = useState(''); const [description, setDescription] = useState('');
const [error, setError] = useState(''); const [error, setError] = useState('');
@@ -14,6 +16,14 @@ const startGame = () => {
setError(''); setError('');
setSuccess(''); 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) { if (!name || !description) {
setError('Game name and description are required.'); setError('Game name and description are required.');
return; return;
@@ -28,7 +38,7 @@ const startGame = () => {
body: JSON.stringify({ body: JSON.stringify({
name, name,
description, description,
gameMasterId: 1, gameMasterId: parsedUserId, // Use parsed ID
participants: [], participants: [],
}), }),
}); });
@@ -41,7 +51,7 @@ const startGame = () => {
const data = await response.json(); const data = await response.json();
setSuccess('Game created successfully!'); 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) { } catch (err) {
setError('Something went wrong. Please try again.'); setError('Something went wrong. Please try again.');
} }
@@ -80,4 +90,4 @@ const startGame = () => {
); );
}; };
export default startGame; export default StartGame;