match_collector: track gold advantage when items are bought

also add api.ts with Riot API types
This commit is contained in:
2026-04-17 10:51:21 +02:00
parent 19a9226dac
commit b7435f0884
3 changed files with 275 additions and 43 deletions

View File

@@ -1,15 +1,35 @@
type GoldAdvantageTag = 'ahead' | 'behind' | 'even'
type ItemTree = {
data: number | undefined
count: number
children: Array<ItemTree>
// Gold advantage tracking
boughtWhen: {
aheadCount: number
behindCount: number
evenCount: number
meanGold: number
}
}
function treeInit(): ItemTree {
return { data: undefined, count: 0, children: [] }
return {
data: undefined,
count: 0,
children: [],
boughtWhen: { aheadCount: 0, behindCount: 0, evenCount: 0, meanGold: 0 }
}
}
function treeNode(data: number, count: number): ItemTree {
return { data: data, count: count, children: [] }
return {
data: data,
count: count,
children: [],
boughtWhen: { aheadCount: 0, behindCount: 0, evenCount: 0, meanGold: 0 }
}
}
/*
@@ -21,31 +41,49 @@ function nodeMerge(itemtree: ItemTree, node: ItemTree) {
let next: ItemTree | null = null
// Try to find an existing node in this tree level with same item
for (const node of itemtree.children) {
if (node.data == item) {
node.count += 1
next = node
for (const child of itemtree.children) {
if (child.data == item) {
child.count += 1
child.boughtWhen.aheadCount += node.boughtWhen.aheadCount
child.boughtWhen.evenCount += node.boughtWhen.evenCount
child.boughtWhen.behindCount += node.boughtWhen.behindCount
next = child
break
}
}
// If not found, add item node at this level
if (next == null) {
if (next == null && item !== undefined) {
next = treeNode(item, count)
itemtree.children.push(next)
}
return next
return next!
}
/*
* Merge a full build path with an existing item tree
*/
function treeMerge(itemtree: ItemTree, items: Array<number>) {
function treeMerge(
itemtree: ItemTree,
items: Array<{ itemId: number; goldAdvantage: GoldAdvantageTag }>
) {
let current = itemtree
for (const item of items) {
current = nodeMerge(current, { data: item, count: 1, children: [] })
current = nodeMerge(current, {
data: item.itemId,
count: 1,
boughtWhen: {
aheadCount: item.goldAdvantage == 'ahead' ? 1 : 0,
evenCount: item.goldAdvantage == 'even' ? 1 : 0,
behindCount: item.goldAdvantage == 'behind' ? 1 : 0,
meanGold: 0
},
children: []
})
}
}
@@ -54,7 +92,12 @@ function treeCutBranches(itemtree: ItemTree, thresholdCount: number, thresholdPe
while (itemtree.children.length > thresholdCount) {
const leastUsedBranch = itemtree.children.reduce(
(a, b) => (Math.min(a.count, b.count) == a.count ? a : b),
{ data: undefined, count: +Infinity, children: [] }
{
data: undefined,
count: +Infinity,
children: [],
boughtWhen: { aheadCount: 0, behindCount: 0, evenCount: 0, meanGold: 0 }
}
)
itemtree.children.splice(itemtree.children.indexOf(leastUsedBranch), 1)
}
@@ -88,7 +131,13 @@ function treeClone(tree: ItemTree): ItemTree {
return {
data: tree.data,
count: tree.count,
children: tree.children.map(child => treeClone(child))
children: tree.children.map(child => treeClone(child)),
boughtWhen: {
aheadCount: tree.boughtWhen.aheadCount,
behindCount: tree.boughtWhen.behindCount,
evenCount: tree.boughtWhen.evenCount,
meanGold: tree.boughtWhen.meanGold
}
}
}
@@ -174,4 +223,13 @@ function areTreeSimilars(t1: ItemTree, t2: ItemTree): number {
return Math.max(0, Math.min(1, similarity))
}
export { ItemTree, treeMerge, treeInit, treeCutBranches, treeSort, treeMergeTree, areTreeSimilars }
export {
ItemTree,
GoldAdvantageTag,
treeMerge,
treeInit,
treeCutBranches,
treeSort,
treeMergeTree,
areTreeSimilars
}