diff --git a/compcount.lua b/compcount.lua index ba44b9e..5208c5c 100644 --- a/compcount.lua +++ b/compcount.lua @@ -45,7 +45,12 @@ local function ensureChainsLoaded() local itemCount = 0 for _, item in ipairs(chain.items or {}) do - if item.stage ~= "item" then + if item.stage == "item" then + itemById[item.item_id] = { + id = item.item_id, + icon_nfp = item.icon_nfp_16x16, + } + else itemCount = itemCount + 1 items[itemCount] = { id = item.item_id, @@ -359,7 +364,7 @@ local function drawPage(base_id) local backWidth, backHeight = imageSize(backIcon) local backX = SCREEN_WIDTH - backWidth + 1 local pageItemIds = {} - local visibleItemCount = math.min(math.max(#pageItems, 9), 9) + local visibleItemCount = math.min(#pageItems, 9) for i = 1, visibleItemCount do pageItemIds[i] = pageItems[i].id @@ -399,15 +404,33 @@ end drawOverview = function() local items = getBaseItemIds() - local visibleItemCount = math.min(math.max(#items, 9), 9) - -- TODO LATER: get item counts from me-system using item, then sort items according to their counts and display the top 9. For now, just use random numbers. + local itemCounts = getMeItemCounts(items) + local sortedItemIds = {} + + for i = 1, #items do + sortedItemIds[i] = items[i] + end + + table.sort(sortedItemIds, function(a, b) + local countA = itemCounts[a] or 0 + local countB = itemCounts[b] or 0 + + if countA ~= countB then + return countA > countB + end + + return a < b + end) + + local visibleItemCount = math.min(#sortedItemIds, 9) frame.buffer:clear() for i = 1, visibleItemCount do - local item = getItemById(items[i]) + local itemId = sortedItemIds[i] + local item = getItemById(itemId) local icon = getItemIcon(item) or getFallbackIcon() - drawItem(icon, 4+(8+4)*((i-1)%3), 2+(5+1)*math.floor((i-1)/3), math.random(0, 1000000000000)) + drawItem(icon, 4+(8+4)*((i-1)%3), 2+(5+1)*math.floor((i-1)/3), itemCounts[itemId] or 0) end assertBufferValid(frame) @@ -424,7 +447,7 @@ drawOverview = function() local index = row * 3 + col + 1 if index <= visibleItemCount then - drawPage(items[index]) + drawPage(sortedItemIds[index]) return end end