From 2fa28ce3d42b7b0c40323399b6b9baa94950eab7 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Fri, 22 Sep 2023 18:24:05 -0700 Subject: [PATCH] Add arrow for fast play --- ui/src/Game.tsx | 165 ++++++++++++++++++++++++++++++++++++---------- ui/src/UI.tsx | 84 ++++++++++++++++++----- ui/src/style.less | 16 +++++ ui/src/utils.ts | 23 ++++++- 4 files changed, 234 insertions(+), 54 deletions(-) diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx index bc83b2a..daa936f 100644 --- a/ui/src/Game.tsx +++ b/ui/src/Game.tsx @@ -1,15 +1,22 @@ import * as React from "react"; +import {useEffect, useMemo, useReducer, useRef, useState} from "react"; import { GameState, - GameWasm, Letter, - Letter as LetterData, + GameWasm, MyResult, PlayedTile, PlayerAndScore, ScoreResult, - Tray, TurnAction, TurnAdvanceResult + Tray, + TurnAction, + TurnAdvanceResult } from "../../pkg/word_grid"; import { + Direction, + GRID_LENGTH, + GridArrowData, + GridArrowDispatchAction, + GridArrowDispatchActionType, HighlightableLetterData, LocationType, matchCoordinate, @@ -19,7 +26,6 @@ import { TileDispatchAction, TileDispatchActionType } from "./utils"; -import {useEffect, useMemo, useReducer, useRef, useState} from "react"; import {TileExchangeModal} from "./TileExchange"; import {Grid, Scores, TileTray} from "./UI"; @@ -42,6 +48,94 @@ export function Game(props: { const [isGameOver, setGameOver] = useState(false); const [confirmedScorePoints, setConfirmedScorePoints] = useState(-1); + const [boardLetters, setBoardLetters] = useState(() => { + const newLetterData = [] as HighlightableLetterData[]; + for(let i=0; i) { + + const result: MyResult = props.wasm.exchange_tiles(selectedArray); + + if(result.response_type === "ERR") { + logDispatch(
{(result.value as string)}
); + } else { + handlePlayerAction(result.value as TurnAction, props.settings.playerName); + setTurnCount(turnCount + 1); + + if(result.game_state.type === "Ended") { + endGame(result.game_state); + } + } + + } + + function addWordFn(word: string) { + props.wasm.add_word(word); + } + + + const [gridArrow, gridArrowDispatch] = useReducer(adjustGridArrow, null); + const [logInfo, logDispatch] = useReducer(addLogInfo, []); + + function movePlayableLetters(playerLetters: PlayableLetterData[], update: TileDispatchAction) { if(update.action === TileDispatchActionType.RETRIEVE) { @@ -79,7 +173,26 @@ export function Game(props: { } return playerLetters.slice(); } else if (update.action === TileDispatchActionType.RETURN) { + gridArrowDispatch({action: GridArrowDispatchActionType.CLEAR}); return mergeTrays(playerLetters, playerLetters); + } else if (update.action === TileDispatchActionType.MOVE_TO_ARROW) { + // let's verify that the arrow is defined, otherwise do nothing + if(gridArrow != null) { + const end_position = { + location: LocationType.GRID, + index: gridArrow.position, + }; + gridArrowDispatch({ + action: GridArrowDispatchActionType.SHIFT, + }); + return movePlayableLetters(playerLetters, { + action: TileDispatchActionType.MOVE, + start: update.start, + end: end_position, + }); + } else { + return playerLetters; + } } else { console.error("Unknown tray update"); console.error({update}); @@ -87,44 +200,14 @@ export function Game(props: { } - - function exchangeFunction(selectedArray: Array) { - - const result: MyResult = props.wasm.exchange_tiles(selectedArray); - - if(result.response_type === "ERR") { - logDispatch(
{(result.value as string)}
); - } else { - handlePlayerAction(result.value as TurnAction, props.settings.playerName); - setTurnCount(turnCount + 1); - - if(result.game_state.type === "Ended") { - endGame(result.game_state); - } - } - - } - - function addWordFn(word: string) { - props.wasm.add_word(word); - } - const [playerLetters, trayDispatch] = useReducer(movePlayableLetters, []); - const [logInfo, logDispatch] = useReducer(addLogInfo, []); const [turnCount, setTurnCount] = useState(1); const playerAndScores: PlayerAndScore[] = useMemo(() => { return props.wasm.get_scores(); }, [turnCount, isGameOver]); - const [boardLetters, setBoardLetters] = useState(() => { - const newLetterData = [] as HighlightableLetterData[]; - for(let i=0; i<15*15; i++) { - newLetterData.push(undefined); - } - return newLetterData; - }); useEffect(() => { const newLetterData = props.wasm.get_board_letters() as HighlightableLetterData[]; @@ -188,6 +271,9 @@ export function Game(props: { else if(action.type == "Pass"){ logDispatch(
{playerName} passed.
); } + + // Clear any on-screen arrows + gridArrowDispatch({action: GridArrowDispatchActionType.CLEAR}); } function endGame(state: GameState) { @@ -303,7 +389,14 @@ export function Game(props: { exchangeFunction={exchangeFunction} />
- +
- +