Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
do not sort always
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake committed Nov 30, 2024
1 parent 5f7d7ac commit 6c8afff
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions frontend/src/components/mahjong/Mahjong.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ export function Mahjong({ level }: { level: LevelInfo }) {
const [elapsedTime, setElapsedTime] = useState(0)
const [tiles, setTiles] = useState<TileT[]>([])

// 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<TileT | null>(null)
const [mergedAt, setMergedAt] = useState<{ x: number, y: number } | null>(null)
const [hint, setHint] = useState<[TileT, TileT] | null>(null)
Expand All @@ -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
})

Expand Down Expand Up @@ -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
}

0 comments on commit 6c8afff

Please sign in to comment.