generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from Arquisoft/multiplayer2
Multiplayer 2
- Loading branch information
Showing
9 changed files
with
172 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 82 additions & 3 deletions
85
webapp/src/components/game/multiplayer/QuestionsMultiPlayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,94 @@ | ||
import { FC } from 'react' | ||
import { FC, useState } from 'react' | ||
import { SocketProps } from './GameMultiPlayer'; | ||
import { Question4Answers } from '../singleplayer/GameSinglePlayer'; | ||
import axios from 'axios'; | ||
|
||
interface QuestionsMultiPlayerProps { | ||
socket: SocketProps; | ||
handleCurrentStage: (n: number) => void | ||
questions: Question4Answers[] | ||
partyCode: string | ||
} | ||
|
||
const QuestionsMultiPlayer: FC<QuestionsMultiPlayerProps> = ({socket,handleCurrentStage,questions}) => { | ||
return <div>QuestionsMultiPlayer</div> | ||
const QuestionsMultiPlayer: FC<QuestionsMultiPlayerProps> = ({socket, questions, partyCode}) => { | ||
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); | ||
|
||
const handleAnswerClick = async (answer: string) => { | ||
if(questions[currentQuestion].correctAnswer === answer){ | ||
setCorrectAnswers(correctAnswers + 1); | ||
|
||
} | ||
setCurrentQuestion(currentQuestion + 1); | ||
if(currentQuestion+2 === questions.length){ | ||
const totalPoints = calculatePoints(correctAnswers, questions.length); | ||
// the player has finished the game | ||
// update stats for each player | ||
const requestData ={ "players": [{ | ||
"uuid": uuid, | ||
"nCorrectAnswers": correctAnswers, | ||
"nWrongAnswers": questions.length - correctAnswers, | ||
"totalScore": totalPoints, | ||
"isWinner": false | ||
}]} | ||
await axios.post(`${apiEndpoint}/updateStats`, requestData); | ||
// pass the points obtained of each player to the socket | ||
socket.emit('playerFinished', partyCode, totalPoints) | ||
} | ||
}; | ||
|
||
const calculatePoints = (correctAnswers: number, totalQuestions: number) => { | ||
const incorrectQuestions = totalQuestions - correctAnswers; | ||
const points = correctAnswers * 100 - incorrectQuestions * 25; | ||
return points; | ||
} | ||
|
||
const getShuffledAnswers = () => { | ||
const answers = [ | ||
questions[currentQuestion].correctAnswer, | ||
questions[currentQuestion].incorrectAnswer1, | ||
questions[currentQuestion].incorrectAnswer2, | ||
questions[currentQuestion].incorrectAnswer3, | ||
]; | ||
|
||
for (let i = answers.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[answers[i], answers[j]] = [answers[j], answers[i]]; | ||
} | ||
|
||
return answers; | ||
}; | ||
|
||
return ( | ||
<div> | ||
{(currentQuestion+1) < questions.length && ( | ||
<> | ||
<h2>Question {currentQuestion + 1}</h2> | ||
<p>{questions[currentQuestion].question}</p> | ||
<div className="answer-grid"> | ||
{getShuffledAnswers().map((answer) => ( | ||
<button | ||
key={answer} | ||
onClick={() => handleAnswerClick(answer)} | ||
> | ||
{answer} | ||
</button> | ||
))} | ||
</div> | ||
</> | ||
)} | ||
{currentQuestion+1 === questions.length && ( | ||
<> | ||
<p>You answered {correctAnswers} out of {questions.length} questions correctly.</p> | ||
<p>You earned {calculatePoints(correctAnswers, questions.length)} points.</p> | ||
<p>Waiting for the rest of the players to finish...</p> | ||
</> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default QuestionsMultiPlayer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters