diff --git a/asc/src/components/GameSetup.js b/asc/src/components/GameSetup.js index be7ae7f..54cf74a 100644 --- a/asc/src/components/GameSetup.js +++ b/asc/src/components/GameSetup.js @@ -1,150 +1,216 @@ import React, { useState } from 'react'; -import { - Button, - Card, - CardContent, - CardHeader, - Grid, - Typography, - FormControl, - FormControlLabel, - Radio, - RadioGroup, - Divider -} from '@mui/material'; import { useScore, ACTIONS } from '../context/ScoreContext'; +import { Button, Grid, Typography, TextField, Box } from '@mui/material'; +import { useNavigate } from 'react-router-dom'; -const GameSetup = ({ onGameStart }) => { - const { dispatch } = useScore(); - const [selectedGameType, setSelectedGameType] = useState(''); - const [selectedTargetFace, setSelectedTargetFace] = useState('standard'); +const ScoreTracker = () => { + const { state, dispatch } = useScore(); + const navigate = useNavigate(); - const handleGameTypeSelect = (gameType) => { - setSelectedGameType(gameType); - setSelectedTargetFace('standard'); // Reset to standard when game type changes + const gameType = state.currentGame.gameType; + + // Determine the number of arrows per round based on game type + const maxArrowsPerRound = gameType === '450' ? 3 : 5; + const maxScore = gameType === '450' ? 10 : 5; + const maxRounds = gameType === '450' ? 16 : 12; + + const [arrowScores, setArrowScores] = useState(Array(maxArrowsPerRound).fill('')); + + const handleScoreChange = (index, value) => { + const updatedScores = [...arrowScores]; + updatedScores[index] = value; + setArrowScores(updatedScores); }; - const startGame = () => { - if (!selectedGameType) return; + const handleAddRound = () => { + const valid = arrowScores.slice(0, maxArrowsPerRound).every(score => + (score >= 0 && score <= maxScore) || score.toUpperCase() === 'X' + ); - dispatch({ - type: ACTIONS.START_GAME, - payload: { - gameType: selectedGameType, - isLeague: false, - targetFace: selectedTargetFace - } + if (!valid) { + alert(`Please enter valid scores between 0-${maxScore} or X for bullseyes.`); + return; + } + + const roundArrows = arrowScores.slice(0, maxArrowsPerRound).map((score) => { + const arrowScore = score.toUpperCase() === 'X' ? maxScore : parseInt(score, 10); + return { + score: arrowScore, + isBullseye: score.toUpperCase() === 'X', + }; }); - onGameStart(); + + const roundTotal = roundArrows.reduce((sum, arrow) => sum + arrow.score, 0); + + dispatch({ + type: ACTIONS.ADD_ROUND, + payload: { + roundIndex: state.currentGame.rounds.length, + arrows: roundArrows, + total: roundTotal + }, + }); + + setArrowScores(Array(maxArrowsPerRound).fill('')); + + if (state.currentGame.rounds.length >= maxRounds - 1) { + navigate('/summary'); + } }; - // Determine if we should show target face options - const showTargetFaceOptions = selectedGameType !== ''; + const handleMainMenu = () => { + dispatch({ type: ACTIONS.RESET_GAME }); + navigate('/'); + }; return ( - - - - - - - - - Select your game type: - - - - - - - - + + {/* Main Menu Button */} + + + - {showTargetFaceOptions && ( - <> - - - - Select your target face: - - - - - setSelectedTargetFace(e.target.value)} - > - } - label="Standard" - /> - {selectedGameType === '300' && ( - } - label="5-Spot" - /> - )} - {selectedGameType === '450' && ( - } - label="3-Spot" - /> - )} - - - + + + {gameType} Round - Round {state.currentGame.rounds.length + 1} + + - - - - - )} - - - + {/* Score input section */} + + + {Array.from({ length: maxArrowsPerRound }).map((_, index) => ( + handleScoreChange(index, e.target.value)} + placeholder="0" + size="small" + sx={{ + width: '60px', + '& .MuiInputBase-input': { + padding: '8px', + textAlign: 'center' + } + }} + inputProps={{ + maxLength: 1, + style: { textAlign: 'center' } + }} + /> + ))} + + + + {/* Score buttons */} + + + {Array.from({ length: maxScore + 1 }).map((_, i) => ( + + ))} + + + + + {/* Control buttons */} + + + + + + + + {/* Scores display */} + + + Total Score: {state.currentGame.totalScore} | + Bullseyes: {state.currentGame.totalBullseyes} + + + + {/* Round history */} + + + {state.currentGame.rounds.length > 0 ? ( + state.currentGame.rounds.map((round, roundIndex) => ( + + Round {roundIndex + 1}: {round.arrows.map(arrow => arrow.score).join(', ')} + (Total: {round.total}, Bullseyes: {round.bullseyes}) + + )) + ) : ( + + No rounds played yet. + + )} + ); }; -export default GameSetup; +export default ScoreTracker; + diff --git a/asc/src/components/ScoreTracker.js b/asc/src/components/ScoreTracker.js index 0ffe297..1be2b61 100644 --- a/asc/src/components/ScoreTracker.js +++ b/asc/src/components/ScoreTracker.js @@ -1,231 +1,162 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; +import { + Button, + Card, + CardContent, + CardHeader, + FormControl, + FormControlLabel, + RadioGroup, + Radio, + Grid, + Typography, + Select, + MenuItem +} from '@mui/material'; import { useScore, ACTIONS } from '../context/ScoreContext'; -import { Button, Grid, Typography, TextField, Box } from '@mui/material'; -import { useNavigate } from 'react-router-dom'; -const ScoreTracker = () => { - const { state, dispatch } = useScore(); - const navigate = useNavigate(); - - const gameType = state.currentGame.gameType; - const isLeague = state.currentGame.isLeague; // Check if it's a league game - const maxArrowsPerRound = gameType === '450' ? 3 : 5; - const maxScore = gameType === '450' ? 10 : 5; - const maxRounds = gameType === '450' ? 16 : 12; +const GameSetup = ({ onGameStart }) => { + const { dispatch } = useScore(); - const [arrowScores, setArrowScores] = useState(Array(maxArrowsPerRound).fill('')); + const [selectedGameType, setSelectedGameType] = useState(''); + const [isLeague, setIsLeague] = useState(false); // This tracks if it's a league game + const [selectedTargetFace, setSelectedTargetFace] = useState('single'); // Assuming single as default target face - const handleScoreChange = (index, value) => { - const updatedScores = [...arrowScores]; - updatedScores[index] = value; - setArrowScores(updatedScores); + // Log isLeague to verify if it changes correctly + useEffect(() => { + console.log("Game mode isLeague:", isLeague); // Check this in the console + }, [isLeague]); + + const handleGameModeChange = (event) => { + const leagueMode = event.target.value === "league"; + setIsLeague(leagueMode); // Update isLeague based on user selection }; - const handleAddRound = () => { - const valid = arrowScores.slice(0, maxArrowsPerRound).every(score => - (score >= 0 && score <= maxScore) || score.toUpperCase() === 'X' - ); - - if (!valid) { - alert(`Please enter valid scores between 0-${maxScore} or X for bullseyes.`); - return; - } + const handleTargetFaceChange = (event) => { + setSelectedTargetFace(event.target.value); + }; - const roundArrows = arrowScores.slice(0, maxArrowsPerRound).map((score) => { - const arrowScore = score.toUpperCase() === 'X' ? maxScore : parseInt(score, 10); - return { - score: arrowScore, - isBullseye: score.toUpperCase() === 'X', - }; + const startGame = () => { + if (!selectedGameType) return; + + // Log the values to ensure they're correct + console.log("Starting game with settings:", { + gameType: selectedGameType, + isLeague: isLeague, + targetFace: selectedTargetFace }); - const roundTotal = roundArrows.reduce((sum, arrow) => sum + arrow.score, 0); - dispatch({ - type: ACTIONS.ADD_ROUND, + type: ACTIONS.START_GAME, payload: { - roundIndex: state.currentGame.rounds.length, - arrows: roundArrows, - total: roundTotal - }, - }); - - setArrowScores(Array(maxArrowsPerRound).fill('')); - - if (state.currentGame.rounds.length >= maxRounds - 1) { - navigate('/summary'); - } - }; - - const handleMainMenu = () => { - dispatch({ type: ACTIONS.RESET_GAME }); - navigate('/'); - }; - - const handleGameStart = (gameType, isLeague) => { - // Dispatch the action to start a new round with the correct game type and isLeague flag - dispatch({ - type: ACTIONS.START_NEW_ROUND, - payload: { - gameType, // '450' or '300' - isLeague // true for league games, false for practice games + gameType: selectedGameType, + isLeague: isLeague, // Pass the league/practice mode correctly + targetFace: selectedTargetFace } }); - navigate('/score-tracker'); // Navigate directly to the score tracker + onGameStart(); }; - return ( - - {/* Main Menu Button */} - - - + + + + + + + + + Select your game type: + + + + + + + + - - - {gameType} Round - Round {state.currentGame.rounds.length + 1} - - - {isLeague ? 'League Game' : 'Practice Game'} {/* Display whether it's a league or practice game */} - - + {/* Select League or Practice Mode */} + + + + Select game mode: + + + } + label="Practice" + /> + } + label="League" + /> + + + - {/* Score input section */} - - - {Array.from({ length: maxArrowsPerRound }).map((_, index) => ( - handleScoreChange(index, e.target.value)} - placeholder="0" - size="small" - sx={{ - width: '60px', - '& .MuiInputBase-input': { - padding: '8px', - textAlign: 'center' - } - }} - inputProps={{ - maxLength: 1, - style: { textAlign: 'center' } - }} - /> - ))} - - + {/* Target face selection */} + + + + Select target face: + + + + - {/* Score buttons */} - - - {Array.from({ length: maxScore + 1 }).map((_, i) => ( - - ))} - - - - - {/* Control buttons */} - - - - - - - - {/* Scores display */} - - - Total Score: {state.currentGame.totalScore} | - Bullseyes: {state.currentGame.totalBullseyes} - - - - {/* Round history */} - - - {state.currentGame.rounds.length > 0 ? ( - state.currentGame.rounds.map((round, roundIndex) => ( - - Round {roundIndex + 1}: {round.arrows.map(arrow => arrow.score).join(', ')} - (Total: {round.total}, Bullseyes: {round.bullseyes}) - - )) - ) : ( - - No rounds played yet. - - )} - + {/* Start Game Button */} + + + + + + ); }; -export default ScoreTracker; +export default GameSetup;