Modul:Infobox

Aus Eco - Deutsches Wiki
Wechseln zu:Navigation, Suche

Die Dokumentation für dieses Modul kann unter Modul:Infobox/Doku erstellt werden

local p = {}

-- Grabs args from the parent frame
-- Trims and parses the args into a table, then returns the table
function norm()
    local origArgs = mw.getCurrentFrame():getParent().args
    local args = {}
    
    for k, v in pairs( origArgs ) do
        v = mw.text.trim( tostring( v ) )
        if v ~= '' then
            args[k] = v
        end
    end
    
    return args
end


-- 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
    
    -- string used to build the infobox
    local infobox = '{| class=\"infobox\"\n'
    
    -- name of the item
    infobox = infobox .. '|- style=\"background-color: darkred; text-align: center;\"\n| colspan=\"2\" | \'\'\'<big>' .. item .. '</big>\'\'\'\n'
    
    -- the item's type (ItemData - group)
    infobox = infobox .. '|- style=\"text-align: center; background-color: '
    if itemTable.group == 'Food' then
        infobox = infobox .. '#4b9130;\"\n| colspan=\"2\" | \'\'\'Food\'\'\'\n'
    elseif itemTable.group == 'Skill Books' or itemTable.group == 'Skill Scrolls' then
        infobox = infobox .. '#af8d33;\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
    else
        infobox = infobox .. '#517ab2;\"\n| colspan=\"2\" | \'\'\'' .. itemTable.group .. '\'\'\'\n'
    end
    
    -- the item's subtype if specified
    if args.subtype ~= nil and args.subtype ~= '' then
        infobox = infobox .. '|- style=\"text-align: center;\"\n| colspan=\"2\" | \'\'\'<small>' .. args.subtype .. '</small>\'\'\'\n'
    end
    
    -- the item's icon
    local image = string.gsub(item, ' ', '') .. '_Icon.png'
    if not mw.title.makeTitle('File', image).exists then
        image = 'NoImage.png'
    end
    infobox = infobox .. '|-\n| colspan=\"2\" style=\"padding: 10px;\" | [[File:' .. image .. '|center|frameless|95px]]\n'
    
    -- 'General' section header
    infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'General\'\'\'\n'
    
    -- created at
    local craftingRecipes = require( "Module:CraftingRecipes" )
    local recipes = craftingRecipes.recipes
    if craftingRecipes.items[item] ~= nil and #craftingRecipes.items[item] >= 1 then
        local found = false
        local last = ''
        for k, v in ipairs( craftingRecipes.items[item] ) do
            if (recipes[v].item1[1] ~= nil and recipes[v].item1[1] == item) or (recipes[v].item2[1] ~= nil and recipes[v].item2[1] == item) then
                if found == false then
                    found = true
                    infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" |'
                end
                if last ~= recipes[v].craftStn[1] then
                    last = recipes[v].craftStn[1]
                    infobox = infobox .. ' [[' .. recipes[v].craftStn[1] .. ']],'
                end
            end
        end
        if found == true then
            infobox = infobox .. '\n'
        else
            infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | N/A\n'
        end
    elseif args.created ~= nil and args.created ~= '' then
        infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | ' .. args.created .. '\n'
    else
        infobox = infobox .. '|-\n| Created at:\n| style=\"text-align: right;\" | N/A\n'
    end
    
    -- calories and nutrients (if Food)
    if itemTable.group == 'Food' then
        infobox = infobox .. '|-\n| Calories:\n| style=\"text-align: right;\" | ' .. itemTable.calories .. '\n|- valign=\"center\"\n| rowspan=\"4\" | Nutrients:\n'
        infobox = infobox .. '| style=\"color: red; text-align: right;\" | Carbs: ' .. itemTable.carbs .. '\n'
        infobox = infobox .. '|- valign=\"center\"\n| style=\"color: orange; text-align: right;\" | Protein: ' .. itemTable.protein .. '\n'
        infobox = infobox .. '|- valign=\"center\"\n| style=\"color: yellow; text-align: right;\" | Fat: ' .. itemTable.fat .. '\n'
        infobox = infobox .. '|- valign=\"center\"\n| style=\"color: limegreen; text-align: right;\" | Vitamins: ' .. itemTable.vitamins .. '\n'
    end
    
    -- carried
    if args.carried ~= nil and args.carried ~= '' then
        infobox = infobox .. '|-\n| Carried in:\n| style=\"text-align: right;\" | ' .. args.carried .. '\n'
    else
        infobox = infobox .. '|-\n| Carried in:\n| style=\"text-align: right;\" | ' .. itemTable.carried .. '\n'
    end
    
    -- weight
    if itemTable.weight ~= nil then
        infobox = infobox .. '|-\n| Weight:\n| style=\"text-align: right;\" | ' .. itemTable.weight .. '\n'
    else
        infobox = infobox .. '|-\n| Weight:\n| style=\"text-align: right;\" | 0.00kg\n'
    end
    
    -- stack limit
    infobox = infobox .. '|-\n| Stack limit:\n| style=\"text-align: right;\" | ' .. itemTable.maxStack .. '\n'
    
    -- fuel
    if itemTable.fuel ~= nil then
        infobox = infobox .. '|-\n| Fuel energy:\n| style=\"text-align: right;\" | ' .. itemTable.fuel .. '\n'
    end
    
    -- 'IDs' section header
    infobox = infobox .. '|- style=\"background-color: #4B9130; text-align: center;\"\n| colspan=\"2\" | \'\'\'IDs\'\'\'\n'
    
    -- item id (type)
    infobox = infobox .. '|- valign=\"center\"\n| Item ID:\n| style=\"text-align: right;\" | ' .. itemTable.type .. '\n'
    
    -- id number (type id)
    infobox = infobox .. '|- valign=\"center\"\n| ID Number:\n| style=\"text-align: right;\" | ' .. itemTable.typeID .. '\n'
    
    infobox = infobox .. '|}'
    return infobox
end


-- Build an Object Infobox
function objectBox( args, itemData )
    return 'Not yet implemented.'
end


-- Build a Skill Infobox
function skillBox( args )
    return 'Not yet implemented.'
end


-- Main entry point for the Module
function p.ItemMain()
    -- get args from the Template
    local args = norm()
    
    -- get item data
    local itemData = require( "Module:ItemData" )
    
    return itemBox( args, itemData )
    
    -- determine the type of infobox
    --if args.boxType ~= nil then
        -- if the call is from Template:Infobox_Item
    --    if args.boxType == 'Item' then
    --        return itemBox( args, itemData )
        
        -- if the call is from Template:Infobox_Object
    --    elseif args.boxType == 'Object' then
    --        return objectBox( args, itemData )
        
        -- if the call is from Template:Infobox_Skill
    --    elseif args.boxType == 'Skill' then
    --        return skillBox( args )
        
        -- wrong type given
    --    else
    --        return 'Invalid boxType given. Please use \'Item\', \'Object\', or \'Skill\'.'
    --    end
    --else
        -- type of infobox was not specified
    --    return 'Invalid use of this Infobox Module. Please see its documentation page for information on proper usage.'
    --end
end


return p