Files
buildpath/frontend/utils/buildHelpers.ts

65 lines
1.8 KiB
TypeScript

import { isEmpty } from './helpers'
/**
* Trims the build tree to only show the first path
* Removes alternate build paths to keep the UI clean
*/
export function trimBuilds(builds: Builds): void {
if (!builds?.tree?.children) return
// Keep only the first child (primary build path)
builds.tree.children.splice(1, builds.tree.children.length - 1)
// Also trim grandchildren to first path only
if (builds.tree.children[0]?.children) {
builds.tree.children[0].children.splice(1, builds.tree.children[0].children.length - 1)
}
}
/**
* Removes late game items that appear in the core build tree
* Prevents duplicate items from being shown
*/
export function trimLateGameItems(builds: Builds): void {
if (!builds?.tree || isEmpty(builds.lateGame)) return
const coreItemIds = new Set<number>()
// Collect all item IDs from the tree
function collectItemIds(tree: ItemTree): void {
if (tree.data !== undefined) {
coreItemIds.add(tree.data)
}
for (const child of tree.children || []) {
collectItemIds(child)
}
}
collectItemIds(builds.tree)
// Remove late game items that appear in core
builds.lateGame = builds.lateGame.filter(item => !coreItemIds.has(item.data))
}
/**
* Gets the index of the build with the highest pickrate
*/
export function getHighestPickrateBuildIndex(runes: Array<{ pickrate: number }>): number {
if (runes.length === 0) return 0
return runes.reduce(
(maxIdx, rune, idx, arr) => (rune.pickrate > arr[maxIdx].pickrate ? idx : maxIdx),
0
)
}
/**
* Gets the first core item for each build variant
*/
export function getFirstCoreItems(runes: unknown[], builds: Builds): number[] {
return runes.map(() => {
const tree = builds?.tree
return tree?.children?.[0]?.data ?? tree?.data ?? 0
})
}