From 5fa7a56a539e9fdd5a60e8c7d4d662f42972ce42 Mon Sep 17 00:00:00 2001 From: Marces Zastrow Date: Fri, 17 Jan 2025 14:12:07 +0100 Subject: [PATCH] k --- backend/database.db | Bin 23224320 -> 23224320 bytes backend/server.js | 7 +++-- frontend/src/pages/gameMasterPage.jsx | 43 +++++++++----------------- frontend/src/pages/startGame.jsx | 20 +++++++++--- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/backend/database.db b/backend/database.db index 6e123deb6187e739b5004ae538acc1f2f8287fe6..98c365321c5c3b7685a42bc25945bd55c46ab3d9 100644 GIT binary patch delta 1904 zcmXxkc~q2D9EWj75EvNdWkf}U5fD*v9TEZgh(+ToASbKZN-@7(v^GiToB&aDZ}*qLT(-<3YY)ZSj( zR*`P8rt~s$jfl0|IBe`T8jULJKI>+y-#X2j60s#6Piz`tiLvKon`|WoML|bizRMr> z2J8KG^+6u|-f*zaztQIp279|bSqU-r<_Paf~W(J&Qo5NR}S5#CIsB~$fKy#UO z-nD_ukT(?a`a|BZ+mjl{Y_i!_5b*h2E{Eg9W==M9ds@4zw`WAB$1;#=wt0j3Uaxbt zgY*9Xo)fQXZLDcYw-|w5MvDK;RMjyl=7I82m9tp4@5mp#Tf(?C< zjDF~k0Z73>q+$@#kPbTrBLkVp!VqL*C~`0i!!ZK67>Q9BjWHODad03H`Ea5DE)=2& z#VEmeOu$4;!emUrR7}Hk%)m^PVism&4(4JW=3@cMP>u>L#3C%l5>#R-mSH(oU?r;H z#wvJFjT+R#i`DSKj{t(GLp@GH143AXwKy62?M}g|SdTC^;52N+>1f0mI1^{#Y@CC0 z(S&B4hx4%s7hp3k#1>qHt+*K5(1J^_9XqfSyKpI5aTzYh71)g{u?JV-YFvYBaUHJ5 z4Y(0E;byeq7VO2XxDB`C4%~^ma5wJ3z1W9#+=mG6#{=lVgLnuJ;}JZH{df$I;{ZDG z1fE0}p2E|32G8O-JdYR9jTdncFX3gpf>-exUdJ1F6K~;dyn}af2=C#29L5Lu5Fg=V ze1cE$89v7sID#+n6~4wd_!i&cd;EZ-_z^$hXB@*X_!Y-zlo%yeiBrrBeM7>03&MB@C;K}gY&nXOQE6d5IZw~)Pw?2?sD_TDS|*fV=%@4ffldtLX7 z@AVHnpZkeRNpz0G6gN}yRQCW=^5)3pp%zDTY8u1Ku$xnjJH{*{)^4@zFejykPxA0| zjdyXfMz{6z_VxB@9~9L++U64+(=N)^xoq7VwB2)s}f#ZVk2P!gq38fD-OACyHol!q_;PyrQD36)U=RZ$K8sE!&4 zKp<+O7J^V4bx;@e5R4GiM<^PgAsV4EnxH9~p*dQhC0e01+8_+!h(IKw&=&2`9?|H4 zj_8EW=z^~3hVJMA-L4mUqYwI`A7aoS0}zXW7=*zXf}t3O;fO;#Mj!zrF$$wG24j(k zaTt#Yn21T3j47CkX-L9!%)m^{!fedJT+G9KEWko6!eT7JQY^!AtiVdF!fLF+S|no~ z)?))UViPuF3+&j6ZP<<-*oj@(jXl_leb|o!NWnoI!eJc2Q5?f@oWMz(LMl$<49?;l z&f@|u;u0?73a;WBuHy!7;udb>4({R}?&AR-;t?L>37+B^p5p~x;uT)w4c_7%-s1y4 z;uAjO3%=qTzT*de;un775B}occ(y+oN?IkI;-I8g9F+`8M#V|Vq&O>?l`M)`F%*kp zRkAAClRw<{HS9}#erGipXsiag^swh>JYKp&7U8$i2D1k~%rIr$;)K=;! zb(MNbuo9xwS3;ErN<*cQ(pYJtG*y}@&6O5POQn_4T4|$%Dd9?l5@|ZXe=&u;{|}_$ B2FCyZ 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;