Module:Activity-2

--[=[

Temporary Lua Module for Character Activities Templates Functions

--]=]

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

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] = " ''*" .. 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 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

function p.OtherSource(frame) local tArgs = util.getArgs(frame) local availableData = mw.loadData('Module:Available/data')

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

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

local tOut = {}

local tShop = {'|-\n|', "", '\n|', "?", ': ', "", "", '\n|\n| colspan="3" data-sort-value="99901" style="text-align: left;"|Instant'}

if event[1] == "" then for sName, data in pairs(availableData) do			if sName ~= "Tokens" and sName ~= "Legendary" and type(data) == "table" then for name, v in pairs(data) do					if v ~= true then for cName in mw.text.gsplit(name, "%s*%$%s*") do							if pagename == 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 local itemName = (v["L"] or sName) local linkName = itemName.. "#" .. (v["A"] or "") .. "|" .. (v["D"] or v["A"] or linkName) if itemName:match("^The Tower Challenge ") then tShop[2] = "" tShop[4] = "" .. itemName .. "" tShop[6] = EC({"Elixirs"}) tShop[7] = v["R"] elseif itemName:match(" Mini Event$") then local meData = mw.loadData('Module:MiniEvent/data') local meName = itemName:match("^(.+) Mini Event$"):lower tShop[2] = "" tShop[4] = "" .. linkName .. "" if v["R"] then tShop[6] = (v["R"]:match("^M") and "Milestone Reward" or "Leaderboard Reward") end elseif itemName:match("^Season") then tShop[2] = "" tShop[4] = "" .. itemName .. "" tShop[6] = v["D"] or "" elseif itemName:match("Calendar$") then tShop[2] = "" tShop[4] = "" .. itemName .. "" tShop[6] = v["A"] or "" elseif itemName == "Discovery Rewards" then tShop[2] = "" tShop[4] = "" .. itemName .. "" tShop[6] = v["A"] or "" else tShop[2] = "" tShop[4] = "" .. linkName .. "" tShop[6] = "" end table.insert(tOut, table.concat(tShop)) end end end end end end end end

if tArgs["me"] then local ME = require("Module:MiniEvent").getME for minievent in mw.text.gsplit(tArgs["me"], "%s*$%s*") do			local me = mw.text.split(minievent, "%s*%#%s*") table.insert(tOut, ME({me[1], (me[2] or 1), "K", me[3] or me[1]})) end end

if tArgs["es"] then tShop[2] = LTE({event[1], (event[2]=="T" and "T" or "N"), "I", 50}) tShop[4] = LTE({event[1], (event[2]=="T" and "T" or "N"), "A", "Event Shop", "Event Shop"}) tShop[6] = EC({tArgs["es"]}) table.insert(tOut, table.concat(tShop)) end

if not tArgs["ts"] and not tArgs["event"] then tShop[2] = ICON({"Merlin"}) tShop[4] = "Tokens Shop" tShop[6] = EC({"Elixirs"}) local VL = mw.ext.VariablesLua tShop[7] = VL.var("elixircost") table.insert(tOut, table.concat(tShop)) elseif tArgs["ts"] and tArgs["ts"] ~= "no" then tShop[2] = ICON({"Merlin"}) tShop[4] = "Merlin's Shop" tShop[6] = EC({"Elixirs"}) local ts = mw.text.split(tArgs["ts"], "%s*%$%s*") tShop[7] = ts[1] if ts[2] and ts[2] ~= "" then tShop[4] = "Tokens Shop" end if ts[3] and ts[3] ~= "" then tShop[7] = tShop[7] .. " ''* Limit: " .. ts[3] .. "'' "		end table.insert(tOut, table.concat(tShop)) end

local CHEST = require("Module:EnchantedChest").getChest local tChest = {} if not tArgs["ec"] and not tArgs["event"] then tChest = require("Module:ECR").getACTTOK(pagename) elseif tArgs["ec"] and tArgs["ec"] ~= "no" then for chest in mw.text.gsplit(tArgs["ec"], "%s*$%s*") do			local tCh = mw.text.split(chest, "%s*%#%s*") local conv = { ["Bronze-Old"]   = {"Bronze", '\n|\n| data-sort-value="99902"|3h'}, ["Silver-Old"]   = {"Silver", '\n|\n| data-sort-value="99903"|6h'}, ["Gold-Old"]     = {"Gold", '\n|\n| data-sort-value="99904"|12h'}, ["Platinum-Old"] = {"Platinum", '\n|\n| data-sort-value="99905"|24h'}, }			if conv[tCh[1]] then table.insert(tChest, {conv[tCh[1]][1], tCh[2], nil, conv[tCh[1]][2]}) else if tArgs["event"] then tCh[1] = tCh[1] .. "-" .. event[1]:upper .. (event[2]=="T" and " TC Event" or " Event") end table.insert(tChest, {tCh[1], tCh[2], tCh[3]}) end end end

for k, v in pairs(tChest) do		tShop[5] = (v[3] and " * Special Chance Only " or "") tShop[6] = "" tShop[7] = "" tShop[8] = v[4] or '\n|\n| data-sort-value="99901"|Instant' if v[2] then tShop[9] = '\n| colspan="2" style="text-align: left;"|x' .. v[2] end

if v[1] == "Special Request Wishes-Current" then tShop[2] = ' ' tShop[4] = "Special Request Wishes" else tShop[2] = ' ' .. CHEST({v[1], "50"}) .. ' '			tShop[4] = CHEST({v[1], "L"}) end

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

local PF = require("Module:Float").float if event[2] == "EC" then table.insert(tOut, PF({nil, event[1], "E"})) elseif PF({nil, event[1], "T"}) ~= "" then table.insert(tOut, PF({nil, event[1], "T"})) end if tArgs["pf"] then for float in mw.text.gsplit(tArgs["pf"], "%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 end

local BA = require("Module:Building").building if event[2] == "EC" 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 if tArgs["ba"] then for attraction in mw.text.gsplit(tArgs["ba"], "%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 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