Module:Shop

Revision as of 21:43, 18 March 2021 by Falterfire (talk | contribs) (increased min-width of shop cost column)

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.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.items ~= nil then
    for i, itemCost in Shared.skpairs(cost.items) do
      local item = Items.getItemByID(itemCost[1])
      table.insert(costArray, Icons.Icon({item.name, type="item", notext=true, qty=itemCost[2]}))
    end
  end

  return table.concat(costArray, ", ")
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

  return table.concat(reqArray, '<br/>')
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:100"|Cost||Requirements'

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

    local isUpgrade = purchase.contains.items == nil or Shared.tableCount(purchase.contains.items) == 0
    local isBundle = purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 1
    local iconTxt = purchase.name
    result = result..Icons.Icon({purchase.name, type=(isUpgrade and 'upgrade' or 'item'), notext=true, size='50'})
    result = result..'||'..purchase.name..'||'
    if isUpgrade then
      result = result..'Upgrade'
    elseif isBundle then
      result = result..'Item Bundle'
    else
      result = result..'Item'
    end

    result = result..'||'..purchase.description..'||style="text-align:right;"'
    if purchase.cost.gp ~= nil and purchase.cost.gp > 0 then
      result = result..' data-sort-value="'..purchase.cost.gp..'"'
    elseif purchase.cost.slayerCoins ~= nil and purchase.cost.slayerCoins > 0 then
      result = result..' data-sort-value="'..purchase.cost.slayerCoins..'"'
    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

return p