From 44e3b97f14d5dfe449b185f2a73f476122653600 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Mon, 7 Aug 2023 22:12:29 -0700 Subject: [PATCH] Start using reducer to track tile locations --- ui/src/elements.tsx | 73 ++++++++++++++++++++++++++++++++++++++++++++- ui/src/index.tsx | 17 ++--------- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/ui/src/elements.tsx b/ui/src/elements.tsx index 26b0bcf..54be1c2 100644 --- a/ui/src/elements.tsx +++ b/ui/src/elements.tsx @@ -1,5 +1,7 @@ import * as React from "react"; -import {Letter as LetterData} from "word_grid"; +import {GameWasm, Letter as LetterData, Tray} from "word_grid"; +import {createRoot} from "react-dom/client"; +import {useReducer, useState} from "react"; export enum LocationType { GRID, @@ -13,6 +15,75 @@ export interface CoordinateData { export type PlayableLetterData = LetterData & CoordinateData; +function matchCoordinate(playerLetters: PlayableLetterData[], coords: CoordinateData): number { + + for (let i=0; i { + let tray: Tray = props.wasm.get_tray(); + + // initial state + let letters: PlayableLetterData[] = tray.letters.map((ld, i) => { + ld["location"] = LocationType.TRAY; + ld["index"] = i; + return ld as PlayableLetterData; + }) + + return letters; + }) + + + return <> + + + ; + + +} + export function Letter(props: { data: LetterData }): React.JSX.Element { return
{props.data.text}
diff --git a/ui/src/index.tsx b/ui/src/index.tsx index c975ce1..22e3254 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -1,7 +1,8 @@ import init, {greet, GameWasm} from '../node_modules/word_grid/word_grid.js'; -import {LocationType, PlayableLetterData, TileTray} from "./elements"; +import {Game, LocationType, PlayableLetterData, TileTray} from "./elements"; import {createRoot} from "react-dom/client"; import {Tray} from "word_grid"; +import * as React from "react"; async function run() { // First up we need to actually load the wasm file, so we use the @@ -32,22 +33,10 @@ async function run() { // modes await init(); - greet("Heyo!"); - let game = new GameWasm(BigInt(1234)); - let tray: Tray = game.get_tray(); - - // initial state - let letters: PlayableLetterData[] = tray.letters.map((ld, i) => { - ld["location"] = LocationType.TRAY; - ld["index"] = i; - return ld as PlayableLetterData; - }) - - console.log({tray}); const root = createRoot(document.getElementById("root")); - root.render(); + root.render(); }