diff --git a/socket/src/roles/player.js b/socket/src/roles/player.js index 1054014..1a23cd0 100644 --- a/socket/src/roles/player.js +++ b/socket/src/roles/player.js @@ -17,7 +17,7 @@ const Player = { id: socket.id, points: 0, } - socket.to(player.room).emit("manager:newPlayer", playerData) + socket.to(player.room).emit("manager:newPlayer", { ...playerData }) game.players.push(playerData) diff --git a/socket/src/utils/round.js b/socket/src/utils/round.js index 6d98094..7e9eae1 100644 --- a/socket/src/utils/round.js +++ b/socket/src/utils/round.js @@ -28,6 +28,11 @@ const sleep = (sec) => new Promise((r) => setTimeout(r, sec * 1000)) export const startRound = async (game, io, socket) => { const question = game.questions[game.currentQuestion] + io.to(game.room).emit("game:updateQuestion", { + current: game.currentQuestion + 1, + total: game.questions.length, + }) + io.to(game.room).emit("game:status", { name: "SHOW_QUESTION", data: { diff --git a/src/components/game/GameWrapper.jsx b/src/components/game/GameWrapper.jsx index 18f4adb..ad01e54 100644 --- a/src/components/game/GameWrapper.jsx +++ b/src/components/game/GameWrapper.jsx @@ -2,10 +2,22 @@ import Image from "next/image" import Button from "@/components/Button" import background from "@/assets/2238431_1694.jpg" import { usePlayerContext } from "@/context/player" +import { useSocketContext } from "@/context/socket" +import { useState } from "react" -export default function GameWrapper({ children }) { +export default function GameWrapper({ children, onNext, manager }) { + const { socket } = useSocketContext() const { player } = usePlayerContext() + const [questionState, setQuestionState] = useState() + + socket.on("game:updateQuestion", ({ current, total }) => { + setQuestionState({ + current, + total, + }) + }) + return (
@@ -16,10 +28,20 @@ export default function GameWrapper({ children }) {
-
- 1/10 -
- + {questionState && ( +
+ {`${questionState.current} / ${questionState.total}`} +
+ )} + + {manager && ( + + )}
{children} diff --git a/src/components/game/join/Username.jsx b/src/components/game/join/Username.jsx index 51c8afd..2bbe61f 100644 --- a/src/components/game/join/Username.jsx +++ b/src/components/game/join/Username.jsx @@ -19,6 +19,7 @@ export default function Username() { payload: username, }) + socket.emit("player:join", { username: username, room: player.room }) router.push("/game") } diff --git a/src/components/game/states/Leaderboard.jsx b/src/components/game/states/Leaderboard.jsx index b955908..7ca705a 100644 --- a/src/components/game/states/Leaderboard.jsx +++ b/src/components/game/states/Leaderboard.jsx @@ -1,24 +1,16 @@ -export default function Leaderboard({ data }) { +export default function Leaderboard({ data: { leaderboard } }) { return ( -
+

Leaderboard

-
- Username - 10000 -
- -
- Username - 10000 -
- -
- Username - 10000 -
+ {leaderboard.map(({ username, points }) => ( +
+ {username} + {points} +
+ ))}
) diff --git a/src/components/game/states/Question.jsx b/src/components/game/states/Question.jsx index 3a2ba8d..2cae8ba 100644 --- a/src/components/game/states/Question.jsx +++ b/src/components/game/states/Question.jsx @@ -19,8 +19,8 @@ export default function Question({ data: { question } }) {
) diff --git a/src/components/game/states/Start.jsx b/src/components/game/states/Start.jsx new file mode 100644 index 0000000..22da620 --- /dev/null +++ b/src/components/game/states/Start.jsx @@ -0,0 +1,38 @@ +import Loader from "@/components/Loader" +import { useSocketContext } from "@/context/socket" +import { useEffect, useState } from "react" + +export default function Start({ data: { text } }) { + const { socket } = useSocketContext() + const [playerList, setPlayerList] = useState([]) + + socket.on("manager:newPlayer", (player) => { + setPlayerList([...playerList, player]) + }) + + useEffect(() => { + socket.emit("manager:createRoom") + }, []) + + return ( +
+

+ {text} +

+ +
+ {playerList.map((player) => ( +
socket.emit("manager:kickPlayer", player.id)} + > + + {player.username} + +
+ ))} +
+
+ ) +} diff --git a/src/pages/game.jsx b/src/pages/game.jsx index 2571e80..02f46fe 100644 --- a/src/pages/game.jsx +++ b/src/pages/game.jsx @@ -14,6 +14,7 @@ const gameStateComponent = { SHOW_QUESTION: Question, WAIT: Wait, SHOW_RESULT: Result, + SHOW_LEADERBOARD: Leaderboard, } export default function Game() { @@ -27,8 +28,6 @@ export default function Game() { return } - socket.emit("player:join", { username: "Test", room: player.room }) - const [state, setState] = useState({ status: { name: "WAIT", @@ -42,17 +41,15 @@ export default function Game() { socket.on("game:status", (status) => { setState({ - ...gameState, + ...state, status: status, + question: { + ...state.question, + current: status.question, + }, }) }) - /*socket.on("game:", (question) => { - setState({ - ...state, - - })*/ - return ( {gameStateComponent[state.status.name] && diff --git a/src/pages/manager.jsx b/src/pages/manager.jsx index b1a1599..28e64fb 100644 --- a/src/pages/manager.jsx +++ b/src/pages/manager.jsx @@ -2,62 +2,71 @@ 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" import { useSocketContext } from "@/context/socket" -import { createElement, useEffect, useMemo, useState } from "react" +import { useRouter } from "next/router" +import { createElement, useState } from "react" -export default function Manager({ children }) { +const gameStateComponent = { + SHOW_START: Start, + SELECT_ANSWER: Answers, + SHOW_QUESTION: Question, + WAIT: Wait, + SHOW_RESULT: Result, + SHOW_LEADERBOARD: Leaderboard, +} + +export default function Manager() { const { socket } = useSocketContext() + const { player } = usePlayerContext() - const [gameState, setGameState] = useState({ + const [state, setState] = useState({ status: { - name: "PENDING", + name: "SHOW_START", + data: { text: "Waiting for the players" }, + }, + question: { + current: 1, + total: null, }, - cooldown: 0, - answers: [], }) socket.on("game:status", (status) => { - setGameState({ - ...gameState, + setState({ + ...state, status: status, - cooldown: 0, + question: { + ...state.question, + current: status.question, + }, }) }) + const handleSkip = () => { + switch (state.status.name) { + case "SHOW_START": + socket.emit("manager:startGame") + break + + case "SHOW_RESPONSES": + socket.emit("manager:showLeaderboard") + break + + case "SHOW_LEADERBOARD": + socket.emit("manager:nextQuestion") + break + } + } + return ( - <> -

{JSON.stringify(gameState)}

-
-
- - - - - - - -
- + + {gameStateComponent[state.status.name] && + createElement(gameStateComponent[state.status.name], { + data: state.status.data, + })} + ) } diff --git a/src/pages/managerold.jsx b/src/pages/managerold.jsx new file mode 100644 index 0000000..b1a1599 --- /dev/null +++ b/src/pages/managerold.jsx @@ -0,0 +1,63 @@ +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 Wait from "@/components/game/states/Wait" +import { useSocketContext } from "@/context/socket" +import { createElement, useEffect, useMemo, useState } from "react" + +export default function Manager({ children }) { + const { socket } = useSocketContext() + + const [gameState, setGameState] = useState({ + status: { + name: "PENDING", + }, + cooldown: 0, + answers: [], + }) + + socket.on("game:status", (status) => { + setGameState({ + ...gameState, + status: status, + cooldown: 0, + }) + }) + + return ( + <> +

{JSON.stringify(gameState)}

+
+
+ + + + + + + +
+ + ) +} diff --git a/src/pages/question.jsx b/src/pages/question.jsx deleted file mode 100644 index 11f0f60..0000000 --- a/src/pages/question.jsx +++ /dev/null @@ -1,43 +0,0 @@ -import Image from "next/image" -import Form from "@/components/Form" -import Button from "@/components/Button" -import Input from "@/components/Input" -import logo from "@/assets/logo.svg" -import { useEffect, useRef, useState } from "react" -import Loader from "@/components/Loader" -import { useSocketContext } from "@/context/socket" -import background from "@/assets/2238431_1694.jpg" - -export default function Questionasas() { - const [loading, setLoading] = useState(false) - - const socket = useSocketContext() - const barRef = useRef(null) - - useEffect(() => { - setTimeout(() => { - barRef.current.style.width = "100%" - }) - }, []) - - return ( -
-
- -
- -

- Lorem ipsum dolor sit ametorrupti. Alias, recusandae officia. -

- -
-
- ) -}