From Dark and Darker Wiki

No edit summary
(Added section header)
 
(12 intermediate revisions by the same user not shown)
Line 24: Line 24:




local modes = {
local MODES = {
["Default"] = "0",
["Default"] = "0",
["PvE"] = "10",
["PvE"] = "10",
Line 32: Line 32:
["???"] = "50"
["???"] = "50"
}
}
local maps = {
local MAPS = {
["Default"] = "0",
["Default"] = "0",
["Goblin Caves"] = "01",
["Goblin Caves"] = "01",
["Firedeep"] = "02",
["Firedeep"] = "02",
["Ice Cavern"] = "11",
["Ice Cavern"] = "11",
["Ice Abyss"] = "12",
["Ice Abyss"] = "12",
["Ruins"] = "21",
["Ruins"] = "21",
["Crypts"] = "22",
["Crypts"] = "22",
["Inferno"] = "23",
["Inferno"] = "23",
["Ship Graveyard"] = "31"
["Ship Graveyard"] ="31"
}
}
-- Convert one two strings to a numeric code. Note that the returned strings are concatenated, so the order matters.
-- Convert one two strings to a numeric code. Note that the returned strings are concatenated, so the order matters.
Line 54: Line 54:
local function interpret_dungeon_strings(a,b)
local function interpret_dungeon_strings(a,b)
-- Convert the input strings to their corresponding numeric codes, if they exist
-- Convert the input strings to their corresponding numeric codes, if they exist
a = modes[tostring(a)] or maps[tostring(a)]
a = MODES[tostring(a)] or MAPS[tostring(a)]
b = modes[tostring(b)] or maps[tostring(b)]
b = MODES[tostring(b)] or MAPS[tostring(b)]


-- If strings weren't matched, default to empty strings instead of nil
-- If strings weren't matched, default to empty strings instead of nil
Line 70: Line 70:




local dungeon_grades = {
local DUNGEON_GRADES = {
["0"] = {"Default","Default"},
["0"]   = {mode = "Default", map = "Default"},
 
["1001"] = {"PvE","Goblin Caves"},
["1011"] = {"PvE","Ice Cavern"},
["1012"] = {"PvE","Ice Abyss"},
["1021"] = {"PvE","Ruins"},
["1022"] = {"PvE","Crypts"},
["1023"] = {"PvE","Inferno"},


["2001"] = {"LR","Goblin Caves"},
["1001"] = {mode = "PvE", map = "Goblin Caves"},
["2011"] = {"LR","Ice Cavern"},
["1002"] = {mode = "PvE", map = "Firedeep"},
["2012"] = {"LR","Ice Abyss"},
["1011"] = {mode = "PvE", map = "Ice Cavern"},
["2021"] = {"LR","Ruins"},
["1012"] = {mode = "PvE", map = "Ice Abyss"},
["2022"] = {"LR","Crypts"},
["1021"] = {mode = "PvE", map = "Ruins"},
["2023"] = {"LR","Inferno"},
["1022"] = {mode = "PvE", map = "Crypts"},
["1023"] = {mode = "PvE", map = "Inferno"},
["1031"] = {mode = "PvE", map = "Ship Graveyard"},


-- ["2101"] = {"LR 125+","Goblin Caves"},
["2001"] = {mode = "LR", map = "Goblin Caves"},
-- ["2111"] = {"LR 125+","Ice Cavern"},
["2002"] = {mode = "LR", map = "Firedeep"},
-- ["2112"] = {"LR 125+","Ice Abyss"},
["2011"] = {mode = "LR", map = "Ice Cavern"},
-- ["2121"] = {"LR 125+","Ruins"},
["2012"] = {mode = "LR", map = "Ice Abyss"},
-- ["2122"] = {"LR 125+","Crypts"},
["2021"] = {mode = "LR", map = "Ruins"},
-- ["2123"] = {"LR 125+","Inferno"},
["2022"] = {mode = "LR", map = "Crypts"},
["2023"] = {mode = "LR", map = "Inferno"},
["2031"] = {mode = "LR", map = "Ship Graveyard"},


["3001"] = {"HR","Goblin Caves"},
["3001"] = {mode = "HR", map = "Goblin Caves"},
["3011"] = {"HR","Ice Cavern"},
["3002"] = {mode = "HR", map = "Firedeep"},
["3012"] = {"HR","Ice Abyss"},
["3011"] = {mode = "HR", map = "Ice Cavern"},
["3021"] = {"HR","Ruins"},
["3012"] = {mode = "HR", map = "Ice Abyss"},
["3022"] = {"HR","Crypts"},
["3021"] = {mode = "HR", map = "Ruins"},
["3023"] = {"HR","Inferno"},
["3022"] = {mode = "HR", map = "Crypts"},
["3023"] = {mode = "HR", map = "Inferno"},
["3031"] = {mode = "HR", map = "Ship Graveyard"},


["4001"] = {"Squire to Riches","Goblin Caves"},
["4001"] = {mode = "StR", map = "Goblin Caves"},
["4011"] = {"Squire to Riches","Ice Cavern"},
["4002"] = {mode = "StR", map = "Firedeep"},
["4012"] = {"Squire to Riches","Ice Abyss"},
["4011"] = {mode = "StR", map = "Ice Cavern"},
["4021"] = {"Squire to Riches","Ruins"},
["4012"] = {mode = "StR", map = "Ice Abyss"},
["4022"] = {"Squire to Riches","Crypts"},
["4021"] = {mode = "StR", map = "Ruins"},
["4023"] = {"Squire to Riches","Inferno"},
["4022"] = {mode = "StR", map = "Crypts"},
["4023"] = {mode = "StR", map = "Inferno"},
["4031"] = {mode = "StR", map = "Ship Graveyard"},


["5001"] = {"???","Goblin Caves"},
-- ["5001"] = {mode = "???", map = "Goblin Caves"},
["5011"] = {"???","Ice Cavern"},
-- ["5002"] = {mode = "???", map = "Firedeep"},
["5012"] = {"???","Ice Abyss"},
-- ["5011"] = {mode = "???", map = "Ice Cavern"},
["5021"] = {"???","Ruins"},
-- ["5012"] = {mode = "???", map = "Ice Abyss"},
["5022"] = {"???","Crypts"},
-- ["5021"] = {mode = "???", map = "Ruins"},
["5023"] = {"???","Inferno"}
-- ["5022"] = {mode = "???", map = "Crypts"},
-- ["5023"] = {mode = "???", map = "Inferno"},
-- ["5031"] = {mode = "???", map = "Ship Graveyardo"}
}
}
-- Convert a numeric dungeon grade to its corresponding mode and map strings
-- Convert a numeric dungeon grade to its corresponding mode and map strings
Line 121: Line 124:
-- - - e.g. (5318008) -> "Default", "Default"; ("test") -> "Default", "Default"
-- - - e.g. (5318008) -> "Default", "Default"; ("test") -> "Default", "Default"
local function interpret_dungeon_grade(dungeon_grade)
local function interpret_dungeon_grade(dungeon_grade)
if dungeon_grade == nil or dungeon_grades[dungeon_grade] == nil then
if dungeon_grade == nil or DUNGEON_GRADES[dungeon_grade] == nil then
return dungeon_grades["0"][1], dungeon_grades["0"][2]
return "Improper dungeon grade code",""
end
end


return dungeon_grades[dungeon_grade][1], dungeon_grades[dungeon_grade][2]
return DUNGEON_GRADES[dungeon_grade].mode, DUNGEON_GRADES[dungeon_grade].map
end
end


Line 143: Line 146:
for _,dungeon_grade in ipairs(ordered_dungeon_grades) do
for _,dungeon_grade in ipairs(ordered_dungeon_grades) do
local mode,map = interpret_dungeon_grade(tostring(dungeon_grade))
local mode,map = interpret_dungeon_grade(tostring(dungeon_grade))
-- if dungeon grade cannot be matched, skip it
if tree[mode] == nil then
if map ~= "" then
tree[mode] = {}
if tree[mode] == nil then
tree[mode].order = {}
tree[mode] = {}
table.insert(tree.order,mode)
tree[mode].order = {}
end
table.insert(tree.order,mode)
if tree[mode][map] == nil then
end
tree[mode][map] = true
if tree[mode][map] == nil then
table.insert(tree[mode].order,map)
tree[mode][map] = true
table.insert(tree[mode].order,map)
end
end
end
end
end
Line 165: Line 170:
-- - - e.g. '\<div class="0-data" style="display:none;">'
-- - - e.g. '\<div class="0-data" style="display:none;">'
-- - - e.g. '\<div class="0-data">
-- - - e.g. '\<div class="0-data">
local function create_div_header(id_prefix, id, is_displayed)
local function create_div_header(id_prefix, id, is_displayed, disambiguation)
local interpretation = interpret_dungeon_strings(id)
local interpretation = interpret_dungeon_strings(id)
if interpretation == ''  then interpretation = id end
if interpretation == ''  then interpretation = id end
local wt = '<div class="'..id_prefix..interpretation
if disambiguation then
wt = wt..disambiguation
end


if is_displayed then
if is_displayed then
return '<div class="'..id_prefix..interpretation..'-data">'
wt = wt..'-data">'
else
else
return '<div class="'..id_prefix..interpretation..'-data" style="display:none;">'
wt = wt..'-data" style="display:none;">'
end
end
return wt
end
end


Line 198: Line 211:
-- - @param tab_id: string - the id of the tab toggle group. Must be unique per tab toggle group.
-- - @param tab_id: string - the id of the tab toggle group. Must be unique per tab toggle group.
-- - - e.g. "0" for the first level of tabs, or 11 for second level in the first group.
-- - - e.g. "0" for the first level of tabs, or 11 for second level in the first group.
local function create_tab_toggles_from_list(tab_list, tab_id)
local function create_tab_toggles_from_list(tab_list, tab_id, is_differentiated)
if tab_list == nil then return '' end
if tab_list == nil then return '' end


Line 206: Line 219:
if interpretation == ''  then interpretation = tab end
if interpretation == ''  then interpretation = tab end


wikitext = wikitext..create_tab_toggle(tab_id, tab_id..interpretation, tab, i==1)
if is_differentiated then
wikitext = wikitext..create_tab_toggle(tab_id, tab_id..interpretation..tostring(i), tab, i==1)
else
wikitext = wikitext..create_tab_toggle(tab_id, tab_id..interpretation, tab, i==1)
end
end
end


Line 224: Line 241:
-- - @return: string - the HTML drop rate table
-- - @return: string - the HTML drop rate table
local function create_drop_rate_wikitext(drop_rate_data, loot_drop_item_counts)
local function create_drop_rate_wikitext(drop_rate_data, loot_drop_item_counts)
local droprate_table = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="width:100%"><caption>Drop rates&nbsp;</caption>'
local droprate_table = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="min-width:30%"><caption>Drop rates&nbsp;</caption>'
..'<tr><th style="width:5%">Luck grade</th><th style="width:5%">Probability</th><th style="width:5%">Probability per item</th><th style="width:5%">Item count</th></tr>'
..'<tr><th>Luck grade</th><th>Probability</th><th>Probability per item</th><th>Item count</th></tr>'


for luckgrade = 1,8 do
for luckgrade = 1,8 do
Line 285: Line 302:
-- - @return: string - the HTML loot table
-- - @return: string - the HTML loot table
local function create_loot_drop_wikitext(items,luck_grades)
local function create_loot_drop_wikitext(items,luck_grades)
local wikitext = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="width:100%">'
local wikitext = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="min-width:30%"><caption>Loot Table&nbsp;</caption>'
..'<caption>Loot Table&nbsp;</caption>'
..'<tr><th>Name</th><th>Luck Grade</th><th>Rarity</th><th>Item Count</th></tr>'
..'<tr><th style="width:5%">Name</th><th style="width:5%">Luck Grade</th><th style="width:5%">Rarity</th><th style="width:5%">Item Count</th></tr>'


for item_name, item_data in pairs(items) do
for item_name, item_data in pairs(items) do
Line 343: Line 359:


-- Create the wikitext containing loot tables specific to a monster/prop/loose loot
-- Create the wikitext containing loot tables specific to a monster/prop/loose loot
-- - @param LDG: table
local function create_wikitext(LDG,dungeon_tree,grade)
-- - @return: string - the wikitext containing tab toggles and loot tables
local wikitext = create_tab_toggles_from_list(dungeon_tree.order,grade.."0") -- mode toggles
local function create_wikitext(LDG,dungeon_tree)
local wikitext = create_tab_toggles_from_list(dungeon_tree.order,"0") -- mode toggles
for i,mode in ipairs(dungeon_tree.order) do -- modes - 4 nodes
for i,mode in ipairs(dungeon_tree.order) do -- modes - 4 nodes
wikitext = wikitext
wikitext = wikitext
..create_div_header("0", mode, i == 1)
..create_div_header(grade.."0", mode, i == 1)
..create_tab_toggles_from_list(dungeon_tree[mode].order,"1"..i) -- map toggles
..create_tab_toggles_from_list(dungeon_tree[mode].order,grade.."1"..i) -- map toggles


for j,map in ipairs(dungeon_tree[mode].order) do -- maps - 1 to 6 nodes
for j,map in ipairs(dungeon_tree[mode].order) do -- maps - 1 to 6 nodes
if not LDG.loot_drop_groups.data[0] and not LDG.loot_drop_groups.data[1] then return "No items drop from this source." end
local dungeon_grade = tonumber(interpret_dungeon_strings(mode, map)) -- e.g. "1001" for "PvE", "Goblin Caves"
local dungeon_grade = tonumber(interpret_dungeon_strings(mode, map)) -- e.g. "1001" for "PvE", "Goblin Caves"
if LDG.loot_drop_groups.data[dungeon_grade] == nil then
-- json decoder forces 0 -> 1 for arrays, so we must rely on a manual fix
wikitext = wikitext.."Dungeon grade:"..dungeon_grade.." could not be matched."
if dungeon_grade == 0 and LDG.loot_drop_groups.data[dungeon_grade] == nil then dungeon_grade = 1 end
break
end


wikitext = wikitext
wikitext = wikitext
..create_div_header("1"..i, map, j == 1) -- prefix "1", the tab toggle's depth, to create a unique identifier
..create_div_header(grade.."1"..i, map, j == 1) -- prefix "1", the tab toggle's depth, to create a unique identifier
..create_tab_toggles_from_list(get_loot_table_ids(LDG.loot_drop_groups.data[dungeon_grade]),"2"..i..j) -- loot table toggles
..create_tab_toggles_from_list(get_loot_table_ids(LDG.loot_drop_groups.data[dungeon_grade]),grade.."2"..i..j,true) -- loot table toggles


for k,loot_table in ipairs(LDG.loot_drop_groups.data[dungeon_grade]) do -- 1 to 6 nodes
for k,loot_table in ipairs(LDG.loot_drop_groups.data[dungeon_grade]) do -- 1 to 6 nodes
wikitext = wikitext
wikitext = wikitext
..create_div_header("2"..i..j, clean_loot_table_id(loot_table.drop_rate_id), k == 1)
..create_div_header(grade.."2"..i..j, clean_loot_table_id(loot_table.drop_rate_id), k == 1,k) -- prefix "2", the tab toggle's depth, to create a unique identifier
 
..'<span style="margin:20px 0px 20px 30px; font-size:24px">Rolls: '..loot_table.roll_count..'</span>'
..'<span style="margin:20px 0px 20px 30px; font-size:24px">Rolls: '..loot_table.roll_count..'</span>'
..create_drop_rate_wikitext(
..create_drop_rate_wikitext(
LDG.drop_rates[loot_table.drop_rate_id],
LDG.drop_rates[loot_table.drop_rate_id],
LDG.loot_drops[loot_table.loot_drop_id].items_per_luck_grade)
LDG.loot_drops[loot_table.loot_drop_id].items_per_luck_grade)
..create_loot_drop_wikitext(
..create_loot_drop_wikitext(
LDG.loot_drops[loot_table.loot_drop_id].items_data,
LDG.loot_drops[loot_table.loot_drop_id].items_data,
Line 388: Line 399:




-- Get the localized string and grade from a page name
local GRADE_ORDER = {"Common","Elite","Nightmare"}
local function get_localized_string_and_grade(page_name)
 
-- Get the last word in the page name, which is expected to be the grade
local grade = page_name:reverse():match("[^%s]+"):reverse()
if grade == "Elite" or grade == "Nightmare" then
local localized_string,_ = page_name:gsub(" "..grade,"")
return localized_string, grade
else
return page_name, "Common"
end
end


-- Some monsters don't have all three grades, so we filter them here
local function get_grades(ML,page_name)
local grades = {}


-- #TODO make this function more generic, so it can be used for props and loose loot as well.
for _,grade in ipairs(GRADE_ORDER) do
-- Currently only formatted for Data:Monster.json
if ML.localized_name[page_name][grade] ~= nil then
local function get_object_data(page_name, id_type)
grades[#grades+1] = grade
local data
end
if id_type == "Monster" then
data = mw.loadJsonData("Data:Monster_Loot.json")
elseif id_type == "Prop" then
data = mw.loadJsonData("Data:Prop.json")
elseif id_type == "Loose_loot" then
data = mw.loadJsonData("Data:Loose_loot.json")
else
return nil -- Invalid id_type
end
end


local localized_string, grade = get_localized_string_and_grade(page_name)
return grades
local id = data.monster_localized_names[localized_string][grade]
end


return mw.loadJsonData("Data:"..data.monster_ids[id].loot_drop_group_id..".json"), get_dungeon_tree(data.monster_ids[id].dungeon_grade_order)
end


-- Create a loot table for a specific monster, prop, or loose loot
-- Create a loot table for a specific monster, prop, or loose loot
Line 426: Line 421:
-- - - frame.args[2]: string - type of the object, either "Monster", "Prop", or "Loose_loot"
-- - - frame.args[2]: string - type of the object, either "Monster", "Prop", or "Loose_loot"
function p.loot_tables(frame)
function p.loot_tables(frame)
-- -- frame.args[1] will always be the localized string of either a monster, prop, or loose loot.
local wt = {}
local LDG, dungeon_tree = get_object_data(frame.args[1], frame.args[2])
local ML = mw.loadJsonData("Loot:Monster.json")
if LDG == nil then return "LootDropGroup file could not be found." end
local pagename = frame.args.name
local LDG_id = ""
local LDG = {}
 
wt[#wt+1] = "<h2 style='width:100%'>Loot Tables</h2>"
 
local grades = get_grades(ML,pagename)
wt[#wt+1] = create_tab_toggles_from_list(grades,"Grade")
for i, grade in ipairs(grades) do
local id = ML.localized_name[pagename][grade]
 
if LDG_id ~= "Loot:"..ML.id[id].loot..".json" then -- only load as needed
LDG_id = "Loot:"..ML.id[id].loot..".json"
LDG = mw.loadJsonData(LDG_id)
end
if LDG == nil then return "LootDropGroup file not found." end
 
wt[#wt+1] = create_div_header("Grade", grade, i == 1)
wt[#wt+1] = create_wikitext(LDG,get_dungeon_tree(ML.id[id].dungeon_grade_order),grade)
wt[#wt+1] = "</div>"
end


return create_wikitext(LDG, dungeon_tree)
return table.concat(wt)
end
end


return p
return p
--[=[
mw.log(p.loot_tables({args={name="Mermaid",category="Monster"}}))
mw.log(p.loot_tables({args={name="Ghost King",category="Monster"}}))
]=]

Latest revision as of 08:38, 7 May 2026

Overview

Functions for making loot table. Data comes from Loot:Monster.json, Loot:Prop.json, Loot:Loose Loot.json, and the Loot: namespace.

Functions

loot_tables

Creates a table of drops

  • name - Name of the object
  • category - Monster, Prop, Loose Loot

{{#invoke:Droprate|loot_tables|name=Lich|category=Monster}}

Loot Tables

Common
Elite
Default
PvE
LR
Default
Monsters_Bosses
Key_High
Key_Med
QuestItemDefaultCommon
QuestItemDefaultCommon
EventCurrency
Rolls: 3
Drop rates 
Luck gradeProbabilityProbability per itemItem count
420%0.5%40
580%2%40

Loot Table 
NameLuck GradeRarityItem Count
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1
4Rare1
5Epic1


--[[
Terminology:
- Dungeon: A specific area in the game where players can explore and fight monsters.  Synonymous with "queue".
- Dungeon grade: A numeric code representing a specific dungeon mode and map combination.
	- Mode: The type of dungeon, such as PvE or LR or HR.
	- Map: The specific area within the dungeon, such as Goblin Caves or Ice Cavern.
- Loot table: A combination of loot drop and drop rate data and roll count
	- Loot drop table: A collection of items and the rarity therein that can be rolled for a loot table.
	- Drop rate table: Contains the rates at which certain luck grades are rolled.
		- Luck grade: A property which determines which items are rolled from within the loot drop table.
	- Roll count: The number of times the loot table is rolled.
- Loot drop group: table of all the loot tables for each specific dungeon grade. It also contains loot drop tables and drop rate tables.

For an easier time following the code, the functions each have a docstring.
Use Lua by Sumneko on VSCode to view workspace function signatures and type hints.

#TODO
	Change code to handle props and loose loot as well.
]]--


local p = {}
local utils = require("Module:Utilities")


local MODES = {
	["Default"] = "0",
	["PvE"] = "10",
	["LR"] = "20",
	["HR"] = "30",
	["Squire to Riches"] = "40",
	["???"] = "50"
}
local MAPS = {
	["Default"] =		"0",
	["Goblin Caves"] =	"01",
	["Firedeep"] =		"02",
	["Ice Cavern"] =	"11",
	["Ice Abyss"] =		"12",
	["Ruins"] =			"21",
	["Crypts"] =		"22",
	["Inferno"] =		"23",
	["Ship Graveyard"] ="31"
}
-- Convert one two strings to a numeric code. Note that the returned strings are concatenated, so the order matters.
-- - @param a: string (optional) - associated with either a mode or a map string
-- - @param b: string (optional) - associated with either a mode or a map string
-- - @return: string - number representing the dungeon mode or map or both
-- - - e.g. ("") -> ""
-- - - e.g. ("Gibberish") -> ""
-- - - e.g. ("PvE") -> "10"
-- - - e.g. ("Goblin Caves") -> "01"
-- - - e.g. ("PvE","Goblin Caves") -> "1001"
local function interpret_dungeon_strings(a,b)
	-- Convert the input strings to their corresponding numeric codes, if they exist
	a = MODES[tostring(a)] or MAPS[tostring(a)]
	b = MODES[tostring(b)] or MAPS[tostring(b)]

	-- If strings weren't matched, default to empty strings instead of nil
	if a == nil then a = '' end
	if b == nil then b = '' end

	-- Default cases cannot be paired with other modes or maps, so return "0" if either is "Default"
	if a == "Default" or b == "Default" then
		return "0"
	else
		return a..b
	end
end


local DUNGEON_GRADES = {
	["0"]    = {mode = "Default",	map = "Default"},

	["1001"] = {mode = "PvE",		map = "Goblin Caves"},
	["1002"] = {mode = "PvE",		map = "Firedeep"},
	["1011"] = {mode = "PvE",		map = "Ice Cavern"},
	["1012"] = {mode = "PvE",		map = "Ice Abyss"},
	["1021"] = {mode = "PvE",		map = "Ruins"},
	["1022"] = {mode = "PvE",		map = "Crypts"},
	["1023"] = {mode = "PvE",		map = "Inferno"},
	["1031"] = {mode = "PvE",		map = "Ship Graveyard"},

	["2001"] = {mode = "LR",		map = "Goblin Caves"},
	["2002"] = {mode = "LR",		map = "Firedeep"},
	["2011"] = {mode = "LR",		map = "Ice Cavern"},
	["2012"] = {mode = "LR",		map = "Ice Abyss"},
	["2021"] = {mode = "LR",		map = "Ruins"},
	["2022"] = {mode = "LR",		map = "Crypts"},
	["2023"] = {mode = "LR",		map = "Inferno"},
	["2031"] = {mode = "LR",		map = "Ship Graveyard"},

	["3001"] = {mode = "HR",		map = "Goblin Caves"},
	["3002"] = {mode = "HR",		map = "Firedeep"},
	["3011"] = {mode = "HR",		map = "Ice Cavern"},
	["3012"] = {mode = "HR",		map = "Ice Abyss"},
	["3021"] = {mode = "HR",		map = "Ruins"},
	["3022"] = {mode = "HR",		map = "Crypts"},
	["3023"] = {mode = "HR",		map = "Inferno"},
	["3031"] = {mode = "HR",		map = "Ship Graveyard"},

	["4001"] = {mode = "StR",		map = "Goblin Caves"},
	["4002"] = {mode = "StR",		map = "Firedeep"},
	["4011"] = {mode = "StR",		map = "Ice Cavern"},
	["4012"] = {mode = "StR",		map = "Ice Abyss"},
	["4021"] = {mode = "StR",		map = "Ruins"},
	["4022"] = {mode = "StR",		map = "Crypts"},
	["4023"] = {mode = "StR",		map = "Inferno"},
	["4031"] = {mode = "StR",		map = "Ship Graveyard"},

	-- ["5001"] = {mode = "???",		map = "Goblin Caves"},
	-- ["5002"] = {mode = "???",		map = "Firedeep"},
	-- ["5011"] = {mode = "???",		map = "Ice Cavern"},
	-- ["5012"] = {mode = "???",		map = "Ice Abyss"},
	-- ["5021"] = {mode = "???",		map = "Ruins"},
	-- ["5022"] = {mode = "???",		map = "Crypts"},
	-- ["5023"] = {mode = "???",		map = "Inferno"},
	-- ["5031"] = {mode = "???",		map = "Ship Graveyardo"}
}
-- Convert a numeric dungeon grade to its corresponding mode and map strings
-- - @param grade: string - integer corresponding to a specific mode and map
-- - @return: string, string - pair of strings corresponding to the mode and map
-- - - e.g. (0) -> "Default", "Default"; (4023) -> "HR 225+", "Inferno"
-- - - e.g. (5318008) -> "Default", "Default"; ("test") -> "Default", "Default"
local function interpret_dungeon_grade(dungeon_grade)
	if dungeon_grade == nil or DUNGEON_GRADES[dungeon_grade] == nil then
		return "Improper dungeon grade code",""
	end

	return DUNGEON_GRADES[dungeon_grade].mode, DUNGEON_GRADES[dungeon_grade].map
end

-- Interpret a list of dungeon grades and return the corresponding tree
-- - @param dungeon_grades: table - ideally the dungeon grades are numericall ordered
-- - - Example: {1001, 2001, 4023}
-- - @return: table - a 2-level tree structure with order lists
-- - - Example: {["mode_order"] = {"PvE", "HR 225+""},
--				["PvE"] = {["map_order"] = {"Goblin Caves", "Ice Cavern"}, ["Goblin Caves"] = true, ["Ice Cavern"] = true},
--				["HR 225+"] = {["map_order"] = {"Ice Cavern", "Ice Abyss"}, ["Ice Cavern"] = true, ["Ice Abyss"] = true}}
local function get_dungeon_tree(ordered_dungeon_grades)
	if ordered_dungeon_grades == nil then return {} end

	local tree = {}
	tree.order = {}

	for _,dungeon_grade in ipairs(ordered_dungeon_grades) do
		local mode,map = interpret_dungeon_grade(tostring(dungeon_grade))
		-- if dungeon grade cannot be matched, skip it
		if map ~= "" then
			if tree[mode] == nil then
				tree[mode] = {}
				tree[mode].order = {}
				table.insert(tree.order,mode)
			end
			if tree[mode][map] == nil then
				tree[mode][map] = true
				table.insert(tree[mode].order,map)
			end
		end
	end

	return tree
end


-- Create a div header with a class based on the id and an optional display argument
-- - @param id: string - represents the id of the div
-- - @param is_displayed: boolean - indicates whether the div should be displayed or hidden
-- - @return: string - the HTML div header
-- - - e.g. '\<div class="0-data" style="display:none;">'
-- - - e.g. '\<div class="0-data">
local function create_div_header(id_prefix, id, is_displayed, disambiguation)
	local interpretation = interpret_dungeon_strings(id)
	if interpretation == ''  then interpretation = id end

	local wt = '<div class="'..id_prefix..interpretation

	if disambiguation then
		wt = wt..disambiguation
	end

	if is_displayed then
		wt = wt..'-data">'
	else
		wt = wt..'-data" style="display:none;">'
	end

	return wt
end


-- Create a tab toggle element with a specific id, number, and content
-- - @param data_tabid: string - determines which class to toggle when the toggle is clicked
-- - @param data_tab: string - determines the toggle group, among which only one can be selected at a time
-- - @param content: string - the content of the tab toggle
-- - @param selected_tab: boolean - if true, the tab will be displayed as selected
-- - @return: string - the HTML tab toggle element
-- - - Example: \<div class="selected-tab tab-toggle tab" data-tabid="0" data-tab="PvE">PvE\</div>
local function create_tab_toggle(data_tab_id, data_tab, content, selected_tab)
	if selected_tab then
		return '<div class="selected-tab tab-toggle tab" data-tabid="'..data_tab_id..'" data-tab="'..data_tab..'">'..content..'</div>'
	else
		return '<div class="tab-toggle tab" data-tabid="'..data_tab_id..'" data-tab="'..data_tab..'">'..content..'</div>'
	end
end


-- Create tab toggles from a list of tabs.
-- - @param tab_list: table - list of content to be displayed for tab toggles.
-- - - e.g. {"PvE", "LR 0-124", "LR 125+", "HR 0-224", "HR 225+"}.
-- - @param tab_id: string - the id of the tab toggle group. Must be unique per tab toggle group.
-- - - e.g. "0" for the first level of tabs, or 11 for second level in the first group.
local function create_tab_toggles_from_list(tab_list, tab_id, is_differentiated)
	if tab_list == nil then return '' end

	local wikitext = ''
	for i,tab in ipairs(tab_list) do
		local interpretation = interpret_dungeon_strings(tab)
		if interpretation == ''  then interpretation = tab end

		if is_differentiated then
			wikitext = wikitext..create_tab_toggle(tab_id, tab_id..interpretation..tostring(i), tab, i==1)
		else
			wikitext = wikitext..create_tab_toggle(tab_id, tab_id..interpretation, tab, i==1)
		end
	end

	return wikitext
end


-- Round a number to a specified decimal place value
local function round(number, decimal_places)
	return tonumber(("%."..(decimal_places or 0).."f"):format(number))
end


-- Create a drop rate table from drop rate data and loot drop item counts
-- - @param drop_rate_data: table - contains drop rate data keyed by luck grade
-- - @param loot_drop_item_counts: table - contains item counts keyed by luck grade
-- - @return: string - the HTML drop rate table
local function create_drop_rate_wikitext(drop_rate_data, loot_drop_item_counts)
	local droprate_table = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="min-width:30%"><caption>Drop rates&nbsp;</caption>'
		..'<tr><th>Luck grade</th><th>Probability</th><th>Probability per item</th><th>Item count</th></tr>'

	for luckgrade = 1,8 do
		local probability = drop_rate_data[luckgrade]
		local item_count = loot_drop_item_counts[luckgrade]
		if probability ~= nil and item_count ~= nil then
			droprate_table = droprate_table
				.."<tr class='cr"..luckgrade.."'>"
					.."<td><b>"..luckgrade.."</b></td>"
					.."<td><b>".. 100*probability.."%</b></td>"
					.."<td><b>"..round(100*probability/item_count,4).."%</b></td>"
					.."<td><b>"..item_count.."</b></td>"
				.."</tr>"
		end
	end

	return droprate_table..'</table><br>'
end


-- Clean an ID string by removing the "I[dD]_..._" prefix and any trailing numbers
local function clean_loot_table_id(loot_table_id)
	local cleaned_id = loot_table_id:gsub("I[dD]_%a*_","")
	cleaned_id = cleaned_id:gsub("_%d+$", "")
	return cleaned_id
end


-- Get the loot table ids for a specific dungeon grade
-- - @param loot_tables: table - contains array of severl loot drop, drop rate, roll count pairs.
-- - @return: table - array of loot drop rate ids
local function get_loot_table_ids(loot_tables)
	local drop_rate_ids = {}
	for _, loot_table in ipairs(loot_tables) do
		table.insert(drop_rate_ids, clean_loot_table_id(loot_table.drop_rate_id))
	end
	return drop_rate_ids
end


-- Get the luck grades for a specific drop rate table
-- - @param drop_rate_table: table - contains drop rate data keyed by luck grade
-- - @return: table - contains luck grades keyed by their numeric value
local function get_luck_grades(drop_rate_table)
	local luck_grades = {}
	for i = 1,8 do
		if drop_rate_table[i] then luck_grades[i]=true end
	end
	return luck_grades
end


-- Create a loot table from loot drop and drop rate data
-- - @param items: table - contains item data keyed by item name
-- - - e.g. {["Unobtainium Ore"] = {{luck_grade = 9, rarity = 9, count = 2}, {luck_grade = 9, rarity = 9, count = 10}}, 
--           ["Mithril Ore"] = {{luck_grade = 8, rarity = 8, count = 1}, {luck_grade = 8, rarity = 8, count = 8}}}
-- - @param luck_grades: table - contains luck grades keyed by their numeric value
-- - - e.g. {3=true, 5=true, 7=true, 8=true}
-- - @return: string - the HTML loot table
local function create_loot_drop_wikitext(items,luck_grades)
	local wikitext = '<table cellspacing="0" class="loottable stripedtable sortable jquery-tablesorter mw-collapsible" style="min-width:30%"><caption>Loot Table&nbsp;</caption>'
		..'<tr><th>Name</th><th>Luck Grade</th><th>Rarity</th><th>Item Count</th></tr>'

	for item_name, item_data in pairs(items) do
		local rowspan = 0
		for _,item_record in ipairs(item_data) do
			if luck_grades[item_record.luck_grade] then rowspan = rowspan + 1 end
		end

		-- Track how many rows have been created for this item; this is typically a subset of the entire record array
		local record_row_index = 1 -- this is incremented at the end of the loop's if block
		for _, item_record in ipairs(item_data) do
			if luck_grades[item_record.luck_grade] then
				local luck_grade = item_record.luck_grade
				local rarity_num = item_record.rarity
				local item_count = item_record.count

				local rarity_name = utils.rarity_num_to_name(rarity_num)
				if rarity_name == nil then return "rarity_num of '" .. rarity_num .. "' was converted to a nil rarity_name." end

				local rowspan_td_cell = ""
				-- if first, record's td must span all records rows and have an appropriate iconbox
				if record_row_index == 1 then
					if rowspan > 1 then
						rowspan_td_cell = "<td rowspan='" .. rowspan .. "'>"
					else
						rowspan_td_cell = "<td>" -- no rowspan needed for items with only one record
					end
					-- create the td element containing the iconbox
					rowspan_td_cell = rowspan_td_cell
							.."<div class='iconbox'>"
								.."<div class='rarity"..rarity_num.." rounded relative'>"
									.."[[File:"..item_name..".png|x80px|link="..item_name.."]]"
								.."</div>"
								.."[["..item_name.."|<b class=cr"..rarity_num..">"..item_name.."</b>]]"
							.."</div>"
						.."</td>"
				end

				-- Add the row to the resulting table
				wikitext = wikitext
					.."<tr>"
						..rowspan_td_cell
						.."<td class='cr"..luck_grade.."'><b>"..luck_grade.."</b></td>"
						.."<td class='cr"..rarity_num.."'><b>"..rarity_name.."</b></td>"
						.."<td>"..item_count.."</td>"
					.."</tr>"

				record_row_index = record_row_index + 1
			end
		end
	end

	return wikitext..'</table><br>'
end

-- Create the wikitext containing loot tables specific to a monster/prop/loose loot
local function create_wikitext(LDG,dungeon_tree,grade)
	local wikitext = create_tab_toggles_from_list(dungeon_tree.order,grade.."0") -- mode toggles
	for i,mode in ipairs(dungeon_tree.order) do -- modes - 4 nodes
		wikitext = wikitext
			..create_div_header(grade.."0", mode, i == 1)
			..create_tab_toggles_from_list(dungeon_tree[mode].order,grade.."1"..i) -- map toggles

		for j,map in ipairs(dungeon_tree[mode].order) do -- maps - 1 to 6 nodes
			if not LDG.loot_drop_groups.data[0] and not LDG.loot_drop_groups.data[1] then return "No items drop from this source." end

			local dungeon_grade = tonumber(interpret_dungeon_strings(mode, map)) -- e.g. "1001" for "PvE", "Goblin Caves"
			-- json decoder forces 0 -> 1 for arrays, so we must rely on a manual fix
			if dungeon_grade == 0 and LDG.loot_drop_groups.data[dungeon_grade] == nil then dungeon_grade = 1 end

			wikitext = wikitext
				..create_div_header(grade.."1"..i, map, j == 1) -- prefix "1", the tab toggle's depth, to create a unique identifier
				..create_tab_toggles_from_list(get_loot_table_ids(LDG.loot_drop_groups.data[dungeon_grade]),grade.."2"..i..j,true) -- loot table toggles

			for k,loot_table in ipairs(LDG.loot_drop_groups.data[dungeon_grade]) do -- 1 to 6 nodes
				wikitext = wikitext
					..create_div_header(grade.."2"..i..j, clean_loot_table_id(loot_table.drop_rate_id), k == 1,k) -- prefix "2", the tab toggle's depth, to create a unique identifier
						..'<span style="margin:20px 0px 20px 30px; font-size:24px">Rolls: '..loot_table.roll_count..'</span>'
						..create_drop_rate_wikitext(
							LDG.drop_rates[loot_table.drop_rate_id],
							LDG.loot_drops[loot_table.loot_drop_id].items_per_luck_grade)
						..create_loot_drop_wikitext(
							LDG.loot_drops[loot_table.loot_drop_id].items_data,
							get_luck_grades(LDG.drop_rates[loot_table.drop_rate_id]))

					..'</div>' -- close the content div
			end
			wikitext = wikitext..'</div>' -- close the map div
		end
		wikitext = wikitext..'</div>' -- close the mode div
	end

	return wikitext
end


local GRADE_ORDER = {"Common","Elite","Nightmare"}


-- Some monsters don't have all three grades, so we filter them here
local function get_grades(ML,page_name)
	local grades = {}

	for _,grade in ipairs(GRADE_ORDER) do
		if ML.localized_name[page_name][grade] ~= nil then
			grades[#grades+1] = grade
		end
	end

	return grades
end


-- Create a loot table for a specific monster, prop, or loose loot
-- - @param frame: table
-- - - frame.args[1]: string - {{PAGENAME}} or localized string + grade
-- - - frame.args[2]: string - type of the object, either "Monster", "Prop", or "Loose_loot"
function p.loot_tables(frame)
	local wt = {}
	local ML = mw.loadJsonData("Loot:Monster.json")
	local pagename = frame.args.name
	local LDG_id = ""
	local LDG = {}

	wt[#wt+1] = "<h2 style='width:100%'>Loot Tables</h2>"

	local grades = get_grades(ML,pagename)
	wt[#wt+1] = create_tab_toggles_from_list(grades,"Grade")
	for i, grade in ipairs(grades) do
		local id = ML.localized_name[pagename][grade]

		if LDG_id ~= "Loot:"..ML.id[id].loot..".json" then -- only load as needed
			LDG_id = "Loot:"..ML.id[id].loot..".json"
			LDG = mw.loadJsonData(LDG_id)
		end
		if LDG == nil then return "LootDropGroup file not found." end

		wt[#wt+1] = create_div_header("Grade", grade, i == 1)
		wt[#wt+1] = create_wikitext(LDG,get_dungeon_tree(ML.id[id].dungeon_grade_order),grade)
		wt[#wt+1] = "</div>"
	end

	return table.concat(wt)
end

return p



--[=[
mw.log(p.loot_tables({args={name="Mermaid",category="Monster"}}))
mw.log(p.loot_tables({args={name="Ghost King",category="Monster"}}))
]=]