Module:ECR

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

local p = {}

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

local util = require('Module:Utility') local RARITY = require("Module:Rarity").rarity local EC = require("Module:EC").getEC local TOK = require("Module:CharacterToken") local CHEST = require("Module:EnchantedChest").getChest local COLL = require("Module:ItemCollection").getCollection local date = require("Dev:Date")

local function addItem(data) local dataUnknown = {data["Unknown"] or ""} local dataCommon = {data["Common"] or ""} local dataUncommon = {data["Uncommon"] or ""} local dataRare = {data["Rare"] or ""} local dataEpic = {data["Epic"] or ""} local dataLegendary = {data["Legendary"] or ""}

local raTok = "" local rbTok = ""

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"})

local tOut = "$" .. tCh

if raTok == "common" then table.insert(dataCommon, (tOut .. ",2 #" .. (data["T"][2] or "1"))) elseif raTok == "uncommon" then table.insert(dataUncommon, (tOut .. ",2 #" .. (data["T"][3] or "1"))) elseif raTok == "rare" then table.insert(dataRare, (tOut .. ",2 #" .. (data["T"][4] or "1"))) elseif raTok == "epic" then table.insert(dataEpic, (tOut .. ",2 #" .. (data["T"][5] or "1"))) elseif raTok == "legendary" then table.insert(dataLegendary, (tOut .. ",2 #" .. (data["T"][6] or "1"))) end

if rbTok == "common" then table.insert(dataCommon, (tOut .. ",3 #" .. (data["T"][2] or "1"))) elseif rbTok == "uncommon" then table.insert(dataUncommon, (tOut .. ",3 #" .. (data["T"][3] or "1"))) elseif rbTok == "rare" then table.insert(dataRare, (tOut .. ",3 #" .. (data["T"][4] or "1"))) elseif rbTok == "epic" then table.insert(dataEpic, (tOut .. ",3 #" .. (data["T"][5] or "1"))) elseif rbTok == "legendary" then table.insert(dataLegendary, (tOut .. ",3 #" .. (data["T"][6] or "1"))) end end end

local tRarity = {} table.insert(tRarity, {"Unknown", table.concat(dataUnknown)}) 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$", "") elseif iData[2] == "ec" then fname = fname:gsub("_chests$", "") else fname = fname:gsub("^([^%(]-)_*%(([^%(]-)%)$", "%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, 35}) 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.getCollections(frame) local tArgs = util.getArgs(frame)

local pagename = tArgs[1] or mw.title.getCurrentTitle.baseText local cName = mw.ustring.gsub(pagename, " Chests", "")

local tOut = {" Go to: \n"} local tColl = {} local cNumb = "2016/01/01" local firstDate = "2016/01/01" local today = os.date("%Y/%m/%d")

for k, v in pairs(chestData[cName]) do		if availableData["Chests"]["Legendary Chests"][k] then if availableData["Chests"]["Legendary Chests"][k][1] <= today then cNumb = availableData["Chests"]["Legendary Chests"][k][1] if firstDate <= availableData["Chests"]["Legendary Chests"][k][1] then firstDate = availableData["Chests"]["Legendary Chests"][k][1] end else cNumb = date(firstDate):adddays("-1"):fmt("%Y/%m/%d") end else cNumb = "2016/01/01" end table.insert(tColl, {cNumb, k}) end

table.sort(tColl, function(a,b) return a[1] > b[1] end)

for _, cItem in pairs(tColl) do		table.insert(tOut, COLL({cItem[2], "A"})) end

return table.concat(tOut) .. " " end

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

local pagename = tArgs[1] or mw.title.getCurrentTitle.baseText local cName = mw.ustring.gsub(pagename, " Chests", "") local tOut = {} local cSection = {} local firstDate = "2016/01/01" local today = os.date("%Y/%m/%d") if chestData[cName] then for k, v in pairs(chestData[cName]) do			if v ~= true and v["R"] then local cNumb = v[1] or 999 if cName == "Legendary" then cNumb = "2016/01/01" if availableData["Chests"]["Legendary Chests"][k] then if availableData["Chests"]["Legendary Chests"][k][1] <= today then cNumb = availableData["Chests"]["Legendary Chests"][k][1] if firstDate <= availableData["Chests"]["Legendary Chests"][k][1] then firstDate = availableData["Chests"]["Legendary Chests"][k][1] end else cNumb = date(firstDate):adddays("-1"):fmt("%Y/%m/%d") end else cNumb = "2016/01/01" end end table.insert(cSection, {cNumb, k, v}) end end

table.sort(cSection, function(a,b) return a[1] > b[1] end)

for _, cItem in pairs(cSection) do if chestData[cName][1] and availableData["Chests"][cName .. " Chests"][cItem[2]] and availableData["Chests"][cName .. " Chests"][cItem[2]][1] and (availableData["Chests"][cName .. " Chests"][cItem[2]][1] >= today) then table.insert(tOut, '|-|') table.insert(tOut, cItem[2]) table.insert(tOut, '=\n') table.insert(tOut, '    ') table.insert(tOut, "Coming Soon...") table.insert(tOut, ' This part of the page is currently not available or/and obtainable in-game. It will be available in the future... Stay tuned!    \n') else 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, '50') else table.insert(tOut, '100') end		   	table.insert(tOut, '%;"\n! colspan="10" id="')   			table.insert(tOut, cItem[2])		    	table.insert(tOut, '"|') table.insert(tOut, cItem[2]) table.insert(tOut, ' Prizes')

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

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 tFirst == true 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|-') tFirst = false elseif tFirst == true 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|-') tFirst = false 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 end return table.concat(tOut) end

function p.getDATA(frame) local tArgs = util.getArgs(frame) local tItems = tArgs[1] local cName = mw.ustring.gsub(tArgs[1], " Chests", "") or "" local cSection = tArgs[2] or "Current" tItems = tItems .. "$" .. cSection

if chestData[cName] and chestData[cName][cSection] then local rarity = chestData[cName][cSection]["R"] local tRarity = addItem(rarity)

for _, item in ipairs(tRarity) do			if item[2] ~= "" then item[2]	= chItems(item[2]) for _, rData in ipairs(item[2]) do tItems = tItems .. "$" .. rData[1] end end end

if chestData[cName][cSection]["S"] then local rarity = chestData[cName][cSection]["S"] local tRarity = addItem(rarity)

for _, item in ipairs(tRarity) do				if item[2] ~= "" then item[2]	= chItems(item[2]) for _, rData in ipairs(item[2]) do tItems = tItems .. "$" .. rData[1] end end end end end

return tItems end

local currently = false

local function getChest(iType, 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 iType == "T" then 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 elseif iName:match("^EC") then iName = EC({iName, "T"}) else iName = TOK.getToken({iName, "T"}) end else if iName:match("^EC") then iName = EC({iName, "T"}) end end

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

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

return tOut 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 = { ["Bronze-Old"]   = {"Bronze", "3h"}, ["Silver-Old"]   = {"Silver", "6h"}, ["Gold-Old"]     = {"Gold", "12h"}, ["Platinum-Old"] = {"Platinum", "24h"}, --				["B"] = {"Bronze"}, ["B"] = {"Silver"}, ["B*"] = {"Bronze", "3h"}, ["S"] = {"Silver", "6h"}, ["G"] = {"Gold", "12h"}, ["P"] = {"Platinum", "24h"}, ["RE"] = {"Resource"}, ["M"] = {"Magical"}, ["R"] = {"Radiant"}, ["RU"] = {"Ruby"}, ["A"] = {"Amber"}, ["C"] = {"Cobalt"}, }			if conv[tArgs[1]] then local old = conv[tArgs[1]][2] or nil tArgs[1] = conv[tArgs[1]][1] or tArgs[1] table.insert(tChest, {tArgs[1], ("x" .. (tArgs[3] or "1")), (tArgs[2] or nil), old}) else table.insert(tChest, {tArgs[1], ("x" .. (tArgs[3] or "1")), (tArgs[2] or nil)}) end end else local pagename = tArgs["P"] or mw.title.getCurrentTitle.text

for cName, chest in pairs(chestData) do if availableData["Chests"][cName .. " Chests"] then for n, v in pairs(availableData["Chests"][cName .. " Chests"]) do					local today = os.date("%Y/%m/%d %H:%M:%S") local data = availableData["Chests"][cName .. " Chests"][n] local UnlockDate = data[1] .. util.dayStart local LockDate = date(data[1]):adddays(data[2]):fmt("%Y/%m/%d") .. (data[3] or util.dayStart) if UnlockDate < today and (data[2] == 0 or today < LockDate) then if chestData[cName][n] and chestData[cName][n]["R"] then table.insert(tChest, getChest("T", chestData[cName][n]["R"], cName, pagename)) end if chestData[cName][n] and chestData[cName][n]["S"] then table.insert(tChest, getChest("T", chestData[cName][n]["S"], cName, pagename, "S")) end

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

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

for k, v in pairs(tChest) do		if v[1] == "Special Request Wishes" then local tRet = {	'|-\n| style="text-align: center;"| \n|Special Request Wishes\n|\n|Instant\n| colspan="2"|', v[2], '\n' }			table.insert(tOut, table.concat(tRet)) else local tRet = {	'|-\n| style="text-align: center;"| ', CHEST({v[1], "45"}), ' \n|', CHEST({v[1], "L"}), "", "", '\n|\n|', 'Instant', '\n| colspan="2"|', v[2], '\n' }

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

if v[4] then tRet[8] = v[4] end

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

return table.concat(tOut) end

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

local tOut = {} local tChest = {} local space = "" local pagename = tArgs["P"] if not pagename then pagename = mw.title.getCurrentTitle.text end

for cName, chest in pairs(chestData) do if availableData["Chests"][cName .. " Chests"] then for n, v in pairs(availableData["Chests"][cName .. " Chests"]) do				local today = os.date("%Y/%m/%d %H:%M:%S") local data = availableData["Chests"][cName .. " Chests"][n] local UnlockDate = data[1] .. util.dayStart local LockDate = date(data[1]):adddays(data[2]):fmt("%Y/%m/%d") .. (data[3] or util.dayStart) if UnlockDate < today and (data[2] == 0 or today < LockDate) then if chestData[cName][n] and chestData[cName][n]["R"] then table.insert(tChest, getChest("I", chestData[cName][n]["R"], cName, pagename)) end if chestData[cName][n] and chestData[cName][n]["S"] then table.insert(tChest, getChest("I", chestData[cName][n]["S"], cName, pagename, "S")) end end end end end

if currently == false then for k, section in pairs(chestData["Legendary"]) do			table.insert(tChest, getChest("I", section["R"], "Legendary", pagename)) end end

for k, v in pairs(tChest) do		local tRet = {space, "", "", "", ""}

if tArgs[1] == "I" then tRet[2] = CHEST({(v[1] .. "-" .. pagename), "S", "S"}) else tRet[2] = CHEST({(v[1] .. "-" .. pagename), "O"}) end

space = " "

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

if table.concat(tOut) ~= "" then table.insert(tOut, " ") end

return table.concat(tOut) end

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

local tOut = "" local prevTab = 0 local pagename = tArgs["P"] if not pagename then pagename = mw.title.getCurrentTitle.text end pagename = mw.ustring.gsub(pagename, " Chests", "") or "" if not chestData[pagename] then return "" end

for cName, chest in pairs(chestData[pagename]) do		if chest ~= true and chest["C"] and chest[1] > prevTab then tOut = chest["C"] prevTab = chest[1] end end

return frame:preprocess(tOut) end

return p