|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import './PlayerApp.css'; |
| 3 | +import ErrorBoundary from "./Error.jsx"; |
| 4 | +import MessageBus from "./MessageBus.js"; |
| 5 | +import Player from "./Player.js"; |
| 6 | +import Map2d from "./map/Map2d.jsx"; |
| 7 | +import InfoPanel from "./panel/InfoPanel.jsx"; |
| 8 | +import '../libs/wasm_exec.js'; |
| 9 | +import './protos/Message_pb.js' |
| 10 | + |
| 11 | +export function PlayerApp() { |
| 12 | + const [messageBus] = useState(new MessageBus()) |
| 13 | + const [player] = useState(new Player(messageBus)) |
| 14 | + const [serverHost] = useState(window.location.host.includes("localhost") ? "http://localhost:8080" : ""); |
| 15 | + const [isWasmLoaded, setIsWasmLoaded] = useState(false) |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + console.log("run run run") |
| 19 | + |
| 20 | + if (!isWasmLoaded) { |
| 21 | + loadWasm(); |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + const urlParams = new URLSearchParams(window.location.search); |
| 26 | + const channel = new BroadcastChannel(urlParams.get("uuid")); |
| 27 | + channel.addEventListener("message", e => { |
| 28 | + console.log("received", e, isWasmLoaded) |
| 29 | + window.testt(e.data, function (data) { |
| 30 | + if (data instanceof Uint8Array) { |
| 31 | + const msg = proto.Message.deserializeBinary(data).toObject() |
| 32 | + messageBus.emit(msg) |
| 33 | + } else { |
| 34 | + // text frame |
| 35 | + // console.log(event.data); |
| 36 | + console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); |
| 37 | + messageBus.emit(JSON.parse(data)) |
| 38 | + } |
| 39 | + |
| 40 | + // console.log(`[message] Data received from server: ${event.data}`); |
| 41 | + // let msg = JSON.parse(event.data) |
| 42 | + // messageBus.emit(msg) |
| 43 | + }) |
| 44 | + }); |
| 45 | + messageBus.listen([13], function (msg) { |
| 46 | + alert(msg.message) |
| 47 | + // window.testt(byteArray) |
| 48 | + }) |
| 49 | + // Connect(this.messageBus) |
| 50 | + |
| 51 | + async function loadWasm() { |
| 52 | + const go = new window.Go(); |
| 53 | + WebAssembly.instantiateStreaming(fetch(serverHost + "/wasm"), go.importObject) |
| 54 | + .then((result) => { |
| 55 | + go.run(result.instance); |
| 56 | + console.log("should be loaded now") |
| 57 | + setIsWasmLoaded(true) |
| 58 | + // window.withDownload("https://corsproxy.io/?" + encodeURIComponent("https://github.com/sparkoo/csgo-2d-demo-viewer/raw/refs/heads/master/test_demos/1-c26b4e22-66ac-4904-87cc-3b2b65a67ddb-1-1.dem.gz")) |
| 59 | + // fetch("https://corsproxy.io/?" + encodeURIComponent("https://github.com/sparkoo/csgo-2d-demo-viewer/raw/refs/heads/master/test_demos/1-c26b4e22-66ac-4904-87cc-3b2b65a67ddb-1-1.dem.gz")) |
| 60 | + // .then((result) => { |
| 61 | + // console.log(result) |
| 62 | + // result.arrayBuffer().then(b => { |
| 63 | + // const data = new Uint8Array(b) |
| 64 | + // console.log(data) |
| 65 | + // window.testt(data, function (data) { |
| 66 | + // if(data instanceof Uint8Array) { |
| 67 | + // const msg = proto.Message.deserializeBinary(data).toObject() |
| 68 | + // messageBus.emit(msg) |
| 69 | + // } else { |
| 70 | + // // text frame |
| 71 | + // // console.log(event.data); |
| 72 | + // console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); |
| 73 | + // messageBus.emit(JSON.parse(data)) |
| 74 | + // } |
| 75 | + |
| 76 | + // // console.log(`[message] Data received from server: ${event.data}`); |
| 77 | + // // let msg = JSON.parse(event.data) |
| 78 | + // // messageBus.emit(msg) |
| 79 | + // }) |
| 80 | + // }) |
| 81 | + // }) |
| 82 | + // .catch(err => console.log(err)) |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + }, [isWasmLoaded]) |
| 87 | + |
| 88 | + |
| 89 | + return ( |
| 90 | + <ErrorBoundary> |
| 91 | + <div className="grid-container"> |
| 92 | + <div className="grid-item map"> |
| 93 | + <Map2d messageBus={messageBus} /> |
| 94 | + </div> |
| 95 | + <div className="grid-item infoPanel"> |
| 96 | + <InfoPanel messageBus={messageBus} /> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </ErrorBoundary>); |
| 100 | +} |
0 commit comments