mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
Update Manager
This commit is contained in:
@@ -60,7 +60,7 @@ const Manager = {
|
|||||||
|
|
||||||
showLoaderboard: (game, io, socket) => {
|
showLoaderboard: (game, io, socket) => {
|
||||||
if (!game.questions[game.currentQuestion + 1]) {
|
if (!game.questions[game.currentQuestion + 1]) {
|
||||||
io.to(game.room).emit("game:status", {
|
io.to(socket).emit("game:status", {
|
||||||
name: "FINISH",
|
name: "FINISH",
|
||||||
data: {
|
data: {
|
||||||
winners: game.players.slice(0, 3).sort((a, b) => b.points - a.points),
|
winners: game.players.slice(0, 3).sort((a, b) => b.points - a.points),
|
||||||
@@ -71,7 +71,7 @@ const Manager = {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
io.to(game.room).emit("game:status", {
|
io.to(socket.id).emit("game:status", {
|
||||||
name: "SHOW_LEADERBOARD",
|
name: "SHOW_LEADERBOARD",
|
||||||
data: {
|
data: {
|
||||||
leaderboard: game.players
|
leaderboard: game.players
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { useState } from "react"
|
|||||||
export default function Room() {
|
export default function Room() {
|
||||||
const { player, dispatch } = usePlayerContext()
|
const { player, dispatch } = usePlayerContext()
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [roomId, setRoomId] = useState("")
|
const [roomId, setRoomId] = useState("207223")
|
||||||
|
|
||||||
const handleLogin = () => {
|
const handleLogin = () => {
|
||||||
dispatch({ type: "JOIN", payload: roomId })
|
dispatch({ type: "JOIN", payload: roomId })
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import Rhombus from "@/components/icons/Rhombus"
|
|||||||
import AnswerButton from "../../AnswerButton"
|
import AnswerButton from "../../AnswerButton"
|
||||||
import { useSocketContext } from "@/context/socket"
|
import { useSocketContext } from "@/context/socket"
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { useState } from "react"
|
import { useEffect, useRef, useState } from "react"
|
||||||
|
import clsx from "clsx"
|
||||||
|
|
||||||
const answersColors = [
|
const answersColors = [
|
||||||
"bg-red-500",
|
"bg-red-500",
|
||||||
@@ -16,12 +17,47 @@ const answersColors = [
|
|||||||
|
|
||||||
const answersIcons = [Triangle, Rhombus, Circle, Square]
|
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 { socket } = useSocketContext()
|
||||||
|
|
||||||
|
const [percentages, setPercentages] = useState([])
|
||||||
const [cooldown, setCooldown] = useState(time)
|
const [cooldown, setCooldown] = useState(time)
|
||||||
const [totalAnswer, setTotalAnswer] = useState(0)
|
const [totalAnswer, setTotalAnswer] = useState(0)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!responses) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setPercentages(calculatePercentages(responses))
|
||||||
|
}, [responses])
|
||||||
|
|
||||||
socket.on("game:cooldown", (sec) => {
|
socket.on("game:cooldown", (sec) => {
|
||||||
setCooldown(sec)
|
setCooldown(sec)
|
||||||
})
|
})
|
||||||
@@ -38,25 +74,50 @@ export default function Answers({ data: { question, answers, image, time } }) {
|
|||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
{/*<Image src={image} className="h-60 w-auto rounded-md" />*/}
|
{/*<Image src={image} className="h-60 w-auto rounded-md" />*/}
|
||||||
|
|
||||||
|
{responses && (
|
||||||
|
<div
|
||||||
|
className={`w-full gap-4 grid grid-cols-${answers.length} px-2 max-w-3xl h-40 mt-8`}
|
||||||
|
>
|
||||||
|
{answers.map((_, key) => (
|
||||||
|
<div
|
||||||
|
key={key}
|
||||||
|
className={clsx(
|
||||||
|
"flex flex-col rounded-md overflow-hidden self-end justify-end",
|
||||||
|
answersColors[key]
|
||||||
|
)}
|
||||||
|
style={{ height: percentages[key] }}
|
||||||
|
>
|
||||||
|
<span className="text-white font-bold drop-shadow-md text-lg bg-black/10 w-full text-center">
|
||||||
|
{responses[key] || 0}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="">
|
<div>
|
||||||
<div className="max-w-7xl mx-auto mb-4 rounded-full w-full flex justify-between gap-1 font-bold text-white text-lg md:text-xl px-2">
|
{!responses && (
|
||||||
<div className="bg-white shadow-inset text-black px-4 font-bold rounded-md flex flex-col items-center text-lg">
|
<div className="max-w-7xl mx-auto mb-4 rounded-full w-full flex justify-between gap-1 font-bold text-white text-lg md:text-xl px-2">
|
||||||
<span className="text-sm">Time</span>
|
<div className="bg-white shadow-inset text-black px-4 font-bold rounded-md flex flex-col items-center text-lg">
|
||||||
<span className="-translate-y-1">{cooldown}</span>
|
<span className="text-sm">Time</span>
|
||||||
|
<span className="-translate-y-1">{cooldown}</span>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white shadow-inset text-black px-4 font-bold rounded-md flex flex-col items-center text-lg">
|
||||||
|
<span className="text-sm">Answers</span>
|
||||||
|
<span className="-translate-y-1">{totalAnswer}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="bg-white shadow-inset text-black px-4 font-bold rounded-md flex flex-col items-center text-lg">
|
)}
|
||||||
<span className="text-sm">Answers</span>
|
|
||||||
<span className="-translate-y-1">{totalAnswer}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="max-w-7xl mx-auto mb-4 rounded-full w-full grid grid-cols-2 gap-1 font-bold text-white text-lg md:text-xl px-2">
|
<div className="max-w-7xl mx-auto mb-4 rounded-full w-full grid grid-cols-2 gap-1 font-bold text-white text-lg md:text-xl px-2">
|
||||||
{answers.map((answer, key) => (
|
{answers.map((answer, key) => (
|
||||||
<AnswerButton
|
<AnswerButton
|
||||||
key={key}
|
key={key}
|
||||||
className={answersColors[key]}
|
className={clsx(answersColors[key], {
|
||||||
|
"opacity-65": responses && correct !== key,
|
||||||
|
})}
|
||||||
icon={answersIcons[key]}
|
icon={answersIcons[key]}
|
||||||
onClick={() => socket.emit("player:selectedAnswer", key)}
|
onClick={() => socket.emit("player:selectedAnswer", key)}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -4,12 +4,6 @@ import { useEffect, useRef } from "react"
|
|||||||
export default function Question({ data: { question } }) {
|
export default function Question({ data: { question } }) {
|
||||||
const barRef = useRef(null)
|
const barRef = useRef(null)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setTimeout(() => {
|
|
||||||
barRef.current.style.width = "100%"
|
|
||||||
}, 1)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="max-w-7xl mx-auto w-full flex-1 relative items-center flex flex-col px-4 h-full">
|
<section className="max-w-7xl mx-auto w-full flex-1 relative items-center flex flex-col px-4 h-full">
|
||||||
<div className="flex items-center flex-1">
|
<div className="flex items-center flex-1">
|
||||||
@@ -20,7 +14,7 @@ export default function Question({ data: { question } }) {
|
|||||||
<div
|
<div
|
||||||
ref={barRef}
|
ref={barRef}
|
||||||
className="h-6 bg-primary mb-32 rounded-full self-start justify-self-end"
|
className="h-6 bg-primary mb-32 rounded-full self-start justify-self-end"
|
||||||
style={{ transition: `width ${6}s linear`, width: "0%" }}
|
style={{ animation: `progressBar ${6}s linear forwards` }}
|
||||||
></div>
|
></div>
|
||||||
</section>
|
</section>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export default function Result({
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="max-w-7xl mx-auto w-full flex-1 relative items-center justify-center flex flex-col">
|
<section className="max-w-7xl mx-auto w-full flex-1 relative items-center justify-center flex flex-col anim-show">
|
||||||
{correct ? (
|
{correct ? (
|
||||||
<CricleCheck className="max-h-60 aspect-square w-full" />
|
<CricleCheck className="max-h-60 aspect-square w-full" />
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ const gameStateComponent = {
|
|||||||
SHOW_QUESTION: Question,
|
SHOW_QUESTION: Question,
|
||||||
WAIT: Wait,
|
WAIT: Wait,
|
||||||
SHOW_RESULT: Result,
|
SHOW_RESULT: Result,
|
||||||
SHOW_LEADERBOARD: Leaderboard,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Game() {
|
export default function Game() {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import GameWrapper from "@/components/game/GameWrapper"
|
|||||||
import Answers from "@/components/game/states/Answers"
|
import Answers from "@/components/game/states/Answers"
|
||||||
import Leaderboard from "@/components/game/states/Leaderboard"
|
import Leaderboard from "@/components/game/states/Leaderboard"
|
||||||
import Question from "@/components/game/states/Question"
|
import Question from "@/components/game/states/Question"
|
||||||
import Result from "@/components/game/states/Result"
|
|
||||||
import Start from "@/components/game/states/Start"
|
import Start from "@/components/game/states/Start"
|
||||||
import Wait from "@/components/game/states/Wait"
|
import Wait from "@/components/game/states/Wait"
|
||||||
import { usePlayerContext } from "@/context/player"
|
import { usePlayerContext } from "@/context/player"
|
||||||
@@ -15,7 +14,7 @@ const gameStateComponent = {
|
|||||||
SELECT_ANSWER: Answers,
|
SELECT_ANSWER: Answers,
|
||||||
SHOW_QUESTION: Question,
|
SHOW_QUESTION: Question,
|
||||||
WAIT: Wait,
|
WAIT: Wait,
|
||||||
SHOW_RESULT: Result,
|
SHOW_RESPONSES: Answers,
|
||||||
SHOW_LEADERBOARD: Leaderboard,
|
SHOW_LEADERBOARD: Leaderboard,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,3 +50,12 @@
|
|||||||
transform: scale(1);
|
transform: scale(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@keyframes progressBar {
|
||||||
|
from {
|
||||||
|
width: 0%;
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user