« Module:Monnaie » : différence entre les versions
De Nefald
Autres actions
Aucun résumé des modifications Balise : Révoqué |
m espace insécable |
||
| (6 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 10 : | Ligne 10 : | ||
} | } | ||
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 16 : | Ligne 21 : | ||
} | } | ||
-- ============================================================ | |||
-- Construit le tooltip au survol | |||
-- ============================================================ | |||
local function buildTooltip(ecus, oboles, sols, total) | local function buildTooltip(ecus, oboles, sols, total) | ||
local parts = {} | local parts = {} | ||
| Ligne 25 : | Ligne 33 : | ||
end | end | ||
local function buildIcon(amount, imageFile, altText | -- ============================================================ | ||
local | -- Construit une icône + valeur via wikilink parsé | ||
-- ============================================================ | |||
local function buildIcon(amount, imageFile, altText) | |||
local img = "[[Fichier:" .. imageFile .. "|24px|link=|alt=" .. altText .. "]]" | |||
return '<span class="monnaie__groupe">' | |||
.. '<span class="monnaie__valeur">' .. amount .. ' </span>' | |||
.. '<span class="monnaie__icone">' .. img .. '</span>' | |||
.. '</span>' | |||
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) | ||
local n = tonumber(input) | local n = tonumber(input) | ||
| Ligne 70 : | Ligne 77 : | ||
end | end | ||
-- ============================================================ | |||
-- Fonction principale | |||
-- ============================================================ | |||
function p.afficher(frame) | function p.afficher(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
| Ligne 75 : | Ligne 85 : | ||
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 81 : | Ligne 92 : | ||
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 96 : | Ligne 108 : | ||
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 101 : | Ligne 114 : | ||
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" | if ecus > 0 then parts[#parts+1] = buildIcon(ecus, IMAGES.ecu, "écu") end | ||
if oboles > 0 then parts[#parts+1] = buildIcon(oboles, IMAGES.obole, "obole" | if oboles > 0 then parts[#parts+1] = buildIcon(oboles, IMAGES.obole, "obole") end | ||
if sols > 0 then parts[#parts+1] = buildIcon(sols, IMAGES.sol, "sol" | if sols > 0 then parts[#parts+1] = buildIcon(sols, IMAGES.sol, "sol") 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") | ||
end | end | ||
local html = '<span class="monnaie" title="' .. tooltip .. '">' | |||
local html = | |||
.. table.concat(parts) | .. table.concat(parts) | ||
.. '</span>' | .. '</span>' | ||