From 99942c228311ecf072cebc53378b9c2f1f2b0060 Mon Sep 17 00:00:00 2001 From: carlospelazas Date: Mon, 11 Mar 2024 20:12:04 +0100 Subject: [PATCH] update player stats when finishing --- webapp/src/components/game/PlayingGame.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/webapp/src/components/game/PlayingGame.tsx b/webapp/src/components/game/PlayingGame.tsx index 977880a..e846ea7 100644 --- a/webapp/src/components/game/PlayingGame.tsx +++ b/webapp/src/components/game/PlayingGame.tsx @@ -1,5 +1,6 @@ import { FC, useState } from 'react' import { Player, Question4Answers } from './Game' +import axios from 'axios'; interface PlayingGameProps { questions: Question4Answers[] @@ -9,6 +10,10 @@ interface PlayingGameProps { } const PlayingGame: FC = ({questions, setCurrentStage, setPlayers, players}) => { + + const uuid = localStorage.getItem("userUUID"); + const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; + const [currentQuestion, setCurrentQuestion] = useState(0); const [correctAnswers, setCorrectAnswers] = useState(0); @@ -26,7 +31,7 @@ const PlayingGame: FC = ({questions, setCurrentStage, setPlaye return points; } - const handleNext = () => { + const handleNext = async () => { const randomPoints = Math.floor(Math.random() * (10000 - 100 + 1) / 50) * 50 + 100; players.map(player => { if(player.isBot){ @@ -38,6 +43,16 @@ const PlayingGame: FC = ({questions, setCurrentStage, setPlaye }) setPlayers(players); setCurrentStage(4); + const sorted = players.sort((a, b) => b.points - a.points); + const requestData ={ "players": [{ + "uuid": uuid, + "nCorrectAnswers": correctAnswers, + "nWrongAnswers": questions.length - correctAnswers, + "totalScore": calculatePoints(correctAnswers, questions.length), + "isWinner": !sorted[0].isBot + }]} + + await axios.post(`${apiEndpoint}/updateStats`, requestData); } const getShuffledAnswers = () => {