Added Game page
This commit is contained in:
Marces Zastrow
2025-01-08 13:35:00 +01:00
parent 2091ce9e0f
commit a252350323
4 changed files with 130 additions and 7 deletions
Binary file not shown.
+11 -7
View File
@@ -63,7 +63,6 @@ app.post('/register', async (req, res) => {
// Login route // Login route
app.post('/login', (req, res) => { app.post('/login', (req, res) => {
const { username, password } = req.body; const { username, password } = req.body;
console.log("Trying to Login: ", username);
db.get('SELECT * FROM users WHERE username = ?', [username], async (err, row) => { db.get('SELECT * FROM users WHERE username = ?', [username], async (err, row) => {
if (err) { if (err) {
return res.status(500).json({ error: 'Internal server error' }); return res.status(500).json({ error: 'Internal server error' });
@@ -198,23 +197,28 @@ app.get('/games/:userId/characters', (req, res) => {
if (err) { if (err) {
return res.status(500).json({ error: 'Internal server error' }); return res.status(500).json({ error: 'Internal server error' });
} }
// Directly return the array of characters
console.log("Characters: ", rows);
res.json(rows); res.json(rows);
}); });
}); });
// Fetch Player Character // Fetch Player Character
app.get('/games/:gameId/:playerId/character', (req, res) => { app.get('/games/:gameId/:userId/character', (req, res) => {
const gameId = req.params.gameId; const gameId = req.params.gameId;
const playerId = req.params.playerId; const userId = req.params.userId;
db.get('SELECT * FROM PlayerCharacter WHERE GameID = ? AND PlayerID = ?', [gameId, playerId], (err, row) => { console.log(`Fetching character for gameId: ${gameId}, userId: ${userId}`); // Debug output
db.get('SELECT * FROM PlayerCharacter WHERE GameID = ? AND PlayerID = ?', [gameId, userId], (err, row) => {
if (err) { if (err) {
console.error('Database error:', err); // Debug output
return res.status(500).json({ error: 'Internal server error' }); return res.status(500).json({ error: 'Internal server error' });
} }
if (!row) {
console.log('No character found'); // Debug output
return res.status(404).json({ error: 'No character found' });
}
console.log('Character found:', row); // Debug output
res.json(row); res.json(row);
}); });
}) });
// Fetch Player Items // Fetch Player Items
app.get('/games/:charId/items', (req, res) => { app.get('/games/:charId/items', (req, res) => {
+5
View File
@@ -9,6 +9,7 @@ import Register from './pages/register';
import JoinGame from './pages/joinGame'; import JoinGame from './pages/joinGame';
import StartGame from './pages/startGame'; import StartGame from './pages/startGame';
import Profile from './pages/profile'; import Profile from './pages/profile';
import Games from './pages/games.jsx';
import { UserProvider } from './context/UserContext.jsx'; import { UserProvider } from './context/UserContext.jsx';
function App() { function App() {
@@ -59,6 +60,10 @@ function App() {
path="/profile" path="/profile"
element={<Profile isLoggedIn={isLoggedIn} />} element={<Profile isLoggedIn={isLoggedIn} />}
/> />
<Route
path='/games/:gameId'
element={<Games isLoggedIn={isLoggedIn} />}
/>
</Routes> </Routes>
</div> </div>
</Router> </Router>
+114
View File
@@ -0,0 +1,114 @@
import { useState, useEffect, useContext } from 'react';
import { Link, useParams } from 'react-router-dom';
import axios from 'axios';
import { UserContext } from '../context/UserContext';
import { Box, Typography, Grid, Card, CardContent, CardMedia, Button } from '@mui/material';
import defaultCharacterImage from '../assets/default-character.png';
const GamesPage = () => {
const { userId } = useContext(UserContext);
const { gameId } = useParams();
const [character, setCharacter] = useState(null);
useEffect(() => {
const fetchCharacter = async () => {
try {
const response = await axios.get(`http://localhost:5000/games/${gameId}/${userId}/character`);
setCharacter(response.data);
} catch (error) {
console.error('Error fetching character:', error);
}
};
if (userId) {
fetchCharacter();
}
}, [userId, gameId]);
if (!character) {
return (
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '100vh', p: 4 }}>
<Typography variant="h4" sx={{ mb: 4 }}>No Character Found</Typography>
<Button variant="contained" color="primary" component={Link} to="/create-character">
Create New Character
</Button>
</Box>
);
}
return (
<Box sx={{ p: 3 }}>
<Grid container spacing={3}>
{/* Character Info Section */}
<Grid item xs={12} md={8}>
<Typography variant="h3" sx={{ mb: 4 }}>{character.CharName}</Typography>
<Box sx={{ mb: 4 }}>
{/* Health Bar */}
<Box sx={{ mb: 2 }}>
<Typography variant="body1" sx={{ display: 'flex', justifyContent: 'space-between' }}>
<span>Health</span>
<span>{character.CurrentHealth}/{character.MaxHealth}</span>
</Typography>
<Box sx={{ width: '100%', backgroundColor: 'gray', borderRadius: '4px', height: '10px' }}>
<Box sx={{ width: `${(character.CurrentHealth / character.MaxHealth) * 100}%`, backgroundColor: 'red', height: '100%', borderRadius: '4px' }}></Box>
</Box>
</Box>
{/* Mana Bar */}
<Box>
<Typography variant="body1" sx={{ display: 'flex', justifyContent: 'space-between' }}>
<span>Mana</span>
<span>{character.CurrentMana}/{character.MaxMana}</span>
</Typography>
<Box sx={{ width: '100%', backgroundColor: 'gray', borderRadius: '4px', height: '10px' }}>
<Box sx={{ width: `${(character.CurrentMana / character.MaxMana) * 100}%`, backgroundColor: 'blue', height: '100%', borderRadius: '4px' }}></Box>
</Box>
</Box>
</Box>
{/* Character Description */}
<Box sx={{ mb: 4 }}>
<Typography variant="h5" sx={{ mb: 2 }}>Description</Typography>
<Typography variant="body1">{character.Looks}</Typography>
</Box>
{/* Inventory */}
<Box>
<Typography variant="h5" sx={{ mb: 2 }}>Inventory</Typography>
<Grid container spacing={2}>
{character.Items?.map((item, index) => (
<Grid item xs={6} sm={4} md={3} key={index}>
<Card>
<CardContent>
<Typography variant="h6">{item.name}</Typography>
<Typography variant="body2" color="textSecondary">Quantity: {item.quantity}</Typography>
</CardContent>
</Card>
</Grid>
))}
</Grid>
</Box>
</Grid>
{/* Character Image and Details */}
<Grid item xs={12} md={4}>
<Card>
<CardMedia
component="img"
height="300"
image={character.Img || defaultCharacterImage}
alt={character.CharName}
/>
<CardContent>
<Typography variant="body1"><strong>Age:</strong> {character.Age}</Typography>
<Typography variant="body1"><strong>Race:</strong> {character.Race}</Typography>
<Typography variant="body1"><strong>Sex:</strong> {character.Sex}</Typography>
<Typography variant="body1"><strong>Job:</strong> {character.Job}</Typography>
</CardContent>
</Card>
</Grid>
</Grid>
</Box>
);
};
export default GamesPage;