Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: wheel of fortune advanced features #43

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Sessions/Pages/SessionDetail.astro
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const translationIcon = await ImportSvg("translation");
{entry.topics.map((t) => <SessionTopic brightness="dark" topic={t} />)}
</div>
<>
<p class="text-lg">{t("session.room")}: {entry.room}</p>
<p class="text-lg">{entry.room}</p>
{
isScheduled && (
<p class="text-lg">
Expand Down
53 changes: 44 additions & 9 deletions src/react/pages/app/LeaderBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { Button, Checkbox } from "react-daisyui";
import { IoMdArrowRoundBack } from "react-icons/io";
import { capitalizeFirstLetter, colorConverter, COLORS_LIST, shuffle, useLeaderboard, type LeaderBoardUser } from "../../utils";
import { TitleBar } from "../../components/TitleBar";
import { Box, MultiSelect, NumberInput, Button as MantineButton, Modal, Chip, Container, Space } from "@mantine/core";
import { Box, MultiSelect, NumberInput, Button as MantineButton, Modal, Chip, Container, Space, Switch } from "@mantine/core";
import ConfettiExplosion from 'react-confetti-explosion';
import { PiNavigationArrowDuotone } from "react-icons/pi";
import { set } from "firebase/database";

const UserRow = ({ user, pos, style }: { user: LeaderBoardUser, pos?: number, style?: CSSProperties }) => {
const UserRow = ({ user, pos, style, hidePoints }: { user: LeaderBoardUser, pos?: number, style?: CSSProperties, hidePoints?:boolean }) => {

return <div
className="flex justify-between items-center p-2 border rounded-xl w-full"
Expand All @@ -23,7 +24,7 @@ const UserRow = ({ user, pos, style }: { user: LeaderBoardUser, pos?: number, st
</div>
<div className="ml-5"><b>{user.nickname}</b></div>
</div>
<div className="mr-5"><b>{user.score} points</b></div>
<div className="mr-5">{ !hidePoints && <b>{user.score} points</b> }</div>
</div>
}

Expand All @@ -44,13 +45,18 @@ export const LeaderBoard = () => {
const [spinningArray, setSpinningArray] = useState<LeaderBoardUser[]>([])
const [wheelTrigger, setWheelTrigger] = useState(false)
const [lastSpeed, setLastSpeed] = useState(0)
const [finalUserToSelect, setFinalUserToSelect] = useState(1)
const [userToSelect, setUserToSelect] = useState(1)
const [instantSpinning, setInstantSpinning] = useState(false)

const selectedQuizNum = Math.floor(spinningArray.length/2)
const borderUsers = 2
const usersInWheel = borderUsers*2+userToSelect

useEffect(() => {
if (wheelStatus == "spinning") {
const copyOfArray = filteredAndSortedUsers.copyWithin(0,filteredAndSortedUsers.length)
setSpinningArray(shuffle(copyOfArray).splice(0, 5))
setFinalUserToSelect(userToSelect)
setSpinningArray(shuffle(copyOfArray).splice(0, usersInWheel))
setLastSpeed(0)
setWheelTrigger(!wheelTrigger)
}
Expand All @@ -65,7 +71,7 @@ export const LeaderBoard = () => {
useEffect(() => {
if (!showWheel || wheelStatus != "spinning") return
setLastSpeed(lastSpeed + (lastSpeed < 80 ? 2 : lastSpeed < 150 ? 8 : lastSpeed < 300 ? 20 : 30))
if (lastSpeed >= 500) {
if (lastSpeed >= 500 || instantSpinning) {
setWheelStatus("stopped")
return
}
Expand Down Expand Up @@ -93,6 +99,15 @@ export const LeaderBoard = () => {
return res[0]
}

useEffect(()=>{
setInstantSpinning(false)
setUserToSelect(1)
}, [showWheel])

const isUserSelected = (userIndex:number):boolean => {
return (userIndex-borderUsers)<finalUserToSelect && (userIndex-borderUsers)>=0
}

const filteredAndSortedUsers = users.filter((ele)=>{
//Group filtering
if (groupFilter.length == 0) return true
Expand Down Expand Up @@ -216,17 +231,37 @@ export const LeaderBoard = () => {
{spinningArray.map((user, i) => (
<Box key={i} style={{ display: "flex", justifyContent:"center", alignItems: "center" }}>
<Box style={{ width: 80, display: "flex", justifyContent:"center", alignItems: "center" }}>
{i == selectedQuizNum && <>
{isUserSelected(i) && <>
<PiNavigationArrowDuotone style={{ rotate: "135deg"}} size={40} />
<Space h="md" />
</>}
</Box>
<UserRow user={user} style={{ marginBottom: 10, marginTop: 10, opacity: selectedQuizNum==i?1:0.4 }} />
<UserRow user={user} style={{ marginBottom: 10, marginTop: 10, opacity: isUserSelected(i)?1:0.4 }} hidePoints />
</Box>
))}
</Container>
</Box> }
<Box className="justify-center flex mt-[20vh]">
<Box className="justify-center flex mt-[3vh] gap-5">
{
wheelStatus != "spinning" && <NumberInput
onChange={(v) => v != "" && setUserToSelect(parseInt(v.toString()))}
value={userToSelect}
min={1}
label="Number of users to extract"
max={users.length-borderUsers*2}
step={1}
/>
}
{
wheelStatus != "spinning" && <Switch
onChange={(v) => setInstantSpinning(v.target.checked)}
checked={instantSpinning}
className="mt-8"
label="Instant Spinning ™️"
/>
}
</Box>
<Box className="justify-center flex my-[10vh]">
{wheelStatus != "spinning" && <MantineButton onClick={()=> setWheelStatus("spinning")} disabled={filteredAndSortedUsers.length == 0}>
{wheelStatus == "stopped"? "Spin the wheel again": "Spin the wheel"}
</MantineButton>}
Expand Down