Editing Module:MonsterVariantsTable
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
require('strict') |
|||
require('Module:Mw.html extension') |
|||
local editbutton = require('Module:Edit button') |
|||
local p = {} |
local p = {} |
||
function p.main(frame) |
function p.main(frame) |
||
return p._main(frame:getParent().args) |
|||
return p._main(args[1] or args.variant or '') |
|||
end |
end |
||
function p.formatProfessionLevel(profession, level, high) |
|||
if not (profession or level) then |
|||
return nil |
|||
end |
|||
profession = profession or 'Unknown profession' |
profession = profession or 'Unknown profession' |
||
local level_text = tostring(level or '?') |
local level_text = tostring(level or '?') |
||
Line 18: | Line 17: | ||
end |
end |
||
function p._main( |
function p._main(args) |
||
local |
local variant = args[1] or args.variant or '' |
||
local query = { |
|||
('[[Variant of::%s]]'):format(variant), |
|||
string.format('[[Variant of::%s]] [[Has query::+]] OR [[Has subobject::<q>[[Variant of::%s]]</q>]]', variant, variant), |
|||
'?Image#64px;x64px = img', |
|||
'?Image#64px;x64px=img', |
|||
'?Name = name', |
|||
'?Has subobject.Image#64px;x64px=img2', |
|||
'?= page', |
|||
'?= page', |
|||
'?Unlock_level = unlock_lvl', |
|||
'?Unlock_level = unlock', |
|||
'?Unlock profession # = unlock_profession', |
|||
'?Has subobject.Unlock_level = unlock2', |
|||
'?Profession_Level_A = combat', |
|||
'?Profession_Level_A = combat', |
|||
'?Profession A # = profession', |
|||
'?Has subobject.Profession_Level_A = combat2', |
|||
'?Version default = default', |
|||
'?Profession A #=profession', |
|||
'?Variant name = variant', |
|||
'?Has subobject.Profession A #=profession2' |
|||
'?Version anchor = version', |
|||
} |
|||
'?-Has subobject#- = subobject', |
|||
'sort=Profession Level A', |
|||
'order=asc' |
|||
} |
|||
mw.logObject(results) |
|||
if results == nil or results[1] == nil then |
|||
return ":''No variants found for "..variant.."''" |
|||
end |
|||
local filtered = p.filter_versions(results) |
|||
local sorted = p.sort_by_combat(filtered) |
|||
return p.create_table(sorted) |
|||
end |
|||
local results = mw.smw.ask(query) |
|||
function p.filter_versions(data) |
|||
local filtered = {} |
|||
if results == nil or results[1] == nil then |
|||
for _, entry in ipairs(data) do |
|||
return 'No data found for table' |
|||
if entry.default then |
|||
entry.page = ('[[%s|%s]]'):format(entry.subobject, entry.name) |
|||
table.insert(filtered, entry) |
|||
elseif entry.version == nil then |
|||
table.insert(filtered, entry) |
|||
end |
|||
end |
end |
||
return filtered |
|||
local sorted = p.sort_by_combat(results) |
|||
--local debug_str = '<pre>'..mw.text.jsonEncode(sorted, mw.text.JSON_PRETTY)..'</pre>' |
|||
return tostring(p.create_table(sorted))--..debug_str |
|||
end |
end |
||
function p.sort_by_combat(data) |
function p.sort_by_combat(data) |
||
table.sort(data, function(a, b) |
|||
-- Determine combat values |
|||
local combatA = a.combat or (a.combat2 and a.combat2[1]) or math.huge |
|||
local combatB = b.combat or (b.combat2 and b.combat2[1]) or math.huge |
|||
-- Sort in ascending order |
|||
return combatA < combatB |
|||
end) |
|||
return data |
|||
end |
end |
||
function p.insert_row(tbl, entry) |
function p.insert_row(tbl, entry) |
||
tbl:tag('tr') |
|||
local edit = editbutton("'''?''' (edit)", entry.name) |
|||
:tag('td') |
|||
:wikitext(entry.img) |
|||
:td{ entry.img, addClass = 'plinkt-image no-border' }:done() |
|||
:td{ entry.page, addClass = 'plinkt-link no-border' }:done() |
|||
:td() |
|||
:IF(entry.unlock_profession) |
|||
:wikitext(entry.unlock_lvl and formatProfessionLevel(entry.unlock_profession, entry.unlock_lvl) or edit) |
|||
:ELSE() |
|||
:addClass('table-na') |
|||
:wikitext('N/A') |
|||
:END() |
|||
:done() |
:done() |
||
: |
:tag('td') |
||
:wikitext(entry. |
:wikitext(entry.page) |
||
:done() |
|||
:tag('td') |
|||
:wikitext(p.formatProfessionLevel(entry.profession, entry.unlock)) |
|||
:done() |
|||
:tag('td') |
|||
:wikitext(p.formatProfessionLevel(entry.profession, entry.combat)) |
|||
:done() |
:done() |
||
:done() |
:done() |
||
return tbl |
return tbl |
||
end |
end |
||
Line 93: | Line 76: | ||
function p.create_table(results) |
function p.create_table(results) |
||
local out = mw.html.create('table') |
local out = mw.html.create('table') |
||
:addClass('wikitable sortable |
:addClass('wikitable sortable') |
||
: |
:tag('tr') |
||
:tag('th') |
|||
:th{ 'Monster', attr = { colspan = 2 } }:done() |
|||
: |
:wikitext('Image') |
||
:done() |
|||
:tag('th') |
|||
:wikitext('Monster') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Unlocked at') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Combat level') |
|||
:done() |
|||
:done() |
:done() |
||
for _, |
for _,entry in ipairs(results) do |
||
local built_entry = { |
|||
out = p.insert_row(out, entry) |
|||
combat = entry.combat or entry.combat2[1] or entry.combat2 or '', |
|||
img = entry.img or entry.img2[1] or entry.img2 or '', |
|||
unlock = entry.unlock or entry.unlock2[1] or entry.unlock2 or '', |
|||
profession = entry.profession or entry.profession2[1] or entry.profession2 or '', |
|||
page = entry.page |
|||
} |
|||
out = p.insert_row(out, built_entry) |
|||
end |
end |
||
return out |
return out |
||
end |
end |