Module:Shop: Difference between revisions

From Melvor Idle
(getShopSkillcapeTable: Move from Module:Items & enforce min-width for icon column)
(getAutoEatTable: Initial implementation; getPurchases: Pass category to check function)
Line 90: Line 90:


   return table.concat(reqArray, '<br/>')
   return table.concat(reqArray, '<br/>')
end
function p._getPurchaseSortValue(purchase)
  local costCurrencies = {'gp', 'slayerCoins', 'raidCoins'}
  for j, curr in ipairs(costCurrencies) do
    local costAmt = purchase.cost[curr]
    if costAmt ~= nil and costAmt > 0 then
      return costAmt
    end
  end
end
end


Line 97: Line 107:
   result = result..'\r\n!colspan="2"|Purchase!!Type!!Description!!style="min-width:90px"|Cost!!Requirements'
   result = result..'\r\n!colspan="2"|Purchase!!Type!!Description!!style="min-width:90px"|Cost!!Requirements'


  local costCurrencies = {'gp', 'slayerCoins', 'raidCoins'}
   for i, purchase in Shared.skpairs(Purchases) do
   for i, purchase in Shared.skpairs(Purchases) do
     result = result..'\r\n|-\r\n|'
     result = result..'\r\n|-\r\n|'
Line 126: Line 135:
     result = result..'||'..purchase.description..'||style="text-align:right;"'
     result = result..'||'..purchase.description..'||style="text-align:right;"'


     for j, curr in Shared.skpairs(costCurrencies) do
     local sortValue = p._getPurchaseSortValue(purchase)
      local costAmt = purchase.cost[curr]
    if sortValue ~= nil then result = result..' data-sort-value="'..sortValue..'"' end
      if costAmt ~= nil and costAmt > 0 then
        result = result..' data-sort-value="'..costAmt..'"'
        break
      end
    end
     result = result..'|'..p.getCostString(purchase.cost)..'||'..p.getRequirementString(purchase.unlockRequirements)
     result = result..'|'..p.getCostString(purchase.cost)..'||'..p.getRequirementString(purchase.unlockRequirements)
   end
   end
Line 196: Line 200:
   for category, purchaseArray in Shared.skpairs(ShopData.Shop) do
   for category, purchaseArray in Shared.skpairs(ShopData.Shop) do
     for i, purchase in Shared.skpairs(purchaseArray) do
     for i, purchase in Shared.skpairs(purchaseArray) do
       if checkFunc(purchase) then
       if checkFunc(category, purchase) then
         table.insert(purchaseList, p.processPurchase(category, i - 1))
         table.insert(purchaseList, p.processPurchase(category, i - 1))
       end
       end
Line 282: Line 286:


   return result
   return result
end
function p.getAutoEatTable()
  local resultPart = {}
  local purchasesAE = p.getPurchases(function(cat, purch) return string.find(purch.name, '^Auto Eat') ~= nil end)
  -- Table header
  table.insert(resultPart, '{| class="wikitable sortable stickyHeader"')
  table.insert(resultPart, '|- class="headerRow-0"')
  table.insert(resultPart, '!colspan="2"|Auto Eat Tier!!Minimum Threshold!!Efficiency!!Max Healing!!Cost')
  -- Rows for each Auto Eat tier
  local mods = {["increasedAutoEatEfficiency"] = 0, ["increasedAutoEatHPLimit"] = 0, ["increasedAutoEatThreshold"] = 0}
  for i, purchase in ipairs(purchasesAE) do
    -- Modifiers must be accumulated as we go
    for modName, modValue in pairs(mods) do
      if purchase.contains.modifiers[modName] ~= nil then
        mods[modName] = mods[modName] + purchase.contains.modifiers[modName]
      end
    end
    local costAmt = p._getPurchaseSortValue(purchase)
    table.insert(resultPart, '|-\r\n|style="min-width:25px; text-align:center;" data-sort-value="' .. purchase.name .. '"| ' .. Icons.Icon({purchase.name, type='upgrade', size=50, notext=true}))
    table.insert(resultPart, '| [[' .. purchase.name .. ']]')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatThreshold .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatThreshold, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatEfficiency .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatEfficiency, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatHPLimit .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatHPLimit, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. costAmt .. '" | ' .. Icons.GP(costAmt))
  end
  table.insert(resultPart, '|}')
  return table.concat(resultPart, '\r\n')
end
end


return p
return p

Revision as of 09:30, 25 July 2021

Documentation for this module may be created at Module:Shop/doc

local p = {}

local ShopData = mw.loadData('Module:Shop/data')

local Shared = require('Module:Shared')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Constants = require('Module:Constants')
local Areas = require('Module:CombatAreas')

function p.processPurchase(category, purchaseID)
  local purchase = Shared.clone(ShopData.Shop[category][purchaseID + 1])
  purchase.id = purchaseID
  purchase.category = category
  return purchase
end

function p.getCostString(cost)
  local costArray = {}
  if cost.gp ~= nil and cost.gp > 0 then
    table.insert(costArray, Icons.GP(cost.gp))
  end
  if cost.slayerCoins ~= nil and cost.slayerCoins > 0 then
    table.insert(costArray, Icons.SC(cost.slayerCoins))
  end
  if cost.raidCoins ~= nil and cost.raidCoins > 0 then
    table.insert(costArray, Icons.RC(cost.raidCoins))
  end
  local itemArray = {}
  if cost.items ~= nil then
    for i, itemCost in Shared.skpairs(cost.items) do
      local item = Items.getItemByID(itemCost[1])
      table.insert(itemArray, Icons.Icon({item.name, type="item", notext=true, qty=itemCost[2]}))
    end

    if Shared.tableCount(itemArray) > 0 then
      table.insert(costArray, table.concat(itemArray, ", "))
    end
  end

  return table.concat(costArray, "<br/>")
end

function p.getRequirementString(reqs)
  if reqs == nil or Shared.tableCount(reqs) == 0 then
    return "None"
  end

  local reqArray = {}
  if reqs.slayerTaskCompletion ~= nil then
    for i, taskReq in Shared.skpairs(reqs.slayerTaskCompletion) do
      local tierName = Constants.getSlayerTierName(taskReq[1])
      table.insert(reqArray, 'Complete '..taskReq[2]..' '..tierName..' Slayer Tasks')
    end
  end

  if reqs.dungeonCompletion ~= nil then
    for i, dungReq in Shared.skpairs(reqs.dungeonCompletion) do
      local dung = Areas.getAreaByID('dungeon', dungReq[1])
      local dungStr = 'Complete '..Icons.Icon({dung.name, type='dungeon'})
      if dungReq[2] > 1 then
        dungStr = dungStr..' '..dungReq[2]..' times'
      end
      table.insert(reqArray, dungStr)
    end
  end

  if reqs.skillLevel ~= nil then
    for i, skillReq in Shared.skpairs(reqs.skillLevel) do
      local skillName = Constants.getSkillName(skillReq[1])
      table.insert(reqArray, Icons._SkillReq(skillName, skillReq[2]))
    end
  end

  if reqs.shopItemPurchased ~= nil then
    for i, shopReq in Shared.skpairs(reqs.shopItemPurchased) do
      local purchase = ShopData.Shop[shopReq[1]][shopReq[2] + 1]
      local isUpgrade = purchase.contains.items == nil or Shared.tableCount(purchase.contains.items) == 0
      table.insert(reqArray, Icons.Icon({purchase.name, type=(isUpgrade and 'upgrade' or 'item')})..' Purchased')
    end
  end

  if reqs.completionPercentage ~= nil then
    table.insert(reqArray, tostring(reqs.completionPercentage) .. '% Completion Log')
  end

  if reqs.text ~= nil then
    table.insert(reqArray, reqs.text)
  end

  return table.concat(reqArray, '<br/>')
end

function p._getPurchaseSortValue(purchase)
  local costCurrencies = {'gp', 'slayerCoins', 'raidCoins'}
  for j, curr in ipairs(costCurrencies) do
    local costAmt = purchase.cost[curr]
    if costAmt ~= nil and costAmt > 0 then
      return costAmt
    end
  end
end

function p._getShopTable(Purchases)
  local result = '{| class="wikitable sortable stickyHeader"'
  result = result..'\r\n|- class="headerRow-0"'
  result = result..'\r\n!colspan="2"|Purchase!!Type!!Description!!style="min-width:90px"|Cost!!Requirements'

  for i, purchase in Shared.skpairs(Purchases) do
    result = result..'\r\n|-\r\n|'

    local purchType = 'Item'
    if purchase.contains.pet ~= nil then
      purchType = 'Pet'
    elseif purchase.contains.modifiers ~= nil or purchase.contains.items == nil or Shared.tableCount(purchase.contains.items) == 0 then
      purchType = 'Upgrade'
    elseif purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 1 then
      purchType = 'Item Bundle'
    end

    -- Translate certain media strings to icons, mostly for Golbin Raid shop objects
    local iconOverrides = {
      ["assets/media/main/logo.svg"] = { icon = 'Melvor Logo', type = nil, noLink = true },
      ["assets/media/skills/prayer/prayer.svg"] = { icon = 'Prayer', type = 'skill', noLink = true }
    }
    local iconName, iconType, iconNoLink = purchase.name, (purchType == 'Item Bundle' and 'item' or string.lower(purchType)), nil
    if iconOverrides[purchase.media] ~= nil then
      iconName, iconType, iconNoLink = iconOverrides[purchase.media].icon, iconOverrides[purchase.media].type, iconOverrides[purchase.media].noLink
    end

    local purchName = purchase.name
    if iconNoLink == nil or iconNoLink ~= true then purchName = '[[' .. purchName .. ']]' end
    result = result..'style="min-width:25px"|'..Icons.Icon({iconName, type=iconType, notext=true, nolink=iconNoLink, size='50'})
    result = result..'||'..purchName..'||'..purchType
    result = result..'||'..purchase.description..'||style="text-align:right;"'

    local sortValue = p._getPurchaseSortValue(purchase)
    if sortValue ~= nil then result = result..' data-sort-value="'..sortValue..'"' end
    result = result..'|'..p.getCostString(purchase.cost)..'||'..p.getRequirementString(purchase.unlockRequirements)
  end

  result = result..'\r\n|}'
  return result
end

function p.getShopTable(frame)
  local cat = frame.args ~= nil and frame.args[1] or frame
  local shopCat = ShopData.Shop[cat]
  if shopCat == nil then
    return 'ERROR: Invalid category '..cat..'[[Category:Pages with script errors]]'
  else
    return p._getShopTable(shopCat)
  end
end

function p.getItemCostArray(itemID)
  local purchaseArray = {}

  for catName, cat in Shared.skpairs(ShopData.Shop) do
    for j, purchase in Shared.skpairs(cat) do
      if purchase.cost.items ~= nil then
        for k, costLine in Shared.skpairs(purchase.cost.items) do
          if costLine[1] == itemID then
            local temp = p.processPurchase(catName, j - 1)
            temp.qty = costLine[2]
            table.insert(purchaseArray, temp)
            break
          end
        end
      end
    end
  end

  return purchaseArray
end

function p.getItemSourceArray(itemID)
  local purchaseArray = {}

  for catName, cat in Shared.skpairs(ShopData.Shop) do
    for j, purchase in Shared.skpairs(cat) do
      if purchase.contains.items ~= nil and purchase.contains.items ~= nil then
        for k, containsLine in Shared.skpairs(purchase.contains.items) do
          if containsLine [1] == itemID then
            local temp = p.processPurchase(catName, j - 1)
            temp.qty = containsLine[2]
            table.insert(purchaseArray, temp)
            break
          end
        end
      end
    end
  end

  return purchaseArray
end

function p.getPurchases(checkFunc)
  local purchaseList = {}
  for category, purchaseArray in Shared.skpairs(ShopData.Shop) do
    for i, purchase in Shared.skpairs(purchaseArray) do
      if checkFunc(category, purchase) then
        table.insert(purchaseList, p.processPurchase(category, i - 1))
      end
    end
  end
  return purchaseList
end

function p._getPurchaseTable(purchase)
  local result = '{| class="wikitable"\r\n|-'
  result = result..'\r\n!colspan="2"|'..Icons.Icon({'Shop'})..' Purchase'
  if purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 1 then
    result = result..' - '..Icons.Icon({purchase.name, type='item'})
  end

  result = result..'\r\n|-\r\n!style="text-align:right;"|Cost'
  result = result..'\r\n|'..p.getCostString(purchase.cost)

  result = result..'\r\n|-\r\n!style="text-align:right;"|Requirements'
  result = result..'\r\n|'..p.getRequirementString(purchase.unlockRequirements)

  result = result..'\r\n|-\r\n!style="text-align:right;"|Contains'
  local containArray = {}
  if purchase.contains.items ~= nil then
    for i, itemLine in Shared.skpairs(purchase.contains.items) do
      local item = Items.getItemByID(itemLine[1])
      table.insert(containArray, Icons.Icon({item.name, type='item', qty=itemLine[2]}))
    end
  end
  if purchase.charges ~= nil and purchase.charges > 0 then
    table.insert(containArray, '+'..purchase.charges..' '..Icons.Icon({purchase.name, type='item'})..' Charges')
  end
  result = result..'\r\n|style="text-align:right;"|'..table.concat(containArray, '<br/>')

  result = result..'\r\n|}'
  return result
end

function p._getItemShopTable(item)
  local tableArray = {}
  local purchaseArray = p.getItemSourceArray(item.id)

  for i, purchase in Shared.skpairs(purchaseArray) do
    table.insert(tableArray, p._getPurchaseTable(purchase))
  end

  return table.concat(tableArray, '\r\n\r\n')
end

function p.getItemShopTable(frame)
  local itemName = frame.args ~= nil and frame.args[1] or frame
  local item = Items.getItem(itemName)
  if item == nil then
    return "ERROR: No item named "..itemName.." exists in the data module"
  end

  return p._getItemShopTable(item)
end

function p.getShopSkillcapeTable()
  local result = ''
  local capeList = Items.getItems(function(item) return Shared.contains(item.name, 'Skillcape') or item.name == 'Cape of Completion' end)

  result = result..'\r\n{|class="wikitable sortable stickyHeader"'
  result = result..'\r\n|- class="headerRow-0"'
  result = result..'\r\n!colspan="2" style="width:200px"|Cape'
  result = result..'!!Description!!style="width:120px"|Price'

  --Sort the table by cost and then name
  table.sort(capeList, function(a, b) 
                         if a.buysFor == b.buysFor then
                           return a.name < b.name
                         else
                           return a.sellsFor < b.buysFor
                         end
                       end)
  for i, thisItem in pairs(capeList) do
    result = result..'\r\n|-\r\n|style="min-width:25px"|'..Icons.Icon({thisItem.name, type='item', size='50', notext=true})
    result = result..'||[['..thisItem.name..']]'
    result = result..'\r\n||'..thisItem.description
    result = result..'||style="text-align:left" data-sort-value="'..thisItem.buysFor..'"'
    result = result..'|'..Icons.GP(thisItem.buysFor)
  end
  result = result..'\r\n|}'

  return result
end

function p.getAutoEatTable()
  local resultPart = {}
  local purchasesAE = p.getPurchases(function(cat, purch) return string.find(purch.name, '^Auto Eat') ~= nil end)

  -- Table header
  table.insert(resultPart, '{| class="wikitable sortable stickyHeader"')
  table.insert(resultPart, '|- class="headerRow-0"')
  table.insert(resultPart, '!colspan="2"|Auto Eat Tier!!Minimum Threshold!!Efficiency!!Max Healing!!Cost')
  -- Rows for each Auto Eat tier
  local mods = {["increasedAutoEatEfficiency"] = 0, ["increasedAutoEatHPLimit"] = 0, ["increasedAutoEatThreshold"] = 0}
  for i, purchase in ipairs(purchasesAE) do
    -- Modifiers must be accumulated as we go
    for modName, modValue in pairs(mods) do
      if purchase.contains.modifiers[modName] ~= nil then
        mods[modName] = mods[modName] + purchase.contains.modifiers[modName]
      end
    end

    local costAmt = p._getPurchaseSortValue(purchase)
    table.insert(resultPart, '|-\r\n|style="min-width:25px; text-align:center;" data-sort-value="' .. purchase.name .. '"| ' .. Icons.Icon({purchase.name, type='upgrade', size=50, notext=true}))
    table.insert(resultPart, '| [[' .. purchase.name .. ']]')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatThreshold .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatThreshold, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatEfficiency .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatEfficiency, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. mods.increasedAutoEatHPLimit .. '" | ' .. Shared.formatnum(Shared.round(mods.increasedAutoEatHPLimit, 0, 0)) .. '%')
    table.insert(resultPart, '| style="text-align:right;" data-sort-value="' .. costAmt .. '" | ' .. Icons.GP(costAmt))
  end
  table.insert(resultPart, '|}')

  return table.concat(resultPart, '\r\n')
end

return p