Module:SummoningEventList

local cargo = mw.ext.cargo local List = require 'Module:ListUtil' local Hash = require 'Module:HashUtil' local Datetime = require 'Module:DatetimeUtil' local escq = require 'Module:EscQ'.main1 local mf = require 'Module:MF'.main1 local SummoningEvent = require 'Module:SummoningEvent' local EXTRA_MAPS = mw.loadData 'Module:SummoningEventList/data'

local makeEventTable = function (event, extraTitle, extraText) return SummoningEvent._table { page = event.Page, name = event.Name, startDate = event.DateStart, endDate = event.DateEnd, heroes = event.Units, extratitle = extraTitle, extratext = extraText, } end

local allList = function (events, frame) SummoningEvent._cacheAllHeroes

local maps = cargo.query('Maps,MapDates', 'StartTime,MapGroup,BookGroup,Map', {		join = "Maps._pageName = MapDates._pageName",		where = '(Map LIKE "S%" OR Map LIKE "X%") AND MapGroup NOT LIKE "Chain Challenge%"',		groupBy = 'MapGroup',		limit = 1000,	})

return table.concat(List.map(events, function (event) local extraTitle = '' local extraTexts = {}

for _, map in ipairs(maps) do			if map.StartTime == event.StartTime then local bannerName = nil local groupName, groupTitle = string.match(map.MapGroup, '^(.*): (.*)$') local isStory = mw.ustring.find(map.Map, '^S') ~= nil extraTitle = isStory and "Accompanied story chapter" or "Accompanied paralogue"

-- Find a story maps released at the same time the event if isStory then if map.BookGroup == 'Book I' then bannerName = string.gsub(map.Map, 'S(..)..', 'C00%1 C.webp') else if not string.find(groupTitle, 'Book') then groupTitle = map.BookGroup .. ', ' .. groupTitle end bannerName = string.gsub(map.Map, 'S(...).', 'C0%1 C.webp') end

-- Find a paralogue maps released at the same time the event else if map.MapGroup == "Paralogue 2: Sibling Bonds" then bannerName = "CX001.webp" else bannerName = ("CX%s.webp"):format(string.match(map.Map, "X(...).")) end end

extraTexts[#extraTexts + 1] = frame:expandTemplate {title = 'Banner Story', args = {bannerName = bannerName, text1 = groupName, text2 = groupTitle}} end end

local extra = EXTRA_MAPS[event.Page] if extra then extraTitle = extra.extraTitle for _, extraBanner in ipairs(extra.extraText) do				extraTexts[#extraTexts + 1] = frame:expandTemplate {title = extraBanner.templateTitle, args = extraBanner.templateArgs} end end

return makeEventTable(event, extraTitle, table.concat(extraTexts, '')) end), '') end

local allNewHeroes = function (_, frame) local events = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF,Units=U,SummoningEventFocuses=SEF2',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF2.Unit SEPARATOR ';')=Units,SE.StartTime=StartTime", {			join = 'SE.WikiName=SEF.WikiName,SEF.Unit=U.WikiName,SE.WikiName=SEF2.WikiName',			where = legendary|mythic|special|tempest|enemy",			groupBy = 'SE.Name',			orderBy = 'SE.StartTime',			limit = 1000,		}) return allList(events, frame) end

local allSpecialHeroes = function (_, frame) local events = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF,Units=U,SummoningEventFocuses=SEF2',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF2.Unit SEPARATOR ';')=Units,SE.StartTime=StartTime", {			join = 'SE.WikiName=SEF.WikiName,SEF.Unit=U.WikiName,SE.WikiName=SEF2.WikiName',			where = 'U.ReleaseDate=DATE(SE.StartTime) AND EventType!="Free Summon" AND U.ReleaseDate>"2017-02-02" AND U.Properties__full LIKE "%special%"',			groupBy = 'SE.Name',			orderBy = 'SE.StartTime',			limit = 1000,		}) return allList(events, frame) end

local allLegendaryHeroes = function (_, frame) local events = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF,Units=U,SummoningEventFocuses=SEF2',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF2.Unit SEPARATOR ';')=Units,SE.StartTime=StartTime", {			join = 'SE.WikiName=SEF.WikiName,SEF.Unit=U.WikiName,SE.WikiName=SEF2.WikiName',			where = 'U.ReleaseDate=DATE(SE.StartTime) AND EventType!="Free Summon" AND U.ReleaseDate>"2017-02-02" AND U.Properties__full LIKE "%legendary%"',			groupBy = 'SE.Name',			orderBy = 'SE.StartTime',			limit = 1000,		}) return allList(events, frame) end

local allMythicHeroes = function (_, frame) local events = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF,Units=U,SummoningEventFocuses=SEF2',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF2.Unit SEPARATOR ';')=Units,SE.StartTime=StartTime", {			join = 'SE.WikiName=SEF.WikiName,SEF.Unit=U.WikiName,SE.WikiName=SEF2.WikiName',			where = 'U.ReleaseDate=DATE(SE.StartTime) AND EventType!="Free Summon" AND U.ReleaseDate>"2017-02-02" AND U.Properties__full LIKE "%mythic%"',			groupBy = 'SE.Name',			orderBy = 'SE.StartTime',			limit = 1000,		}) return allList(events, frame) end

-- summoning events matching a given name (optionally page as well) local byName = function (args, frame) local name = args[1] local event = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF.Unit SEPARATOR ';')=Units", {			join = 'SE.WikiName=SEF.WikiName',			where = ("SE.Name='%s'%s"):format(escq(name), args.page and (" AND SE._pageName='%s'"):format(args.page) or ''),			groupBy = 'SE._pageName',			limit = 1,		})[1] if not event then return require 'Module:Error'.error(('Summoning event "%s" not found.'):format(name)) end

return tostring(makeEventTable(event, args[2], args[3])) end

-- ongoing summoning events local currentEvents = function (args) local eventQueryResult = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF.Unit SEPARATOR ';')=Units", {			join = 'SE.WikiName=SEF.WikiName',			where = 'NOW BETWEEN SE.StartTime AND SE.EndTime',			groupBy = 'SE._pageName',			orderBy = 'StartTime DESC',		})

if #eventQueryResult == 0 then return '(no values)' end

return table.concat(List.map(eventQueryResult, function (event) return tostring(makeEventTable(event)) end)) end

-- Get countdown text local countdownFunc = function (timediff) if timediff > 172800 then return 'Ends in ' .. math.floor(timediff / 86400) .. ' day(s).' elseif timediff > 86400 then return Ends in ' .. math.floor(timediff / 86400) .. ' day(s). elseif timediff > 3600 then return Ends in ' .. math.floor(timediff / 3600) .. ' hr(s). else return Ends in ' .. math.floor(timediff / 60) .. ' min(s). end end

local currentEventsSlideshow = function (args, frame) local eventQueryResult = cargo.query(		'SummoningEvents=SE,SummoningEventFocuses=SEF',		"SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,SE.EndTime=EndTime,GROUP_CONCAT(DISTINCT SEF.Unit SEPARATOR ';')=Units", {			join = 'SE.WikiName=SEF.WikiName',			where = 'NOW BETWEEN SE.StartTime AND SE.EndTime',			groupBy = 'SE._pageName',			orderBy = 'StartTime DESC',		})

if #eventQueryResult == 0 then return '(no values)' end

local now = os.time local galleryparams = table.concat(List.map(eventQueryResult, function (event) -- Get the banner name from Name field local bannerName = event.Name

local timediff = Datetime.from_cargo(event.EndTime) - now

-- Add the banner in the format we need. -- Example format below -- File:Banner Focus Choose Your Legends Top 8 Block B.png|Choose Your Legends Top 8 Block B (Focus)|Choose Your Legends Top 8 Block B return ("File:Banner Focus %s.png|%s|%s %s\n"):format(			mf(mw.ustring.gsub(event.Page, ' %(Focus%)', '')), event.Page, bannerName, event.Page, bannerName, countdownFunc(timediff)) end))

return frame:extensionTag('gallery', galleryparams, {mode = 'slideshow', widths = '300px', heights = '300px'}) end

-- all summoning events, grouped by month local allByMonth = function (args) SummoningEvent._cacheAllHeroes

-- Cargo queries local eventQueryResult = List.group_by(cargo.query( 'SummoningEvents=SE,SummoningEventFocuses=SEF', "SE._pageName=Page,SE.Name=Name,DATE(SE.StartTime)=DateStart,DATE(SE.EndTime)=DateEnd,GROUP_CONCAT(DISTINCT SEF.Unit SEPARATOR ';')=Units", { join = 'SE.WikiName=SEF.WikiName', groupBy = 'SE._pageName', orderBy = 'StartTime DESC', limit = 1000, }), function (v) return mw.ustring.match(v.DateStart, '^%d+%-%d+') end)

Hash.map_self(eventQueryResult, function (events, ym)		-- events is already sorted by StartTime DESC		List.map_self(events, function (event) return tostring(makeEventTable(event)) end)		local year, month = mw.ustring.match(ym, '(%d+)%-(%d+)')		return (' %s %s'):format(os.date('%B %Y', os.time {year = year, month = month, day = 1}), table.concat(events))	end)

local ordered = {} for ym, v in Hash.sorted_pairs(eventQueryResult) do		ordered[#ordered + 1] = v	end List.reverse_self(ordered) return table.concat(ordered, ' ') end

return require 'Module:MakeMWModule'.makeMWModule { allNewHeroes = allNewHeroes, allSpecialHeroes = allSpecialHeroes, allLegendaryHeroes = allLegendaryHeroes, allMythicHeroes = allMythicHeroes,

byName = byName, currentEvents = currentEvents, currentEventsSlideshow = currentEventsSlideshow, allByMonth = allByMonth, }