Skip to content

Commit

Permalink
update player stats when finishing
Browse files Browse the repository at this point in the history
  • Loading branch information
pelazas committed Mar 11, 2024
1 parent a4d14c9 commit 99942c2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion webapp/src/components/game/PlayingGame.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FC, useState } from 'react'
import { Player, Question4Answers } from './Game'
import axios from 'axios';

interface PlayingGameProps {
questions: Question4Answers[]
Expand All @@ -9,6 +10,10 @@ interface PlayingGameProps {
}

const PlayingGame: FC<PlayingGameProps> = ({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);

Expand All @@ -26,7 +31,7 @@ const PlayingGame: FC<PlayingGameProps> = ({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){
Expand All @@ -38,6 +43,16 @@ const PlayingGame: FC<PlayingGameProps> = ({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 = () => {
Expand Down

0 comments on commit 99942c2

Please sign in to comment.