Module:PlantInfoCard

De Eco - Wiki Français
Aller à :navigation, rechercher

La documentation pour ce module peut être créée à Module:PlantInfoCard/doc

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

-- Récupération et nettoyage des arguments
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

-- Point d'entrée principal du module
function p.main()
    local args = norm()
    local species = ''
    local check = ''

    -- Détermine si c'est une plante ou un arbre (priorité plante)
    if args.plante ~= nil then
        species = args.plante
        check = 'plante'
    elseif args.arbre ~= nil then
        species = args.arbre
        check = 'arbre'
    end

    if species == '' then
        return "Erreur : un paramètre 'plante=' ou 'arbre=' doit être indiqué."
    end

    local speciesData = {}
    local speciesTable = {}

    -- Charge les bonnes données
    if check == 'plante' then
        speciesData = require("Module:PlantData")
        speciesTable = speciesData.plants[species]
    else
        speciesData = require("Module:TreeData")
        speciesTable = speciesData.trees[species]
    end

    -- Test correct : nil au lieu de {}
    if speciesTable == nil then
        if check == 'plante' then
            return "La plante « " .. species .. " » est introuvable dans Module:PlantData."
        else
            return "L’arbre « " .. species .. " » est introuvable dans Module:TreeData."
        end
    end

    ------------------------------------------------------------------
    -- Génération de l’infobox
    ------------------------------------------------------------------
    local infobox = '{| class="infobox"\n'

    -- Nom principal
    infobox = infobox .. '|- style="color:white; background-color:#1165AF; text-align:center;"\n'
    infobox = infobox .. '| colspan="2" | \'\'\'<big>' .. species .. '</big>\'\'\'\n'

    -- Sous-titre plante/arbre
    infobox = infobox .. '|- style="text-align:center; color:white; background-color:'
    if check == 'plante' then
        infobox = infobox .. 'seagreen;"\n| colspan="2" | \'\'\'Plante\'\'\' [[Catégorie:Plantes]]\n'
    else
        infobox = infobox .. 'olivedrab;"\n| colspan="2" | \'\'\'Arbre\'\'\' [[Catégorie:Arbres]]\n'
    end

    -- Image
    local checkImage = {'_Plant.png','_Plant.jpg','_Plant.jpeg','_Tree.png','_Tree.jpg','_Tree.jpeg'}
    local image = ''
    local speciesEN = speciesTable.untranslated
    local speciesimagename = string.gsub(speciesEN, ' ', '')

    for i,v in ipairs(checkImage) do
        image = speciesimagename .. v
        if mw.title.makeTitle('File', image).file.exists then
            break
        end
        if i == #checkImage then
            image = 'NoImage.png'
        end
    end

    infobox = infobox .. '|-\n| colspan="2" style="padding:10px;" | [[File:' .. image .. '|center|border|250px]]\n'

    -- ==== Section Général ====
    infobox = infobox .. '|- style="background-color:#4688C0; text-align:center;"\n'
    infobox = infobox .. '| colspan="2" | \'\'\'Général\'\'\'\n'

    if speciesTable.resourceItem or speciesTable.requireHarvestable then
        infobox = infobox .. '|- valign="center"\n| Récoltable :\n| style="text-align:right; padding:3px;" | Oui\n'
    end
    if speciesTable.isWater then
        infobox = infobox .. '|- valign="center"\n| Plante aquatique :\n| style="text-align:right; padding:3px;" | Oui\n'
    end
    if speciesTable.isDecorative then
        infobox = infobox .. '|- valign="center"\n| Décorative :\n| style="text-align:right; padding:3px;" | Oui\n'
    end
    if speciesTable.height then
        infobox = infobox .. '|- valign="center"\n| Hauteur :\n| style="text-align:right; padding:3px;" | ' .. speciesTable.height .. ' m\n'
    end

    -- ==== Agriculture ====
    if speciesTable.resourceItem or speciesTable.requireHarvestable then
        
        infobox = infobox .. '|- style="background-color:#4688C0; text-align:center;"\n'
        infobox = infobox .. '| colspan="2" | \'\'\'Agriculture\'\'\'\n'

        infobox = infobox .. '|- style="text-align:center;"\n| colspan="2" | \'\'\'Récolte\'\'\'\n'

        if speciesTable.harvestTool then
            infobox = infobox .. '|- valign="center"\n| Outil de récolte :\n| style="text-align:right; padding:3px;" | ' .. speciesTable.harvestTool .. '\n'
        end

        if speciesTable.resourceItem then
            infobox = infobox .. '|- valign="center"\n| Ressource obtenue :\n| style="text-align:right; padding:3px;" | ' .. speciesTable.resourceItem .. '\n'
        end

        if speciesTable.calorieValue then
            infobox = infobox .. '|- valign="center"\n| Calories :\n| style="text-align:right; padding:3px;" | ' .. speciesTable.calorieValue .. '\n'
        end

        if speciesTable.experiencePerHarvest then
            infobox = infobox .. '|- valign="center"\n| Expérience par récolte :\n| style="text-align:right; padding:3px;" | ' .. speciesTable.experiencePerHarvest .. '\n'
        end
    end

    -- ==== Environnement ====
    infobox = infobox .. '|- style="background-color:#4688C0; text-align:center;"\n'
    infobox = infobox .. '| colspan="2" | \'\'\'Effets sur l’environnement\'\'\'\n'

    if speciesTable.carbonRelease then
        local carb = tonumber(string.sub(speciesTable.carbonRelease, 2))
        infobox = infobox .. '|- valign="center"\n| Absorption carbone :\n| style="text-align:right; padding:3px;" | ' .. carb .. ' ppm\n'
    end

    infobox = infobox .. '|}'
    return infobox
end

return p