ATTENTION! The process of updating WiKi to version Eco 10.x has begun. Those wishing to participate can find out more Information on our ECO Contribution Wiki Discord.

Module:Infobox Item: Difference between revisions

From Eco - English Wiki
[unchecked revision][checked revision]
No edit summary
(Fat Colour change)
(45 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- Credit: Original Infobox (now known as Infobox_Item) created by Pradoxzon was then edited by Nesphit and TreeNuts0. Fyre (FishAus) and Scotty (ZeelNightwolf) further edited the Infobox_Item. They then duplicated and used as a base for Infobox_Skill, Infobox_Plant, and Infobox_Animal.
local p = {}
local p = {}
local Utils = require('Module:Utils')
local L = require('Module:Localization')
-- Build an Item Infobox
function itemBox(args, itemData)
    -- check that all necessary arguments are passed correctly
    if args.name == nil or args.name == '' then
        return '\'name\' must be specified.'
    end
    local item = args.name
    local itemTable = itemData.items[item]
    if itemTable == nil then
        return item .. ' could not be found in Module:ItemData.'
    end
    local itemType = itemTable.type
    local itemEN = string.sub(itemType, 1, -5)
    local itemimagename = string.gsub(itemEN, ' ', '')
    -- string used to build the infobox
    local infobox = '{| class=\"infobox\"\n'
    -- 'Name and Image' section
    -- name of the item
    infobox = infobox .. '|- style=\"color: white; background-color: #1165AF; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. item .. '</big>\'\'\'\n'
    -- the item's type (ItemData - group)
    infobox = infobox .. '|- style=\"text-align: center; color: white; background-color: '
    local text_colour = '#78B1FF'
    if itemTable.group == L.t('Food') then
        text_colour = '#85D66B'
    elseif itemTable.group == L.t('Skill Books') or itemTable.group == L.t('Skill Scrolls') then
        text_colour = '#FFCF4D'
    elseif itemTable.group == L.t('Skill Scrolls') then
        text_colour = '#FFCF4D'
    end
    infobox = infobox .. text_colour .. ';\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
    local icon = Utils.build_icon(item)
    infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | '..icon .. '\n'
    -- 'Description' section header
    infobox = infobox .. sectionHeader('Description')
    if itemTable.description == nil or itemTable.description == '' then
        infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | \'\'\'' .. L.t('None') .. '\'\'\'\n'
    else
        infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. itemTable.description .. '\n'
    end
    --Crafting
    local craftingRecipes = mw.loadData("Module:CraftingRecipes")
    infobox = infobox .. generalSection(item, itemTable, craftingRecipes, args)
    infobox = infobox .. IDsSection(itemTable)
    -- Tags Header
    if itemTable.tagGroups ~= nil and itemTable.tagGroups ~= {} then
        infobox = infobox .. tagSection(itemTable, itemData)
    end
    -- 'Item' World Object header (if itemTable.group = Placeable or Blocks)
    if itemTable.group == L.t('Block Items') or itemTable.group == L.t('World Object Items') then
        infobox = infobox .. placementSection(itemTable, itemimagename)
        -- Object Form Image
        if itemTable.group == L.t('Block Items') then
            infobox = infobox .. objectFormSection(itemTable, itemimagename)
        end
        -- 'Housing' section (if there is a Room Category)
        if itemTable.roomCategory ~= nil then
            infobox = infobox .. housingSection(itemTable)
        end
        -- 'Storage' Section (if inventorySlots is not nil)
        if itemTable.inventorySlots ~= nil then
            infobox = infobox .. storageSection(itemTable)
        end
        -- 'Power' section (if EngeryType is ``not nil)
        if itemTable.energyType ~= nil then
            infobox = infobox .. powerSection(itemTable)
        end
        -- 'Fuel' Section (if fuelsUsed by Object)
        if itemTable.fuelsUsed ~= nil then
            infobox = infobox .. fuelsSection(itemTable)
        end
        -- 'Fluid' section (if fludisUsed is not nil)
        if itemTable.fluidsUsed ~= nil or itemTable.fluidsProduced ~= nil then
            infobox = infobox .. fluidsSection(itemTable)
        end
    end
    -- Road Object header (if group == Road Items)
    if itemTable.group == L.t('Road Items') then
        infobox = infobox .. roadItemsSection(itemTable, itemimagename)
    end
    infobox = infobox .. '|}'
    return infobox
end
function sectionHeader(title, count)
    return "|- style=\"background-color: #4688C0; text-align: center;\"\n| colspan=\"2\" | '''" .. L.t(title, count) .. "'''\n"
end
function sectionRow(label, content, count, unit)
    if unit ~= nil then
        content = tonumber(content)
        unit = L.t(unit, content)
        local lang = mw.getContentLanguage()
        content = lang:formatNum(content)
    else
        unit = ''
    end
    return '|-\n| ' .. L.t(label, count) .. ':\n| style=\"text-align: right; padding: 3px;\" | ' .. content .. unit .. '\n'
end
function sectionImage(imageName, suffix)
    local image = checkImage(imageName, suffix)
    return '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|border|240px]]\n'
end
local function addToSet(set, key)
    set[key] = true
end
local function setNotContains(set, key)
    return set[key] == nil
end
local function craftingSubSection(title, item, productsOrIngredients, recipes)
    local stations = {}
    local sortStations = {}
    local stationString = ''
    for _, recipeName in ipairs(productsOrIngredients[item]) do
        local currentRecipe = recipes[recipeName]
        if currentRecipe ~= nil then
            if currentRecipe.variants[recipeName] ~= nil then
                local currentStation = currentRecipe.craftStn[1][1]
                if setNotContains(stations, currentStation) then
                    addToSet(stations, currentStation)
                end
            end
        end
    end
    if stations then
        for a, _ in pairs(stations) do
            table.insert(sortStations, a)
        end
        table.sort(sortStations)
        for i, n in ipairs(sortStations) do
            stationString = stationString .. ' [[' .. n .. ']]'
            if (n ~= sortStations[#sortStations]) then
                -- add a comma
                stationString = stationString .. ', '
            end
        end
        return sectionRow(title, stationString)
    else
        return sectionRow(title, L.t('N/A'))
    end
end
function generalSection(item, itemTable, craftingRecipes, args)
    -- 'General' section header
    section = sectionHeader('General')
    -- Is a product at these tables
    if craftingRecipes.products[item] ~= nil and Utils.tableLen(craftingRecipes.products[item]) >= 1 then
        section = section .. craftingSubSection('Created at', item, craftingRecipes.products, craftingRecipes.recipes)
    end
    -- Is an ingredient at these tables
    if craftingRecipes.ingredients[item] ~= nil and Utils.tableLen(craftingRecipes.ingredients[item]) >= 1 then
        section = section .. craftingSubSection('Used at', item, craftingRecipes.ingredients, craftingRecipes.recipes)
    end
    -- calories and nutrients (if itemTable.group == 'Food')
    if itemTable.group == L.t('Food') then
        section = section .. sectionRow('Calorie', itemTable.calories, tonumber(itemTable.calories), 'cal')
        section = section .. '|- valign=\"center\"\n| rowspan=\"4\" | ' .. L.t('Nutrients') .. ':\n'
        section = section .. '| style=\"color: red; text-align: right; padding: 3px;\" | ' .. L.t('Carbs') .. ': ' .. itemTable.carbs .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: orange; text-align: right; padding: 3px;\" | ' .. L.t('Protein') .. ': ' .. itemTable.protein .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: darkkhaki; text-align: right; padding: 3px;\" | ' .. L.t('Fat') .. ': ' .. itemTable.fat .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: limegreen; text-align: right; padding: 3px;\" | ' .. L.t('Vitamins') .. ': ' .. itemTable.vitamins .. '\n'
        section = section .. sectionRow('Nutrition Density', L.t('%s per 100 cals'):format(itemTable.density))
    end
    -- carried
    local carried
    if args.carried ~= nil and args.carried ~= '' then
        carried = args.carried
    else
        carried = itemTable.carried
    end
    section = section .. sectionRow('Carried in', carried)
    -- weight
    local weight
    if itemTable.weight ~= nil then
        weight = itemTable.weight
    else
        weight = '0.0'
    end
    section = section .. sectionRow('Weight', weight, nil, 'kg')


-- Header for the wikitable
    -- stack limit
function header( args )
    if itemTable.maxStack ~= nil then
     local headerStr = '{| class=\"wikitable mw-collapsible\" style=\"text-align: center;\"\n|-\n'
        section = section .. sectionRow('Stack limit', itemTable.maxStack)
     end


     -- Show or hide the Crafting Station column
     -- yield
     if args[1] == '1' then
     if itemTable.yield ~= nil then
         headerStr = headerStr .. '! Crafting Station !'
         section = section .. sectionRow('Improve Gathering', itemTable.yield)
     end
     end


     -- Item, Level Needed, Materials, Related Specialty
     -- fuel
     headerStr = headerStr .. '! colspan=\"4\" | Item !! colspan=\"4\" | Materials !! Level Needed !! Crafting Time <br>(mins) !! Labour Cost || XP Gain !! style="width: 100px" | Related <br> Specialty\n'
     if itemTable.fuel ~= nil then
        section = section .. sectionRow('Fuel energy', itemTable.fuel, nil, 'J')
    end


     return headerStr
    -- currency
    if itemTable.currency ~= nil then
        section = section .. "|- style=\"text-align: center;\"\n| colspan=\"2\" | " .. L.t('Can back a currency') .. "\n"
    end
     return section
end
end


function imagedisp( name )
function IDsSection(itemTable)
     local str = ''
     -- 'IDs' section header
local image = ''
    section = sectionHeader('ID', 2)
     -- Manual override for image file
 
     if name then
     -- item id (type)
image = string.gsub(name, ' ', '') .. '_Icon.png'
     section = section .. sectionRow('Item ID', itemTable.type)
str = str .. ' <div class="iconContainer"><div class="iconStack">[[File:' .. image .. '|center|50px|link=' .. name .. '|class=iconBlue]]</div><div class=iconBorder style=\"position:absolute;\"></div></div> '
 
else
    -- id number (type id)
image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. name .. '_Icon.png'
    section = section .. sectionRow('ID Number', itemTable.typeID)
str = str .. '[[File:' .. image .. '|frameless|50px|class=iconBlue]]'
 
end
    return section
str = str .. '<br> [[' .. name .. ']]'
return str
end
end


function stationcell( args )
function tagSection(itemTable, itemData)
  local str = ''
    -- Tags Header
str = str .. '| ' .. imagedisp(args[1])
    local tags = itemTable.tagGroups
return str
    section = sectionHeader('Tag', Utils.tableLen(tags))
    local list = ''
    -- for each item in the list (a is position, b is value)
    for a, b in ipairs(tags) do
        --if not these tags listed here
        if (b ~= L.t('Object') or b ~= L.t('World  Object') or b ~= L.t('Housing  Object')) then
            -- add the tag to the list
            -- HACK: Some tag localisations have spaces in the tag name, but not in the tag data
            local tagLink
            -- Some tags have multiple spaces? Possible data quality issue
            bClean = b:gsub("%s+", " ")
            if itemData.tags[bClean] ~= nil then
                tagLink = L.t('%s Tag'):format(bClean)
            else
                tagLink = L.t('%s Tag'):format(bClean:gsub(' ', ''))
            end
 
            list = list .. '[[' .. tagLink .. '|' .. bClean .. ']][[Category:' .. bClean .. ']]'
        end
        -- if not the last item in the list
        if (b ~= tags[#tags]) then
            -- add a comma
            list = list .. ', '
        end
    end
    -- Now the list is made add it to the infobox
    section = section .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. list .. '\n'
 
    return section
end
end


function placementSection(itemTable, itemImageName)
    section = sectionHeader('World Object')
    -- Object Placed Image
    section = section .. sectionImage(itemImageName, 'Placed')


function itemcell( args )
    -- 'Placement' section
     local str = ''
    --Placement Header
str = str .. '| ' .. imagedisp(args[1])
    section = section .. sectionHeader('Placement')
str = str .. '<br>x' .. args[2] .. '\n'
 
return str
    --Vechile
     if itemTable.mobile ~= nil then
        section = section .. sectionRow('Vehicle/Mobile Object', itemTable.mobile)
    end
 
    --Dimensions
    if itemTable.footprint ~= nil then
        section = section .. sectionRow('Dimensions (X,Y,Z)', itemTable.footprint)
    end
 
    --Material Tier
    if itemTable.materialTier ~= nil or itemTable.materialTier == 0 then
        section = section .. sectionRow('Room Material', itemTable.materialTier) .. '[[Category:Tier ' .. itemTable.materialTier .. ']]\n'
    end
 
    --Room Req.
    if itemTable.roomContainReq ~= nil then
        section = section .. sectionRow('Room Required', itemTable.roomContainReq)
    end
 
    --Room Size. Req.
    if itemTable.roomSizeReq ~= nil then
        section = section .. sectionRow('Room Size', itemTable.roomSizeReq, nil, '')
    end
 
    --Room Mat. Req.
    if itemTable.roomMatReq ~= nil then
        section = section .. sectionRow('Room Materials', itemTable.roomMatReq)
    end
    return section
end
end


function skillreqcell( args )
function objectFormSection(itemTable, itemImageName)
     local str = ''
     return sectionImage(itemImageName, 'Form')
str = str .. '| ' .. imagedisp(args[1])
str = str .. '<br> Level ' .. args[2] .. ' \n'
return str
end
end


function groupedskillcell( args )
function housingSection(itemTable)
     local str = ''
     -- Housing Header
str = str .. '| ' .. imagedisp(args[1])
    section = sectionHeader('Housing')
if args[2] ~= nil then
 
str = str .. ' <br> ' .. imagedisp(args[2])
    --roomCategory
end
    section = section .. sectionRow('Room Category', itemTable.roomCategory) .. '[[Category:' .. itemTable.roomCategory .. ']]\n'
return str
 
    if itemTable.roomCategory ~= L.t('Industrial') then
        if itemTable.furnitureType ~= nil then
            --furnitureType
            section = section .. sectionRow('Furniture Type', itemTable.furnitureType) .. '[[Category:' .. itemTable.furnitureType .. ']]\n'
 
            --Value of the object
            section = section .. sectionRow('Value', itemTable.skillValue)
 
            --Dim. Return % of Object
            section = section .. sectionRow('Dim. Return %', itemTable.repeatsDepreciation * 100, nil, '%')
        end
    end
 
    if itemTable.roomCategory == L.t('Industrial') then
        section = section .. "|- style=\"background-color: #red; text-align: center;\"\n| colspan=\"2\" | '''" .. L.t('ALL ROOM VALUE LOST') .. "'''\n"
    end
 
    return section
end
 
function storageSection(itemTable)
    -- Storage Header
    section = sectionHeader('Storage')
 
    --Inventory Slots
    section = section .. sectionRow('Inventory Slots', itemTable.inventorySlots)
 
    --inventoryMaxWeight
    if itemTable.inventoryMaxWeight ~= nil then
        maxWeightKg = itemTable.inventoryMaxWeight / 1000
        section = section .. sectionRow('Inventory Max Weight', maxWeightKg, nil, 'kg')
    else
        section = section .. sectionRow('Inventory Max Weight', L.t('Unlimited'))
    end
 
    return section
end
 
function powerSection(itemTable)
    -- Power Header
    section = sectionHeader('Power')
 
    --EngergyType
    section = section .. sectionRow('Energy Type', itemTable.energyType) .. '[[Category:' .. itemTable.energyType .. ']]\n'
 
    --Grid Radius
    section = section .. sectionRow('Grid Radius', itemTable.gridRadius, nil, 'm')
 
    --Energy Produced
    section = section .. sectionRow('Energy Produced', itemTable.energyProduced, nil, 'w')
 
    --Energy Used
    section = section .. sectionRow('Energy Used', itemTable.energyUsed, nil, 'w')
 
    return section
end
 
function fuelsSection(itemTable)
    -- Fuel Header
    section = sectionHeader('Fuel')
 
    --Fuels Used by Object
    section = section .. sectionRow('Fuels Used', '[[' .. L.t('%s Tag'):format(itemTable.fuelsUsed:gsub('[%[%]]+', '')) .. ']]')
 
    return section
end
end


-- Create a wikitable of recipes
function fluidsSection(itemTable)
function p.main( recipes )
     -- Liquid/Gas Header
    local rows = ''
     section = sectionHeader('Liquid/Gas')
     --Not relevant for v0.9
     -- find number of times each Affect by Skill occurs
    --local prevSkill = recipes[1].efficiencySkills[1]
    --local skillIndex = {}
    --local prevSkillnum = 0
--local count = 0
    --for num = 1, #recipes do
--if prevSkill ~= recipes[num].efficiencySkills[1] then
--skillIndex[#skillIndex + 1] = prevSkillnum + count
--prevSkillnum = 1
--if #recipes[num].ingredients > 4 then
--count = 1
--else
--count = 0
--end
--prevSkill = recipes[num].efficiencySkills[1]
--else
--prevSkillnum = prevSkillnum + 1
--if #recipes[num].ingredients > 4 then
--count = count + 1
--end
--end
    --end
    --skillIndex[#skillIndex + 1] = prevSkillnum + count
    -- Get each row
    local j = 0
    local k = 1
    for i = 1, #recipes do


-- get information from the current recipe for building
    --Input (fludisUsed)
local checkImage = recipes[i].checkImage
    if itemTable.fluidsUsed ~= nil then
local craftStn = recipes[i].craftStn
        local list = ''
local skillNeeds = recipes[i].skillNeeds
        for a, b in ipairs(itemTable.fluidsUsed) do
--use last variant as this is always the default variant
            local acceptedType = b[1]
local numberOfVariants = recipes[i].numberOfVariants
            local cRateString = string.gsub(b[2], "%s+", "")
local products = {{'',''}}
            local consumingRate = tonumber(cRateString)
local ingredients = {{'',''}}
            list = list .. L.t('%s at %sL'):format(acceptedType, consumingRate)
--loop through variants to select default variant ingredients
for k, v in pairs(recipes[i].variants) do
--given pairs doesn't guarantee order, check that product matches default variant
if v.products[1][1] == recipes[i].defaultVariant then
ingredients = v.ingredients
products = v.products
else
--handle scenarios where product ~= default variant (for example Refine Tallow)
ingredients = v.ingredients
products = v.products
end
end
local baseCraftTime = recipes[i].baseCraftTime
local baseLaborCost = recipes[i].baseLaborCost
local baseXPGain = recipes[i].baseXPGain
-- determine the size needed for products to display correctly
local ingrednum = #ingredients
local rowspan = '|'
local rowspanProducts = '|'
--Code to incorporate rowspan for ingrediants and variants here
local rowspanCount = 0


--accomodate both variants and multiple ingredients
            if (a ~= #itemTable.fluidsUsed) then
                list = list .. ', '
if ingrednum > 4 then
            end
rowspanCount = rowspanCount + 2
        end
end
        section = section .. sectionRow('Input', list)
--suspect there will be a bug here for many variants & many ingredients - might need x2 multipler per variant.
    end
if(#recipes == 1) then --only add variant rowspans if there is one recipe (i.e. variants will be displayed)
if tonumber(recipes[i].numberOfVariants) > 1 then
rowspanCount = rowspanCount + tonumber(recipes[i].numberOfVariants)
rowspanProducts = '| rowspan=\"' .. rowspanCount - recipes[i].numberOfVariants - 1 .. '\" '
end
end
if rowspanCount > 0 then
rowspan = '| rowspan=\"' .. rowspanCount .. '\" '
if(#recipes > 1) then
rowspanProducts = '| rowspan=\"' .. rowspanCount .. '\" '
else
rowspanProducts = '| rowspan=\"' .. rowspanCount - recipes[i].numberOfVariants - 1 .. '\" '
end
end


    --Output (liquidProduced)
--original rowspan working
    if itemTable.fluidsProduced ~= nil then
--if ingrednum > 4 then
        local list = ''
-- rowspan = '| rowspan=\"2\" '
        for a, b in ipairs(itemTable.fluidsProduced) do
--end
            local producedType = b[1]
            local pRateString = string.gsub(b[2], "%s+", "")
-- String to return
            local producingRate = tonumber(pRateString)
local row = '|-\n'
            if (producingRate == 0) then
                producingRate = 'Rate of Input'
-- Add new row if products or ingredients are passed in with at least 1 item
            end
if (products[1] and ingredients[1]) then
            list = list .. L.t('%s at %sL'):format(producedType, producingRate)
     
            if (a ~= #itemTable.fluidsProduced) then
-- Show or hide the Crafting Station column
                list = list .. ', '
if recipes[i].dispCraftStn == '1' then
            end
row = row .. rowspan .. stationcell({craftStn[1]}) .. '\n'
        end
end
        section = section .. sectionRow('Output', list)
       
    end
-- Add the products columns
local prodnum = #products
for a = 1, prodnum do
if prodnum == 1 then
row = row .. rowspanProducts .. ' colspan=\"4\" '
end
if ((prodnum == 2) or (prodnum == 3 and a == 2)) then
row = row .. rowspanProducts .. ' colspan=\"2\" '
end
row = row .. itemcell({products[a][1], products[a][2]})
end
-- Add the FIRST row of Ingredients column
local numing = ingrednum
if ingrednum > 4 then
numing = 4
end
for b = 1, numing do
if (ingrednum == 1) then
row = row .. '| colspan=\"4\" '
end
if ((ingrednum == 2) or (ingrednum == 3 and b == 2)) then
row = row .. '| colspan=\"2\" '
end
row = row .. itemcell({ingredients[b][2], ingredients[b][3]})
end


-- Add the Skill needed column
    return section
if (skillNeeds[1] ~= '' and skillNeeds[1] ~= nil) then
end
row = row .. rowspan .. skillreqcell({skillNeeds[1][1], skillNeeds[1][2]})
else
row = row .. rowspan
if ingrednum > 4 then
row = row .. '|'
end
row = row .. ' \'\'None\'\' \n'
end
-- Add the Crafting time column
row = row .. rowspan
if rowspanCount > 1 then
row = row .. '|'
end
if baseCraftTime then
row = row .. baseCraftTime .. '\n'
else
row = row .. rowspan .. ' \'\'missing\'\' \n'
end
-- Add the Labour time column
row = row .. rowspan
if rowspanCount > 1 then
row = row .. '|'
end
if baseLaborCost then
row = row .. baseLaborCost .. '\n'
else
row = row .. rowspan .. ' \'\'missing\'\' \n'
end
-- Add the XP gain column
row = row .. rowspan
if rowspanCount > 1 then
row = row .. '|'
end
if baseXPGain then
row = row .. baseXPGain .. '\n'
else
row = row .. rowspan .. ' \'\'missing\'\' \n'
end
-- not in v0.9
-- Add the Affect by Skills column IF new skill grouping
--if j == 0 and (recipes[i].efficiencySkills[1] ~= nil and recipes[i].efficiencySkills[1] ~= 'nil')  then
--row = row .. '| rowspan=\"' .. skillIndex[k] .. '\" ' .. groupedskillcell({recipes[i].efficiencySkills[1], recipes[i].speedSkills}) .. '\n'
--elseif (recipes[i].efficiencySkills[1] == nil or recipes[i].efficiencySkills[1] == 'nil')  then
--row = row .. '| \n'
--end
--j = j + 1
--if #ingredients > 4 then
--j = j + 1
--end
--if j == skillIndex[k] then
--k = k + 1
--j = 0
--end
-- Add the SECOND row of ingredients to ingredients column if applicable
if ingrednum > 4 then
row = row .. '|-\n'
for b = 5, ingrednum do
if (ingrednum == 5) then
row = row .. '| colspan=\"4\" '
end
if ((ingrednum == 6) or (ingrednum == 7 and b == 6)) then
row = row .. '| colspan=\"2\" '
end
row = row .. itemcell({ingredients[b][2], ingredients[b][3]})
end
end
rows = rows .. row
end


--Show variants in table if only 1 recipe in list
function roadItemsSection(itemTable, itemImageName)
if #recipes == 1 and tonumber(recipes[i].numberOfVariants) > 1 then
    section = sectionHeader('Road Object')
for k, v in pairs(recipes[i].variants) do
--start new row for variants
row = '|-\n'
--Skip default variant
if v.products[1][1] ~= recipes[i].defaultVariant then
local prodnum = #products
for a = 1, prodnum do
if prodnum == 1 then
row = row .. rowspanProducts .. ' colspan=\"4\" '
end
if ((prodnum == 2) or (prodnum == 3 and a == 2)) then
row = row .. rowspanProducts .. ' colspan=\"2\" '
end
row = row .. itemcell({v.products[a][1], v.products[a][2]})
end


--will need to cycle ingrediants to fill out complete table
    -- Object Placed Image
--row = row .. itemcell({v.products[1][1], v.products[1][2]})
    section = section .. sectionImage(itemImageName, 'Placed')
local numing = ingrednum
    return section
if ingrednum > 4 then
end
numing = 4
 
end
function checkImage(imageName, suffix)
for b = 1, numing do
    local image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. imageName .. '_' .. suffix .. '.png|[[Category:Pages_with_missing_' .. suffix:lower() .. ']]'
if (ingrednum == 1) then
    if mw.title.makeTitle('File', imageName .. '_' .. suffix .. '.png').file.exists then
row = row .. '| colspan=\"4\" '
        image = imageName .. '_' .. suffix .. '.png'
end
    elseif mw.title.makeTitle('File', imageName .. '_' .. suffix .. '.jpg').file.exists then
if ((ingrednum == 2) or (ingrednum == 3 and b == 2)) then
        image = imageName .. '_' .. suffix .. '.jpg'
row = row .. '| colspan=\"2\" '
    end
end
    return image
row = row .. itemcell({v.ingredients[b][2], v.ingredients[b][3]})
end
end
end
rows = rows .. row
end


end
-- Main entry point for the Module
end
function p.ItemMain(frame)
    -- get args from the Template
    local args = Utils.normaliseArgs(frame)


     -- get item data
    local itemData = mw.loadData("Module:ItemData")
     -- Return the full wikitable
    return itemBox(args, itemData)
return header({recipes[1].dispCraftStn}) .. rows .. '|-\n|}\n'
end
end


return p
return p

Revision as of 12:38, 30 April 2021

Documentation

This module provides the back end functionality of the Template:Infobox_Item.

If the template is passed an Infobox will be made using details from the following Modules:

Credit

Original Infobox (now known as Infobox_Item) created by Pradoxzon was then edited by Nesphit and TreeNuts0. Fyre (FishAus) and Scotty (ZeelNightwolf) further edited the Infobox_Item. They then duplicated and used as a base for Infobox_Skill, Infobox_Plant, and Infobox_Animal.


-- Credit: Original Infobox (now known as Infobox_Item) created by Pradoxzon was then edited by Nesphit and TreeNuts0. Fyre (FishAus) and Scotty (ZeelNightwolf) further edited the Infobox_Item. They then duplicated and used as a base for Infobox_Skill, Infobox_Plant, and Infobox_Animal.

local p = {}
local Utils = require('Module:Utils')
local L = require('Module:Localization')

-- Build an Item Infobox
function itemBox(args, itemData)
    -- check that all necessary arguments are passed correctly
    if args.name == nil or args.name == '' then
        return '\'name\' must be specified.'
    end

    local item = args.name
    local itemTable = itemData.items[item]

    if itemTable == nil then
        return item .. ' could not be found in Module:ItemData.'
    end

    local itemType = itemTable.type
    local itemEN = string.sub(itemType, 1, -5)
    local itemimagename = string.gsub(itemEN, ' ', '')

    -- string used to build the infobox
    local infobox = '{| class=\"infobox\"\n'

    -- 'Name and Image' section
    -- name of the item
    infobox = infobox .. '|- style=\"color: white; background-color: #1165AF; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. item .. '</big>\'\'\'\n'

    -- the item's type (ItemData - group)
    infobox = infobox .. '|- style=\"text-align: center; color: white; background-color: '

    local text_colour = '#78B1FF'

    if itemTable.group == L.t('Food') then
        text_colour = '#85D66B'
    elseif itemTable.group == L.t('Skill Books') or itemTable.group == L.t('Skill Scrolls') then
        text_colour = '#FFCF4D'
    elseif itemTable.group == L.t('Skill Scrolls') then
        text_colour = '#FFCF4D'
    end

    infobox = infobox .. text_colour .. ';\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
    local icon = Utils.build_icon(item)
    infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | '..icon .. '\n'

    -- 'Description' section header
    infobox = infobox .. sectionHeader('Description')

    if itemTable.description == nil or itemTable.description == '' then
        infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | \'\'\'' .. L.t('None') .. '\'\'\'\n'
    else
        infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. itemTable.description .. '\n'
    end

    --Crafting
    local craftingRecipes = mw.loadData("Module:CraftingRecipes")

    infobox = infobox .. generalSection(item, itemTable, craftingRecipes, args)

    infobox = infobox .. IDsSection(itemTable)

    -- Tags Header
    if itemTable.tagGroups ~= nil and itemTable.tagGroups ~= {} then
        infobox = infobox .. tagSection(itemTable, itemData)
    end

    -- 'Item' World Object header (if itemTable.group = Placeable or Blocks)
    if itemTable.group == L.t('Block Items') or itemTable.group == L.t('World Object Items') then
        infobox = infobox .. placementSection(itemTable, itemimagename)

        -- Object Form Image
        if itemTable.group == L.t('Block Items') then
            infobox = infobox .. objectFormSection(itemTable, itemimagename)
        end

        -- 'Housing' section (if there is a Room Category)
        if itemTable.roomCategory ~= nil then
            infobox = infobox .. housingSection(itemTable)
        end

        -- 'Storage' Section (if inventorySlots is not nil)
        if itemTable.inventorySlots ~= nil then
            infobox = infobox .. storageSection(itemTable)
        end

        -- 'Power' section (if EngeryType is ``not nil)
        if itemTable.energyType ~= nil then
            infobox = infobox .. powerSection(itemTable)

        end

        -- 'Fuel' Section (if fuelsUsed by Object)
        if itemTable.fuelsUsed ~= nil then
            infobox = infobox .. fuelsSection(itemTable)
        end

        -- 'Fluid' section (if fludisUsed is not nil)
        if itemTable.fluidsUsed ~= nil or itemTable.fluidsProduced ~= nil then
            infobox = infobox .. fluidsSection(itemTable)
        end
    end

    -- Road Object header (if group == Road Items)
    if itemTable.group == L.t('Road Items') then
        infobox = infobox .. roadItemsSection(itemTable, itemimagename)
    end
    infobox = infobox .. '|}'
    return infobox
end

function sectionHeader(title, count)
    return "|- style=\"background-color: #4688C0; text-align: center;\"\n| colspan=\"2\" | '''" .. L.t(title, count) .. "'''\n"
end

function sectionRow(label, content, count, unit)
    if unit ~= nil then
        content = tonumber(content)
        unit = L.t(unit, content)
        local lang = mw.getContentLanguage()
        content = lang:formatNum(content)
    else
        unit = ''
    end
    return '|-\n| ' .. L.t(label, count) .. ':\n| style=\"text-align: right; padding: 3px;\" | ' .. content .. unit .. '\n'
end

function sectionImage(imageName, suffix)
    local image = checkImage(imageName, suffix)
    return '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|border|240px]]\n'
end

local function addToSet(set, key)
    set[key] = true
end

local function setNotContains(set, key)
    return set[key] == nil
end

local function craftingSubSection(title, item, productsOrIngredients, recipes)
    local stations = {}
    local sortStations = {}
    local stationString = ''
    for _, recipeName in ipairs(productsOrIngredients[item]) do
        local currentRecipe = recipes[recipeName]
        if currentRecipe ~= nil then
            if currentRecipe.variants[recipeName] ~= nil then
                local currentStation = currentRecipe.craftStn[1][1]
                if setNotContains(stations, currentStation) then
                    addToSet(stations, currentStation)
                end
            end
        end
    end

    if stations then
        for a, _ in pairs(stations) do
            table.insert(sortStations, a)
        end
        table.sort(sortStations)
        for i, n in ipairs(sortStations) do
            stationString = stationString .. ' [[' .. n .. ']]'
            if (n ~= sortStations[#sortStations]) then
                -- add a comma
                stationString = stationString .. ', '
            end
        end
        return sectionRow(title, stationString)
    else
        return sectionRow(title, L.t('N/A'))
    end
end

function generalSection(item, itemTable, craftingRecipes, args)
    -- 'General' section header
    section = sectionHeader('General')

    -- Is a product at these tables
    if craftingRecipes.products[item] ~= nil and Utils.tableLen(craftingRecipes.products[item]) >= 1 then
        section = section .. craftingSubSection('Created at', item, craftingRecipes.products, craftingRecipes.recipes)
    end

    -- Is an ingredient at these tables
    if craftingRecipes.ingredients[item] ~= nil and Utils.tableLen(craftingRecipes.ingredients[item]) >= 1 then
        section = section .. craftingSubSection('Used at', item, craftingRecipes.ingredients, craftingRecipes.recipes)
    end

    -- calories and nutrients (if itemTable.group == 'Food')
    if itemTable.group == L.t('Food') then
        section = section .. sectionRow('Calorie', itemTable.calories, tonumber(itemTable.calories), 'cal')
        section = section .. '|- valign=\"center\"\n| rowspan=\"4\" | ' .. L.t('Nutrients') .. ':\n'
        section = section .. '| style=\"color: red; text-align: right; padding: 3px;\" | ' .. L.t('Carbs') .. ': ' .. itemTable.carbs .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: orange; text-align: right; padding: 3px;\" | ' .. L.t('Protein') .. ': ' .. itemTable.protein .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: darkkhaki; text-align: right; padding: 3px;\" | ' .. L.t('Fat') .. ': ' .. itemTable.fat .. '\n'
        section = section .. '|- valign=\"center\"\n| style=\"color: limegreen; text-align: right; padding: 3px;\" | ' .. L.t('Vitamins') .. ': ' .. itemTable.vitamins .. '\n'
        section = section .. sectionRow('Nutrition Density', L.t('%s per 100 cals'):format(itemTable.density))
    end

    -- carried
    local carried
    if args.carried ~= nil and args.carried ~= '' then
        carried = args.carried
    else
        carried = itemTable.carried
    end
    section = section .. sectionRow('Carried in', carried)

    -- weight
    local weight
    if itemTable.weight ~= nil then
        weight = itemTable.weight
    else
        weight = '0.0'
    end
    section = section .. sectionRow('Weight', weight, nil, 'kg')

    -- stack limit
    if itemTable.maxStack ~= nil then
        section = section .. sectionRow('Stack limit', itemTable.maxStack)
    end

    -- yield
    if itemTable.yield ~= nil then
        section = section .. sectionRow('Improve Gathering', itemTable.yield)
    end

    -- fuel
    if itemTable.fuel ~= nil then
        section = section .. sectionRow('Fuel energy', itemTable.fuel, nil, 'J')
    end

    -- currency
    if itemTable.currency ~= nil then
        section = section .. "|- style=\"text-align: center;\"\n| colspan=\"2\" | " .. L.t('Can back a currency') .. "\n"
    end
    return section
end

function IDsSection(itemTable)
    -- 'IDs' section header
    section = sectionHeader('ID', 2)

    -- item id (type)
    section = section .. sectionRow('Item ID', itemTable.type)

    -- id number (type id)
    section = section .. sectionRow('ID Number', itemTable.typeID)

    return section
end

function tagSection(itemTable, itemData)
    -- Tags Header
    local tags = itemTable.tagGroups
    section = sectionHeader('Tag', Utils.tableLen(tags))
    local list = ''
    -- for each item in the list (a is position, b is value)
    for a, b in ipairs(tags) do
        --if not these tags listed here
        if (b ~= L.t('Object') or b ~= L.t('World  Object') or b ~= L.t('Housing  Object')) then
            -- add the tag to the list
            -- HACK: Some tag localisations have spaces in the tag name, but not in the tag data
            local tagLink
            -- Some tags have multiple spaces? Possible data quality issue
            bClean = b:gsub("%s+", " ")
            if itemData.tags[bClean] ~= nil then
                tagLink = L.t('%s Tag'):format(bClean)
            else
                tagLink = L.t('%s Tag'):format(bClean:gsub(' ', ''))
            end

            list = list .. '[[' .. tagLink .. '|' .. bClean .. ']][[Category:' .. bClean .. ']]'
        end
        -- if not the last item in the list
        if (b ~= tags[#tags]) then
            -- add a comma
            list = list .. ', '
        end
    end
    -- Now the list is made add it to the infobox
    section = section .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. list .. '\n'

    return section
end

function placementSection(itemTable, itemImageName)
    section = sectionHeader('World Object')
    -- Object Placed Image
    section = section .. sectionImage(itemImageName, 'Placed')

    -- 'Placement' section
    --Placement Header
    section = section .. sectionHeader('Placement')

    --Vechile
    if itemTable.mobile ~= nil then
        section = section .. sectionRow('Vehicle/Mobile Object', itemTable.mobile)
    end

    --Dimensions
    if itemTable.footprint ~= nil then
        section = section .. sectionRow('Dimensions (X,Y,Z)', itemTable.footprint)
    end

    --Material Tier
    if itemTable.materialTier ~= nil or itemTable.materialTier == 0 then
        section = section .. sectionRow('Room Material', itemTable.materialTier) .. '[[Category:Tier ' .. itemTable.materialTier .. ']]\n'
    end

    --Room Req.
    if itemTable.roomContainReq ~= nil then
        section = section .. sectionRow('Room Required', itemTable.roomContainReq)
    end

    --Room Size. Req.
    if itemTable.roomSizeReq ~= nil then
        section = section .. sectionRow('Room Size', itemTable.roomSizeReq, nil, 'm³')
    end

    --Room Mat. Req.
    if itemTable.roomMatReq ~= nil then
        section = section .. sectionRow('Room Materials', itemTable.roomMatReq)
    end
    return section
end

function objectFormSection(itemTable, itemImageName)
    return sectionImage(itemImageName, 'Form')
end

function housingSection(itemTable)
    -- Housing Header
    section = sectionHeader('Housing')

    --roomCategory
    section = section .. sectionRow('Room Category', itemTable.roomCategory) .. '[[Category:' .. itemTable.roomCategory .. ']]\n'

    if itemTable.roomCategory ~= L.t('Industrial') then
        if itemTable.furnitureType ~= nil then
            --furnitureType
            section = section .. sectionRow('Furniture Type', itemTable.furnitureType) .. '[[Category:' .. itemTable.furnitureType .. ']]\n'

            --Value of the object
            section = section .. sectionRow('Value', itemTable.skillValue)

            --Dim. Return % of Object
            section = section .. sectionRow('Dim. Return %', itemTable.repeatsDepreciation * 100, nil, '%')
        end
    end

    if itemTable.roomCategory == L.t('Industrial') then
        section = section .. "|- style=\"background-color: #red; text-align: center;\"\n| colspan=\"2\" | '''" .. L.t('ALL ROOM VALUE LOST') .. "'''\n"
    end

    return section
end

function storageSection(itemTable)
    -- Storage Header
    section = sectionHeader('Storage')

    --Inventory Slots
    section = section .. sectionRow('Inventory Slots', itemTable.inventorySlots)

    --inventoryMaxWeight
    if itemTable.inventoryMaxWeight ~= nil then
        maxWeightKg = itemTable.inventoryMaxWeight / 1000
        section = section .. sectionRow('Inventory Max Weight', maxWeightKg, nil, 'kg')
    else
        section = section .. sectionRow('Inventory Max Weight', L.t('Unlimited'))
    end

    return section
end

function powerSection(itemTable)
    -- Power Header
    section = sectionHeader('Power')

    --EngergyType
    section = section .. sectionRow('Energy Type', itemTable.energyType) .. '[[Category:' .. itemTable.energyType .. ']]\n'

    --Grid Radius
    section = section .. sectionRow('Grid Radius', itemTable.gridRadius, nil, 'm')

    --Energy Produced
    section = section .. sectionRow('Energy Produced', itemTable.energyProduced, nil, 'w')

    --Energy Used
    section = section .. sectionRow('Energy Used', itemTable.energyUsed, nil, 'w')

    return section
end

function fuelsSection(itemTable)
    -- Fuel Header
    section = sectionHeader('Fuel')

    --Fuels Used by Object
    section = section .. sectionRow('Fuels Used', '[[' .. L.t('%s Tag'):format(itemTable.fuelsUsed:gsub('[%[%]]+', '')) .. ']]')

    return section
end

function fluidsSection(itemTable)
    -- Liquid/Gas Header
    section = sectionHeader('Liquid/Gas')

    --Input (fludisUsed)
    if itemTable.fluidsUsed ~= nil then
        local list = ''
        for a, b in ipairs(itemTable.fluidsUsed) do
            local acceptedType = b[1]
            local cRateString = string.gsub(b[2], "%s+", "")
            local consumingRate = tonumber(cRateString)
            list = list .. L.t('%s at %sL'):format(acceptedType, consumingRate)

            if (a ~= #itemTable.fluidsUsed) then
                list = list .. ', '
            end
        end
        section = section .. sectionRow('Input', list)
    end

    --Output (liquidProduced)
    if itemTable.fluidsProduced ~= nil then
        local list = ''
        for a, b in ipairs(itemTable.fluidsProduced) do
            local producedType = b[1]
            local pRateString = string.gsub(b[2], "%s+", "")
            local producingRate = tonumber(pRateString)
            if (producingRate == 0) then
                producingRate = 'Rate of Input'
            end
            list = list .. L.t('%s at %sL'):format(producedType, producingRate)
            if (a ~= #itemTable.fluidsProduced) then
                list = list .. ', '
            end
        end
        section = section .. sectionRow('Output', list)
    end

    return section
end

function roadItemsSection(itemTable, itemImageName)
    section = sectionHeader('Road Object')

    -- Object Placed Image
    section = section .. sectionImage(itemImageName, 'Placed')
    return section
end

function checkImage(imageName, suffix)
    local image = 'NoImage.png|link=https://wiki.play.eco/index.php?title=Special:Upload&wpDestFile=' .. imageName .. '_' .. suffix .. '.png|[[Category:Pages_with_missing_' .. suffix:lower() .. ']]'
    if mw.title.makeTitle('File', imageName .. '_' .. suffix .. '.png').file.exists then
        image = imageName .. '_' .. suffix .. '.png'
    elseif mw.title.makeTitle('File', imageName .. '_' .. suffix .. '.jpg').file.exists then
        image = imageName .. '_' .. suffix .. '.jpg'
    end
    return image
end

-- Main entry point for the Module
function p.ItemMain(frame)
    -- get args from the Template
    local args = Utils.normaliseArgs(frame)

    -- get item data
    local itemData = mw.loadData("Module:ItemData")
    return itemBox(args, itemData)
end

return p