Module:Activity-2

--[=[

Temporary Lua Module for Character Activities Templates Functions

--]=]

local p = {} local util = require("Module:Utility")

local oneItem = require("Module:MultiItems").getCost local CHEST = require("Module:EnchantedChest").getChest local EC = require("Module:EC").getEC local LTE = require("Module:LTEvent").getLTE local TOK = require("Module:CharacterToken").getToken local ICON = require("Module:CharacterAvatar").getAvatar local COLL = require("Module:ItemCollection").getCollection local TASK = require("Module:Tasks").getData local NPC = require("Module:NPC").getNPC local date = require("Dev:Date") local today = os.date("%Y/%m/%d %H:%M:%S")

local function unavailable(tokens, type, pagename, event) if not tokens or tokens == "" then return "" end local tOut = {} for _, k in ipairs(tokens) do		if pagename == TOK({k, "T"}) then if type == "S" then return 'class="dmk-act-unavailable"' elseif type == "T" then return " '* Unavailable during " .. LTE({event, "N", "S"}) .. "' "			end end end return "" end

local function eventchanges(afterevent, duringevent) if afterevent == duringevent then return nil end if not (type(afterevent) == "table" and type(duringevent) == "table") then return {from = afterevent, to = duringevent} end

local result = {} for k, v in pairs(afterevent) do		result[k] = eventchanges(v, duringevent[k]) end for k, v in pairs(duringevent) do		if afterevent[k] == nil then result[k] = eventchanges(nil, v)		end end

return next(result) and result end

function p.getconflicts(tokens, event) if type(tokens) ~= "table" then return "" end local allcharacters = – local tOut = {'{| class="article-table" style="text-align: center; line-height: 1.0; width: fit-content;"'} local i = 6

for _, data in ipairs(tokens) do		if type(TOK({data, "X"})) == "table" then for _, v in ipairs(TOK({data, "X"})) do				if not allcharacters[v] then if v:match("^IP-") then table.insert(allcharacters[1], 'AAAA-' .. v)					elseif v:match("^Fabric ") then table.insert(allcharacters[1], 'ZZZZ-' .. v)					else table.insert(allcharacters[1], v)					end allcharacters[v] = v				end end end end

table.sort(allcharacters[1], function(a, b) return a < b end)

for k, name in pairs(allcharacters[1]) do		if i == 6 then i = 0 table.insert(tOut, '\n|-') end if name:match("^AAAA-") then table.insert(tOut, ('\n| style="width: 15%; padding: 5px;"|' .. COLL({mw.ustring.lower(name):match("^aaaa%-ip%-(.+)$"), "P"}))) i = i + 1 elseif name:match("^ZZZZ-") then table.insert(tOut, ('\n| style="width: 15%; padding: 5px;"|' .. TOK({name:match("^ZZZZ%-(.+)$"), "50"}) .. ' ' .. TOK({name:match("^ZZZZ%-(.+)$"), "L"}) .. ' '))			i = i + 1 elseif name ~= "" then table.insert(tOut, ('\n| style="width: 15%; padding: 5px;"|' .. ICON({name, (event or '')}) .. ' ' .. name .. ' ')) i = i + 1 end end

table.insert(tOut, "\n|}\n Note: if the collection icon is displayed, it means that this token conflicts with the whole collection") return table.concat(tOut) end

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

local eventstart = today local eventend = today

local fullpagename = tArgs["F"] or mw.title.getCurrentTitle.prefixedText or "" local pagename = tArgs["P"] or mw.title.getCurrentTitle.text or "" pagename = pagename:gsub("^([^,]-)%s*/%s*(.-)$", "%1")

local char1 = tArgs["ch1"] or "? #0" local ch1name, ch1lvl = mw.ustring.match(char1, "^(.-)%s*#%s*([%d%,]*)$") if not ch1name then ch1name = char1 end if ch1lvl == "0" then ch1lvl = "?" end

local char2 = tArgs["ch2"] local ch2name = nil local ch2lvl = nil if char2 then ch2name, ch2lvl = mw.ustring.match(char2, "^(.-)%s*#%s*([%d%,]*)$") if not ch2name then ch2name = char2 end if ch2lvl == "0" then ch2lvl = "?" end end

local actanim = tArgs["anim"] local actname = tArgs["name"] or "?" local actaltname = tArgs["altname"] or actname local actreq = tArgs["req"] local acttime = tArgs["time"] or "?h"

local wish = tArgs["wish"] local trophy = tArgs["trophy"] local notokens = tArgs["notok"] local tokens = tArgs["tok"] or ""

local event = tArgs["event"] or "" if event ~= "" then eventstart = LTE({event, nil, "SD"}) eventend = LTE({event, nil, "ED"}) if eventstart > today then event = "" end end local noec = tArgs["noec"] local eventtokens = tArgs["eventtok"] or ""

if (ch1name and ch1name == "Grumpy") or (ch2name and ch2name == "Grumpy") then event = event .. "grumpy" end

local tOut = {	'|-', "", '\n|', "", '\n| style="text-align: left;"|', (actanim and (" ") or ""), ("" .. ch1name .. ": " .. actname), "", "", '\n| nowrap|', (wish and NPC({"Wish Granter"}) or ""), '\n', TASK({acttime, (ch2name and "J" or ""), event}), "", (tokens~="" and '\n| data-sort-value="1"|' or '\n| data-sort-value="0"|'), TOK({tokens, nil, "B"}), ("\n"), "" }

if ch2name then tOut[7] = "" .. ch1name .. " + " .. ch2name .. ": " .. actname tOut[11] = ' ' .. ICON({ch2name, nil, 30}) .. '  ' .. (ch2lvl and (" ''Lvl " .. ch2lvl .. "'' ") or "") tOut[18] = "" end

if noec and noec == event then tOut[13] = TASK({acttime, (ch2name and "J" or "")}) end

if not fullpagename:match("^Template") or fullpagename:match("%-Event%-") then tOut[17] = "" tOut[18] = "" end

if pagename == ch1name and fullpagename == ch1name then tOut[4] = ch1lvl or "" tOut[7] = actname elseif ch2name and pagename == ch2name then if fullpagename == ch2name then tOut[4] = ch2lvl or "" else tOut[4] = ICON({ch2name}) .. (ch2lvl and (" ''Level " .. ch2lvl .. "'' ") or "") end tOut[7] = actaltname tOut[11] = ' ' .. ICON({ch1name, nil, 30}) .. '  ' .. (ch1lvl and (" ''Lvl " .. ch1lvl .. "'' ") or "") else tOut[4] = ICON({ch1name}) .. (ch1lvl and (" ''Level " .. ch1lvl .. "'' ") or "") end if actreq then local sep = "" local rOut = "" local requirements = mw.text.split(actreq, "%s*%$%s*") for idx = #requirements, 1, -1 do			local reqname, reqlvl = mw.ustring.match(requirements[idx], "^(.-)%s*#%s*([%d%,]*)$") if not reqname then reqname = requirements[idx] reqlvl = nil end if reqlvl and reqlvl == "" then reqlvl = nil end rOut = " " .. reqname .. "" .. (reqlvl and (" (Ench. Lvl. " .. reqlvl .. ")") or "") .. sep .. rOut if sep == "" then sep = " &" else sep = "," end end tOut[8] = " ''* Requires " .. rOut .. "'' "	end if trophy and (event=="grumpy" or today <= eventend) then tOut[14] = ", " .. NPC({"Gold Trophies", "G"}) end

if tArgs["reward"] then if tArgs["alttext"] then tOut[8] = " ''* " .. tArgs["alttext"] .. "'' " end if acttime == "Instant" then tOut[13] = '| data-sort-value="0"|Instant' else local durNumb, durHMS = string.match(acttime, "^([%d%?]+)([hms])$") local durNumb = tonumber(durNumb) if durNumb then if durHMS == "h" then durNumb = durNumb * 3600 elseif durHMS == "m" then durNumb = durNumb * 60 end end tOut[13] = '| data-sort-value="' .. (durNumb or 99900) .. '"|' .. acttime end tOut[14] = "\n| nowrap|" .. (tArgs["reward"]~="X" and tArgs["reward"] or "") if tArgs["reward"]:match("^EC%-") then tOut[14] = "\n| nowrap|" .. EC({tArgs["reward"]}) end end if notokens and ((notokens == "ME" and not pagename:match(" Token$")) or notokens ~= "ME") then if notokens == "ME" then if tOut[11] == "" then tOut[10] = "" tOut[5] = '\n| colspan="2" style="text-align: left;"|' end tOut[13] = "| nowrap|" tOut[14] = acttime tOut[15] = "" tOut[16] = "" elseif notokens == "O" then if event ~= "" and eventtokens ~= tokens then if eventstart > today then return "" elseif eventstart <= today then if today <= eventend then tOut[2] = 'class="dmk-act-available"' tOut[9] = " '* Available only during " .. LTE({event, "N", "S"}) .. "' "						if eventtokens ~= "" then tOut[15] = '\n| data-sort-value="1"|' end tOut[16] = TOK({eventtokens, nil, "B"}) end end end else tOut[15] = "" tOut[16] = "" end else if event ~= "" and event~="grumpy" and eventtokens ~= tokens then if eventstart <= today then if today <= eventend then local alltokens = {} alltokens["After"] = mw.text.split(tokens, "%s*%$%s*") alltokens["During"] = mw.text.split(eventtokens, "%s*%$%s*") local unavtokens = {}

for _, data in pairs(eventchanges(alltokens["After"], alltokens["During"])) do					for y, v in pairs(data) do						if y == "from" then table.insert(unavtokens, v) end end end

tOut[2] = unavailable(unavtokens, "S", pagename, event) tOut[9] = unavailable(unavtokens, "T", pagename, event)

if eventtokens ~= "" then tOut[15] = '\n| data-sort-value="1"|' end tOut[16] = TOK({eventtokens, nil, "B"}) end end end end return table.concat(tOut) end

local event = {"", ""}

local function oneSource(srcName, anchName, dispName, altText, rewType, costVal, altText2, availability) local tOut = {'|-\n| ', "", ' \n|', "", ': ', "", "", '\n|\n|', "Instant", '\n| colspan="2" data-sort-value="99901" style="text-align: left;"|', ""}

local conv = { ["Bronze"]   = '3h', ["Silver"]   = '6h', ["Gold"]     = '12h', ["Platinum"] = '24h', }

if rewType~="" then tOut[6] = (rewType:match("^M") and "Milestone Reward" or "Leaderboard Reward") elseif costVal~="" then tOut[6] = oneItem(costVal) elseif altText~="" and altText:match("^x%d") then tOut[11] = altText elseif altText~="" then tOut[6] = altText end if altText2~="" then tOut[7] = " ''* " .. altText2 .. "'' "	end if availability~="" then tOut[7] = tOut[7] .. " * " .. util.countdown(availability, " for ", " until ") .. " "	end

local srcNumb = 1 local fileName = "" local linkName = srcName .. (anchName~="" and ("#" .. anchName) or "") .. "|" .. dispName

if srcName == "Special Request Wishes-Current Chests" then srcNumb = 7 fileName = "special_request_wishes" linkName = "Special Request Wishes" elseif srcName:match(" Chests") then local nb = string.gsub((altText or 0), "[,/x]", "") srcNumb = 10 + (tonumber(nb) or 0) srcName = srcName:gsub(" Chests", "") if anchName=="Old" and conv[srcName] then tOut[9] = conv[srcName] end tOut[2] = CHEST({srcName, "50"}) tOut[4] = CHEST({srcName, "L"}) elseif srcName:match("^The Tower Challenge ") then srcNumb = 3 fileName = "update-20-event_shop" elseif srcName:match(" Mini Event$") then srcNumb = 4 local ME = require("Module:MiniEvent").getME local meName = srcName:match("^(.+) Mini Event$") local meNumb = anchName:match("^srcName %#") tOut[2] = ME({meName, meNumb, "I", 60}) elseif srcName:match("^Season") then srcNumb = 2 fileName = "season_pass" elseif srcName:match("Calendar$") then srcNumb = 5 fileName = "home-calendar" elseif srcName:match("^Bundle Shop") and anchName and anchName:match(": %d$") then srcNumb = 7 fileName = "home-chain_offer" linkName = "Bundle Shop#" .. anchName .. "|" .. anchName elseif srcName:match("^Bundle Shop") then srcNumb = 7 fileName = "home-shop" linkName = "Bundle Shop#" .. anchName .. "|" .. dispName elseif srcName == "Discovery Rewards" then srcNumb = 6 fileName = "discovery" elseif dispName == "Merlin's Shop" then srcNumb = 9 fileName = "c-merlin-1" elseif srcName == "Tokens Shop" then srcNumb = 9 fileName = "ba-tokens_shop" else srcNumb = 8 tOut[2] = ICON({srcName}) end

if tOut[2]=="" then tOut[2] = "" end if tOut[4]=="" then tOut[4] = "" .. linkName .. "" end

if event[1]=="me" then tOut[10] = "" tOut[11] = "" end

return {srcNumb, tOut} end

function p.OtherSource(frame) local tArgs = util.getArgs(frame) local availableData = mw.loadData('Module:Available/data') local PF = require("Module:Float").float local BA = require("Module:Building").building

local pagename = tArgs["P"] or mw.title.getCurrentTitle.text or "" pagename = pagename:gsub("^([^,]-)%s*/%s*(.-)$", "%1") local tokenpagename = pagename:gsub(" Token$", "") if availableData["Tokens"][tokenpagename] and availableData["Tokens"][tokenpagename][1] > today then return "|}" end

if tArgs["event"] then event = mw.text.split(tArgs["event"], "%s*%$%s*") end event[1] = event[1]:lower

local tOther = {} local tOut = {}

if event[1] == "" then for sName, data in pairs(availableData) do			if sName ~= "Tokens" and sName ~= "Legendary" then for name, v in pairs(data) do					if v ~= true then for cName in mw.text.gsplit(name, "%s*%$%s*") do							if tokenpagename == cName then local UnlockDate = v[1] .. util.dayStart local LockDate = date(v[1]):adddays(v[2]):fmt("%Y/%m/%d") .. (v[3] or util.dayStart) if UnlockDate < today and (v[2] == 0 or today < LockDate) then table.insert(tOther, oneSource(sName, (v["A"] or ""), (v["D"] or v["A"] or sName), (v["A"] or ""), (v["R"] or ""), (v["C"] or ""), (v["S"] or ""), (v[2]>0 and LockDate or ""))) end end end end end end end end

if not tArgs["event"] then tArgs = require("Module:ECR").getTokenFormat(tArgs, pagename) tArgs = require("Module:Bundle").getTokenFormat(tArgs, pagename) end

if tArgs["Event Shop"] then local eventLink = LTE({event[1], (event[2]=="T" and "T" or "N"), "L"}) tArgs[eventLink] = tArgs["Event Shop"] tArgs["Event Shop"] = "" end

if tArgs["ec"] and tArgs["event"] and tArgs["ec"]==tArgs["event"] then table.insert(tOut, PF({nil, event[1], "E"})) tArgs["Event Shop"] = nil elseif PF({nil, event[1], "T"})~="" then table.insert(tOut, PF({nil, event[1], "T"})) end

if tArgs["ec"] and tArgs["event"] and tArgs["ec"]==tArgs["event"] then table.insert(tOut, BA({nil, nil, event[1], "E"})) elseif BA({nil, nil, event[1], "T"})~="" then table.insert(tOut, BA({nil, nil, event[1], "T"})) end

for arg, value in pairs(tArgs) do		if arg~="event" and arg~="ec" and arg~="P" then if arg == "Floats" then for float in mw.text.gsplit(value, "%s*$%s*") do					if event[1] == "me" then table.insert(tOut, PF({float, nil, "ME"})) else table.insert(tOut, PF({float, event[1]})) end end elseif arg == "Attractions" then for attraction in mw.text.gsplit(value, "%s*$%s*") do					local tBa = mw.text.split(attraction, "%s*%#%s*") if event[1] == "me" then table.insert(tOut, BA({tBa[1], tBa[2], nil, "ME"})) else table.insert(tOut, BA({tBa[1], tBa[2], event[1]})) end end elseif value~="" then local v = mw.text.split(value, "%s*%$%s*") if tArgs["event"] and v[1]=="" and arg:match("Chests$") then arg = arg .. "-" .. event[1]:upper .. (event[2]=="T" and " TC Event" or " Event") end local disp = arg ; if v[2] and v[2] ~= "" then disp = v[2] elseif v[1] and v[1] ~= "" then disp = v[1] end table.insert(tOther, oneSource(arg, v[1], disp, (v[3] or ""), (v[4] or ""), (v[5] or ""), (v[6] or ""), (v[7] or ""))) end end end

table.sort(tOther, function(a, b) return a[1] < b[1] end)

for _, value in pairs(tOther) do		table.insert(tOut, table.concat(value[2])) end

if table.concat(tOut) ~= "" then table.insert(tOut, 1, '|-\n! colspan="7"|Other Sources') end

if event[1] ~= "me" then table.insert(tOut, "|}") end return table.concat(tOut, "\n") end

return p