diff --git a/src/components/GameController.tsx b/src/components/GameController.tsx deleted file mode 100644 index 080bb44..0000000 --- a/src/components/GameController.tsx +++ /dev/null @@ -1,134 +0,0 @@ -'use client'; - -import React, { useEffect, useState, useRef } from 'react'; - -interface MQTTMessage { - topic: string; - payload: string; -} - -export default function GameController() { - const [status, setStatus] = useState('Connecting...'); - const [swordStatus, setSwordStatus] = useState('N/A'); - const [throneStatus, setThroneStatus] = useState('N/A'); - const [ soundInput, setSoundInput ] = useState("Chosen"); - const wsRef = useRef(null); - - useEffect(() => { - const tourCode = "TESTTEST!"; - wsRef.current = new WebSocket(`ws://localhost:8080/ws/bridge?tourCode=${tourCode}`); - - wsRef.current.onopen = () => { - setStatus('Connected to Server Bridge'); - }; - - wsRef.current.onmessage = (event) => { - const data: MQTTMessage = JSON.parse(event.data); - - // Handle incoming messages based on specifications - 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") { - alert("PULL EVENT!"); - } else if (data.payload === "pulled-out") { - alert("PULLED OUT"); - } else if (data.payload === "put-in") { - alert("PUT IN EVENT!"); - } else if (data.payload === "ceremonyDone") { - alert("ceremonyDone"); - } - } - }; - - wsRef.current.onclose = () => { - setStatus('Disconnected from Server Bridge'); - }; - - return () => { - wsRef.current?.close(); - }; - }, []); - - // Function to send commands (Set Data) - const sendCommand = (topic: string, payload: string) => { - if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { - wsRef.current.send(JSON.stringify({ topic, payload })); - } else { - alert('WebSocket is not connected.'); - } - }; - - return ( -
-

King Arthur - Station Control Dashboard

-

Connection State: {status}

- -
- {/* Sword Controls (Excalibur) */} -
-

Excalibur (Sword)

-

Stat:

{swordStatus}

- -

- -

- -

- -

- setSoundInput(e.target.value)}/> -

- -
- - {/* Throne Controls */} - { /*
-

Throne (Coronation)

-

Stat: {throneStatus}

- -

- -
*/ } -
-
- ); -} \ No newline at end of file diff --git a/src/components/riddles/Riddle_2.tsx b/src/components/riddles/Riddle_2.tsx index 0baa6fb..97de2b8 100644 --- a/src/components/riddles/Riddle_2.tsx +++ b/src/components/riddles/Riddle_2.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useMemo, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import OldButton from "../OldButton"; import styles from "@/css/RiddleStyles.module.css"; import { useLocalStorage } from "@/hooks/useLocalStorage"; @@ -15,6 +15,13 @@ import { Player } from "@/types/player"; import { getSwordState } from "@/utils/swordTalk"; import GameController from "../GameController"; +export type ConnectionStatus = "disconnected" | "connected"; + +interface MQTTMessage { + topic: string; + payload: string; +} + export default function Riddle_2({ page, setPage, @@ -25,6 +32,8 @@ export default function Riddle_2({ requestingHint, dismissHint, }: RiddleScreenControls) { + const [ canGoNext, setCanGoNext ] = useState(false); + const [ isSwordBackIn, setIsSwordBackIn ] = useState(true); const [ playersStr, ] = useLocalStorage(PREF.players, ""); const [ playerIndex, setPlayerIndex ] = useState(0); @@ -44,6 +53,63 @@ export default function Riddle_2({ return indices; }, [players]); + const isNextIndexLastPlayer = players.length-2 <= playerIndex; + + const [status, setStatus] = useState("disconnected"); + const [swordStatus, setSwordStatus] = useState('N/A'); + //const [throneStatus, setThroneStatus] = useState('N/A'); + const [ soundInput, setSoundInput ] = useState("Chosen"); + const wsRef = useRef(null); + const [ showControls, setShowControls ] = useState(false); + + useEffect(() => { + const tourCode = "TESTTEST!"; + wsRef.current = new WebSocket(`ws://localhost:8080/ws/bridge?tourCode=${tourCode}`); + + wsRef.current.onopen = () => { + setStatus("connected"); + }; + + 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") { + setCanGoNext(true); + } else if (data.payload === "pulled-out") { + setIsSwordBackIn(false); + } else if (data.payload === "put-in") { + setIsSwordBackIn(true); + } else if (data.payload === "ceremonyDone") { + setCanGoNext(true); + } + } + }; + + wsRef.current.onclose = () => { + setStatus("disconnected"); + }; + + return () => { + wsRef.current?.close(); + }; + }, []); + + const sendCommand = (topic: string, payload: string) => { + if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { + wsRef.current.send(JSON.stringify({ topic, payload })); + return true; + } else { + alert('WebSocket is not connected.'); + return false; + } + }; + const currentPlayer: Player = players[playerOrder[playerIndex]]; const pages = [ @@ -61,7 +127,13 @@ export default function Riddle_2({

Jetzt seid ihr an der Reihe. Die Prophezeiung selbst bestimmt, wer vortreten darf, einer nach dem anderen, bis der Stahl seinen neuen Träger erkennt.

- setPage(1)}>WEITER + { + if (!sendCommand('outdoor/king-arthur/sword/set', 'stayLocked')) { + return; + } + setCanGoNext(false); + setPage(1); + }}>WEITER
), @@ -72,7 +144,53 @@ export default function Riddle_2({

{currentPlayer.name},

zieh nun am Schwert und finde heraus, ob du Excaliburs {currentPlayer.gender === "female" ? "Erbin" : "Erbe"} bist!

- +
+

King Arthur - Station Control Dashboard

+

Connection State: {status}

+ +
+
+

Excalibur (Sword)

+

Stat:

{swordStatus}

+ +

+ +

+ +

+ +

+ setSoundInput(e.target.value)}/> +

+ +
+
+
{ @@ -80,9 +198,19 @@ export default function Riddle_2({ setPage(2); addPoints(riddle.points); } else { + if (isNextIndexLastPlayer) { + if (!sendCommand('outdoor/king-arthur/sword/set', 'releaseLock')) { + return; + } + } else { + if (!sendCommand('outdoor/king-arthur/sword/set', 'stayLocked')) { + return; + } + } + setCanGoNext(false); setPlayerIndex(playerIndex+1); } - }} disabled={false}>WEITER + }} disabled={!canGoNext}>WEITER {playerIndex < 1 ? setPage(0)} className={styles["backButton"]}>ZURÜCK : null}
@@ -102,7 +230,7 @@ export default function Riddle_2({

Erst wenn euer Beweis für die Nachwelt geschaffen ist, steckt Excalibur zurück in den Stein. Denn aus der Ferne erreicht euch noch immer kein Jubel, sondern nur Schweigen. Offenbar genügt es nicht mehr, das Schwert zu ziehen.

- exitFunction()}>ZURÜCK ZUR KARTE + exitFunction()}>ZURÜCK ZUR KARTE
)