206 lines
9.5 KiB
TypeScript
206 lines
9.5 KiB
TypeScript
"use client";
|
|
|
|
import { useLocalStorage } from "@/hooks/useLocalStorage"
|
|
import { API_PREFIX, PREF } from "@/utils/const"
|
|
import ChargedButton from "../ChargedButton";
|
|
import styles from "@/css/Tour.module.css";
|
|
import MapView, { PlaceMarker } from "../MapView";
|
|
import { act, MouseEventHandler, useEffect, useMemo, useState } from "react";
|
|
import { usePermissions } from "@/hooks/usePermission";
|
|
import PermissionCheck from "../PermissionCheck";
|
|
import clsx from "clsx";
|
|
import OldButton from "../OldButton";
|
|
import RiddleScreen from "../RiddleScreen";
|
|
import OldPopUp from "../OldPopUp";
|
|
import { MAX_POINTS, MIN_POINTS } from "@/utils/points";
|
|
import { Riddle } from "@/types/riddle";
|
|
import * as maplibregl from 'maplibre-gl';
|
|
import { createStationName } from "@/utils/stringer";
|
|
import { getRiddleData } from "@/utils/riddledata";
|
|
import { heartbeat } from "@/utils/sessionHandler";
|
|
import { getCompletedRiddles } from "@/utils/completedRiddles";
|
|
|
|
export default function Tour() {
|
|
const [ showTooFar, setShowTooFar ] = useState(false);
|
|
const [ completedRiddlesStr, ] = useLocalStorage(PREF.completed_riddles, "");
|
|
const [ riddleDataStr, ] = useLocalStorage(PREF.riddle_data, "");
|
|
const riddleData: Riddle[] = useMemo(() => {
|
|
return getRiddleData();
|
|
}, [riddleDataStr]);
|
|
|
|
useEffect(() => {
|
|
if (!riddleData) {
|
|
return;
|
|
}
|
|
|
|
const completedRiddles = getCompletedRiddles();
|
|
const nextStep = completedRiddles.length;
|
|
|
|
const markers = [];
|
|
const data = riddleData.find(v => v.step === nextStep);
|
|
if (data) {
|
|
markers.push({
|
|
id: data.id,
|
|
lng: data.longitude,
|
|
lat: data.latitude
|
|
});
|
|
}
|
|
setMarkers(markers);
|
|
}, [riddleData, completedRiddlesStr]);
|
|
|
|
const [ stepStr, setStep ] = useLocalStorage(PREF.step, "0");
|
|
const [ activeRiddle, setActiveRiddle ] = useState<Riddle | null>(null);
|
|
const [ clickedMarker, setClickedMarker ] = useState<number | null>(null);
|
|
const [ tourCode, setTourCode ] = useLocalStorage(PREF.code, "");
|
|
|
|
const [ players, setPlayers ] = useLocalStorage(PREF.players, "");
|
|
const [ pointsStr, setPoints ] = useLocalStorage(PREF.points, "0");
|
|
const [ markers, setMarkers ] = useState<PlaceMarker[]>([]);
|
|
const { gpsStatus, cameraStatus } = usePermissions();
|
|
const points = Number.parseInt(pointsStr);
|
|
const [ worthyAnswersStr, ] = useLocalStorage(PREF.worthyWords, "");
|
|
const [ boughtHintsStr, ] = useLocalStorage(PREF.boughtHints, "");
|
|
const [ playerCountStr, ] = useLocalStorage(PREF.playerCount, "");
|
|
const [ voiceOverStr, ] = useLocalStorage(PREF.voiceover, "");
|
|
|
|
const permissionsGranted = gpsStatus === "granted" && cameraStatus === "granted";
|
|
|
|
const _dismissFunc = () => {
|
|
setActiveRiddle(null);
|
|
setClickedMarker(null);
|
|
};
|
|
|
|
const dismissFunc: MouseEventHandler<HTMLImageElement> = (e) => {
|
|
_dismissFunc();
|
|
e.stopPropagation();
|
|
};
|
|
|
|
const [ code, ] = useLocalStorage(PREF.code, "");
|
|
|
|
useEffect(() => {
|
|
if (code.length === 0) {
|
|
return;
|
|
}
|
|
const intervalId = setInterval(() => {
|
|
heartbeat({
|
|
"player_count": playerCountStr,
|
|
"players": players,
|
|
"worthy_answers": worthyAnswersStr,
|
|
"completed_riddles": completedRiddlesStr,
|
|
"points": pointsStr,
|
|
"bought_hints": boughtHintsStr,
|
|
"voice_over": voiceOverStr
|
|
}, code);
|
|
}, 50000);
|
|
|
|
return () => {
|
|
clearInterval(intervalId);
|
|
};
|
|
}, [playerCountStr, players, worthyAnswersStr, completedRiddlesStr, pointsStr, boughtHintsStr, voiceOverStr]);
|
|
|
|
const selectedActiveRiddleData = clickedMarker !== null ? riddleData.find(v => v.id === clickedMarker) as Riddle : null;
|
|
|
|
return (
|
|
<div className={styles["screen-container"]}>
|
|
<div className={styles["top-bar"]}>
|
|
<div className={styles["top-bar-content"]}>
|
|
<div className={styles["corner-bar"]}>
|
|
<img src="/container/Ecke_Verzierung.webp" className={clsx(styles["corner"])} style={{rotate: "90deg"}} />
|
|
<img src="/container/Ecke_Verzierung.webp" className={clsx(styles["corner"])} style={{rotate: "180deg"}} />
|
|
</div>
|
|
<img src="/icons/krone.svg" className={clsx(styles["crown"])} />
|
|
<p className="eisenkraft-regular">Rufpunkte</p>
|
|
<span className={styles["points-outer-box"]}>
|
|
<svg
|
|
style={{position: "absolute", inset: "0", width: "100%", height: "100%", overflow: "visible"}}
|
|
viewBox="0 0 290 20"
|
|
preserveAspectRatio="none"
|
|
>
|
|
<rect width="100%" height="100%" fill="none" />
|
|
<path d="M 6 0 H 284 Q 284 6 290 6 V 14 Q 284 14 284 20 H 6 Q 6 14 0 14 V 6 Q 6 6 6 0 Z"
|
|
fill="none"
|
|
stroke="#71654d"
|
|
strokeWidth="2"
|
|
vectorEffect="non-scaling-stroke"
|
|
/>
|
|
</svg>
|
|
<div className={styles["points-inner-box"]} style={{width: `${((points+Math.abs(MIN_POINTS))/(MAX_POINTS+Math.abs(MIN_POINTS)))*100}%`}}>
|
|
</div>
|
|
</span>
|
|
<p className="eisenkraft-regular" style={{marginTop: "4px"}}>{`${points} / ${MAX_POINTS}`}</p>
|
|
</div>
|
|
</div>
|
|
<div className={styles["main-content"]}>
|
|
<div className={styles["map"]}>
|
|
<MapView markers={markers} onMarkerClick={(marker, position) => {
|
|
console.log(marker, position);
|
|
if (!position) {
|
|
return;
|
|
}
|
|
|
|
const r = riddleData.find(v => v.id === marker.id) as Riddle;
|
|
if (!r) {
|
|
alert("Invalid Riddle ID!");
|
|
return;
|
|
}
|
|
|
|
const userLngLat = new maplibregl.LngLat(position[0], position[1]);
|
|
const markerLngLat = new maplibregl.LngLat(marker.lng, marker.lat);
|
|
|
|
const distanceMeters = userLngLat.distanceTo(markerLngLat);
|
|
const isNearby = distanceMeters <= r.requiredDistance;
|
|
|
|
if (isNearby) {
|
|
setClickedMarker(marker.id);
|
|
} else {
|
|
setShowTooFar(true);
|
|
}
|
|
}}></MapView>
|
|
</div>
|
|
{ clickedMarker && <div className={styles["riddle-overlay"]}>
|
|
{ activeRiddle === null ?
|
|
<OldPopUp onDismiss={dismissFunc}>
|
|
<div style={{textAlign: "center", width: "100%"}}>
|
|
<p className="eisenkraft-regular" style={{fontSize: "1.25rem"}}>{createStationName(selectedActiveRiddleData)}</p>
|
|
<p className="eisenkraft-regular" style={{fontSize: "1.75rem", textWrap: "nowrap"}}>{selectedActiveRiddleData?.name}</p>
|
|
<OldButton
|
|
red={false}
|
|
long={true}
|
|
style={{margin: "auto", marginTop: "8px"}}
|
|
onAction={(e) => {
|
|
setActiveRiddle(riddleData.find(v => v.id === clickedMarker) as Riddle);
|
|
e.stopPropagation();
|
|
}}
|
|
>STARTEN</OldButton>
|
|
</div>
|
|
</OldPopUp>
|
|
:
|
|
<RiddleScreen onDismiss={_dismissFunc} riddle={activeRiddle}></RiddleScreen>
|
|
}
|
|
</div> }
|
|
{ showTooFar && <div className={styles["riddle-overlay"]}>
|
|
<OldPopUp onDismiss={() => setShowTooFar(false)}>
|
|
<div style={{textAlign: "center", width: "100%"}}>
|
|
<p className="lancelot-regular" style={{fontSize: "1.3rem", marginTop: "8px"}}>Ihr seid leider noch zu weit von der Station entfernt!</p>
|
|
<OldButton
|
|
red={true}
|
|
long={true}
|
|
style={{margin: "auto", marginTop: "16px"}}
|
|
onAction={(e) => {
|
|
setShowTooFar(false);
|
|
e.stopPropagation();
|
|
}}
|
|
>SCHLIEßEN</OldButton>
|
|
</div>
|
|
</OldPopUp>
|
|
</div> }
|
|
{ !permissionsGranted ?
|
|
<div className={styles["permissions-overlay"]}>
|
|
<PermissionCheck className={styles["permission-check"]}></PermissionCheck>
|
|
</div>
|
|
: null
|
|
}
|
|
</div>
|
|
</div>
|
|
)
|
|
} |