Module:HousingTable: Difference between revisions

From Eco - English Wiki
[checked revision][checked revision]
No edit summary
No edit summary
Line 3: Line 3:
local Utils = require('Module:Utils')
local Utils = require('Module:Utils')
local L = require('Module:Localization')
local L = require('Module:Localization')
local HEADER = [[<table class="wikitable sortable jquery-tablesorter">
<tr>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
</tr>]]


function p.table(frame)
function p.table(frame)
Line 9: Line 19:
     local ItemData = mw.loadData("Module:ItemData")
     local ItemData = mw.loadData("Module:ItemData")


     local returnStr = [[<table class="wikitable sortable jquery-tablesorter">
     local returnStr = string.format(HEADER, L.t('Item'), L.t('Room'), L.t('Type'), L.t('Value'), L.t('Dim. Return %'), L.t('Dimensions'))
<tr>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Item') .. [[</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Room') .. [[</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Type') .. [[</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Value') .. [[</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Dim. Return %') .. [[</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">]] .. L.t('Dimensions') .. [[</th>
</tr>]]


     local items = {}
     local items = {}
Line 30: Line 32:


     for _, item in pairs(items) do
     for _, item in pairs(items) do
         returnStr = returnStr .. ' < tr > < td > [[' .. item.item .. ']] </td><td>' .. item.room .. '</td><td>' .. item.type .. '</td><td>' .. item.value .. '</td><td>' .. item.depreciation .. '</td><td>' .. item.dimensions .. '</td></tr>'
         returnStr = returnStr .. '<tr><td>[[' .. item.item .. ']]</td><td>' .. item.room .. '</td><td>' .. item.type .. '</td><td>' .. item.value .. '</td><td>' .. item.depreciation .. '</td><td>' .. item.dimensions .. '</td></tr>'
     end
     end



Revision as of 01:18, 19 March 2021

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

local p = {}

local Utils = require('Module:Utils')
local L = require('Module:Localization')

local HEADER = [[<table class="wikitable sortable jquery-tablesorter">
<tr>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
</tr>]]

function p.table(frame)
    local args = Utils.normaliseArgs(frame)

    local ItemData = mw.loadData("Module:ItemData")

    local returnStr = string.format(HEADER, L.t('Item'), L.t('Room'), L.t('Type'), L.t('Value'), L.t('Dim. Return %'), L.t('Dimensions'))

    local items = {}

    for itemName, item in pairs(ItemData.items) do
        if item.roomCategory and item.furnitureType and (args.category == nil or args.category == item.roomCategory) and (args.type == nil or args.type == item.furnitureType) then
            table.insert(items, { item = itemName, room = item.roomCategory, type = item.furnitureType, value = item.skillValue, depreciation = item.repeatsDepreciation, dimensions = item.footprint })
        end
    end

    table.sort(items, compareItems)

    for _, item in pairs(items) do
        returnStr = returnStr .. '<tr><td>[[' .. item.item .. ']]</td><td>' .. item.room .. '</td><td>' .. item.type .. '</td><td>' .. item.value .. '</td><td>' .. item.depreciation .. '</td><td>' .. item.dimensions .. '</td></tr>'
    end

    return returnStr .. '</table>'

end

function compareItems(a, b)
    if a.room == b.room then
        if a.type == b.type then
            return a.item < b.item
        else
            return a.type < b.type
        end
    else
        return a.room < b.room
    end
end

return p