Basculer le menu
Changer de menu des préférences
Basculer le menu personnel
Non connecté(e)
Votre adresse IP sera visible au public si vous faites des modifications.

« Module:Monnaie » : différence entre les versions

De Nefald
Hiob (discussion | contributions)
Aucun résumé des modifications
Balise : Révocation manuelle
Hiob (discussion | contributions)
Aucun résumé des modifications
Ligne 12 : Ligne 12 :
local ICON_SIZE = "24px"
local ICON_SIZE = "24px"


-- ============================================================
-- Aliases acceptés pour chaque monnaie
-- ============================================================
local ALIASES = {
local ALIASES = {
     ecu  = { "écu", "ecu", "écus", "ecus", "e" },
     ecu  = { "écu", "ecu", "écus", "ecus", "e" },
Ligne 21 : Ligne 18 :
}
}


-- ============================================================
-- Construit le tooltip au survol
-- ============================================================
local function buildTooltip(ecus, oboles, sols, total)
local function buildTooltip(ecus, oboles, sols, total)
     local parts = {}
     local parts = {}
Ligne 33 : Ligne 27 :
end
end


-- ============================================================
local function buildIcon(amount, imageFile, altText, frame)
-- Construit une icône + valeur
-- Les pipes dans [[Fichier:...]] cassent le parsing
-- ============================================================
-- On utilise {{!}} + preprocess pour les injecter proprement
local function buildIcon(amount, imageFile, altText)
     local wikilink = "[[Fichier:" .. imageFile
     local img = "[[Fichier:" .. imageFile
         .. "{{!}}" .. ICON_SIZE
         .. "|" .. ICON_SIZE
         .. "{{!}}link={{!}}alt=" .. altText .. "]]"
         .. "|link=|alt=" .. altText .. "]]"
    local img = frame:preprocess(wikilink)
     return '<span class="monnaie__groupe">'
     return '<span class="monnaie__groupe">'
         .. '<span class="monnaie__valeur">' .. amount .. "</span>"
         .. '<span class="monnaie__valeur">' .. amount .. "</span>"
Ligne 46 : Ligne 40 :
end
end


-- ============================================================
-- Parse une chaîne mixte ex: "1écu, 2 oboles, 5sols"
-- Retourne le total en sols, ou nil si invalide
-- ============================================================
local function parseString(input)
local function parseString(input)
    -- Tentative de lecture comme nombre pur (= sols)
     local n = tonumber(input)
     local n = tonumber(input)
     if n then return math.floor(n) end
     if n then return math.floor(n) end
Ligne 62 : Ligne 51 :
         local amount = tonumber(amount_str) or 0
         local amount = tonumber(amount_str) or 0
         unit = mw.text.trim(unit)
         unit = mw.text.trim(unit)
         for monnaie, aliases in pairs(ALIASES) do
         for monnaie, aliases in pairs(ALIASES) do
             for _, alias in ipairs(aliases) do
             for _, alias in ipairs(aliases) do
Ligne 80 : Ligne 68 :
end
end


-- ============================================================
-- Fonction principale
-- ============================================================
function p.afficher(frame)
function p.afficher(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local total
     local total


    -- Mode 3 paramètres : au moins args[2] ou args[3] est déclaré
     if args[2] ~= nil or args[3] ~= nil then
     if args[2] ~= nil or args[3] ~= nil then
         local ecus  = tonumber(mw.text.trim(args[1] or "")) or 0
         local ecus  = tonumber(mw.text.trim(args[1] or "")) or 0
Ligne 95 : Ligne 78 :
         total = (ecus * ECU_VALUE) + (oboles * OBOLE_VALUE) + sols
         total = (ecus * ECU_VALUE) + (oboles * OBOLE_VALUE) + sols


    -- Mode 1 paramètre : nombre brut (sols) ou chaîne texte libre
     elseif args[1] ~= nil then
     elseif args[1] ~= nil then
         local arg1 = mw.text.trim(args[1])
         local arg1 = mw.text.trim(args[1])
Ligne 111 : Ligne 93 :
     end
     end


    -- Décomposition en écus / oboles / sols
     local ecus  = math.floor(total / ECU_VALUE)
     local ecus  = math.floor(total / ECU_VALUE)
     local reste  = total % ECU_VALUE
     local reste  = total % ECU_VALUE
Ligne 117 : Ligne 98 :
     local sols  = reste % OBOLE_VALUE
     local sols  = reste % OBOLE_VALUE


    -- Tooltip
     local tooltip = buildTooltip(ecus, oboles, sols, total)
     local tooltip = buildTooltip(ecus, oboles, sols, total)


    -- Construction du HTML
     local parts = {}
     local parts = {}
     if ecus  > 0 then parts[#parts+1] = buildIcon(ecus,  IMAGES.ecu,  "écu")   end
     if ecus  > 0 then parts[#parts+1] = buildIcon(ecus,  IMAGES.ecu,  "écu",  frame) end
     if oboles > 0 then parts[#parts+1] = buildIcon(oboles, IMAGES.obole, "obole") end
     if oboles > 0 then parts[#parts+1] = buildIcon(oboles, IMAGES.obole, "obole", frame) end
     if sols  > 0 then parts[#parts+1] = buildIcon(sols,  IMAGES.sol,  "sol")   end
     if sols  > 0 then parts[#parts+1] = buildIcon(sols,  IMAGES.sol,  "sol",  frame) end
     if #parts == 0 then
     if #parts == 0 then
         parts[#parts+1] = buildIcon(0, IMAGES.sol, "sol")
         parts[#parts+1] = buildIcon(0, IMAGES.sol, "sol", frame)
     end
     end


    -- Chargement du TemplateStyles
     local styles = frame:extensionTag("templatestyles", "", { src = "Modèle:Monnaie/styles.css" })
     local styles = frame:extensionTag("templatestyles", "", { src = "Modèle:Monnaie/styles.css" })



Version du 1 mars 2026 à 13:53

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

local p = {}

local ECU_VALUE   = 4096
local OBOLE_VALUE = 64

local IMAGES = {
    ecu   = "Écu_pièce_or.png",
    obole = "Obole_pièce_argent.png",
    sol   = "Sol_pièce_cuivre.png",
}

local ICON_SIZE = "24px"

local ALIASES = {
    ecu   = { "écu", "ecu", "écus", "ecus", "e" },
    obole = { "obole", "oboles", "o" },
    sol   = { "sol", "sols", "s" },
}

local function buildTooltip(ecus, oboles, sols, total)
    local parts = {}
    if ecus   > 0 then parts[#parts+1] = ecus   .. " écu"   .. (ecus   > 1 and "s" or "") end
    if oboles > 0 then parts[#parts+1] = oboles .. " obole" .. (oboles > 1 and "s" or "") end
    if sols   > 0 then parts[#parts+1] = sols   .. " sol"   .. (sols   > 1 and "s" or "") end
    if #parts == 0 then parts[#parts+1] = "0 sol" end
    return table.concat(parts, ", ") .. " (" .. total .. " sols)"
end

local function buildIcon(amount, imageFile, altText, frame)
-- Les pipes dans [[Fichier:...]] cassent le parsing
-- On utilise {{!}} + preprocess pour les injecter proprement
    local wikilink = "[[Fichier:" .. imageFile
        .. "{{!}}" .. ICON_SIZE
        .. "{{!}}link={{!}}alt=" .. altText .. "]]"
    local img = frame:preprocess(wikilink)
    return '<span class="monnaie__groupe">'
        .. '<span class="monnaie__valeur">' .. amount .. "</span>"
        .. '<span class="monnaie__icone">'  .. img    .. "</span>"
        .. "</span>"
end

local function parseString(input)
    local n = tonumber(input)
    if n then return math.floor(n) end

    local total = 0
    local found = false
    local s = mw.ustring.lower(input)

    for amount_str, unit in mw.ustring.gmatch(s, "(%d+)%s*([%a]+)") do
        local amount = tonumber(amount_str) or 0
        unit = mw.text.trim(unit)
        for monnaie, aliases in pairs(ALIASES) do
            for _, alias in ipairs(aliases) do
                if unit == alias then
                    if monnaie == "ecu"   then total = total + amount * ECU_VALUE   end
                    if monnaie == "obole" then total = total + amount * OBOLE_VALUE end
                    if monnaie == "sol"   then total = total + amount               end
                    found = true
                    break
                end
            end
        end
    end

    if not found then return nil end
    return total
end

function p.afficher(frame)
    local args = frame:getParent().args
    local total

    if args[2] ~= nil or args[3] ~= nil then
        local ecus   = tonumber(mw.text.trim(args[1] or "")) or 0
        local oboles = tonumber(mw.text.trim(args[2] or "")) or 0
        local sols   = tonumber(mw.text.trim(args[3] or "")) or 0
        total = (ecus * ECU_VALUE) + (oboles * OBOLE_VALUE) + sols

    elseif args[1] ~= nil then
        local arg1 = mw.text.trim(args[1])
        if arg1 == "" then
            return '<span class="error">Paramètre manquant</span>'
        end
        total = parseString(arg1)

    else
        return '<span class="error">Paramètre manquant</span>'
    end

    if not total or total < 0 then
        return '<span class="error">Montant invalide</span>'
    end

    local ecus   = math.floor(total / ECU_VALUE)
    local reste  = total % ECU_VALUE
    local oboles = math.floor(reste / OBOLE_VALUE)
    local sols   = reste % OBOLE_VALUE

    local tooltip = buildTooltip(ecus, oboles, sols, total)

    local parts = {}
    if ecus   > 0 then parts[#parts+1] = buildIcon(ecus,   IMAGES.ecu,   "écu",   frame) end
    if oboles > 0 then parts[#parts+1] = buildIcon(oboles, IMAGES.obole, "obole", frame) end
    if sols   > 0 then parts[#parts+1] = buildIcon(sols,   IMAGES.sol,   "sol",   frame) end
    if #parts == 0 then
        parts[#parts+1] = buildIcon(0, IMAGES.sol, "sol", frame)
    end

    local styles = frame:extensionTag("templatestyles", "", { src = "Modèle:Monnaie/styles.css" })

    return styles
        .. '<span class="monnaie" title="' .. tooltip .. '">'
        .. table.concat(parts)
        .. "</span>"
end

return p
Les témoins (''cookies'') nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de témoins.