Rebrand to kaboot
This commit is contained in:
parent
156c210dea
commit
8a8ec9bc0e
8 changed files with 59 additions and 27 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { Quiz, Player, GameState, GameRole, NetworkMessage, AnswerOption, Question } from '../types';
|
||||
import { generateQuiz } from '../services/geminiService';
|
||||
import { POINTS_PER_QUESTION, QUESTION_TIME } from '../constants';
|
||||
import { POINTS_PER_QUESTION, QUESTION_TIME, PLAYER_COLORS } from '../constants';
|
||||
import { Peer, DataConnection } from 'peerjs';
|
||||
|
||||
export const useGame = () => {
|
||||
|
|
@ -19,6 +19,8 @@ export const useGame = () => {
|
|||
const [selectedOption, setSelectedOption] = useState<AnswerOption | null>(null);
|
||||
const [currentPlayerScore, setCurrentPlayerScore] = useState(0);
|
||||
const [currentStreak, setCurrentStreak] = useState(0);
|
||||
const [currentPlayerId, setCurrentPlayerId] = useState<string | null>(null);
|
||||
const [currentPlayerName, setCurrentPlayerName] = useState<string | null>(null);
|
||||
|
||||
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const peerRef = useRef<Peer | null>(null);
|
||||
|
|
@ -71,20 +73,23 @@ export const useGame = () => {
|
|||
const pin = generateGamePin();
|
||||
setGamePin(pin);
|
||||
|
||||
const peer = new Peer(`openhoot-${pin}`);
|
||||
const peer = new Peer(`kaboot-${pin}`);
|
||||
peerRef.current = peer;
|
||||
|
||||
peer.on('open', (id) => {
|
||||
const hostPlayer: Player = {
|
||||
id: 'host',
|
||||
name: 'Host (You)',
|
||||
name: 'Host',
|
||||
score: 0,
|
||||
streak: 0,
|
||||
lastAnswerCorrect: null,
|
||||
isBot: false,
|
||||
avatarSeed: Math.random()
|
||||
avatarSeed: Math.random(),
|
||||
color: PLAYER_COLORS[0]
|
||||
};
|
||||
setPlayers([hostPlayer]);
|
||||
setCurrentPlayerId('host');
|
||||
setCurrentPlayerName('Host');
|
||||
setGameState('LOBBY');
|
||||
});
|
||||
|
||||
|
|
@ -103,21 +108,23 @@ export const useGame = () => {
|
|||
|
||||
const handleHostData = (conn: DataConnection, data: NetworkMessage) => {
|
||||
if (data.type === 'JOIN') {
|
||||
const newPlayer: Player = {
|
||||
id: conn.peer,
|
||||
name: data.payload.name,
|
||||
score: 0,
|
||||
streak: 0,
|
||||
lastAnswerCorrect: null,
|
||||
isBot: false,
|
||||
avatarSeed: Math.random()
|
||||
};
|
||||
setPlayers(prev => {
|
||||
if (prev.find(p => p.id === newPlayer.id)) return prev;
|
||||
if (prev.find(p => p.id === conn.peer)) return prev;
|
||||
const colorIndex = prev.length % PLAYER_COLORS.length;
|
||||
const newPlayer: Player = {
|
||||
id: conn.peer,
|
||||
name: data.payload.name,
|
||||
score: 0,
|
||||
streak: 0,
|
||||
lastAnswerCorrect: null,
|
||||
isBot: false,
|
||||
avatarSeed: Math.random(),
|
||||
color: PLAYER_COLORS[colorIndex]
|
||||
};
|
||||
return [...prev, newPlayer];
|
||||
});
|
||||
connectionsRef.current.set(conn.peer, conn);
|
||||
conn.send({ type: 'WELCOME', payload: { playerId: conn.peer, quizTitle: 'OpenHoot', players: [] } });
|
||||
conn.send({ type: 'WELCOME', payload: { playerId: conn.peer, quizTitle: 'Kaboot', players: [] } });
|
||||
}
|
||||
|
||||
if (data.type === 'ANSWER') {
|
||||
|
|
@ -247,11 +254,13 @@ export const useGame = () => {
|
|||
setRole('CLIENT');
|
||||
setError(null);
|
||||
setGamePin(pin);
|
||||
setCurrentPlayerName(name);
|
||||
const peer = new Peer();
|
||||
peerRef.current = peer;
|
||||
|
||||
peer.on('open', () => {
|
||||
const conn = peer.connect(`openhoot-${pin}`);
|
||||
peer.on('open', (id) => {
|
||||
setCurrentPlayerId(id);
|
||||
const conn = peer.connect(`kaboot-${pin}`);
|
||||
hostConnectionRef.current = conn;
|
||||
conn.on('open', () => {
|
||||
conn.send({ type: 'JOIN', payload: { name } });
|
||||
|
|
@ -369,7 +378,7 @@ export const useGame = () => {
|
|||
}, []);
|
||||
|
||||
return {
|
||||
role, gameState, quiz, players, currentQuestionIndex, timeLeft, error, gamePin, hasAnswered, lastPointsEarned, currentCorrectShape, selectedOption, currentPlayerScore, currentStreak,
|
||||
role, gameState, quiz, players, currentQuestionIndex, timeLeft, error, gamePin, hasAnswered, lastPointsEarned, currentCorrectShape, selectedOption, currentPlayerScore, currentStreak, currentPlayerId,
|
||||
startQuizGen, startManualCreation, finalizeManualQuiz, joinGame, startGame: startHostGame, handleAnswer, nextQuestion
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue