diff --git a/src/components/GameController.tsx b/src/components/GameController.tsx new file mode 100644 index 0000000..080bb44 --- /dev/null +++ b/src/components/GameController.tsx @@ -0,0 +1,134 @@ +'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 eecb224..0baa6fb 100644 --- a/src/components/riddles/Riddle_2.tsx +++ b/src/components/riddles/Riddle_2.tsx @@ -1,6 +1,6 @@ "use client"; -import { useMemo, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import OldButton from "../OldButton"; import styles from "@/css/RiddleStyles.module.css"; import { useLocalStorage } from "@/hooks/useLocalStorage"; @@ -12,6 +12,8 @@ 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"; export default function Riddle_2({ page, @@ -70,6 +72,7 @@ export default function Riddle_2({

{currentPlayer.name},

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

+
{ diff --git a/src/components/screens/Tour.tsx b/src/components/screens/Tour.tsx index b27ad31..9c4ce05 100644 --- a/src/components/screens/Tour.tsx +++ b/src/components/screens/Tour.tsx @@ -176,8 +176,8 @@ export default function Tour() { }, []); const [ stepStr, setStep ] = useLocalStorage(PREF.step, "0"); - const [ activeRiddle, setActiveRiddle ] = useState(null); - const [ clickedMarker, setClickedMarker ] = useState(null); + const [ activeRiddle, setActiveRiddle ] = useState(riddleData["riddles"][1]); + const [ clickedMarker, setClickedMarker ] = useState(2); const [ tourCode, setTourCode ] = useLocalStorage(PREF.code, ""); const [ completedRiddlesStr, setCompletedRiddles ] = useLocalStorage(PREF.completed_riddles, ""); const [ , setPlayers ] = useLocalStorage(PREF.players, ""); diff --git a/src/utils/codeChecker.ts b/src/utils/codeChecker.ts new file mode 100644 index 0000000..7f606c6 --- /dev/null +++ b/src/utils/codeChecker.ts @@ -0,0 +1 @@ +// TODO: CodeChecker \ No newline at end of file diff --git a/src/utils/const.ts b/src/utils/const.ts index 2bf2981..5509dc8 100644 --- a/src/utils/const.ts +++ b/src/utils/const.ts @@ -9,4 +9,6 @@ export enum PREF { readerState = "PREF_LAST_READER_STATE", voiceover = "PREF_VOICEOVER_STATE", boughtHints = "PREF_BOUGHT_HINTS" -}; \ No newline at end of file +}; + +export const API_PREFIX = "http://localhost:8080"; \ No newline at end of file diff --git a/src/utils/swordTalk.ts b/src/utils/swordTalk.ts new file mode 100644 index 0000000..4d85f3f --- /dev/null +++ b/src/utils/swordTalk.ts @@ -0,0 +1,15 @@ +// TODO: THISSSSS + +import { API_PREFIX } from "./const"; + +export async function getSwordState() { + const response = await fetch(API_PREFIX + "/sword", { + "method": "GET", + "cache": "no-cache" + }); + console.log(response); + if (response.ok) { + console.log(`${await response.text()}`); + } + return response; +} \ No newline at end of file