From 4c8d27b65916e4c1d6c3edf57fcce23fb56e1821 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Sat, 19 Aug 2023 14:47:34 -0700 Subject: [PATCH] Improve tray handling Fix bugs when the tray is not fully filled, and retain custom user tray ordering between turns. --- ui/src/elements.tsx | 58 ++++++++++++++++++++++++++++++++++++++------- ui/src/index.tsx | 2 +- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/ui/src/elements.tsx b/ui/src/elements.tsx index cf9402e..4058248 100644 --- a/ui/src/elements.tsx +++ b/ui/src/elements.tsx @@ -2,6 +2,10 @@ import * as React from "react"; import {GameWasm, Letter as LetterData, MyResult, PlayedTile, PlayerAndScore, Tray, WordResult} from "word_grid"; import {ChangeEvent, JSX, useEffect, useMemo, useReducer, useState} from "react"; +export interface Settings { + trayLength: number; +} + export enum LocationType { GRID, TRAY @@ -63,7 +67,7 @@ function matchCoordinate(playerLetters: PlayableLetterData[], coords: Coordinate for (let i=0; i { return props.wasm.get_board_cell_types(); @@ -99,12 +103,46 @@ export function Game(props: {wasm: GameWasm}) { if(update.action === TileDispatchActionType.RETRIEVE) { let tray: Tray = props.wasm.get_tray("Player"); + + // we may need to check against the existing tray to retain whatever user reorderings there are + const freeSpots = new Array(); + for (let i = 0; i { + return x !== undefined && x !== null; + }).forEach((x) => { + if (x.location === LocationType.TRAY) { + freeSpots[x.index] = null; + } + }); + + const firstNotNull = (): number => { + for (let i of freeSpots) { + if (i !== null) { + freeSpots[i] = null; + return i; + } + } + + return null; + } + // initial state let letters: PlayableLetterData[] = tray.letters.map((ld, i) => { - ld["location"] = LocationType.TRAY; - ld["index"] = i; + if(ld !== undefined) { + ld["location"] = LocationType.TRAY; + + if (playerLetters[i] !== undefined && playerLetters[i] !== null && playerLetters[i].location === LocationType.TRAY) { + ld["index"] = playerLetters[i].index; + } else { + ld["index"] = firstNotNull(); + } + } return ld as PlayableLetterData; - }) + }); + return letters; } else if (update.action === TileDispatchActionType.MOVE) { @@ -175,7 +213,7 @@ export function Game(props: {wasm: GameWasm}) { - +