Module:ECR

--[=[ -- LUA script to get Enchanted Chests Rewards -- --]=]

local p = {}

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

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

local function addItem(data) local dataCommon = {} if data["Common"] then table.insert(dataCommon, data["Common"]) end local dataUncommon = {} if data["Uncommon"] then table.insert(dataUncommon, data["Uncommon"]) end local dataRare = {} if data["Rare"] then table.insert(dataRare, data["Rare"]) end local dataEpic = {} if data["Epic"] then table.insert(dataEpic, data["Epic"]) end local dataLegendary = {} if data["Legendary"] then table.insert(dataLegendary, data["Legendary"]) end

local raTok = "" local rbTok = "" local daTok = "" local dbTok = ""

if data["T"] and data["T"][1] then for tCh in mw.text.gsplit(data["T"][1], "%s*$%s*") do raTok = TOK.getToken({(tCh .. ",2"), "R"}) rbTok = TOK.getToken({(tCh .. ",3"), "R"}) daTok = string.upper(string.sub(raTok, 1, 1))..string.sub(raTok, 2) dbTok = string.upper(string.sub(rbTok, 1, 1))..string.sub(rbTok, 2)

if daTok == "Common" then table.insert(dataCommon, (" $ " .. tCh .. ",2 #" .. (data["T"][2] or "1"))) elseif daTok == "Uncommon" then table.insert(dataUncommon, (" $ " .. tCh .. ",2 #" .. (data["T"][3] or "1"))) elseif daTok == "Rare" then table.insert(dataRare, (" $ " .. tCh .. ",2 #" .. (data["T"][4] or "1"))) elseif daTok == "Epic" then table.insert(dataEpic, (" $ " .. tCh .. ",2 #" .. (data["T"][5] or "1"))) elseif daTok == "Legendary" then table.insert(dataLegendary, (" $ " .. tCh .. ",2 #" .. (data["T"][6] or "1"))) end

if dbTok == "Common" then table.insert(dataCommon, (" $ " .. tCh .. ",3 #" .. (data["T"][2] or "1"))) elseif dbTok == "Uncommon" then table.insert(dataUncommon, (" $ " .. tCh .. ",3 #" .. (data["T"][3] or "1"))) elseif dbTok == "Rare" then table.insert(dataRare, (" $ " .. tCh .. ",3 #" .. (data["T"][4] or "1"))) elseif dbTok == "Epic" then table.insert(dataEpic, (" $ " .. tCh .. ",3 #" .. (data["T"][5] or "1"))) elseif dbTok == "Legendary" then table.insert(dataLegendary, (" $ " .. tCh .. ",3 #" .. (data["T"][6] or "1"))) end end end

local tRarity = {} table.insert(tRarity, {"Common", table.concat(dataCommon)}) table.insert(tRarity, {"Uncommon", table.concat(dataUncommon)}) table.insert(tRarity, {"Rare", table.concat(dataRare)}) table.insert(tRarity, {"Epic", table.concat(dataEpic)}) table.insert(tRarity, {"Legendary", table.concat(dataLegendary)})

return tRarity end

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

local function oneItem(item, count, withName, rarity) nCount = count 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.getToken({item, 40}) if withName then tItem[4] = TOK.getToken({item, "L"}) if nCount ~= "1" then tItem[5] = "s" end end end

return table.concat(tItem) end

local function colItem(iNumb, tNumb, aNumb, item) local tOut = {}

if iNumb == 1 then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, '\n| colspan="2"|') elseif iNumb == 2 then if aNumb == (tNumb - 1) then table.insert(tOut, '\n|\n| colspan="2"|') table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == tNumb then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) table.insert(tOut, '\n| colspan="2"|\n|') end elseif iNumb == 3 then if aNumb == (tNumb - 2) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == (tNumb - 1) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == tNumb then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) table.insert(tOut, '\n| colspan="2"|') end elseif iNumb == 4 then if aNumb == (tNumb - 3) then table.insert(tOut, '\n|\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == (tNumb - 2) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == (tNumb - 1) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) elseif aNumb == tNumb then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) table.insert(tOut, '\n|') end else table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) end

return table.concat(tOut) end

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

local cName = tArgs[1] or mw.title.getCurrentTitle.baseText cName = mw.ustring.gsub(cName, " Chests", "") local tOut = {} local cSection = {} if chestData[cName] then for k, v in pairs(chestData[cName]) do			if v["R"] then local cNumb = v[1] if not cNumb then local iData = cData.Items[k] if iData and iData[1] then cNumb = iData[1] else cNumb = 999 end end table.insert(cSection, {cNumb, k, v}) end end

if cName == "Legendary" then table.sort(cSection, function(a,b) return a[1] < b[1] end) else table.sort(cSection, function(a,b) return a[1] > b[1] end) 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["D"] or ecData["C"] then table.insert(tOut, ' About  ') if ecData["D"] then table.insert(tOut, ' ') end if ecData["C"] then table.insert(tOut, ' ') end table.insert(tOut, ' \n') end table.insert(tOut, '{| class="article-table dmk-chests" style="width: ')			if ecData["D"] or ecData["C"] then table.insert(tOut, '55') else table.insert(tOut, '100') end		   	table.insert(tOut, '%;"\n! colspan="10"|') table.insert(tOut, cItem[2]) table.insert(tOut, ' ') table.insert(tOut, cName) table.insert(tOut, ' Chests Rewards') table.insert(tOut, ' ')

local tRarity = addItem(ecData["R"])

for i, item in ipairs(tRarity) do			   if item[2] ~= "" then table.insert(tOut, '\n|-\n! colspan="10"|') table.insert(tOut, RARITY({item[1], "ECR"})) if i == 1 and (ecData["D"] or ecData["C"]) then table.insert(tOut, '\n|} \n{| class="article-table dmk-chests" style="width: 100%;"\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n|-') elseif i == 1 then table.insert(tOut, '\n|-\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n| style="height: 1px;"|\n|-') else table.insert(tOut, '\n|-') end item[2] = chItems(item[2])

local tItems = {} local tNo = "" local rNo = "" for No, rData in ipairs(item[2]) do					table.insert(tItems, oneItem(rData[1], rData[2], true, item[1])) tNo = No					rNo = No				end local iNo = 0 if tNo > 5 then repeat rNo = rNo - 5 iNo = iNo + 1 until rNo < 5 end local br = 6 for i, iName in ipairs(tItems) do					if i == br then table.insert(tOut, '\n|-') br = br + 5 end if i <= (iNo * 5) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, iName) else table.insert(tOut, colItem(rNo, tNo, i, iName)) end end end end if ecData["S"] then table.insert(tOut, "\n|-\n! colspan=\"10\"| Only at ''") table.insert(tOut, (ecData["S"][1] or "10")) table.insert(tOut, "% Special Chance'': ") local tRarity= addItem(ecData["S"])

for _, item in pairs(tRarity) do				   if item[2] ~= "" then table.insert(tOut, '\n|-\n! colspan="10"|') table.insert(tOut, RARITY({item[1], "ECR"})) table.insert(tOut, '\n|-') item[2] = chItems(item[2])

local tItems = {} local tNo = "" local rNo = "" for No, rData in ipairs(item[2]) do						table.insert(tItems, oneItem(rData[1], rData[2], true, item[1])) tNo = No						rNo = No					end local iNo = 0 if tNo > 5 then repeat rNo = rNo - 5 iNo = iNo + 1 until rNo < 5 end for i, item in ipairs(tItems) do						if i == 6 or i == 11 then table.insert(tOut, '\n|-') end if i <= (iNo * 5) then table.insert(tOut, '\n| colspan="2"|') table.insert(tOut, item) else table.insert(tOut, colItem(rNo, tNo, i, item)) end end end end end table.insert(tOut, '\n|}\n') if tArgs[cItem[2]] then table.insert(tOut, tArgs[cItem[2]]) end end end return table.concat(tOut) end

local function getChest(data, cName, pagename, special, old) local tRarity = addItem(data)

for _, item in pairs(tRarity) do		if item[2] ~= "" then item[2] = chItems(item[2]) for _, rData in ipairs(item[2]) do				local iName = rData[1]

if pagename == "Collection" or pagename == "Common" or pagename == "Uncommon" or pagename == "Rare" or pagename == "Epic" or pagename == "Legendary" or pagename == "Relic Common" or pagename == "Relic Uncommon" or pagename == "Relic Rare" then iName = iName else iName = TOK.getToken({iName, "T"}) end

if rData[2] then rData[2] = "x" .. rData[2] end

if pagename == iName then return {cName, rData[2], special, old} end end end end

return pagename .. iName end

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

local tOut = {} local tChest = {} if tArgs[1] then if chestData[tArgs[1]] then table.insert(tChest, {tArgs[1], ("x" .. (tArgs[3] or "1")), (tArgs[2] or nil)}) else local conv = { ["B"] = {"Bronze"}, ["B*"] = {"Bronze", true}, ["S"] = {"Silver"}, ["G"] = {"Gold"}, ["P"] = {"Platinum"}, ["DE"] = {"Decoration"}, ["RE"] = {"Resource"}, ["CO"] = {"Concession"}, ["AE"] = {"Attraction Enchantment"}, ["M"] = {"Magical"}, ["R"] = {"Radiant"}, ["RU"] = {"Ruby"}, ["SA"] = {"Sapphire"}, ["A"] = {"Amber"}, ["C"] = {"Cobalt"}, }			local old = conv[tArgs[1]][2] or nil tArgs[1] = conv[tArgs[1]][1] or tArgs[1] if chestData[tArgs[1]] then table.insert(tChest, {tArgs[1], (tArgs[3] or nil), (tArgs[2] or nil), old}) else return '|-\n| style="text-align: center;"| \n|Unkwown Chest\n|\n|Instant\n| colspan="2"|x?\n' end end else local pagename = tArgs["P"] if not pagename then pagename = mw.title.getCurrentTitle.text end pagename = mw.text.decode(pagename, true)

for cName, chest in pairs(chestData) do			if chestData[cName]["Current"] and chestData[cName]["Current"]["R"] then return getChest(chestData[cName]["Current"]["R"], cName, pagename) end if chestData[cName]["Current"] and chestData[cName]["Current"]["S"] then table.insert(tChest, getChest(chestData[cName]["Current"]["S"], cName, pagename, "S")) end

local pRarity = TOK.tokenType({pagename}) pRarity = string.upper(string.sub(pRarity, 1, 1))..string.sub(pRarity, 2) if pRarity:match("^.+ relic$") then pRarity = "Relic " .. mw.ustring.gsub(pRarity, " relic", "") end

if chestData[cName]["Current"] and chestData[cName]["Current"]["R"] then table.insert(tChest, getChest(chestData[cName]["Current"]["R"], cName, pRarity)) end if chestData[cName]["Current"] and chestData[cName]["Current"]["S"] then table.insert(tChest, getChest(chestData[cName]["Current"]["S"], cName, pRarity, "S")) end end end

for k, v in pairs(tChest) do		local tRet = {'|-\n| style="text-align: center;"| \n|', "",	    			' Chest', "", '\n|\n|', 'Instant', '\n| colspan="2"|', "", '\n' }

tRet[ 2] = mw.ustring.lower(mw.ustring.gsub(v[1], " ", "_")) tRet[ 4] = v[1] tRet[ 6] = v[1] tRet[ 8] = v[1] tRet[10] = v[1]

if v[3] then tRet[12] = " * Special Chance Only " end

if v[1] == "Bronze" and v[4] then tRet[14] = '3h' elseif v[1] == "Silver" then tRet[14] = '6h' elseif v[1] == "Gold" then tRet[14] = '12h' elseif v[1] == "Platinum" then tRet[14] = '24h' end

tRet[16] = v[2]

table.insert(tOut, table.concat(tRet)) end

return table.concat(tOut) end

return p