No edit summary |
No edit summary |
||
(35 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
− | function p. |
+ | function p.incense(frame) |
if frame == mw.getCurrentFrame() then |
if frame == mw.getCurrentFrame() then |
||
args = require('Module:ProcessArgs').merge(true) |
args = require('Module:ProcessArgs').merge(true) |
||
Line 7: | Line 7: | ||
frame = mw.getCurrentFrame() |
frame = mw.getCurrentFrame() |
||
end |
end |
||
⚫ | |||
− | -- get some implicit args |
||
− | local |
+ | local name = mw.title.getCurrentTitle().text |
− | local isWeapon = (args.type == 'Weapon' or args.type == 'Bow') |
||
+ | local fields = '_pageName, name, incense, effects, DLC' |
||
− | -- get tags |
||
+ | local cargoArgs = { orderBy = '_pageName', groupBy = 'name', where = 'incense="' .. name .. '"', limit = 5000 } |
||
⚫ | |||
+ | return buildhtml(data, frame) |
||
− | local tags = {} |
||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
+ | function p.main(frame) |
||
+ | if frame == mw.getCurrentFrame() then |
||
+ | args = require('Module:ProcessArgs').merge(true) |
||
+ | else |
||
+ | frame = mw.getCurrentFrame() |
||
end |
end |
||
-- basic cargo query |
-- basic cargo query |
||
− | local fields = '_pageName, name, |
+ | local fields = '_pageName, name, tags, slots, items, effects, DLC' |
+ | local cargoArgs = { orderBy = '_pageName', groupBy = 'name', limit = 5000 } |
||
− | local args = { |
||
+ | local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs) |
||
− | orderBy = '_pageName' |
||
⚫ | |||
⚫ | |||
+ | -- actual search |
||
⚫ | |||
− | local results = |
+ | local results = search(data, args) |
− | local i = 1 |
||
− | local debug = '' |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | if isWeapon and slot == 'Weapon' then |
||
⚫ | |||
− | elseif args.type == 'Bow' and slot == 'Bow' then |
||
⚫ | |||
− | elseif args.type == 'Helmet' and slot == 'Helmet' then |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | local metItems = false |
||
− | + | if #results < 1 then |
|
⚫ | |||
⚫ | |||
+ | end |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | metTags = true |
||
− | break |
||
⚫ | |||
⚫ | |||
− | if metTags then break end |
||
⚫ | |||
⚫ | |||
− | metTags = true |
||
⚫ | |||
+ | return buildhtml(results, frame) |
||
⚫ | |||
+ | end |
||
− | local enchantItems = split(enchant.items, ',') |
||
− | for _,item in ipairs(enchantItems) do |
||
⚫ | |||
− | metItems = true |
||
− | break |
||
⚫ | |||
− | end |
||
− | else |
||
− | metItems = true |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | function search(data, args) |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | + | local results = {} |
|
⚫ | |||
⚫ | |||
⚫ | |||
+ | -- check for explicit item requirements |
||
+ | if notempty(enchant.items) then |
||
+ | local name = args.name or mw.title.getCurrentTitle().text |
||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | if not isCompatible and notempty(enchant.slots) then |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | isCompatible = hasmatch(enchTags, itemTags) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
end |
end |
||
+ | return results |
||
+ | end |
||
− | + | function buildhtml(results, frame) |
|
⚫ | |||
⚫ | |||
− | |||
-- build table output |
-- build table output |
||
local html = mw.html.create() |
local html = mw.html.create() |
||
− | + | -- removing hatnote for now: {{hatnote|<i>DLC: {{The Soroboreans}} [[The Soroboreans]]</i>}} |
|
+ | html:wikitext(frame:preprocess('<b>{{PAGENAME}}</b> is compatible with the following [[Enchantments]]:')) |
||
local table = html:tag('table'):addClass('wikitable sortable') |
local table = html:tag('table'):addClass('wikitable sortable') |
||
Line 101: | Line 91: | ||
for _,v in ipairs(results) do |
for _,v in ipairs(results) do |
||
local tr = table:tag('tr') |
local tr = table:tag('tr') |
||
− | tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess('[[' .. v._pageName .. ']]')) |
+ | tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess('[[' .. v._pageName .. '|' .. v.name .. ']]' .. " {{" .. v.DLC .. "}}")) |
tr:tag('td'):wikitext(frame:preprocess(v.effects)) |
tr:tag('td'):wikitext(frame:preprocess(v.effects)) |
||
end |
end |
||
+ | |||
⚫ | |||
return html |
return html |
||
+ | end |
||
+ | |||
+ | ---------------------------------------------------------------- |
||
+ | --------------------------- HELPERS ---------------------------- |
||
+ | |||
+ | function find(tbl, val) |
||
+ | for k, v in pairs(tbl) do |
||
⚫ | |||
⚫ | |||
+ | return nil |
||
+ | end |
||
+ | |||
+ | function notempty(string) |
||
+ | return string ~= nil and string ~= '' |
||
+ | end |
||
+ | |||
+ | function hasmatch(table1, table2) |
||
⚫ | |||
+ | if contains(table2, v) then |
||
⚫ | |||
⚫ | |||
+ | end |
||
+ | return false |
||
+ | end |
||
+ | |||
+ | function contains(table, value) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | end |
||
+ | return false |
||
end |
end |
||
Line 113: | Line 135: | ||
end |
end |
||
local t={} |
local t={} |
||
− | + | if notempty(inputstr) then |
|
+ | for str in string.gmatch(inputstr .. ',', "([^"..sep.."]+)") do |
||
− | table.insert(t, str) |
+ | table.insert(t, str) |
⚫ | |||
end |
end |
||
return t |
return t |
Latest revision as of 18:26, 23 December 2020
Module invoked by Template:UsedInEnchantments.
The above documentation is transcluded from Module:UsedInEnchantments/doc. (edit | history)
local p = {}
function p.incense(frame)
if frame == mw.getCurrentFrame() then
args = require('Module:ProcessArgs').merge(true)
else
frame = mw.getCurrentFrame()
end
local name = mw.title.getCurrentTitle().text
local fields = '_pageName, name, incense, effects, DLC'
local cargoArgs = { orderBy = '_pageName', groupBy = 'name', where = 'incense="' .. name .. '"', limit = 5000 }
local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)
return buildhtml(data, frame)
end
function p.main(frame)
if frame == mw.getCurrentFrame() then
args = require('Module:ProcessArgs').merge(true)
else
frame = mw.getCurrentFrame()
end
-- basic cargo query
local fields = '_pageName, name, tags, slots, items, effects, DLC'
local cargoArgs = { orderBy = '_pageName', groupBy = 'name', limit = 5000 }
local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs)
-- actual search
local results = search(data, args)
if #results < 1 then
return '<b>No results found!</b>'
end
return buildhtml(results, frame)
end
-- build list of compatible enchants (too complex for cargo query)
function search(data, args)
local itemTags = split(args.tags, ',')
local results = {}
for _,enchant in ipairs(data) do
local isCompatible = false
-- check for explicit item requirements
if notempty(enchant.items) then
local name = args.name or mw.title.getCurrentTitle().text
local items = split(enchant.items, ',')
if contains(items, name) then
isCompatible = true
end
end
-- check slot and tags
if not isCompatible and notempty(enchant.slots) then
local slots = split(enchant.slots, ',')
if hasmatch(slots, itemTags) then
if notempty(enchant.tags) then
local enchTags = split(enchant.tags, ',')
isCompatible = hasmatch(enchTags, itemTags)
else
isCompatible = true
end
end
end
-- actually add it to the matches list
if isCompatible then
table.insert(results, enchant)
end
end
return results
end
function buildhtml(results, frame)
-- build table output
local html = mw.html.create()
-- removing hatnote for now: {{hatnote|<i>DLC: {{The Soroboreans}} [[The Soroboreans]]</i>}}
html:wikitext(frame:preprocess('<b>{{PAGENAME}}</b> is compatible with the following [[Enchantments]]:'))
local table = html:tag('table'):addClass('wikitable sortable')
local header = table:tag('tr')
header:tag('th'):wikitext('Enchantment')
header:tag('th'):wikitext('Effects')
for _,v in ipairs(results) do
local tr = table:tag('tr')
tr:tag('td'):cssText('text-align:center'):wikitext(frame:preprocess('[[' .. v._pageName .. '|' .. v.name .. ']]' .. " {{" .. v.DLC .. "}}"))
tr:tag('td'):wikitext(frame:preprocess(v.effects))
end
return html
end
----------------------------------------------------------------
--------------------------- HELPERS ----------------------------
function find(tbl, val)
for k, v in pairs(tbl) do
if k == val then return v end
end
return nil
end
function notempty(string)
return string ~= nil and string ~= ''
end
function hasmatch(table1, table2)
for _,v in ipairs(table1) do
if contains(table2, v) then
return true
end
end
return false
end
function contains(table, value)
for _,v in ipairs(table) do
if v == value then
return true
end
end
return false
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
if notempty(inputstr) then
for str in string.gmatch(inputstr .. ',', "([^"..sep.."]+)") do
table.insert(t, str)
end
end
return t
end
return p