From Dark and Darker Wiki

Documentation for this module may be created at Module:GenerateClassGearTables/doc

local p = {}
local WD = mw.loadJsonData("Data:Weapon.json")
local AD = mw.loadJsonData("Data:Armor.json")
local concat = table.concat

-- Check if item is for the given class, items that don't specify a class are for all classes so by default we have "found" the class until we see otherwise
local function find_class(class, classes)
	local found_class = true
	if classes then
		for _, c in ipairs(classes) do
			found_class = false
			if string.match(c, class) then
				found_class = true
				break
			end
		end
	end
	return found_class
end

local function get_weapon_list_from_label(wt, label, class, table_type)
	local handtype, slottype
	-- Map the row lables into the form we actually see in the item data
	if label == "Main Hand + Two Handed" then
		handtype = "Two Handed"
		slottype = "Main-Hand"
	elseif label == "Main Hand + One Handed" then
		handtype = "One Handed"
		slottype = "Main-Hand"
	elseif label == "Off Hand" then
		handtype = "One Handed"
		slottype = "Off-Hand"
	end
	for _, weapon_name in ipairs(WD.Weapon.order) do
		weapon = WD.Weapon[weapon_name]
		-- This is slightly hacky but the idea here is that if we see a label like "Main Hand + One Handed" we make sure those match the item's fields for those
		-- We then look at the first weapon type so we can check that type list like Arming Sword is a Sword, then we check the Sword's list based on the passed-in table_type
		-- so we look at Sword["Uncraftable"] for the Arming Sword, since it's in there we know it can't also have another type that is Craftable so we add it if the table_type
		-- was Uncraftable, Another example: Bardiche is a Polearm and an Axe but it will not be an Uncraftable Polearm and a Craftable Axe so if we see Bardiche in any Uncraftable list we know it's Uncraftable
		-- and we don't have to check both the Polearm and Axe lists to know this because it will be in both their Uncraftable lists
		-- The same applies for Utilities it's just the first check is different since we can't guarantee the handtype even though it seems like it's always "Two Handed"
		if label == "Utility" and weapon.categories.Utility or weapon.handtype == handtype and weapon.slottype == slottype then
			local first_weapon_type = weapon.types.order[1]
			if WD[first_weapon_type] and WD[first_weapon_type][table_type] then -- if the table_type matches (Uncraftable, Craftable, etc.)
				local type_list = WD[first_weapon_type][table_type]
				local found_type = false
				for _, t in ipairs(type_list) do
					if t == weapon.name then
						found_type = true
						break
					end
				end
				if find_class(class, weapon.classes) and found_type then
					wt[#wt+1] = string.format("[[%s]]", weapon.name)
					wt[#wt+1] = " • "
				end
			end
		end
	end
	if wt[#wt] == " • " then 
		table.remove(wt) -- remove the trailing " • " that is added
	end
end

local function get_armor_list_from_label(wt, label, class, table_type)
	for _, armor_name in ipairs(AD.Armor.order) do
		armor = AD.Armor[armor_name]
		if armor.slottype == label then
			if AD[label] and AD[label][table_type] then -- if the table_type matches (Uncraftable, Craftable, etc.)
				local type_list = AD[label][table_type]
				local found_type = false
				for _, t in ipairs(type_list) do
					if t == armor.name then
						found_type = true
						break
					end
				end
				if find_class(class, armor.classes) and found_type then
					wt[#wt+1] = string.format("[[%s]]", armor.name)
					wt[#wt+1] = " • "
				end
			end
		end
	end
	if wt[#wt] == " • " then 
		table.remove(wt) -- remove the trailing " • " that is added
	end
end

local function build_table(wt, row_labels, column_labels, class, table_type)
	wt[#wt+1] = '<table class="wikitable sortable jquery-tablesorter" cellspacing="0" style="width:60%; color:#eee; background:transparent; text-align:center; vertical-align:middle;">'
	wt[#wt+1] = string.format([[<tr>
<th style="font-weight:bold; background-color: rgb(220,220,220,0.2); width:15%%" class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
<th style="font-weight:bold; background-color: rgb(220,220,220,0.2); width:50%%" class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">%s</th>
</tr>]], column_labels[1], column_labels[2])

	for _, label in ipairs(row_labels) do
		wt[#wt+1] = '<tr style="height:100px">'
		wt[#wt+1] = string.format('<td style="font-weight:bold; background-color: rgb(220,220,220,0.2); text-align:middle">%s</td>', label)
		wt[#wt+1] = "<td>"
		
		-- Leaving this non-dynamic for now since we only care about these two and it's unlikely we want more
		if column_labels[2] == "Weapons" then
			get_weapon_list_from_label(wt, label, class, table_type)
		else
			get_armor_list_from_label(wt, label, class, table_type)
		end
		
		wt[#wt+1] = "</td>"
		wt[#wt+1] = "</tr>"
	end
	wt[#wt+1] = "</table>"
end

function p.gear_table(frame)
	local class = frame.args[1]
	local table_type = frame.args[2]
	
	local weapon_row_labels = {"Main Hand + Two Handed", "Main Hand + One Handed", "Off Hand", "Utility"}
	local weapon_column_labels = {"Hand/Slot Type", "Weapons"} -- Can't add more columns dynamically right now
	
	local armor_row_labels = {"Head", "Chest", "Legs", "Hands", "Foot", "Back"}
	local armor_column_labels = {"Slot Type", "Armors"}
	
	local wt = {}
	build_table(wt, weapon_row_labels, weapon_column_labels, class, table_type)
	wt[#wt+1] = "<br>"
	build_table(wt, armor_row_labels, armor_column_labels, class, table_type)
	return concat(wt)
end

return p

-- mw.log(p.gear_table({args={"Rogue", "Uncraftable"}}))
-- mw.log(p.gear_table({args={"Rogue", "Craftable"}}))
-- mw.log(p.gear_table({args={"Rogue", "Artifact"}}))

-- mw.log(p.gear_table({args={"Wizard", "Uncraftable"}}))