171 lines
7.5 KiB
TypeScript
171 lines
7.5 KiB
TypeScript
import { useLocalStorage } from "@/hooks/useLocalStorage"
|
|
import { PREF } from "@/utils/const"
|
|
import ChargedButton from "../ChargedButton";
|
|
import { usePermissions } from "@/hooks/usePermission";
|
|
import styles from "@/css/Intro.module.css";
|
|
import { ChangeEvent, InputEvent, MouseEvent, MouseEventHandler, PointerEvent, useRef, useState } from "react";
|
|
import Image from "next/image";
|
|
import PermissionCheck from "../PermissionCheck";
|
|
import { PermissionStateResult } from "@/utils/permissions";
|
|
import StyledContainer from "../StyledContainer";
|
|
import OldButton from "../OldButton";
|
|
import OldTextInput from "../OldTextInput";
|
|
import clsx from "clsx";
|
|
import { MAX_PLAYERS, MIN_PLAYERS } from "@/utils/players";
|
|
|
|
export default function Intro() {
|
|
const [ stepStr, setStep ] = useLocalStorage(PREF.step, "0");
|
|
const [ , setPoints ] = useLocalStorage(PREF.points, "0");
|
|
const [ , setCompletedRiddles ] = useLocalStorage(PREF.completed_riddles, "");
|
|
const [ , setPlayers ] = useLocalStorage(PREF.players, "");
|
|
const [ , setWorthyWords ] = useLocalStorage(PREF.worthyWords, "");
|
|
const [ rewatchScreen, setRewatchScreen ] = useState(false);
|
|
const [ enterPlayerCount, setEnterPlayerCount ] = useState(false);
|
|
const [ videoEnded, setVideoEnded ] = useState(false);
|
|
const [ videoPaused, setVideoPaused ] = useState(false);
|
|
const [ voiceOverState, setVoiceOverState ] = useLocalStorage(PREF.voiceover, "off");
|
|
const [ boughtHintsStr, setBoughtHints ] = useLocalStorage(PREF.boughtHints, "");
|
|
const introVideoRef = useRef<HTMLVideoElement | null>(null);
|
|
const [ granted, setGranted ] = useState(false);
|
|
|
|
const [ , setPlayerCount ] = useLocalStorage(PREF.playerCount, "");
|
|
const [ playerCountStr, setPlayerCountStr] = useState("");
|
|
|
|
const validPlayerCount = (number: number) => {
|
|
return !isNaN(number) && (number <= MAX_PLAYERS && number >= MIN_PLAYERS);
|
|
};
|
|
|
|
const handleVideoClick = (e: PointerEvent<HTMLVideoElement>): boolean => {
|
|
e.currentTarget.play();
|
|
return true;
|
|
};
|
|
|
|
const handlePermissions = (gps: PermissionStateResult, cam: PermissionStateResult) => {
|
|
setGranted(gps === "granted" && cam === "granted");
|
|
};
|
|
|
|
const handleChange = (event: ChangeEvent<HTMLInputElement>): boolean => {
|
|
const value = event.target.value;
|
|
const parsedValue = Number.parseInt(value);
|
|
|
|
if (isNaN(parsedValue)) {
|
|
if (value.length > 0) {
|
|
event.preventDefault();
|
|
return false;
|
|
}
|
|
}
|
|
//parsedValue = Math.min(MAX_PLAYERS, Math.max(MIN_PLAYERS, parsedValue));
|
|
|
|
setPlayerCountStr(parsedValue.toString());
|
|
return true;
|
|
};
|
|
|
|
const isPlayerCountValid = validPlayerCount(Number.parseInt(playerCountStr));
|
|
|
|
return (
|
|
<div className="screen-container">
|
|
<img src={"/bg/Hintergrund_ohne_Verzierung.webp"} style={{
|
|
position: "absolute",
|
|
top: 0,
|
|
left: 0,
|
|
width: "100%",
|
|
height: "100%",
|
|
objectFit: "fill",
|
|
zIndex: 1
|
|
}} alt="Background" loading="eager"/>
|
|
<StyledContainer innerClassName={styles["container"]}>
|
|
{ !enterPlayerCount || !granted ?
|
|
<div>
|
|
<PermissionCheck onRefresh={handlePermissions}></PermissionCheck>
|
|
<OldButton
|
|
red={false}
|
|
long={true}
|
|
onAction={() => {
|
|
setRewatchScreen(false);
|
|
setEnterPlayerCount(true);
|
|
}}
|
|
style={{margin: "auto", marginTop: "3rem"}}
|
|
disabled={!granted}
|
|
>WEITER</OldButton>
|
|
</div>
|
|
: !rewatchScreen ?
|
|
<div className="flex-center">
|
|
<p style={{color: "var(--blue)", fontSize: "2rem"}} className="eisenkraft-regular">Spieleranzahl</p>
|
|
<p>Wie viele Abenteurer seid ihr?</p>
|
|
<OldTextInput
|
|
type="number"
|
|
value={playerCountStr}
|
|
onChange={handleChange}
|
|
aria-label="Spieleranzahl"
|
|
placeholder=""
|
|
style={{
|
|
width: "200px",
|
|
textAlign: "center",
|
|
}}/>
|
|
{ !isPlayerCountValid && !isNaN(Number.parseInt(playerCountStr)) ?
|
|
<p style={{color: "red", fontSize: "1.1rem"}}>Die Spieleranzahl beträgt 2 bis 8 Personen.</p>
|
|
: null }
|
|
<br></br>
|
|
<ChargedButton onAction={() => {
|
|
setPlayerCount(playerCountStr);
|
|
setRewatchScreen(true);
|
|
}} holdDuration={500} disabled={!isPlayerCountValid}>
|
|
ABENTEUER BEGINNEN!
|
|
</ChargedButton>
|
|
<p className={clsx("lancelot-italic")} style={{marginTop: "1rem"}}>Auf dem Knopf halten, um ihn aufzuladen!</p>
|
|
</div>
|
|
:
|
|
<div className={styles["intro-video-container"]}>
|
|
<video
|
|
ref={introVideoRef}
|
|
disablePictureInPicture={true}
|
|
autoPlay={true}
|
|
loop={false}
|
|
autoFocus={true}
|
|
width={"100%"}
|
|
onEnded={() => setVideoEnded(true)}
|
|
onAbort={() => setVideoEnded(true)}
|
|
onError={() => setVideoEnded(true)}
|
|
onPause={() => setVideoPaused(true)}
|
|
onPlay={() => setVideoPaused(false)}
|
|
onClick={handleVideoClick}
|
|
onContextMenu={(e) => {
|
|
e.preventDefault();
|
|
return false;
|
|
}}
|
|
playsInline={true}
|
|
>
|
|
<source src="/res/intro/intro.mp4" type="video/mp4"/>
|
|
</video>
|
|
{ !videoEnded && videoPaused ?
|
|
<div className={styles["pause-screen"]}>
|
|
<Image src="/icons/pause.svg" alt="Pause Icon" width={48} height={48}/>
|
|
</div>
|
|
:
|
|
null
|
|
}
|
|
{ videoEnded ? <div className={styles["rewatch-controls"]}>
|
|
<ChargedButton holdDuration={500} onAction={() => {
|
|
if (introVideoRef.current) {
|
|
introVideoRef.current.pause();
|
|
introVideoRef.current.currentTime = 0;
|
|
introVideoRef.current.play();
|
|
setVideoEnded(false);
|
|
}
|
|
}} whiteText={true}>Video wiederholen?</ChargedButton>
|
|
<ChargedButton holdDuration={500} onAction={() => {
|
|
setPoints("0");
|
|
setCompletedRiddles("");
|
|
setBoughtHints("");
|
|
setPlayers("");
|
|
setWorthyWords("");
|
|
setVoiceOverState("on");
|
|
setStep("2");
|
|
}} whiteText={true}>Wir sind bereit!</ChargedButton>
|
|
</div> : null }
|
|
</div>
|
|
}
|
|
</StyledContainer>
|
|
</div>
|
|
)
|
|
} |