R7, R2, Heartbeat, Sessions, Code, cleanup, Outro
This commit is contained in:
1 parent
b5e5cc502d
commit
5b9fe3ea21
25 files changed
+346
-116
No files matched your search
@@ -4,7 +4,7 @@ import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import OldButton from "../OldButton";
|
||||
import styles from "@/css/RiddleStyles.module.css";
|
||||
import { useLocalStorage } from "@/hooks/useLocalStorage";
|
||||
import { PREF } from "@/utils/const";
|
||||
import { API_PREFIX, PREF, WS_SCHEME } from "@/utils/const";
|
||||
import { RiddleScreenControls } from "../RiddleScreen";
|
||||
import { addPoints } from "@/utils/points";
|
||||
import RiddleCompleteBanner from "../RiddleCompleteBanner";
|
||||
@@ -12,17 +12,44 @@ import { getPlayers } from "@/utils/players";
|
||||
import VoiceOver from "../VoiceOver";
|
||||
import RiddleHintPage from "../RiddleHintPage";
|
||||
import { Player } from "@/types/player";
|
||||
import { getSwordState } from "@/utils/swordTalk";
|
||||
import GameController from "../GameController";
|
||||
import { addCompletedRiddle } from "@/utils/completedRiddles";
|
||||
|
||||
export type ConnectionStatus = "disconnected" | "connected";
|
||||
export type ConnectionStatus = "disconnected" | "connected" | "connecting";
|
||||
|
||||
interface MQTTMessage {
|
||||
topic: string;
|
||||
payload: string;
|
||||
}
|
||||
|
||||
let awaitTryAbort = false;
|
||||
|
||||
/**
|
||||
* Waits until a condition function returns true or a timeout occurs.
|
||||
*
|
||||
* @param {Function} conditionFn - A function that returns a boolean.
|
||||
* @param {number} timeoutMs - Timeout in milliseconds (default: 3000ms).
|
||||
* @param {number} intervalMs - Polling interval in milliseconds (default: 100ms).
|
||||
* @returns {Promise<boolean>} Resolves to true if condition is met, rejects on timeout.
|
||||
*/
|
||||
function waitUntilTrue(conditionFn: () => boolean, timeoutMs = 3000, intervalMs = 100) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const startTime = Date.now();
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
if (conditionFn()) {
|
||||
clearInterval(intervalId);
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Date.now() - startTime >= timeoutMs) {
|
||||
clearInterval(intervalId);
|
||||
reject(new Error("Timeout"));
|
||||
}
|
||||
}, intervalMs);
|
||||
});
|
||||
}
|
||||
|
||||
export default function Riddle_2({
|
||||
page,
|
||||
setPage,
|
||||
@@ -32,10 +59,12 @@ export default function Riddle_2({
|
||||
voiceOverMode,
|
||||
requestingHint,
|
||||
dismissHint,
|
||||
onRegisterCloseInterceptor
|
||||
}: RiddleScreenControls) {
|
||||
const [ canGoNext, setCanGoNext ] = useState(false);
|
||||
const [ isSwordBackIn, setIsSwordBackIn ] = useState(true);
|
||||
const [ playersStr, ] = useLocalStorage(PREF.players, "");
|
||||
const [ tourCode, ] = useLocalStorage(PREF.code, "");
|
||||
|
||||
const [ playerIndex, setPlayerIndex ] = useState(0);
|
||||
const players = useMemo(() => getPlayers(), [playersStr]);
|
||||
@@ -68,52 +97,90 @@ export default function Riddle_2({
|
||||
wsRef.current.send(JSON.stringify({ topic, payload }));
|
||||
return true;
|
||||
} else {
|
||||
alert('WebSocket is not connected.');
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const tourCode = "TESTTEST!";
|
||||
wsRef.current = new WebSocket(`ws://localhost:8080/ws/bridge?tourCode=${tourCode}`);
|
||||
let isCancelled = false;
|
||||
let reconnectTimeout: NodeJS.Timeout;
|
||||
|
||||
wsRef.current.onopen = () => {
|
||||
setStatus("connected");
|
||||
};
|
||||
const connect = () => {
|
||||
if (isCancelled) return;
|
||||
setStatus("connecting");
|
||||
|
||||
const ws = new WebSocket(`${WS_SCHEME}://${API_PREFIX}/ws/bridge?tourCode=${tourCode}`);
|
||||
wsRef.current = ws;
|
||||
|
||||
wsRef.current.onmessage = (event) => {
|
||||
const data: MQTTMessage = JSON.parse(event.data);
|
||||
|
||||
if (data.topic === 'outdoor/king-arthur/sword/stat') {
|
||||
setSwordStatus(data.payload);
|
||||
} else if (data.topic === 'outdoor/king-arthur/throne/stat') {
|
||||
//setThroneStatus(data.payload);
|
||||
} else if (data.topic.includes('/event')) {
|
||||
console.log(`Event received on ${data.topic}:`, data.payload);
|
||||
if (data.payload === "pull-denied") {
|
||||
setCanGoNext(true);
|
||||
} else if (data.payload === "pulled-out") {
|
||||
setIsSwordBackIn(false);
|
||||
} else if (data.payload === "locked") {
|
||||
setIsSwordBackIn(true);
|
||||
} else if (data.payload === "ceremonyDone") {
|
||||
setCanGoNext(true);
|
||||
ws.onopen = () => {
|
||||
if (!isCancelled) {
|
||||
setStatus("connected");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const data: MQTTMessage = JSON.parse(event.data);
|
||||
|
||||
if (data.topic === 'outdoor/king-arthur/sword/stat') {
|
||||
setSwordStatus(data.payload);
|
||||
} else if (data.topic === 'outdoor/king-arthur/throne/stat') {
|
||||
//setThroneStatus(data.payload);
|
||||
} else if (data.topic.includes('/event')) {
|
||||
console.log(`Event received on ${data.topic}:`, data.payload);
|
||||
if (data.payload === "pull-denied") {
|
||||
setCanGoNext(true);
|
||||
} else if (data.payload === "pulled-out") {
|
||||
setIsSwordBackIn(false);
|
||||
} else if (data.payload === "locked") {
|
||||
setIsSwordBackIn(true);
|
||||
} else if (data.payload === "ceremonyDone") {
|
||||
setCanGoNext(true);
|
||||
}
|
||||
} else if (data.topic.includes('/set')) {
|
||||
if (data.payload === "abortTry") {
|
||||
awaitTryAbort = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
if (!isCancelled) {
|
||||
setStatus("disconnected");
|
||||
reconnectTimeout = setTimeout(connect, 3000);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
ws.close();
|
||||
};
|
||||
};
|
||||
|
||||
wsRef.current.onclose = () => {
|
||||
sendCommand('outdoor/king-arthur/sword/set', 'abortTry');
|
||||
setStatus("disconnected");
|
||||
};
|
||||
connect();
|
||||
|
||||
return () => {
|
||||
isCancelled = true;
|
||||
clearTimeout(reconnectTimeout);
|
||||
wsRef.current?.close();
|
||||
};
|
||||
}, []);
|
||||
}, [tourCode]);
|
||||
|
||||
const currentPlayer: Player = players[playerOrder[playerIndex]];
|
||||
|
||||
useEffect(() => {
|
||||
const handleIntercept = async () => {
|
||||
if (page === 1) {
|
||||
awaitTryAbort = false;
|
||||
sendCommand('outdoor/king-arthur/sword/set', 'abortTry');
|
||||
waitUntilTrue(() => {
|
||||
return awaitTryAbort
|
||||
});
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
onRegisterCloseInterceptor(handleIntercept);
|
||||
}, [onRegisterCloseInterceptor]);
|
||||
|
||||
const pages = [
|
||||
(
|
||||
<div key={0}>
|
||||
@@ -135,7 +202,7 @@ export default function Riddle_2({
|
||||
}
|
||||
setCanGoNext(false);
|
||||
setPage(1);
|
||||
}}>WEITER</OldButton>
|
||||
}}>PRÜFUNG STARTEN</OldButton>
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
@@ -213,7 +280,7 @@ export default function Riddle_2({
|
||||
setCanGoNext(false);
|
||||
setPlayerIndex(playerIndex+1);
|
||||
}
|
||||
}} disabled={!canGoNext}>WEITER</OldButton>
|
||||
}} disabled={!canGoNext}>{(playerIndex >= players.length-1) ? "PRÜFUNG BEENDEN" : "NÄCHSTER PRÜFLING"}</OldButton>
|
||||
{playerIndex < 1 ? <OldButton red={true} onAction={() => setPage(0)} className={styles["backButton"]}>ZURÜCK</OldButton> : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user