Difference between revisions of "Module:EnchantmentList"
Jump to navigation
Jump to search
(Create an enchantment scroll list by slot or skill) |
(Fix table.sort callback function – Scroll of Crafting produced `nil` value) |
||
| (5 intermediate revisions by 3 users not shown) | |||
| Line 12: | Line 12: | ||
local module_name = data_module_names[data_type] | local module_name = data_module_names[data_type] | ||
if loaded_data_modules[module_name] == nil then | if loaded_data_modules[module_name] == nil then | ||
| − | loaded_data_modules[module_name] = | + | local status, module = pcall(mw.loadData, module_name) |
| + | if not status then | ||
| + | status, module = pcall(require, module_name) | ||
| + | end | ||
| + | if status then | ||
| + | loaded_data_modules[module_name] = module | ||
| + | else | ||
| + | error("Failed to load module: " .. module_name .. "\n" .. module) | ||
| + | end | ||
end | end | ||
| Line 164: | Line 172: | ||
s = s .. "</th>" | s = s .. "</th>" | ||
s = s .. "</tr>" | s = s .. "</tr>" | ||
| − | + | local items = p.loadData('item') | |
| + | local scrollIds = {} | ||
| + | for key, item in pairs(items) do | ||
if item.class == "enchanted-scroll" then | if item.class == "enchanted-scroll" then | ||
if slot and item.categories then | if slot and item.categories then | ||
if contains(item.categories, slot) then | if contains(item.categories, slot) then | ||
if (item.name ~= "Scroll of the Soul Wisdom") then | if (item.name ~= "Scroll of the Soul Wisdom") then | ||
| − | s = s .. createRow(item) | + | table.insert(scrollIds, {key = key, level = item.level}) |
| + | -- s = s .. createRow(item) | ||
end | end | ||
end | end | ||
elseif skill and item.tags then | elseif skill and item.tags then | ||
if contains(item.tags, skill) then | if contains(item.tags, skill) then | ||
| − | s = s .. createRow(item) | + | if (item.name ~= "Scroll of the Soul Wisdom") then |
| + | table.insert(scrollIds, {key = key, level = item.level}) | ||
| + | -- s = s .. createRow(item) | ||
| + | end | ||
end | end | ||
end | end | ||
end | end | ||
| + | end | ||
| + | table.sort(scrollIds, function(a, b) | ||
| + | local alvl, blvl = a.level, b.level | ||
| + | if alvl == nil then return true end | ||
| + | if blvl == nil then return false end | ||
| + | return alvl < blvl | ||
| + | end) | ||
| + | for _, scroll in ipairs(scrollIds) do | ||
| + | s = s .. createRow(items[scroll.key]) | ||
end | end | ||
s = s .. "</table>" | s = s .. "</table>" | ||
| Line 189: | Line 212: | ||
function p._list(args) | function p._list(args) | ||
| − | + | local slot | |
| + | local skill | ||
| + | if args.slot then | ||
| + | slot = args.slot:lower() | ||
| + | elseif args.skill then | ||
| + | skill = args.skill:lower() | ||
| + | else | ||
| + | slot = args[1]:lower() | ||
| + | end | ||
| + | return createTable(slot, skill) | ||
end | end | ||
return p | return p | ||
Latest revision as of 03:19, 24 September 2025
local p = {}
local data_module_names = {
item = 'Module:Items/data',
enchantment = 'Module:Enchantment/data',
craftaug = 'Module:CraftingAugmenting/data',
}
local loaded_data_modules = {}
function p.loadData(data_type)
local module_name = data_module_names[data_type]
if loaded_data_modules[module_name] == nil then
local status, module = pcall(mw.loadData, module_name)
if not status then
status, module = pcall(require, module_name)
end
if status then
loaded_data_modules[module_name] = module
else
error("Failed to load module: " .. module_name .. "\n" .. module)
end
end
return loaded_data_modules[module_name]
end
local function getItem(id)
return p.loadData("item")[tostring(id)]
end
local function getEnchantment(id)
return p.loadData("enchantment")[tostring(id)]
end
local function getCraftAug(id)
return p.loadData("craftaug")[tostring(id)]
end
local function fullUrl(url)
local newUrl = url
if url:sub(1,5) == "https" then
return newUrl
end
if url:sub(1,1) ~= "/" then
newUrl = "/" .. newUrl
end
newUrl = "https://www.play.idlescape.com" .. newUrl
return newUrl
end
local function icon(name, url, word)
local s = fullUrl(url)
s = "[[" .. name .. "|<img src=\"" .. s
s = s .. "\" alt=\"" .. name .. "\" width=\"20\">"
if word then
s = s .. name
end
s = s .. "]]"
return s
end
local function addSeparator(num)
return tostring(tonumber(num)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end
local function contains (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
local function createRow(item)
local s = ""
s = s .. "<tr>"
s = s .. "<td>"
-- Image and Name
s = s .. icon(item.name, item.itemImage, 1)
s = s .. "</td>"
s = s .. "<td>"
-- Slot
if item.categories then
for index, val in pairs(item.categories) do
s = s .. val .. ", "
end
s = s:sub(1, -3)
end
s = s .. "</td>"
s = s .. "<td>"
-- Level
if item.level then
s = s .. item.level
end
s = s .. "</td>"
s = s .. "<td>"
-- Silver Amount
local craft = getCraftAug(item.id)
if craft and craft.scrollcrafting and craft.scrollcrafting["111"] then
s = s .. craft.scrollcrafting["111"]
end
s = s .. "</td>"
s = s .. "<td>"
-- Rune Amount
if craft and craft.scrollcrafting then
local skip = false
for index, val in pairs(craft.scrollcrafting) do
if skip == false and index >= "510" and index <= "519" then
s = s .. val
skip = true
end
end
end
s = s .. "</td>"
s = s .. "<td>"
-- Rune Type
if craft and craft.scrollcrafting then
local remove = false
for index, val in pairs(craft.scrollcrafting) do
if index >= "510" and index <= "519" then
local ing = getItem(index)
s = s .. ing.name
s = s:sub(1, -6) .. ", "
remove = true
end
end
if remove then
s = s:sub(1, -3)
end
end
s = s .. "</td>"
s = s .. "<td>"
if item.enchantmentID then
local enchant = getEnchantment(item.enchantmentID)
s = s .. enchant.desc
end
s = s .. "</td>"
s = s .. "</tr>"
return s
end
local function createTable(slot, skill)
local s = ""
s = s .. "<table class=\"wikitable sortable hover-highlight jqery-tablesorter mw-collapsible -mw-made-collapsible\">"
s = s .. "<tr>"
s = s .. "<th class=\"headerSort\">"
s = s .. "<img src=\"https://play.idlescape.com/images/enchanting/enchanted_scroll.png\" alt=\"Scrollcrafting\" width=\"40px\" height=\"40px\">"
s = s .. "Scroll"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Slot"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Level"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Silver Amount"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Rune Amount"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Runes"
s = s .. "</th>"
s = s .. "<th class=\"headerSort\">"
s = s .. "Effect per scroll"
s = s .. "</th>"
s = s .. "</tr>"
local items = p.loadData('item')
local scrollIds = {}
for key, item in pairs(items) do
if item.class == "enchanted-scroll" then
if slot and item.categories then
if contains(item.categories, slot) then
if (item.name ~= "Scroll of the Soul Wisdom") then
table.insert(scrollIds, {key = key, level = item.level})
-- s = s .. createRow(item)
end
end
elseif skill and item.tags then
if contains(item.tags, skill) then
if (item.name ~= "Scroll of the Soul Wisdom") then
table.insert(scrollIds, {key = key, level = item.level})
-- s = s .. createRow(item)
end
end
end
end
end
table.sort(scrollIds, function(a, b)
local alvl, blvl = a.level, b.level
if alvl == nil then return true end
if blvl == nil then return false end
return alvl < blvl
end)
for _, scroll in ipairs(scrollIds) do
s = s .. createRow(items[scroll.key])
end
s = s .. "</table>"
return s
end
function p.list(frame)
local args = frame:getParent().args
return p._list(args)
end
function p._list(args)
local slot
local skill
if args.slot then
slot = args.slot:lower()
elseif args.skill then
skill = args.skill:lower()
else
slot = args[1]:lower()
end
return createTable(slot, skill)
end
return p