Module:Usedin: Difference between revisions
From IdleOn MMO Wiki
Kiokurashi (talk | contribs) mNo edit summary |
Kiokurashi (talk | contribs) mNo edit summary |
||
(32 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
local cargo = mw.ext.cargo | local cargo = mw.ext.cargo | ||
local NumberFormater = require('Module:NumberFormater') | local NumberFormater = require('Module:NumberFormater') | ||
local | local Utility = require('Module:Utility') | ||
local rowstruct = [=[<tr> | local rowstruct = [=[<tr> | ||
<td>[[File:%s. | <td>[[File:%s.%s|40px|link=]] [[%s]]</td> | ||
<td>%s</td> | <td>%s</td> | ||
<td>%s</td> | <td>%s</td> | ||
Line 10: | Line 10: | ||
local ret = {} | local ret = {} | ||
function | local function replaceHTMLEntity(str, entityString, replaceString) | ||
local | local x, y = str:find(entityString) | ||
if x == nil then return str end | |||
- | local first = string.sub(str, 1, x-1) | ||
local second = string.sub(str, y+1, str:len()) | |||
local full = first .. replaceString .. second | |||
return full | |||
return | |||
end | end | ||
local function QuerySmithingRecipes( | local function QuerySmithingRecipes(inItem) | ||
local tables = 'AnvilCraft' | local tables = 'AnvilCraft' | ||
-- Replace Character escape code(s) | |||
local item = replaceHTMLEntity(inItem, "'", "'") | |||
-- Multi-column dependent quantities so query them all. | -- Multi-column dependent quantities so query them all. | ||
local fields = ' | local fields = 'Item, Resource1, Resource2, Resource3, Resource4, Quantity1, Quantity2, Quantity3, Quantity4' | ||
local args = { | local args = { | ||
where = 'AnvilCraft.Resource1="'.. item ..'" OR ' .. 'AnvilCraft.Resource2="'.. item ..'" OR ' .. 'AnvilCraft.Resource3="'.. item ..'" OR ' .. 'AnvilCraft.Resource4="'.. item ..'"', | where = 'AnvilCraft.Resource1="'.. item ..'" OR ' .. 'AnvilCraft.Resource2="'.. item ..'" OR ' .. 'AnvilCraft.Resource3="'.. item ..'" OR ' .. 'AnvilCraft.Resource4="'.. item ..'"', | ||
Line 34: | Line 35: | ||
for r = 1, #results do | for r = 1, #results do | ||
-- Set specific quantity | -- Set specific quantity | ||
local quantity | local quantity | ||
if results[r].Resource1 == item then | |||
quantity = results[r].Quantity1 | |||
elseif results[r].Resource2 == item then | |||
quantity = results[r].Quantity2 | |||
elseif results[r].Resource3 == item then | |||
quantity = results[r].Quantity3 | |||
elseif results[r].Resource4 == item then | |||
quantity = results[r].Quantity4 | |||
end | |||
-- Build string and add to return table | -- Build string and add to return table | ||
ret[#ret+1] = string.format(rowstruct, results[r]. | ret[#ret+1] = string.format(rowstruct, results[r].Item, "png", results[r].Item, NumberFormater.formatnumberwithtootip(quantity), "Smithing" ) | ||
end | end | ||
end | end | ||
Line 46: | Line 55: | ||
local fields = '_pageName, Bonus' | local fields = '_pageName, Bonus' | ||
local args = { | local args = { | ||
where = 'Stamps.Material="'.. item, | where = 'Stamps.Material="'.. item .. '"', | ||
orderBy = 'Stamps._pageName' | orderBy = 'Stamps._pageName' | ||
} | } | ||
Line 52: | Line 61: | ||
if #results > 0 then | if #results > 0 then | ||
for r = 1, #results do | for r = 1, #results do | ||
ret[#ret+1] = string.format(rowstruct, results[r]._pageName, results[r]._pageName, | ret[#ret+1] = string.format(rowstruct, results[r]._pageName, "png", results[r]._pageName, | ||
"Lots", "Stamps" .. string.format(tooltipstruct, results[ | "Lots", "Stamps " .. string.format(Utility.tooltipstruct, results[r].Bonus, "More Info.")) | ||
end | end | ||
end | end | ||
Line 62: | Line 71: | ||
local fields = 'Icon, Source, Amount, Type' | local fields = 'Icon, Source, Amount, Type' | ||
local args = { | local args = { | ||
where = 'UsedinExtraData.Item="'.. item, | where = 'UsedinExtraData.Item="'.. item .. '"', | ||
orderBy = 'UsedinExtraData.Source' | orderBy = 'UsedinExtraData.Source' | ||
} | } | ||
Line 68: | Line 77: | ||
if #results > 0 then | if #results > 0 then | ||
for r = 1, #results do | for r = 1, #results do | ||
ret[#ret+1] = string.format(rowstruct, results[r].Icon, results[r].Source, | local amount = '' | ||
if results[r].Amount == "Lots" then | |||
amount = 'Lots' | |||
else | |||
amount = NumberFormater.formatnumberwithtootip(results[r].Amount) | |||
end | |||
ret[#ret+1] = string.format(rowstruct, results[r].Icon, "png", results[r].Source, amount, results[r].Type) | |||
end | end | ||
end | end | ||
end | end | ||
-- | local function NumberNabber(searchText, templist) | ||
-- | -- finds the first match and returns it from the list given. Used for grabbing information from Quests as their wikitext can interfere. | ||
local ret = "" | |||
-- Remove image size and tooltip data | |||
local list = Utility.explode(templist:gsub("%d+px","px"):gsub('data%-simple%-tooltip="[^>]*">', ''):gsub('</span>', ''), ",") | |||
local n | |||
for i = 1, #list do | |||
if(list[i]:find('data-simple-tooltip=', 1, true)) then return 'Parsing Error[[Category:Has Quest Number Nabber Parsing Error]]' end | |||
if(list[i]:find(searchText, 1, true)) then | |||
-- if first characters are the digits then grab those digits. Else if there is a colon, grab the digits after the colon. | |||
if (list[i]:sub(1, 2):match("%d")) then | |||
n = list[i]:match('%d[%d.,]*') | |||
elseif(list[i]:find(": ", 1, true)) then | |||
n = list[i]:match(':.*%f[%d.](%d*%.?%d+)') | |||
end | |||
ret = NumberFormater.formatnumberwithtootip(n) | |||
if(list[i]:match("[Cc]raft")) then ret = ret .. " (Crafted)" end | |||
if(list[i]:match("[Bb]uy")) then ret = ret .. " (Bought)" end | |||
return ret | |||
end | |||
end | |||
return ret | |||
end | |||
local function QueryQuests(item) | local function QueryQuests(item) | ||
local tables = 'Quests' | local tables = 'Quests' | ||
Line 87: | Line 125: | ||
local nabber = require('Module:Loops') --testing direct call | local nabber = require('Module:Loops') --testing direct call | ||
for r = 1, #results do | for r = 1, #results do | ||
ret[#ret+1] = string.format(rowstruct, results[r]._pageName, "gif", | |||
ret[#ret+1] = string.format( | string.format('%s#%s|%s', results[r]._pageName, results[r].QuestName, results[r].QuestName), NumberNabber(item, results[r].Requirements), "Quests") | ||
string.format(' | |||
end | end | ||
end | end | ||
Line 98: | Line 133: | ||
function p.main(frame) | function p.main(frame) | ||
local item = frame.args[1] | local item = frame.args[1] | ||
local header = [[<table class='wikitable sortable mw-collapsible' style ='min-width:45%%'><caption class='nowrap'>What %s is used in</caption><tr><th>Name</th><th>Quantity</th><th>Type</th><tr>]] | |||
-- QuerySmithingRecipes(item) | QuerySmithingRecipes(item) | ||
QueryStamps(item) | |||
QueryExtraData(item) | |||
QueryQuests(item) | QueryQuests(item) | ||
return table.concat(ret) | if #ret > 0 then | ||
return string.format(header, item) .. table.concat(ret) .. "</table>" | |||
end | |||
return '' -- return empty string if there are no results. | |||
end | end | ||
return p | return p |
Latest revision as of 17:58, 13 May 2024
Documentation for this module may be created at Module:Usedin/doc
local p = {}
local cargo = mw.ext.cargo
local NumberFormater = require('Module:NumberFormater')
local Utility = require('Module:Utility')
local rowstruct = [=[<tr>
<td>[[File:%s.%s|40px|link=]] [[%s]]</td>
<td>%s</td>
<td>%s</td>
</tr>]=]
local ret = {}
local function replaceHTMLEntity(str, entityString, replaceString)
local x, y = str:find(entityString)
if x == nil then return str end
local first = string.sub(str, 1, x-1)
local second = string.sub(str, y+1, str:len())
local full = first .. replaceString .. second
return full
end
local function QuerySmithingRecipes(inItem)
local tables = 'AnvilCraft'
-- Replace Character escape code(s)
local item = replaceHTMLEntity(inItem, "'", "'")
-- Multi-column dependent quantities so query them all.
local fields = 'Item, Resource1, Resource2, Resource3, Resource4, Quantity1, Quantity2, Quantity3, Quantity4'
local args = {
where = 'AnvilCraft.Resource1="'.. item ..'" OR ' .. 'AnvilCraft.Resource2="'.. item ..'" OR ' .. 'AnvilCraft.Resource3="'.. item ..'" OR ' .. 'AnvilCraft.Resource4="'.. item ..'"',
orderBy = 'AnvilCraft.Item'
}
local results = cargo.query( tables, fields, args )
-- if there are any results.
if #results > 0 then
for r = 1, #results do
-- Set specific quantity
local quantity
if results[r].Resource1 == item then
quantity = results[r].Quantity1
elseif results[r].Resource2 == item then
quantity = results[r].Quantity2
elseif results[r].Resource3 == item then
quantity = results[r].Quantity3
elseif results[r].Resource4 == item then
quantity = results[r].Quantity4
end
-- Build string and add to return table
ret[#ret+1] = string.format(rowstruct, results[r].Item, "png", results[r].Item, NumberFormater.formatnumberwithtootip(quantity), "Smithing" )
end
end
end
local function QueryStamps(item)
local tables = 'Stamps'
local fields = '_pageName, Bonus'
local args = {
where = 'Stamps.Material="'.. item .. '"',
orderBy = 'Stamps._pageName'
}
local results = cargo.query( tables, fields, args )
if #results > 0 then
for r = 1, #results do
ret[#ret+1] = string.format(rowstruct, results[r]._pageName, "png", results[r]._pageName,
"Lots", "Stamps " .. string.format(Utility.tooltipstruct, results[r].Bonus, "More Info."))
end
end
end
local function QueryExtraData(item)
local tables = 'UsedinExtraData'
local fields = 'Icon, Source, Amount, Type'
local args = {
where = 'UsedinExtraData.Item="'.. item .. '"',
orderBy = 'UsedinExtraData.Source'
}
local results = cargo.query( tables, fields, args )
if #results > 0 then
for r = 1, #results do
local amount = ''
if results[r].Amount == "Lots" then
amount = 'Lots'
else
amount = NumberFormater.formatnumberwithtootip(results[r].Amount)
end
ret[#ret+1] = string.format(rowstruct, results[r].Icon, "png", results[r].Source, amount, results[r].Type)
end
end
end
local function NumberNabber(searchText, templist)
-- finds the first match and returns it from the list given. Used for grabbing information from Quests as their wikitext can interfere.
local ret = ""
-- Remove image size and tooltip data
local list = Utility.explode(templist:gsub("%d+px","px"):gsub('data%-simple%-tooltip="[^>]*">', ''):gsub('</span>', ''), ",")
local n
for i = 1, #list do
if(list[i]:find('data-simple-tooltip=', 1, true)) then return 'Parsing Error[[Category:Has Quest Number Nabber Parsing Error]]' end
if(list[i]:find(searchText, 1, true)) then
-- if first characters are the digits then grab those digits. Else if there is a colon, grab the digits after the colon.
if (list[i]:sub(1, 2):match("%d")) then
n = list[i]:match('%d[%d.,]*')
elseif(list[i]:find(": ", 1, true)) then
n = list[i]:match(':.*%f[%d.](%d*%.?%d+)')
end
ret = NumberFormater.formatnumberwithtootip(n)
if(list[i]:match("[Cc]raft")) then ret = ret .. " (Crafted)" end
if(list[i]:match("[Bb]uy")) then ret = ret .. " (Bought)" end
return ret
end
end
return ret
end
local function QueryQuests(item)
local tables = 'Quests'
local fields = '_pageName, QuestName, Requirements'
local temp = ''
local args = {
where = 'Quests.Requirements LIKE "%>'.. item ..'<%"',
groupBy = 'Quests._pageName',
}
local results = cargo.query( tables, fields, args )
if #results > 0 then
local nabber = require('Module:Loops') --testing direct call
for r = 1, #results do
ret[#ret+1] = string.format(rowstruct, results[r]._pageName, "gif",
string.format('%s#%s|%s', results[r]._pageName, results[r].QuestName, results[r].QuestName), NumberNabber(item, results[r].Requirements), "Quests")
end
end
end
function p.main(frame)
local item = frame.args[1]
local header = [[<table class='wikitable sortable mw-collapsible' style ='min-width:45%%'><caption class='nowrap'>What %s is used in</caption><tr><th>Name</th><th>Quantity</th><th>Type</th><tr>]]
QuerySmithingRecipes(item)
QueryStamps(item)
QueryExtraData(item)
QueryQuests(item)
if #ret > 0 then
return string.format(header, item) .. table.concat(ret) .. "</table>"
end
return '' -- return empty string if there are no results.
end
return p