|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import reactLogo from "./assets/react.svg"; |
| 3 | +import viteLogo from "/vite.svg"; |
| 4 | +import "./App.css"; |
| 5 | +import { init } from "@dojoengine/sdk"; |
| 6 | + |
| 7 | +import { dojoConfig } from "../dojoConfig.ts"; |
| 8 | +import { Subscription } from "@dojoengine/torii-wasm"; |
| 9 | + |
| 10 | +interface Moves { |
| 11 | + player: string; |
| 12 | + remaining: number; |
| 13 | + last_direction: Direction; |
| 14 | + can_move: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +interface DirectionsAvailable { |
| 18 | + player: string; |
| 19 | + directions: Direction[]; |
| 20 | +} |
| 21 | + |
| 22 | +interface Position { |
| 23 | + player: string; |
| 24 | + vec: Vec2; |
| 25 | +} |
| 26 | + |
| 27 | +enum Direction { |
| 28 | + None, |
| 29 | + Left, |
| 30 | + Right, |
| 31 | + Up, |
| 32 | + Down, |
| 33 | +} |
| 34 | + |
| 35 | +interface Vec2 { |
| 36 | + x: number; |
| 37 | + y: number; |
| 38 | +} |
| 39 | + |
| 40 | +type Schema = { |
| 41 | + dojo_starter: { |
| 42 | + moves: Moves; |
| 43 | + directionsAvailable: DirectionsAvailable; |
| 44 | + position: Position; |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +const db = await init<Schema>({ |
| 49 | + rpcUrl: dojoConfig.rpcUrl, |
| 50 | + toriiUrl: dojoConfig.toriiUrl, |
| 51 | + relayUrl: dojoConfig.relayUrl, |
| 52 | + worldAddress: |
| 53 | + "0x5d475a9221f6cbf1a016b12400a01b9a89935069aecd57e9876fcb2a7bb29da", |
| 54 | +}); |
| 55 | + |
| 56 | +function App() { |
| 57 | + // useEffect(() => { |
| 58 | + // let unsubscribe: Subscription | undefined; |
| 59 | + |
| 60 | + // const subscribe = async () => { |
| 61 | + // try { |
| 62 | + // unsubscribe = await db.subscribeQuery( |
| 63 | + // { |
| 64 | + // moves: {}, |
| 65 | + // }, |
| 66 | + // (resp) => { |
| 67 | + // if (resp.error) { |
| 68 | + // console.error( |
| 69 | + // "Error querying moves:", |
| 70 | + // resp.error.message |
| 71 | + // ); |
| 72 | + // return; |
| 73 | + // } |
| 74 | + // if (resp.data) { |
| 75 | + // console.log("Moves for player 123:", resp.data); |
| 76 | + // } |
| 77 | + // }, |
| 78 | + // { logging: true } |
| 79 | + // ); |
| 80 | + // } catch (error) { |
| 81 | + // console.error("Subscription error:", error); |
| 82 | + // } |
| 83 | + // }; |
| 84 | + |
| 85 | + // subscribe(); |
| 86 | + |
| 87 | + // return () => { |
| 88 | + // if (unsubscribe) { |
| 89 | + // unsubscribe.cancel(); |
| 90 | + // console.log("Unsubscribed from moves query"); |
| 91 | + // } |
| 92 | + // }; |
| 93 | + // }, []); |
| 94 | + |
| 95 | + useEffect(() => { |
| 96 | + const fetchEntities = async () => { |
| 97 | + try { |
| 98 | + const entities = await db.getEntities( |
| 99 | + { |
| 100 | + dojo_starter: { |
| 101 | + moves: { |
| 102 | + $: { |
| 103 | + where: { |
| 104 | + remaining: { |
| 105 | + $eq: 97, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + (resp) => { |
| 113 | + if (resp.error) { |
| 114 | + console.error( |
| 115 | + "Error querying completed important tasks:", |
| 116 | + resp.error.message |
| 117 | + ); |
| 118 | + return; |
| 119 | + } |
| 120 | + if (resp.data) { |
| 121 | + console.log( |
| 122 | + "Completed important tasks:", |
| 123 | + resp.data |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + ); |
| 128 | + console.log("Queried entities:", entities.dojo_starter); |
| 129 | + } catch (error) { |
| 130 | + console.error("Error querying entities:", error); |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + fetchEntities(); |
| 135 | + }, []); |
| 136 | + return <></>; |
| 137 | +} |
| 138 | + |
| 139 | +export default App; |
0 commit comments