Module:Villager Total Material Table

local p = {} local cargo = mw.ext.cargo local materials = { [1] = 'Cotton', [2] = 'Preserves', [3] = 'Steel', [4] = 'Wood', [5] = 'Paper', [6] = 'Civic Essence', [7] = 'Cool Essence', [8] = 'Cute Essence', [9] = 'Elegant Essence', [10] = 'Harmonious Essence', [11] = 'Hip Essence', [12] = 'Historical Essence', [13] = 'Modern Essence', [14] = 'Natural Essence', [15] = 'Rustic Essence', [16] = 'Sporty Essence', [17] = 'Sparkle Stones', [18] = 'Friend Powder', [19] = 'Bells', [20] = 'Craft Time' }

function p._villagerTotalMaterialTable(args) -- Cargo Query villagerFields = '_pageName=name,Material,Theme,ReqFriendshipLevel=Level,Furniture1,Furniture2,Furniture3,Furniture4,Furniture5' local villagerArgs = { where = "IsTraveller='0'", groupBy = '_pageName', orderBy = '_pageName', limit = 500, } local villagerQueryResult = cargo.query( 'Villagers', villagerFields, villagerArgs ) if villagerQueryResult == nil then return "(no values)" end

-- Initialize the table local tbl = mw.html.create('table') :addClass('wikitable sortable') :css('text-align','center')

-- Table headers tbl:tag('th') :wikitext('Villager') :css('padding', '1em') tbl:tag('th') :wikitext('Theme') :css('padding', '1em') tbl:tag('th') :wikitext('Material') :css('padding', '1em') tbl:tag('th') :wikitext('Invite Level') :css('padding', '1em') tbl:tag('th') :wikitext('Furniture') :addClass('unsortable') :css('padding', '1em')

for k,v in sortedPairs(materials) do   tbl:tag('th') :wikitext(v) :addClass('indv-mat-column') :attr('data-sort-type', 'number') end

tbl:tag('th') :wikitext('Total Material Cost') :addClass('unsortable') :css('padding', '1em')

if type( villagerQueryResult ) == "table" then -- Go through all the villagers in the query result for key,villager in sortedPairs( villagerQueryResult ) do     local tr = tbl:tag('tr')

-- Icon tr:tag('td') :wikitext( ' ' .. villager.name .. '' ) :css('padding', '1em')

-- Theme tr:tag('td') :wikitext( '')

-- Material tr:tag('td') :wikitext( ' ' .. villager.Material .. '' )

-- Invite Level local inviteLevel = '-' if villager.Level == '1' then inviteLevel = '1' elseif villager.Level == '3' then inviteLevel = '3' elseif villager.Level == '5' then inviteLevel = '5' elseif villager.Level == '7' then inviteLevel = '6' end tr:tag('td') :wikitext('Level ' .. inviteLevel)

-- Furniture local totalFurniture = 4; local furnitureStr = '' furnitureStr =  .. villager.Furniture1 ..  furnitureStr = furnitureStr .. ' ' .. villager.Furniture2 .. '' furnitureStr = furnitureStr .. ' ' .. villager.Furniture3 .. '' furnitureStr = furnitureStr .. ' ' .. villager.Furniture4 .. '' if villager.Furniture5 ~= '' then furnitureStr = furnitureStr .. ' ' .. villager.Furniture5 .. '' totalFurniture = 5; end

tr:tag('td') :wikitext(furnitureStr) :css('padding', '1em')

local totalMaterialTable = { ['Cotton'] = 0, ['Preserves'] = 0, ['Steel'] = 0, ['Wood'] = 0, ['Paper'] = 0, ['Civic Essence'] = 0, ['Cool Essence'] = 0, ['Cute Essence'] = 0, ['Elegant Essence'] = 0, ['Harmonious Essence'] = 0, ['Hip Essence'] = 0, ['Historical Essence'] = 0, ['Modern Essence'] = 0, ['Natural Essence'] = 0, ['Rustic Essence'] = 0, ['Sporty Essence'] = 0, ['Sparkle Stones'] = 0, ['Friend Powder'] = 0, ['Bells'] = 0, ['Craft Time'] = 0 }

local furnitureQueryArgs = { where = '_pageName="' .. villager.Furniture1 .. '" OR ' .. '_pageName="' .. villager.Furniture2 .. '" OR ' .. '_pageName="' .. villager.Furniture3 .. '" OR ' .. '_pageName="' .. villager.Furniture4 .. '" OR ' .. '_pageName="' .. villager.Furniture5 .. '"', groupBy = '_pageName', limit = 500, }     local furnitureQueryFields = 'Mat1,Mat1Cost,Mat2,Mat2Cost,Mat3,Mat3Cost,Mat4,Mat4Cost,Mat5,Mat5Cost,Mat6,Mat6Cost,CraftTimeSec,NumBells' local furnitureQueryResult = cargo.query( 'Furniture', furnitureQueryFields, furnitureQueryArgs ) for keyF,furniture in sortedPairs( furnitureQueryResult ) do       if (furniture.Mat1 ~= nil and furniture.Mat1 ~= '') then if (totalMaterialTable[furniture.Mat1] ~= nil ) then totalMaterialTable[furniture.Mat1] = totalMaterialTable[furniture.Mat1] + tonumber(furniture.Mat1Cost) end end

if (furniture.Mat2 ~= nil and furniture.Mat2 ~= '') then if (totalMaterialTable[furniture.Mat2] ~= nil ) then totalMaterialTable[furniture.Mat2] = totalMaterialTable[furniture.Mat2] + tonumber(furniture.Mat2Cost) end end

if (furniture.Mat3 ~= nil and furniture.Mat3 ~= '') then if (totalMaterialTable[furniture.Mat3] ~= nil ) then totalMaterialTable[furniture.Mat3] = totalMaterialTable[furniture.Mat3] + tonumber(furniture.Mat3Cost) end end

if (furniture.Mat4 ~= nil and furniture.Mat4 ~= '') then if (totalMaterialTable[furniture.Mat4] ~= nil ) then totalMaterialTable[furniture.Mat4] = totalMaterialTable[furniture.Mat4] + tonumber(furniture.Mat4Cost) end end

if (furniture.Mat5 ~= nil and furniture.Mat5 ~= '') then if (totalMaterialTable[furniture.Mat5] ~= nil ) then totalMaterialTable[furniture.Mat5] = totalMaterialTable[furniture.Mat5] + tonumber(furniture.Mat5Cost) end end

if (furniture.Mat6 ~= nil and furniture.Mat6 ~= '') then if (totalMaterialTable[furniture.Mat6] ~= nil ) then totalMaterialTable[furniture.Mat6] = totalMaterialTable[furniture.Mat6] + tonumber(furniture.Mat6Cost) end end

if (furniture.CraftTimeSec ~= nil and furniture.CraftTimeSec ~= '' ) then totalMaterialTable['Craft Time'] = totalMaterialTable['Craft Time'] + tonumber(furniture.CraftTimeSec) end end

local totalMatCostStr = '' local totalBells = '' for k,v in pairs(totalMaterialTable) do       if v ~= 0 then if k == 'NumBells' then totalBells = ' ' .. k .. ' x ' .. v .. ' '         elseif k == 'Craft Time' then --do nothing else totalMatCostStr = totalMatCostStr .. ' ' .. k .. ' x ' .. v .. ' '         end end end totalMatCostStr = totalMatCostStr .. totalBells

for k,v in sortedPairs(materials) do       if totalMaterialTable[v] ~= 0 then if v == 'Craft Time' then tr:tag('td') :wikitext(convertSeconds(totalMaterialTable[v])) :attr('data-sort-value', totalMaterialTable[v]) :addClass('indv-mat-column') else tr:tag('td') :wikitext(totalMaterialTable[v]) :addClass('indv-mat-column') end else tr:tag('td') :wikitext('0') :addClass('indv-mat-column') end end

tr:tag('td') :wikitext(totalMatCostStr) :css('padding', '1em') end return tostring(tbl) end end

function convertSeconds(value) local time = '' local hours = math.floor(value/3600) local minutes = (value % 3600)/60 if hours > 0 then time = hours .. ' hour(s) ' end if minutes > 0 then time = time .. minutes .. ' minute(s)' end return time end

-- Because key,value pairs are stored arbitrarily in the tables, this -- function sorts the keys and returns the key,value pairs in sorted order. function sortedPairs(unsortedTable) -- Get all the keys from the table local keys = {} for key in pairs(unsortedTable) do   keys[#keys+1] = key end -- Sort'em! table.sort(keys) local i = 0 return function i = i + 1 if keys[i] then return keys[i], unsortedTable[keys[i]] end end end function p.villagerTotalMaterialTable(frame) return p._villagerTotalMaterialTable end

return p