Module:ItemData/nav: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m process to a list then sort it alphabetically before returning the string
Sur (talk | contribs)
m sort by souls first, then item name; seems it wasnt necessary since nothing seems to be different, call it future proof then
Line 17: Line 17:
--Retrieve all items that fit the bounds
--Retrieve all items that fit the bounds
local items = {}
local items = {}
--1st layer: souls
--2nd layer: list of items with that soul count
for item_key, item_data in pairs(items_data) do
for item_key, item_data in pairs(items_data) do
local card
if item_data['Disabled'] == nil and item_data['IsDisabled'] ~= nil then  
if item_data['Disabled'] == nil and item_data['IsDisabled'] ~= nil then  
return "REMINDER: 'Disabled' was renamed to 'IsDisabled'"
return "REMINDER: 'Disabled' was renamed to 'IsDisabled'"
end
end
local cost = tonumber(item_data["Cost"])
local cost = tonumber(item_data["Cost"])
local slot_ = item_data["Slot"]
local slot_ = item_data["Slot"]
if item_data["Name"] ~= nil and item_data["Disabled"] == false and cost ~= nil and slot ~= nil then
if item_data["Name"] ~= nil and item_data["Disabled"] == false and cost ~= nil and slot ~= nil then
if slot == slot_ and cost>=min_souls and cost<max_souls then
if slot == slot_ and cost>=min_souls and cost<max_souls then
table.insert(items, lang_module.get_string(item_key))
if items[cost] == nil then
items[cost] = {} --list, ensure it exists before inserting to it
end
table.insert(items[cost], lang_module.get_string(item_key))
end
end
end
end
end
end
--Order list alphabetically
--Order each list of items for that cost by item name alphabetically
table.sort(items) --O(nlogn)
for cost, items_within_souls in pairs(items) do
table.sort(items[cost])
end
--Order the lists by souls
table.sort(items)
--2 level hash is now ordered first by cost, second by name alphabetically
--Add each item to output
--Add each item to output
local ret = ''
local ret = ''
for index, item_name in ipairs(items) do
for cost, items_within_souls in pairs(items) do
ret = ret .. '{{ItemIcon|' .. item_name .. '}} &bull; '
for index, item_name in ipairs(items_within_souls) do
ret = ret .. '{{ItemIcon|' .. item_name .. '}} &bull; '
end
end
end

Revision as of 03:11, 16 October 2024

Overview

Functions for creating navigation boxes/lists for items, grouped by a slot/category and souls

Functions

get_item_nav_bulletpoints

Gets a list of items that are each sent to the Template:ItemIcon template, separated by bullet points.

Filters down to a slot/specific category, and within a range of souls.

Parameters

  • slot - Slot/category that the items should be, should be Weapon, Armor, or Tech
  • min_souls - Minimum souls that the items should have
  • max_souls - Maximum souls that the items should have
  • debug_mode - (OPTIONAL) - if set to 'true', the wikitext is unprocessed, allowing for it to be read more clearly. Also used for showcasing the documentation examples more clearly.

Examples

With debug_mode on (for illustration purposes) {{#invoke:ItemData/nav|get_item_nav_bulletpoints|Armor|1250|3000|debug_mode=true}}

Script error: The function "get_item_nav_bulletpoints" does not exist.


With debug_mode off {{#invoke:ItemData/nav|get_item_nav_bulletpoints|Armor|1250|3000}}

Script error: The function "get_item_nav_bulletpoints" does not exist.

get_item_nav_cards

Gets a list of items that are each sent to the Template:ItemBox template, separated by space.

Filters down to a specific slot/category, and within a range of souls.

Parameters

Same as get_item_nav_bulletpoints

Examples

With debug_mode on (for illustration purposes) {{#invoke:ItemData/nav|get_item_nav_bulletpoints|Armor|1250|3000|debug_mode=true}}

Bullet Armor Bullet Lifesteal Combat Barrier Debuff Reducer Divine Barrier Enchanter's Barrier Enduring Speed Healbane Healing Booster Healing Nova Reactive Barrier Restorative Locket Return Fire Spirit Armor Spirit Lifesteal


With debug_mode off {{#invoke:ItemData/nav|get_item_nav_bulletpoints|Armor|1250|3000}}

Bullet Armor Bullet Lifesteal Combat Barrier Debuff Reducer Divine Barrier Enchanter's Barrier Enduring Speed Healbane Healing Booster Healing Nova Reactive Barrier Restorative Locket Return Fire Spirit Armor Spirit Lifesteal

write_item_slot_subgroup

Writes a sub group for the navbox on Template:Item Navbox and Template:Infobox ShopItems. The sub group contains all items in each price range from "ItemPricePerTier" in Data:GenericData.json

Parameters

  • slot - Slot/category to create the subgroup for, ie Weapon, Armor, or Tech
  • type - The subfunction to call that determines the list formatting style. Options are 'get_item_nav_bulletpoints' or 'get_item_nav_cards'
  • debug_mode - (OPTIONAL) - if set to 'true', the wikitext is unprocessed, allowing for it to be read more clearly. Also used for showcasing the documentation examples more clearly.

Examples

With debug_mode on (for illustration purposes) {{#invoke:ItemData/nav|write_item_slot_subgroup|Weapon|get_item_nav_bulletpoints|debug_mode=true}}

Script error: The function "write_item_slot_subgroup" does not exist.


With debug_mode off {{#invoke:ItemData/nav|write_item_slot_subgroup|Weapon|get_item_nav_bulletpoints}}

Script error: The function "write_item_slot_subgroup" does not exist.

Recall that it creates subgroup parameters for the mentioned templates.


local p = {}
local items_data = mw.loadJsonData("Data:ItemData.json")
local lang_module = require('Module:Lang')

-- for [[Template:Item Navbox]], i.e. {{ItemIcon|Basic Magazine}} &bull; {{ItemIcon|Close Quarters}}
function p.get_item_nav_cards(frame)
	local slot = frame.args[1]
	local min_souls = frame.args[2]
	local max_souls = frame.args[3]
	if slot ~= 'Weapon' and slot ~= 'Armor' and slot ~= 'Tech' then
		return 'slot must be Weapon, Armor (Vitality), or Tech (Spirit)'
	end
	local min_souls = tonumber(min_souls)
	local max_souls = tonumber(max_souls)
	if min_souls == nil or max_souls == nil then return 'Min/Max souls must be numerical' end
	
	--Retrieve all items that fit the bounds
	local items = {}
	--1st layer: souls
	--2nd layer: list of items with that soul count
	for item_key, item_data in pairs(items_data) do
		if item_data['Disabled'] == nil and item_data['IsDisabled'] ~= nil then 
			return "REMINDER: 'Disabled' was renamed to 'IsDisabled'"
		end
		
		local cost = tonumber(item_data["Cost"])
		local slot_ = item_data["Slot"]
		
		if item_data["Name"] ~= nil and item_data["Disabled"] == false and cost ~= nil and slot ~= nil then
			if slot == slot_ and cost>=min_souls and cost<max_souls then
				if items[cost] == nil then
					items[cost] = {} --list, ensure it exists before inserting to it
				end
				table.insert(items[cost], lang_module.get_string(item_key))
			end
		end
	end
	
	--Order each list of items for that cost by item name alphabetically
	for cost, items_within_souls in pairs(items) do
		table.sort(items[cost])
	end
	
	--Order the lists by souls
	table.sort(items)
	
	--2 level hash is now ordered first by cost, second by name alphabetically

	--Add each item to output
	local ret = ''
	for cost, items_within_souls in pairs(items) do
		for index, item_name in ipairs(items_within_souls) do
			ret = ret .. '{{ItemIcon|' .. item_name .. '}} &bull; '
		end
	end
	
	--Remove the last bullet point
	ret = string.sub(ret, 1, -(string.len(' &bull; '))-1)
	
	return frame:preprocess(ret)
end

return p