@@ -2,14 +2,19 @@ import "../Styles/Pages/GamePage.css";
22import { useEffect , useState } from "react" ;
33import Alert from "../Components/Alert" ;
44import UserData from "../UserData" ;
5+ import axios from "axios"
56const GAME_URI = import . meta. env . VITE_GAME_URI || '' ; // Get the game URI from environment variables
6- const userId = JSON . parse ( localStorage . getItem ( 'id' ) ) ; // Retrieve user ID from localStorage
7+ const quizId = localStorage . getItem ( 'quizId' ) ; // Retrieve user ID from localStorage
78const user = JSON . parse ( localStorage . getItem ( 'user' ) ) ;
8- console . log ( user ) ;
9+ import { useNavigate } from "react-router-dom" ;
910const GamePage = ( ) => {
11+ const URI = import . meta. env . VITE_APP_URI || '' ;
12+ const navigate = useNavigate ( ) ;
1013 const character = user . selectedCharacter ;
1114 const weapon = user . selectedWeapon ;
1215 const hat = user . selectedHat ;
16+ const userId = JSON . parse ( localStorage . getItem ( 'id' ) ) ;
17+ console . log ( `${ GAME_URI } ?sessionId=${ quizId } &character=${ character } &weapon=${ weapon } &hat=${ hat } ` ) ;
1318 const [ showAlert , setShowAlert ] = useState ( true )
1419 const [ alertText , setAlertText ] = useState ( '' )
1520 const [ gameFinished , setGameFinished ] = useState ( false ) ;
@@ -23,9 +28,41 @@ const GamePage = () => {
2328 try {
2429 const data = JSON . parse ( event . data ) ;
2530 setScore ( data . payload . monstersSlain ) ;
26- setAlertText ( "Monsters Slain: " , score ) ;
31+ // setAlertText("Monsters Slain: ", score);
32+ localStorage . setItem ( 'results' , score )
2733 setShowAlert ( true ) ;
2834 setGameFinished ( true ) ;
35+ axios ( {
36+ method : "put" ,
37+ url : `${ URI } users/updateMonstersSlain/${ userId } ` ,
38+ data : {
39+ "monsterSlain" : score
40+ } ,
41+ headers : {
42+ "Content-Type" : "application/json"
43+ }
44+ } )
45+ . then ( ( response ) => {
46+ console . log ( "Update Success:" , response . data ) ;
47+
48+ // Update local storage or UI with new user data if needed
49+ localStorage . setItem ( 'user' , JSON . stringify ( response . data ) ) ;
50+ } )
51+ . catch ( ( error ) => {
52+ const response = error . response ;
53+ // alert("It did not work")
54+ if ( response ) {
55+ console . log ( response . data ) ;
56+ console . log ( response . status ) ;
57+ console . log ( response . headers ) ;
58+ } else if ( error . request ) {
59+ console . log ( error . request ) ;
60+ } else {
61+ console . log ( "Error" , error . message ) ;
62+ }
63+ } ) ;
64+ navigate ( '/results' ) ;
65+
2966 // console.log("Data: ", data.payload.monstersSlain);
3067 } catch ( err ) {
3168 console . warn ( "Invalid message received:" , event . data ) ;
@@ -50,17 +87,17 @@ const GamePage = () => {
5087 < div className = "game-page-container" >
5188 { ! gameFinished && (
5289 < iframe
53- src = { `${ GAME_URI } ?sessionId=${ userId } &character=${ character } &weapon=${ weapon } &hat=${ hat } ` }
90+ src = { `${ GAME_URI } ?sessionId=${ quizId } &character=${ character } &weapon=${ weapon } &hat=${ hat } ` }
5491 style = { { width : "100%" , height : "100vh" , border : "none" } }
5592 title = "My Game"
5693 />
5794 ) }
5895 </ div >
59- { score !== null && (
96+ { /* { score !== null && (
6097 <div className="score-display">
6198 🎉 Final Score: {score}
6299 </div>
63- ) }
100+ )} */ }
64101 </ >
65102 ) ;
66103} ;
0 commit comments