No edit summary |
No edit summary |
||
(12 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 |
||
+ | args = require('Module:ProcessArgs').merge(true) |
||
+ | else |
||
+ | frame = mw.getCurrentFrame() |
||
⚫ | |||
+ | |||
+ | 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 |
if frame == mw.getCurrentFrame() then |
||
args = require('Module:ProcessArgs').merge(true) |
args = require('Module:ProcessArgs').merge(true) |
||
Line 10: | Line 26: | ||
-- basic cargo query |
-- basic cargo query |
||
− | local fields = '_pageName, name, tags, slots, items, effects' |
+ | local fields = '_pageName, name, tags, slots, items, effects, DLC' |
− | local cargoArgs = { orderBy = '_pageName', groupBy = 'name' } |
+ | local cargoArgs = { orderBy = '_pageName', groupBy = 'name', limit = 5000 } |
− | local data = mw.ext.cargo.query(' |
+ | local data = mw.ext.cargo.query('EnchantRecipes', fields, cargoArgs) |
-- actual search |
-- actual search |
||
Line 21: | Line 37: | ||
end |
end |
||
+ | return buildhtml(results, frame) |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 44: | Line 44: | ||
local itemTags = split(args.tags, ',') |
local itemTags = split(args.tags, ',') |
||
local results = {} |
local results = {} |
||
− | local i = 1 |
||
for _,enchant in ipairs(data) do |
for _,enchant in ipairs(data) do |
||
local isCompatible = false |
local isCompatible = false |
||
Line 72: | Line 71: | ||
-- actually add it to the matches list |
-- actually add it to the matches list |
||
if isCompatible then |
if isCompatible then |
||
− | results |
+ | table.insert(results, enchant) |
⚫ | |||
end |
end |
||
end |
end |
||
return results |
return results |
||
+ | end |
||
+ | |||
+ | function buildhtml(results, frame) |
||
⚫ | |||
⚫ | |||
+ | -- removing hatnote for now: {{hatnote|<i>DLC: {{The Soroboreans}} [[The Soroboreans]]</i>}} |
||
⚫ | |||
+ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
end |
end |
||
---------------------------------------------------------------- |
---------------------------------------------------------------- |
||
--------------------------- HELPERS ---------------------------- |
--------------------------- HELPERS ---------------------------- |
||
+ | |||
+ | function find(tbl, val) |
||
+ | for k, v in pairs(tbl) do |
||
⚫ | |||
+ | end |
||
+ | return nil |
||
+ | end |
||
function notempty(string) |
function notempty(string) |
||
Line 87: | Line 113: | ||
function hasmatch(table1, table2) |
function hasmatch(table1, table2) |
||
− | local flag = false |
||
for _,v in ipairs(table1) do |
for _,v in ipairs(table1) do |
||
if contains(table2, v) then |
if contains(table2, v) then |
||
− | + | return true |
|
− | break |
||
end |
end |
||
end |
end |
||
− | return |
+ | return false |
end |
end |
||
function contains(table, value) |
function contains(table, value) |
||
− | local flag = false |
||
for _,v in ipairs(table) do |
for _,v in ipairs(table) do |
||
if v == value then |
if v == value then |
||
− | + | return true |
|
− | break |
||
end |
end |
||
end |
end |
||
− | return |
+ | return false |
end |
end |
||
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