Module:ItemSources: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
m (support multiple drop json tables for single item type/frequency)
Line 52: Line 52:
return false
return false
end
end
end

local function parseDropJson(out, source, drop)
local dropJSON = mw.text.jsonDecode(drop or '{}')

local q2 = {
'[[:' .. dropJSON['Dropped from'] .. ']]',
'?Profession Requirement A=professionreq',
}
local sourcesmwdata = mw.smw.ask(q2)

for _, v in ipairs(sourcesmwdata) do
local professionreq = v.professionreq or 'Unknown'
local professionlevel = professionreq
if professionreq ~= 'Unknown' then
local profession, level = v.professionreq:match("([^,]+),([^,]+)")
professionlevel = pcp(profession, level)
end

local frequency = dropJSON['Frequency']
local frequency_value
if frequencies[string.lower(tostring(frequency))] then
frequency = params.ucflc(frequency)
else
frequency_value = frequency:gsub(',','') --temp place to put this without overriding frequency
local rv1, rv2 = string.match(frequency_value, '([%d%.]+)/([%d%.]+)')
if rv1 and rv2 then
frequency = commas(rv1) .. '/' .. commas(rv2)
frequency_value = rv1/rv2
else
frequency_value = expr(frequency)
end
end

local freq_class, freq_sort = '', nil
if frequency_value == nil then
freq_class, freq_sort = unpack(frequencies[string.lower(tostring(frequency))] or frequencies._default)
elseif frequency_value == false then
freq_class, freq_sort = unpack(frequencies._default)
else
freq_sort = 1/frequency_value
freq_class = get_frequency_class(frequency_value)
end

out:tag('tr')
:tag('td'):wikitext(source.source):done()
:tag('td'):wikitext(professionlevel):done()
:tag('td'):wikitext(dropJSON['Dropped quantity']):done()
:tag('td'):wikitext(frequency)
:attr('data-sort-value', freq_sort)
:addClass(freq_class)
:done()
:done()
end

return out
end
end


function p._main(args)
function p._main(args)
local item = args[1] or mw.title.getCurrentTitle().fullText
local item = args[1] or mw.title.getCurrentTitle().fullText

local q = {
local q = {
'[[Dropped item::' .. item .. ']]',
'[[Dropped item::' .. item .. ']]',
Line 63: Line 119:
}
}
local sourcessmwdata = mw.smw.ask(q)
local sourcessmwdata = mw.smw.ask(q)

if not sourcessmwdata then
if not sourcessmwdata then
return ":''No sources found. To force an update, click "
return ":''No sources found. To force an update, click "
Line 69: Line 125:
..".''[[Category:Empty source lists]]"
..".''[[Category:Empty source lists]]"
end
end

local out = mw.html.create('table')
local out = mw.html.create('table')
:addClass('wikitable sortable autosort=4,a align-center-2 align-center-3 align-center-4')
:addClass('wikitable sortable autosort=4,a align-center-2 align-center-3 align-center-4')
Line 78: Line 134:
:tag('th'):wikitext('Frequency'):done()
:tag('th'):wikitext('Frequency'):done()
:done()
:done()

for _, source in ipairs(sourcessmwdata) do
for _, source in ipairs(sourcessmwdata) do
local dropJSON = mw.text.jsonDecode(source['Drop JSON'] or '{}')
local drop = source['Drop JSON']
if type(drop) == 'string' then
out = parseDropJson(out, source, drop)
local q2 = {
elseif type(drop) == 'table' then
'[[:' .. dropJSON['Dropped from'] .. ']]',
for _, v in ipairs(drop) do
'?Profession Requirement A=professionreq',
out = parseDropJson(out, source, v)
}
local sourcesmwdata = mw.smw.ask(q2)
for _, v in ipairs(sourcesmwdata) do
local professionreq = v.professionreq or 'Unknown'
local professionlevel = professionreq
if professionreq ~= 'Unknown' then
local profession, level = v.professionreq:match("([^,]+),([^,]+)")
professionlevel = pcp(profession, level)
end
end
local frequency = dropJSON['Frequency']
local frequency_value
if frequencies[string.lower(tostring(frequency))] then
frequency = params.ucflc(frequency)
else
frequency_value = frequency:gsub(',','') --temp place to put this without overriding frequency
local rv1, rv2 = string.match(frequency_value, '([%d%.]+)/([%d%.]+)')
if rv1 and rv2 then
frequency = commas(rv1) .. '/' .. commas(rv2)
frequency_value = rv1/rv2
else
frequency_value = expr(frequency)
end
end
local freq_class, freq_sort = '', nil
if frequency_value == nil then
freq_class, freq_sort = unpack(frequencies[string.lower(tostring(frequency))] or frequencies._default)
elseif frequency_value == false then
freq_class, freq_sort = unpack(frequencies._default)
else
freq_sort = 1/frequency_value
freq_class = get_frequency_class(frequency_value)
end
out:tag('tr')
:tag('td'):wikitext(source.source):done()
:tag('td'):wikitext(professionlevel):done()
:tag('td'):wikitext(dropJSON['Dropped quantity']):done()
:tag('td'):wikitext(frequency)
:attr('data-sort-value', freq_sort)
:addClass(freq_class)
:done()
:done()
end
end
end
end

return out
return out
end
end