+
Added functionallity to start a new Game
This commit is contained in:
Binary file not shown.
+17
-2
@@ -31,8 +31,7 @@ db.run(`CREATE TABLE IF NOT EXISTS users (
|
|||||||
|
|
||||||
// Create games table if it doesn't exist
|
// Create games table if it doesn't exist
|
||||||
db.run(`CREATE TABLE IF NOT EXISTS games (
|
db.run(`CREATE TABLE IF NOT EXISTS games (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
game_id TEXT PRIMARY KEY,
|
||||||
game_id TEXT,
|
|
||||||
name TEXT,
|
name TEXT,
|
||||||
description TEXT,
|
description TEXT,
|
||||||
game_master_id INTEGER,
|
game_master_id INTEGER,
|
||||||
@@ -88,6 +87,22 @@ app.get('/games/:userId', (req, res) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//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) {
|
||||||
|
if (err) {
|
||||||
|
return res.status(400).json({ error: 'Failed to create game.' });
|
||||||
|
}
|
||||||
|
res.status(201).json({message: "User joined Game!", gameId: this.gameId})
|
||||||
|
})
|
||||||
|
stmt.finalize();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Create a new game
|
// Create a new game
|
||||||
app.post('/games', (req, res) => {
|
app.post('/games', (req, res) => {
|
||||||
const { name, description, gameMasterId, participants } = req.body;
|
const { name, description, gameMasterId, participants } = req.body;
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; // Use Routes here
|
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
import Header from './components/header'; // Make sure the import path is correct
|
import Header from './components/header';
|
||||||
import Home from './pages/home';
|
import Home from './pages/home';
|
||||||
import Login from './pages/login';
|
import Login from './pages/login';
|
||||||
import Register from './pages/register';
|
import Register from './pages/register';
|
||||||
import JoinGame from './pages/joinGame';
|
import JoinGame from './pages/joinGame';
|
||||||
|
import StartGame from './pages/startGame';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
@@ -44,6 +45,10 @@ function App() {
|
|||||||
path="joinGame"
|
path="joinGame"
|
||||||
element={<JoinGame isLoggedIn={isLoggedIn} />}
|
element={<JoinGame isLoggedIn={isLoggedIn} />}
|
||||||
/>
|
/>
|
||||||
|
<Route
|
||||||
|
path="/startGame"
|
||||||
|
element={<StartGame isLoggedIn={isLoggedIn} />}
|
||||||
|
/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
</Router>
|
</Router>
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
.create-game-page {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
background: #2e2e3f;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.create-game-page h1 {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
input,
|
||||||
|
textarea {
|
||||||
|
width: calc(100% - 22px);
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
resize: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #007bff;
|
||||||
|
color: white;
|
||||||
|
font-size: 16px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error-message {
|
||||||
|
color: red;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.success-message {
|
||||||
|
color: green;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
import './startGame.css';
|
||||||
|
|
||||||
|
const startGame = () => {
|
||||||
|
const [name, setName] = useState('');
|
||||||
|
const [description, setDescription] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [success, setSuccess] = useState('');
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const handleSubmit = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setError('');
|
||||||
|
setSuccess('');
|
||||||
|
|
||||||
|
if (!name || !description) {
|
||||||
|
setError('Game name and description are required.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('http://localhost:5000/createGame', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
gameMasterId: 1, // Placeholder for game master ID; replace with dynamic value if needed
|
||||||
|
participants: [], // Default empty participants array
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json();
|
||||||
|
setError(errorData.error || 'Failed to create game.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
setSuccess('Game created successfully!');
|
||||||
|
navigate(`/games/${data.gameId}`); // Redirect to the game details page
|
||||||
|
} catch (err) {
|
||||||
|
setError('Something went wrong. Please try again.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="create-game-page">
|
||||||
|
<h1>Create a New Game</h1>
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<div className="form-group">
|
||||||
|
<label htmlFor="gameName">Game Name</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="gameName"
|
||||||
|
value={name}
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
placeholder="Enter game name"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label htmlFor="gameDescription">Game Description</label>
|
||||||
|
<textarea
|
||||||
|
id="gameDescription"
|
||||||
|
value={description}
|
||||||
|
onChange={(e) => setDescription(e.target.value)}
|
||||||
|
placeholder="Enter game description"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{error && <p className="error-message">{error}</p>}
|
||||||
|
{success && <p className="success-message">{success}</p>}
|
||||||
|
<button type="submit">Create Game</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default startGame;
|
||||||
|
|||||||
Reference in New Issue
Block a user