From 6838f04828528b414689c359a6ed7ee3fe40ed55 Mon Sep 17 00:00:00 2001 From: Ralex91 <95540504+Ralex91@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:02:28 +0100 Subject: [PATCH] Update Manager --- socket/src/roles/manager.js | 4 +- src/components/game/join/Room.jsx | 2 +- src/components/game/states/Answers.jsx | 87 +++++++++++++++++++++---- src/components/game/states/Question.jsx | 8 +-- src/components/game/states/Result.jsx | 2 +- src/pages/game.jsx | 1 - src/pages/manager.jsx | 3 +- src/styles/globals.css | 9 +++ 8 files changed, 89 insertions(+), 27 deletions(-) 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 } }) { {/**/} + + {responses && ( +
+ {answers.map((_, key) => ( +
+ + {responses[key] || 0} + +
+ ))} +
+ )} -
-
-
- Time - {cooldown} +
+ {!responses && ( +
+
+ Time + {cooldown} +
+
+ Answers + {totalAnswer} +
-
- Answers - {totalAnswer} -
-
+ )}
{answers.map((answer, key) => ( socket.emit("player:selectedAnswer", key)} > diff --git a/src/components/game/states/Question.jsx b/src/components/game/states/Question.jsx index 2cae8ba..8cfe37e 100644 --- a/src/components/game/states/Question.jsx +++ b/src/components/game/states/Question.jsx @@ -4,12 +4,6 @@ import { useEffect, useRef } from "react" export default function Question({ data: { question } }) { const barRef = useRef(null) - useEffect(() => { - setTimeout(() => { - barRef.current.style.width = "100%" - }, 1) - }, []) - return (
@@ -20,7 +14,7 @@ export default function Question({ data: { question } }) {
) diff --git a/src/components/game/states/Result.jsx b/src/components/game/states/Result.jsx index 5aaf203..1904fe8 100644 --- a/src/components/game/states/Result.jsx +++ b/src/components/game/states/Result.jsx @@ -16,7 +16,7 @@ export default function Result({ }, []) return ( -
+
{correct ? ( ) : ( diff --git a/src/pages/game.jsx b/src/pages/game.jsx index 02f46fe..4121cb0 100644 --- a/src/pages/game.jsx +++ b/src/pages/game.jsx @@ -14,7 +14,6 @@ const gameStateComponent = { SHOW_QUESTION: Question, WAIT: Wait, SHOW_RESULT: Result, - SHOW_LEADERBOARD: Leaderboard, } export default function Game() { diff --git a/src/pages/manager.jsx b/src/pages/manager.jsx index 84081bb..9431fbc 100644 --- a/src/pages/manager.jsx +++ b/src/pages/manager.jsx @@ -2,7 +2,6 @@ import GameWrapper from "@/components/game/GameWrapper" import Answers from "@/components/game/states/Answers" import Leaderboard from "@/components/game/states/Leaderboard" import Question from "@/components/game/states/Question" -import Result from "@/components/game/states/Result" import Start from "@/components/game/states/Start" import Wait from "@/components/game/states/Wait" import { usePlayerContext } from "@/context/player" @@ -15,7 +14,7 @@ const gameStateComponent = { SELECT_ANSWER: Answers, SHOW_QUESTION: Question, WAIT: Wait, - SHOW_RESULT: Result, + SHOW_RESPONSES: Answers, SHOW_LEADERBOARD: Leaderboard, } diff --git a/src/styles/globals.css b/src/styles/globals.css index 34188e5..b9cd4cf 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -50,3 +50,12 @@ transform: scale(1); } } + +@keyframes progressBar { + from { + width: 0%; + } + to { + width: 100%; + } +}