diff --git a/socket/src/roles/manager.js b/socket/src/roles/manager.js
index 4804cb9..da24828 100644
--- a/socket/src/roles/manager.js
+++ b/socket/src/roles/manager.js
@@ -60,7 +60,7 @@ const Manager = {
showLoaderboard: (game, io, socket) => {
if (!game.questions[game.currentQuestion + 1]) {
- io.to(game.room).emit("game:status", {
+ io.to(socket).emit("game:status", {
name: "FINISH",
data: {
winners: game.players.slice(0, 3).sort((a, b) => b.points - a.points),
@@ -71,7 +71,7 @@ const Manager = {
return
}
- io.to(game.room).emit("game:status", {
+ io.to(socket.id).emit("game:status", {
name: "SHOW_LEADERBOARD",
data: {
leaderboard: game.players
diff --git a/src/components/game/join/Room.jsx b/src/components/game/join/Room.jsx
index f9493a4..8e060a6 100644
--- a/src/components/game/join/Room.jsx
+++ b/src/components/game/join/Room.jsx
@@ -7,7 +7,7 @@ import { useState } from "react"
export default function Room() {
const { player, dispatch } = usePlayerContext()
const [loading, setLoading] = useState(false)
- const [roomId, setRoomId] = useState("")
+ const [roomId, setRoomId] = useState("207223")
const handleLogin = () => {
dispatch({ type: "JOIN", payload: roomId })
diff --git a/src/components/game/states/Answers.jsx b/src/components/game/states/Answers.jsx
index d5725a7..6d17e6f 100644
--- a/src/components/game/states/Answers.jsx
+++ b/src/components/game/states/Answers.jsx
@@ -5,7 +5,8 @@ import Rhombus from "@/components/icons/Rhombus"
import AnswerButton from "../../AnswerButton"
import { useSocketContext } from "@/context/socket"
import Image from "next/image"
-import { useState } from "react"
+import { useEffect, useRef, useState } from "react"
+import clsx from "clsx"
const answersColors = [
"bg-red-500",
@@ -16,12 +17,47 @@ const answersColors = [
const answersIcons = [Triangle, Rhombus, Circle, Square]
-export default function Answers({ data: { question, answers, image, time } }) {
+const calculatePercentages = (objectResponses) => {
+ const keys = Object.keys(objectResponses)
+ const values = Object.values(objectResponses)
+
+ if (!values.length) {
+ return []
+ }
+
+ const totalSum = values.reduce(
+ (accumulator, currentValue) => accumulator + currentValue,
+ 0
+ )
+
+ let result = {}
+
+ keys.map((key) => {
+ result[key] = ((objectResponses[key] / totalSum) * 100).toFixed() + "%"
+ })
+
+ console.log(result)
+
+ return result
+}
+
+export default function Answers({
+ data: { question, answers, image, time, responses, correct },
+}) {
const { socket } = useSocketContext()
+ const [percentages, setPercentages] = useState([])
const [cooldown, setCooldown] = useState(time)
const [totalAnswer, setTotalAnswer] = useState(0)
+ useEffect(() => {
+ if (!responses) {
+ return
+ }
+
+ setPercentages(calculatePercentages(responses))
+ }, [responses])
+
socket.on("game:cooldown", (sec) => {
setCooldown(sec)
})
@@ -38,25 +74,50 @@ export default function Answers({ data: { question, answers, image, time } }) {
{/*