Module:Infobox Biome

From Eco - English Wiki

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

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

-- Build an Biome Infobox
function p.biomeBox(args)
    -- check that all necessary arguments are passed correctly
    if args.id == nil or args.id == '' then
        return '\'id\' must be specified.'
    end

    local biomeData = mw.loadData("Module:BiomeData")

    local biome_id = args.id
    local biome = biomeData[biome_id]

    if biome == nil then
        return "Biome " .. biome_id .. " not found"
    end

    local infobox = '{| class=\"infobox\"\n'

    foreground, background = Utils.mapColour(biome.Color)

    infobox = infobox ..
        "|- style=\"color: white; background-color: " ..
        background .. "; text-align: center;\"\n| colspan=\"2\" | '''<big>" .. biome.Name .. "</big>'''\n"

    infobox = infobox .. sectionImage(biome.ID, "Biome")

    infobox = infobox .. sectionHeader('Description')
    infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | ' .. biome.Description .. '\n'

    infobox = infobox .. sectionHeader('Details')
    infobox = infobox .. sectionRow("Can have lakes?", Utils.formatBoolToYesNo(biome.Lakes))
    infobox = infobox .. sectionRow("Elevation", biome.ElevationMin * 100 .. ' -  ' .. biome.ElevationMax * 100)
    infobox = infobox ..
        sectionRow("Temperature", biome.TemperatureMin * 40 - 10 .. ' -  ' .. biome.TemperatureMax * 40 - 10 .. ' °C')
    infobox = infobox .. sectionRow("Moisture", biome.MoistureMin * 100 .. ' -  ' .. biome.MoistureMax * 100)

    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

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 / 100
    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 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 .. '.jpeg').file.exists then
        image = imageName .. '_' .. suffix .. '.jpeg'
    elseif mw.title.makeTitle('File', imageName .. '_' .. suffix .. '.png').file.exists then
        image = imageName .. '_' .. suffix .. '.png'
    end
    return image
end

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

return p