Mudanças entre as edições de "Módulo:Gb"

De Wiki Gla
Ir para navegação Ir para pesquisar
Linha 1: Linha 1:
-- [[Module:Gb]] — Calculadora de custos de tiers/estrelas
-- [[Module:Gb]] — Calculadora de custos de tiers/estrelas
-- Idiomático para MediaWiki (Scribunto), com parsing de argumentos via Module:Arguments (se existir)
-- Autor: Você ; Licença: CC BY-SA 4.0 (ajuste conforme a wiki)




-- ============== CORE ==================
local p = {}
local function computePath(ti, si, tf, sf)
 
local steps, total = {}, { berry=0, frag=0, pedras=0 }
 
local iTier, iStar = ti, si
-- Dependências opcionais
local function push(step) steps[#steps+1]=step; total=addCost(total, step.custo) end
local getArgs
while (iTier < tf) or (iTier == tf and iStar < sf) do
local ok, Args = pcall(require, 'Module:Arguments')
if iStar < 5 and (iTier < tf or (iTier == tf and iStar < sf)) then
if ok and type(Args) == 'table' then
local tierName = getTierName(iTier)
getArgs = function(frame)
local toStar = iStar + 1
return Args.getArgs(frame, { remap = { ["mostrar_tabela"] = "mostrar_tabela" } })
push({ tipo='estrela', tier=tierName, de=iStar, para=toStar, custo=starCost(tierName, toStar) })
end
iStar = toStar
else
else
if iTier >= #CONFIG.tiers then break end
-- Fallback simples se Module:Arguments não existir
local fromTier = getTierName(iTier)
getArgs = function(frame)
push({ tipo='tier', de=fromTier, para=getTierName(iTier+1), custo=tierUpCost(fromTier) })
return frame:getParent() and frame:getParent().args or frame.args or {}
iTier = iTier + 1; iStar = 0
end
end
end
end
return steps, total
end




-- ============= RENDER =================
-- =============== CONFIG ===============
local function pill(class, value)
local CONFIG = {
return string.format('<span class="gb-pill %s">%s</span>', class, value)
tiers = { 'bronze', 'prata', 'ouro', 'diamante' },
labels = { berry = 'Berry', frag = 'Fragmentos', pedras = 'Pedras' },
costs = {
star = {
bronze = { [2]={berry=270,frag=40}, [3]={berry=1800,frag=80}, [4]={berry=13500,frag=180}, [5]={berry=75000,frag=540} },
prata = { [2]={berry=540,frag=40}, [3]={berry=3600,frag=80}, [4]={berry=27000,frag=180}, [5]={berry=180000,frag=540} },
ouro = { [2]={berry=1800,frag=40}, [3]={berry=18000,frag=80}, [4]={berry=180000,frag=180}, [5]={berry=1800000,frag=540} },
diamante={ [2]={berry=18000,frag=40}, [3]={berry=180000,frag=80}, [4]={berry=1800000,frag=180}, [5]={berry=5000000,frag=0,pedras=100} },
},
tierUp = { bronze={berry=0,frag=0,pedras=0}, prata={berry=0,frag=0,pedras=0}, ouro={berry=0,frag=0,pedras=0}, diamante=nil },
},
aliases = { ouro = {'gold'} },
}
 
 
-- ============== UTIL ==================
local function norm(s)
if type(s) ~= 'string' then return nil end
s = mw.ustring.lower(s)
s = s:gsub('á','a'):gsub('â','a'):gsub('ã','a'):gsub('é','e'):gsub('ê','e'):gsub('í','i'):gsub('ó','o'):gsub('ô','o'):gsub('ú','u')
return s
end
end




local function renderTable(steps, total, opts)
local function yesno(v)
local h = mw.html.create('div'):addClass('calc-tier')
local t = type(v)
if opts.titulo and opts.titulo ~= '' then h:tag('h3'):wikitext(opts.titulo) end
if t == 'boolean' then return v end
local tbl = h:tag('table'):addClass('wikitable'):css('width','100%')
if t == 'number' then return v ~= 0 end
local tr = tbl:tag('tr')
if t == 'string' then
tr:tag('th'):wikitext('Passo'); tr:tag('th'):wikitext('Ação')
v = norm(v)
tr:tag('th'):wikitext(CONFIG.labels.berry)
return not (v == 'nao' or v == 'não' or v == 'n' or v == 'no' or v == 'false' or v == '0' or v == '')
tr:tag('th'):wikitext(CONFIG.labels.frag)
tr:tag('th'):wikitext(CONFIG.labels.pedras)
for i, st in ipairs(steps) do
local r = tbl:tag('tr')
r:tag('td'):wikitext(i)
if st.tipo == 'estrela' then r:tag('td'):wikitext(('Tier %s: %d → %d★'):format(st.tier, st.de, st.para))
else r:tag('td'):wikitext(('Subir de %s → %s'):format(st.de, st.para)) end
r:tag('td'):wikitext(pill('gold', formatNumber(st.custo.berry, opts.sep)))
r:tag('td'):wikitext(pill('green', formatNumber(st.custo.frag, opts.sep)))
r:tag('td'):wikitext(formatNumber(st.custo.pedras, opts.sep))
end
end
local box = h:tag('div'):addClass('calc-tier-total'):css('margin-top','8px')
return false
box:wikitext(('<b>Total:</b> %s • %s • %s: %s')
:format(pill('gold', formatNumber(total.berry, opts.sep)), pill('green', formatNumber(total.frag, opts.sep)), CONFIG.labels.pedras, formatNumber(total.pedras, opts.sep)))
return tostring(h)
end
end




local function renderCompact(total, opts)
local function clamp(v, lo, hi)
local h = mw.html.create('div'):addClass('calc-tier')
v = tonumber(v) or lo
if opts.titulo and opts.titulo ~= '' then h:tag('h3'):wikitext(opts.titulo) end
if v < lo then return lo end
local box = h:tag('div'):addClass('calc-tier-total')
if v > hi then return hi end
box:wikitext(('<b>Total:</b> %s • %s • %s: %s')
return math.floor(v)
:format(pill('gold', formatNumber(total.berry, opts.sep)), pill('green', formatNumber(total.frag, opts.sep)), CONFIG.labels.pedras, formatNumber(total.pedras, opts.sep)))
return tostring(h)
end
end




-- ============= ENTRY ==================
local function tierIndex(name)
function p.calcular(frame)
local n = norm(name)
local args = getArgs(frame)
if not n then return nil end
local inicioTier = args.inicio_tier or args.de_tier or args.tier_inicial
for i, t in ipairs(CONFIG.tiers) do if n == t then return i end end
local fimTier = args.fim_tier or args.ate_tier or args.tier_final
for base, list in pairs(CONFIG.aliases or {}) do
if not inicioTier or not fimTier then return "Erro: defina 'inicio_tier' e 'fim_tier'." end
for _, alias in ipairs(list) do if n == alias then return tierIndex(base) end end
end
return nil
end




local ti, tf = tierIndex(inicioTier), tierIndex(fimTier)
local function getTierName(idx) return CONFIG.tiers[idx] end
if not ti or not tf then return 'Erro: tier inválido. Use: bronze, prata, ouro, diamante.' end




local inicioEst = clamp(args.inicio_estrela or 0, 0, 5)
local function addCost(a, b)
local fimEst = clamp(args.fim_estrela or 0, 0, 5)
a = a or { berry=0, frag=0, pedras=0 }
if (ti > tf) or (ti == tf and inicioEst > fimEst) then return 'Erro: o alvo precisa ser maior que o estado inicial.' end
b = b or { berry=0, frag=0, pedras=0 }
 
return { berry=(a.berry or 0)+(b.berry or 0), frag=(a.frag or 0)+(b.frag or 0), pedras=(a.pedras or 0)+(b.pedras or 0) }
 
local steps, total = computePath(ti, inicioEst, tf, fimEst)
 
 
local layout = norm(args.layout or '')
local mostrarTabela = yesno(args.mostrar_tabela ~= 'nao') -- compat
if layout == '' then layout = mostrarTabela and 'tabela' or 'compacto' end
 
 
local opts = { titulo = args.titulo, sep = args.separador_milhar or '.' }
if layout == 'compacto' then return renderCompact(total, opts) end
return renderTable(steps, total, opts)
end
end




local function starCost(tierName, starTo)
local t = CONFIG.costs.star[tierName]
return p
return p
end

Edição das 21h11min de 26 de setembro de 2025

A documentação para este módulo pode ser criada em Módulo:Gb/doc

-- [[Module:Gb]] — Calculadora de custos de tiers/estrelas
-- Idiomático para MediaWiki (Scribunto), com parsing de argumentos via Module:Arguments (se existir)
-- Autor: Você ; Licença: CC BY-SA 4.0 (ajuste conforme a wiki)


local p = {}


-- Dependências opcionais
local getArgs
local ok, Args = pcall(require, 'Module:Arguments')
if ok and type(Args) == 'table' then
getArgs = function(frame)
return Args.getArgs(frame, { remap = { ["mostrar_tabela"] = "mostrar_tabela" } })
end
else
-- Fallback simples se Module:Arguments não existir
getArgs = function(frame)
return frame:getParent() and frame:getParent().args or frame.args or {}
end
end


-- =============== CONFIG ===============
local CONFIG = {
tiers = { 'bronze', 'prata', 'ouro', 'diamante' },
labels = { berry = 'Berry', frag = 'Fragmentos', pedras = 'Pedras' },
costs = {
star = {
bronze = { [2]={berry=270,frag=40}, [3]={berry=1800,frag=80}, [4]={berry=13500,frag=180}, [5]={berry=75000,frag=540} },
prata = { [2]={berry=540,frag=40}, [3]={berry=3600,frag=80}, [4]={berry=27000,frag=180}, [5]={berry=180000,frag=540} },
ouro = { [2]={berry=1800,frag=40}, [3]={berry=18000,frag=80}, [4]={berry=180000,frag=180}, [5]={berry=1800000,frag=540} },
diamante={ [2]={berry=18000,frag=40}, [3]={berry=180000,frag=80}, [4]={berry=1800000,frag=180}, [5]={berry=5000000,frag=0,pedras=100} },
},
tierUp = { bronze={berry=0,frag=0,pedras=0}, prata={berry=0,frag=0,pedras=0}, ouro={berry=0,frag=0,pedras=0}, diamante=nil },
},
aliases = { ouro = {'gold'} },
}


-- ============== UTIL ==================
local function norm(s)
if type(s) ~= 'string' then return nil end
s = mw.ustring.lower(s)
s = s:gsub('á','a'):gsub('â','a'):gsub('ã','a'):gsub('é','e'):gsub('ê','e'):gsub('í','i'):gsub('ó','o'):gsub('ô','o'):gsub('ú','u')
return s
end


local function yesno(v)
local t = type(v)
if t == 'boolean' then return v end
if t == 'number' then return v ~= 0 end
if t == 'string' then
v = norm(v)
return not (v == 'nao' or v == 'não' or v == 'n' or v == 'no' or v == 'false' or v == '0' or v == '')
end
return false
end


local function clamp(v, lo, hi)
v = tonumber(v) or lo
if v < lo then return lo end
if v > hi then return hi end
return math.floor(v)
end


local function tierIndex(name)
local n = norm(name)
if not n then return nil end
for i, t in ipairs(CONFIG.tiers) do if n == t then return i end end
for base, list in pairs(CONFIG.aliases or {}) do
for _, alias in ipairs(list) do if n == alias then return tierIndex(base) end end
end
return nil
end


local function getTierName(idx) return CONFIG.tiers[idx] end


local function addCost(a, b)
a = a or { berry=0, frag=0, pedras=0 }
b = b or { berry=0, frag=0, pedras=0 }
return { berry=(a.berry or 0)+(b.berry or 0), frag=(a.frag or 0)+(b.frag or 0), pedras=(a.pedras or 0)+(b.pedras or 0) }
end


local function starCost(tierName, starTo)
local t = CONFIG.costs.star[tierName]
return p
end