Module:ItemValues

From Ethyrial Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

This is a script that gets the "value" of items (price / weight).

By default it limits results to 100, but it can be increased by changing the limit.

Used on the Item Values page.

Usage

{{#invoke:ItemValues|main|limit=20}}

Lua error at line 22: attempt to index field 'cargo' (a nil value).


local lang = mw.getLanguage('en')
local p = {}

function p.main(frame)
    if frame == mw.getCurrentFrame() then
        args = require('Module:ProcessArgs').merge(true)
    else
        frame = mw.getCurrentFrame()
    end

    local _limit = args.limit or 100

    local cargoArgs = {
        orderBy = 'name',
        limit = _limit
    }

    local cargoFields = {
        '_pageName', 'name', 'image', 'weight', 'buy', 'sell', 'objectid'
    }
    
    local result = mw.ext.cargo.query(
        'ItemData',
        table.concat(cargoFields, ','),
        cargoArgs
    )

    if not result[1] then return 'null result' end

    local html = mw.html.create()

    local tbl = html:tag('table'):addClass('wikitable sortable')

    local headerRow = tbl:tag('tr')
    headerRow:tag('th'):wikitext('Name')
    headerRow:tag('th'):wikitext('Value (price/weight)')
    headerRow:tag('th'):wikitext('Sell Price')
    headerRow:tag('th'):wikitext('Weight')
    headerRow:tag('th'):wikitext('Buy Price')
    headerRow:tag('th'):wikitext('Object ID')

    for i, v in ipairs(result) do
        if tonumber(v.weight) ~= nil and tonumber(v.sell) ~= nil then            
            local weight = tonumber(v.weight)
            local price = tonumber(v.sell)
            local value = 0
            if weight ~= 0 then
                value = price / weight
            else
                value = math.huge -- cannot divide by 0. But also, items with no weight are not sellable.
            end

            value = round(value, 2)

            if value ~= nil and value ~= math.huge and value ~= -math.huge then
                local tr = tbl:tag('tr')
                tr:tag('td'):wikitext(frame:preprocess('[[' .. v._pageName .. '| ' .. v.name .. ']]'))
                tr:tag('td'):attr('data-sort-value', value):cssText('text-align: center'):wikitext(value .. 'x')
                tr:tag('td'):attr('data-sort-value', v.sell):cssText('text-align: center'):wikitext(frame:preprocess(v.sell .. ' {{s}}'))
                tr:tag('td'):attr('data-sort-value', v.weight):cssText('text-align: center'):wikitext(frame:preprocess(v.weight))
                tr:tag('td'):attr('data-sort-value', v.buy):cssText('text-align: center'):wikitext(frame:preprocess(v.buy .. ' {{s}}'))
                tr:tag('td'):attr('data-sort-value', v.objectid):cssText('text-align: center'):wikitext(frame:preprocess(v.objectid))
            end
        end
    end

    return html
end

function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end

return p