-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
393c6d0
commit 6082985
Showing
5 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react' | ||
import { Game, Option } from '../types/Game' | ||
|
||
export default function useCremona( | ||
game: Game, | ||
onFinish: (score: number) => void, | ||
) { | ||
const [state, setState] = useState({ | ||
currentIndex: 0, | ||
successfulChoices: 0, | ||
}) | ||
|
||
const update = (selectedOption: Option) => { | ||
setState((prev) => { | ||
const currentIndex = prev.currentIndex + 1 | ||
const isValid = | ||
game.questions[prev.currentIndex].validOption === selectedOption.id | ||
const successfulChoices = isValid | ||
? prev.successfulChoices + 1 | ||
: prev.successfulChoices | ||
|
||
if (game.questions.length <= currentIndex) { | ||
onFinish(successfulChoices * 10) | ||
} | ||
|
||
return { | ||
...prev, | ||
currentIndex, | ||
successfulChoices, | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
...state, | ||
totalQuestions: game.questions.length, | ||
gameEnded: game.questions.length <= state.currentIndex, | ||
update, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import classnames from 'classnames' | ||
import React, { useState } from 'react' | ||
import Heading from '../../../ui/Heading' | ||
import { useRouter } from 'next/router' | ||
import { useDocument } from '@nandorojo/swr-firestore' | ||
import { Game, Option } from '../../../types/Game' | ||
import { Player } from '../../../types/Player' | ||
import Button from '../../../ui/Button' | ||
import useCremona from '../../../hooks/useCremona' | ||
|
||
const PlayerId = () => { | ||
const router = useRouter() | ||
const gameId = Array.isArray(router.query.gameId) | ||
? router.query.gameId[0] | ||
: router.query.gameId | ||
const playerId = Array.isArray(router.query.playerId) | ||
? router.query.playerId[0] | ||
: router.query.playerId | ||
|
||
const { data: game, error, loading } = useDocument<Game>( | ||
gameId ? `challenges/${gameId}` : null, | ||
{ | ||
parseDates: ['createdAt'], | ||
listen: true, | ||
}, | ||
) | ||
const { | ||
data: player, | ||
error: errorPlayer, | ||
loading: loadingPlayer, | ||
update, | ||
} = useDocument<Player>( | ||
playerId ? `challenges/${gameId}/players/${playerId}` : null, | ||
{ | ||
listen: true, | ||
}, | ||
) | ||
|
||
if (loading || loadingPlayer) { | ||
return <p className="italic text-center">Loading...</p> | ||
} | ||
|
||
if (error || errorPlayer) { | ||
return <p className="italic text-center">There was an error.</p> | ||
} | ||
|
||
if (!game || !game.id || !player) { | ||
return ( | ||
<p className="italic text-center">There is no challenge or player.</p> | ||
) | ||
} | ||
|
||
/* TODO: verify game and player status */ | ||
if (player.status === 'finished') { | ||
/* return <PlayerScore /> */ | ||
return <p>You finished.</p> | ||
} | ||
|
||
const onFinish = (score: number) => { | ||
console.log(score) | ||
|
||
update({ | ||
status: 'finished', | ||
score, | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<Heading type="h1">{game.name}</Heading> | ||
{player.status === 'playing' ? ( | ||
<PlayerGame game={game} onFinish={onFinish} /> | ||
) : ( | ||
<Button | ||
onClick={() => { | ||
update({ | ||
status: 'playing', | ||
}) | ||
}} | ||
> | ||
Play | ||
</Button> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default PlayerId | ||
|
||
type GameProps = { | ||
game: Game | ||
onFinish: (score: number) => void | ||
} | ||
|
||
function PlayerGame({ game, onFinish }: GameProps) { | ||
const { | ||
currentIndex, | ||
totalQuestions, | ||
update, | ||
gameEnded, | ||
successfulChoices, | ||
} = useCremona(game, onFinish) | ||
const [selectedOption, setSelectedOption] = useState<Option | null>(null) | ||
|
||
const currentQuestion = game.questions[currentIndex] | ||
|
||
if (gameEnded) { | ||
return <Heading type="h2">You got {successfulChoices * 10} points.</Heading> | ||
} | ||
|
||
return ( | ||
<div className=""> | ||
<div className="mb-4"> | ||
<Heading type="h2">{currentQuestion.description}</Heading> | ||
</div> | ||
<ul className="flex flex-wrap"> | ||
{currentQuestion.options.map((option) => ( | ||
<li key={option.id} className="w-1/2"> | ||
<button | ||
className={classnames([ | ||
'block px-4 py-2 my-4 bg-white border w-full text-left', | ||
selectedOption && | ||
currentQuestion.validOption === option.id && | ||
'bg-green-300', | ||
selectedOption && | ||
currentQuestion.validOption !== option.id && | ||
'bg-red-300', | ||
])} | ||
onClick={() => { | ||
setSelectedOption(option) | ||
}} | ||
disabled={!!selectedOption} | ||
> | ||
{selectedOption?.id === option.id && '😎 '} | ||
{option.content} | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
<p className="mt-4 text-center italic"> | ||
Question {currentIndex + 1} of {totalQuestions} | ||
</p> | ||
{selectedOption && ( | ||
<Button | ||
onClick={() => { | ||
update(selectedOption) | ||
setSelectedOption(null) | ||
}} | ||
> | ||
{currentIndex === totalQuestions - 1 ? 'Finish' : 'Next'} | ||
</Button> | ||
)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type Player = { | ||
name: string | ||
email: string | ||
score: number | ||
status: 'created' | 'playing' | 'finished' | ||
} |
6082985
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: