Add display of previous tiles

This commit is contained in:
Joel Therrien 2023-08-17 18:05:20 -07:00
parent fd7cf2d6af
commit c61b2e3f75
2 changed files with 37 additions and 21 deletions

View file

@ -54,6 +54,16 @@ impl GameWasm {
serde_wasm_bindgen::to_value(&cell_types) serde_wasm_bindgen::to_value(&cell_types)
} }
pub fn get_board_letters(&self) -> Result<JsValue, Error> {
let board = self.0.get_board();
let letters: Vec<Option<Letter>> = board.cells.iter().map(|cell| -> Option<Letter> {
cell.value.clone()
}).collect();
serde_wasm_bindgen::to_value(&letters)
}
pub fn receive_play(&mut self, player: &str, tray_tile_locations: JsValue, commit_move: bool) -> Result<JsValue, JsValue> { pub fn receive_play(&mut self, player: &str, tray_tile_locations: JsValue, commit_move: bool) -> Result<JsValue, JsValue> {
let tray_tile_locations: Vec<Option<PlayedTile>> = serde_wasm_bindgen::from_value(tray_tile_locations)?; let tray_tile_locations: Vec<Option<PlayedTile>> = serde_wasm_bindgen::from_value(tray_tile_locations)?;

View file

@ -1,6 +1,6 @@
import * as React from "react"; import * as React from "react";
import {GameWasm, Letter as LetterData, MyResult, PlayedTile, PlayerAndScore, Tray, WordResult} from "word_grid"; import {GameWasm, Letter as LetterData, MyResult, PlayedTile, PlayerAndScore, Tray, WordResult} from "word_grid";
import {useEffect, useMemo, useReducer, useState} from "react"; import {JSX, useEffect, useMemo, useReducer, useState} from "react";
export enum LocationType { export enum LocationType {
GRID, GRID,
@ -132,19 +132,7 @@ export function Game(props: {wasm: GameWasm}) {
} }
const [playerLetters, trayDispatch] = useReducer(movePlayableLetters, null, (_) => { const [playerLetters, trayDispatch] = useReducer(movePlayableLetters, []);
let tray: Tray = props.wasm.get_tray("Player");
// initial state
let letters: PlayableLetterData[] = tray.letters.map((ld, i) => {
ld["location"] = LocationType.TRAY;
ld["index"] = i;
return ld as PlayableLetterData;
})
return letters;
});
const [logInfo, logDispatch] = useReducer(addLogInfo, []); const [logInfo, logDispatch] = useReducer(addLogInfo, []);
@ -152,7 +140,11 @@ export function Game(props: {wasm: GameWasm}) {
const [turnCount, setTurnCount] = useState<number>(1); const [turnCount, setTurnCount] = useState<number>(1);
const playerAndScores: PlayerAndScore[] = useMemo(() => { const playerAndScores: PlayerAndScore[] = useMemo(() => {
return props.wasm.get_scores(); return props.wasm.get_scores();
}, [turnCount]) }, [turnCount]);
const boardLetters: LetterData[] = useMemo(() => {
return props.wasm.get_board_letters();
}, [turnCount]);
useEffect(() => { useEffect(() => {
logDispatch(<h4>Turn {turnCount}</h4>); logDispatch(<h4>Turn {turnCount}</h4>);
setConfirmedScorePoints(-1); setConfirmedScorePoints(-1);
@ -164,7 +156,7 @@ export function Game(props: {wasm: GameWasm}) {
return <> return <>
<div className="board-log"> <div className="board-log">
<Grid cellTypes={cellTypes} playerLetters={playerLetters} dispatch={trayDispatch}/> <Grid cellTypes={cellTypes} playerLetters={playerLetters} boardLetters={boardLetters} dispatch={trayDispatch}/>
<div className="message-log"> <div className="message-log">
<Scores playerScores={playerAndScores}/> <Scores playerScores={playerAndScores}/>
<div className="log"> <div className="log">
@ -301,16 +293,30 @@ export function TileTray(props: { letters: Array<PlayableLetterData>, trayLength
} }
function Grid(props: {cellTypes: CellType[], playerLetters: Array<PlayableLetterData>, dispatch: TileDispatch}) { function Grid(props: {
cellTypes: CellType[],
playerLetters: Array<PlayableLetterData>,
boardLetters: LetterData[],
dispatch: TileDispatch}) {
const elements = props.cellTypes.map((ct, i) => { const elements = props.cellTypes.map((ct, i) => {
const {className, text} = cell_type_to_details(ct); const {className, text} = cell_type_to_details(ct);
return <div key={i} className={"grid-spot " + className}> let tileElement: JSX.Element;
if (props.boardLetters[i] !== undefined) {
tileElement = <Letter data={props.boardLetters[i]} />;
} else {
tileElement = <>
<span>{text}</span> <span>{text}</span>
<TileSlot <TileSlot
tile={undefined} tile={undefined}
location={{location: LocationType.GRID, index: i}} location={{location: LocationType.GRID, index: i}}
dispatch={props.dispatch} /> dispatch={props.dispatch} /></>;
}
return <div key={i} className={"grid-spot " + className}>
{tileElement}
</div>; </div>;
}); });