Module:EnchantedChest

--[=[ Lua script replacement for wikitext template



Parameters: 1: Enchanted Chest name 2: Size in pixels optional, defaults to 25 If equal to "N", includes the chest Name Link also If equal to "L", includes the chest Name Link only 3: Enchanted Chest link if diffrent from name -- Unknown is returned if Enchanted Chest is not in data.

--]=]

local p = {}

local chestData = mw.loadData( 'Module:EnchantedChest/data' ) local cData = mw.loadData( 'Module:ItemCollection/data' )

local EC = require("Module:EC").getEC local RARITY = require("Module:Rarity").rarity local TOK = require("Module:CharacterToken").getToken local COLL = require("Module:ItemCollection").getCollection local util = require('Module:Utility')

local function getCollection(item) local collName = "" for _, data in pairs(cData.Collections) do		if (data[3] and data[3] == item) or item == data[1] then collName = data[1] end end if collName == "" then collName = COLL({item, "N"}) end return collName end

function p.getChest(frame) local tArgs = util.getArgs(frame)

local allChest = tArgs[1] or "" local size = tArgs[2] local plural = tArgs[3] local outType = ""

if not tonumber(size) then outType = size if outType == "P" then size = 35 else size = 25 end end

local tAll = {} local space = ""

for chest in mw.text.gsplit(allChest, "%s*[!%$]%s*") do		local tChest = {space, "", "", "", "", "",	   				"", "", "", "", ""	    				}		local cName, version, count = mw.ustring.match(chest, "^([^,]-)%s*-*%s*(%w+)%s*,%s*(.-)$") if not cName then cName, version = mw.ustring.match(chest, "^([^,]-)%s*-%s*(.-)$") if not cName then cName = chest end end if not cName then cName = chest end cName = mw.ustring.gsub(cName, " Chests", "") if outType == "S" then version = mw.title.getCurrentTitle.text end local cFile = "work_in_progress" local cLink = "Work In Progress" local cDisp = cName .. " Chest" if chestData[cName] then tChest[ 7] = "ec-" cFile = mw.ustring.lower(mw.ustring.gsub(cName, " ", "_")) cLink = cName .. " Chests" cDisp = cName .. " Chest" if plural or (count and count > "1") then cDisp = cDisp .. "s" end if cName == "Legendary" and version then if outType ~= "S" then cDisp = getCollection(version) .. " " .. cDisp if outType ~= "L" then local bSize = math.floor(size / 3.3) local lSize = math.floor(size / 2.6) local cSize = math.floor(size / 1.6) tChest[ 6] = "" .. COLL({getCollection(version), cSize}) .. " [[File:"					end   			end    			cLink = cLink .. "#" .. getCollection(version)    		elseif (cName == "Sapphire" and version == "Halloween") or (cName == "Red Ribbon" and version == "Christmas") then    				cLink = version .. " Chests"   					cDisp = cDisp .. " (" .. cLink .. ")"    		end	    end		if outType == "N" then			tChest[ 2] = "[["			tChest[ 3] = cLink			tChest[ 4] = "|" .. cDisp			tChest[ 5] = "]] ("			tChest[16] = ")"		elseif outType == "L" then			tChest[ 6] = ""			tChest[10] = cDisp			tChest[11] = ""			tChest[13] = ""			tChest[15] = ""		elseif outType == "P" then			tChest[16] = " "			tChest[19] = cDisp			tChest[20] = " "		elseif outType == "S" then			tChest[ 6] = "FREE ("			tChest[10] = cDisp			tChest[11] = ") "			tChest[13] = ""			tChest[15] = ""			space = " "		end		if outType ~= "L" and outType ~= "S" then			tChest[ 8] = cFile			tChest[12] = cDisp			tChest[14] = cLink			space = " "			if count and count > "1" then 				if outType == "N" then					tChest[ 5] = "]]" .. count .. " ("				else					tChest[15] = "]]" .. count 				end			end		end   	table.insert(tAll, table.concat(tChest))	end    return table.concat(tAll) end

local function chItems(inStr) local tIn = mw.text.split(inStr, "%s*%$%s*") local tOut = {} tOut.count = tIn[1] for idx = 1, #tIn do		local tt = mw.text.split(tIn[idx], "%s*#%s*") if not tt[2] or tt[2] == "" then tt[2] = 1 end table.insert(tOut, tt) end return tOut end

local function oneItem(item, count, withName, rarity) nCount = string.gsub(count or "", "[,]", "") or 0 if nCount == 0 then return "" end

local conv = { ["ch"] = "cp", ["cos"] = "cp", }

local tItem = {"", " " .. nCount, (withName and " " or ""), "", "",			(withName and " " or ""), }

local iName = item local iData = cData.Items[iName]

if iData and iData[2] then local fname = iData.I or iName:lower:gsub("[':%,%.]", ""):gsub(" ", "_") if iData[2] == "pf" then fname = fname:gsub("_float$", "") else fname = fname:gsub("^([^%(]-)_*%(([^%d]+%d*)%)$", "%1-%2")		end

tItem[1] = table.concat({								""							}) tItem[2] = "" if withName then tItem[4] = table.concat({"", iName, ""}) end elseif item:match("^EC") then tItem[1] = EC({item, nil, nil, nil, 30}) if withName then tItem[4] = EC({item, "L", S=(nCount == 1)}) end elseif item:match(" Decorations") then tItem[1] = "" tItem[2] = "" if withName then tItem[4] = "Some " .. rarity .. " Decorations" end elseif item:match(" Concessions") then tItem[1] = "" tItem[2] = "" if withName then tItem[4] = "Some " .. rarity .. " Concessions" end else tItem[1] = TOK({item, 40}) if withName then tItem[4] = TOK({item, "L"}) if nCount ~= 1 then tItem[5] = "s" end end end

return table.concat(tItem) end

function p.getTable(frame) local tArgs = util.getArgs(frame)

local cName = tArgs[1] or mw.title.getCurrentTitle.baseText cName = mw.ustring.gsub(cName, " Chests", "") local conv = { ["Common"] = "1", ["Uncommon"] = "2", ["Rare"] = "3", ["Epic"] = "4", ["Legendary"] = "5", }	local tOut = {} local cSection = {} if chestData[cName] then for k, v in pairs(chestData[cName]) do			if v["R"] then table.insert(cSection, {(v[1] or 999), k, v}) end end table.sort(cSection, function(a,b) return a[1] > b[1] end) for _, cItem in pairs(cSection) do			local ecData = cItem[3] table.insert(tOut, '|-|') table.insert(tOut, cItem[2]) table.insert(tOut, '=\n') if ecData["Duration"] or ecData["Cost"] then table.insert(tOut, ' About  ') if ecData["Duration"] then table.insert(tOut, ' ') end if ecData["Cost"] then table.insert(tOut, ' ') end table.insert(tOut, ' \n') end table.insert(tOut, '{| class="article-table dmk-calendar" style="width: ')			if ecData["Duration"] or ecData["Cost"] then table.insert(tOut, '55') else table.insert(tOut, '100') end	  		table.insert(tOut, '%;"\n!') table.insert(tOut, cItem[2]) table.insert(tOut, ' ') table.insert(tOut, cName) table.insert(tOut, ' Chests Rewards') local tItem = {} for k, v in pairs(ecData["R"]) do   			table.insert(tItem, {conv[k], k, v}) end table.sort(tItem, function(a,b) return a[1] < b[1] end) for _, item in pairs(tItem) do table.insert(tOut, '\n|-\n! colspan="5"|') table.insert(tOut, RARITY({item[2], "ECR"})) if item[2] == "Common" then table.insert(tOut, '\n|} \n{| class="article-table dmk-calendar" style="width: 100%;"\n') else table.insert(tOut, '\n|-\n') end item[3] = chItems(item[3]) for rNo, rData in ipairs(item[3]) do					if rNo == 6 then table.insert(tOut, '\n|-') end table.insert(tOut, '\n| style="width: 20%; border-bottom: none;"|') table.insert(tOut, oneItem(rData[1], rData[2], true, item[2])) end end if ecData["S"] then table.insert(tOut, "\n|-\n! colspan=\"5\"| Only at ''") table.insert(tOut, (ecData["Special"] or "10")) table.insert(tOut, "% Special Chance'': ") local tItem = {} for k, v in pairs(ecData["S"]) do   				table.insert(tItem, {conv[k], k, v}) end table.sort(tItem, function(a,b) return a[1] < b[1] end) for _, item in pairs(tItem) do table.insert(tOut, '\n|-\n! colspan="5"|') table.insert(tOut, RARITY({item[2], "ECR"})) table.insert(tOut, '\n|-') item[3] = chItems(item[3]) for rNo, rData in ipairs(item[3]) do						if rNo == 6 then table.insert(tOut, '\n|-') end table.insert(tOut, '\n| style="width: 20%; border-bottom: none;"|') table.insert(tOut, oneItem(rData[1], rData[2], true)) end end end table.insert(tOut, '\n|}\n') end end return table.concat(tOut) end

return p