Module:Shop: Difference between revisions

1,657 bytes added ,  14 September 2021
getPurchaseContents: Initial implementation
(Implement getPurchase, getPurchaseStat, getCostString, _getPurchaseType, _getPurchaseContents, getPurchaseIcon; facilitates implementing a 'PurchaseBox' template)
(getPurchaseContents: Initial implementation)
Line 48: Line 48:
     return p.getRequirementString(purchase.unlockRequirements)
     return p.getRequirementString(purchase.unlockRequirements)
   elseif stat == 'contents' then
   elseif stat == 'contents' then
     return p._getPurchaseContents(purchase)
     return p._getPurchaseContents(purchase, true)
   elseif stat == 'type' then
   elseif stat == 'type' then
     return p._getPurchaseType(purchase)
     return p._getPurchaseType(purchase)
Line 166: Line 166:
end
end


function p._getPurchaseContents(purchase)
function p._getPurchaseContents(purchase, asList)
  if asList == nil then asList = true end
   local containArray = {}
   local containArray = {}
   if purchase.contains.items ~= nil then
   if purchase.contains.items ~= nil and Shared.tableCount(purchase.contains.items) > 0 then
    if not asList then
      table.insert(containArray, '{| class="wikitable sortable stickyHeader"')
      table.insert(containArray, '|- class="headerRow-0"')
      table.insert(containArray, '! colspan="2" | Item !! Quantity')
    end
     for i, itemLine in Shared.skpairs(purchase.contains.items) do
     for i, itemLine in Shared.skpairs(purchase.contains.items) do
       local item = Items.getItemByID(itemLine[1])
       local item = Items.getItemByID(itemLine[1])
       table.insert(containArray, Icons.Icon({item.name, type='item', qty=itemLine[2]}))
       if asList then
        table.insert(containArray, Icons.Icon({item.name, type='item', qty=itemLine[2]}))
      else
        table.insert(containArray, '|-\r\n| style="min-width:25px"| ' .. Icons.Icon({item.name, type='item', notext=true, size='25'}))
        table.insert(containArray, '| [[' .. item.name .. ']]\r\n| data-sort-value="' .. itemLine[2] .. '" style="text-align:right" | ' .. Shared.formatnum(itemLine[2]))
      end
     end
     end
   end
   end
   if purchase.charges ~= nil and purchase.charges > 0 then
   if purchase.charges ~= nil and purchase.charges > 0 then
     table.insert(containArray, '+'..purchase.charges..' '..Icons.Icon({purchase.name, type='item'})..' Charges')
     if asList then
      table.insert(containArray, '+'..purchase.charges..' '..Icons.Icon({purchase.name, type='item'})..' Charges')
    else
      table.insert(containArray, '|-\r\n| style="min-width:25px"| ' .. Icons.Icon({purchase.name, type='item', notext=true, size='25'}))
      table.insert(containArray, '| [[' .. item.name .. ']] Charges\r\n| data-sort-value="' .. purchase.charges .. '" style="text-align:right" | ' .. Shared.formatnum(purchase.charges))
    end
  end
  if not asList and Shared.tableCount(containArray) > 0 then table.insert(containArray, '|}') end
 
  local delim = (asList and '<br/>' or '\r\n')
  return table.concat(containArray, delim)
end
 
function p.getPurchaseContents(frame)
  local args = frame.args ~= nil and frame.args or frame
  local purchaseName = args[1]
  local asList = ((args[2] ~= nil and args[2] == 'true') or false)
  local purchase = p.getPurchase(purchaseName)
 
  if purchase == nil then
    return "ERROR: Couldn't find purchase with name '" .. purchaseName .. "'[[Category:Pages with script errors]]"
  else
    return p._getPurchaseContents(purchase, asList)
   end
   end
  return table.concat(containArray, '<br/>')
end
end


Line 445: Line 477:


   result = result..'\r\n|-\r\n!style="text-align:right;"|Contains'
   result = result..'\r\n|-\r\n!style="text-align:right;"|Contains'
   result = result..'\r\n|style="text-align:right;"|'..p._getPurchaseContents(purchase)
   result = result..'\r\n|style="text-align:right;"|'..p._getPurchaseContents(purchase, true)


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