From 6c8afff83c7cc0c9308175f1c895121b65416e99 Mon Sep 17 00:00:00 2001 From: evermake Date: Sat, 30 Nov 2024 21:06:34 +0500 Subject: [PATCH] do not sort always --- frontend/src/components/mahjong/Mahjong.tsx | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/frontend/src/components/mahjong/Mahjong.tsx b/frontend/src/components/mahjong/Mahjong.tsx index e36d943..ab9a583 100644 --- a/frontend/src/components/mahjong/Mahjong.tsx +++ b/frontend/src/components/mahjong/Mahjong.tsx @@ -40,17 +40,6 @@ export function Mahjong({ level }: { level: LevelInfo }) { const [elapsedTime, setElapsedTime] = useState(0) const [tiles, setTiles] = useState([]) - // sort tiles by z, then by x, then by y - tiles.sort((a, b) => { - if (a.coord.z !== b.coord.z) { - return a.coord.z - b.coord.z - } - if (a.coord.x !== b.coord.x) { - return a.coord.x - b.coord.x - } - return a.coord.y - b.coord.y - }) - const [selected, setSelected] = useState(null) const [mergedAt, setMergedAt] = useState<{ x: number, y: number } | null>(null) const [hint, setHint] = useState<[TileT, TileT] | null>(null) @@ -77,12 +66,13 @@ export function Mahjong({ level }: { level: LevelInfo }) { level.choices, ) g.onTilesChange = (newTitles) => { + newTitles.sort(compareTiles) setTiles(newTitles) } g.onSelectedTileChange = (newSelected) => { setSelected(newSelected) } - setTiles(g.tiles()) + setTiles(g.tiles().sort(compareTiles)) return g }) @@ -487,3 +477,13 @@ function getScaleToFitField( fieldHeight, } } + +function compareTiles(a: TileT, b: TileT) { + if (a.coord.z !== b.coord.z) { + return a.coord.z - b.coord.z + } + if (a.coord.x !== b.coord.x) { + return a.coord.x - b.coord.x + } + return a.coord.y - b.coord.y +}