Module:CombinedCYL

local cargo = mw.ext.cargo local HashUtil = require 'Module:HashUtil' local p = {}

local TITLES = { ["Shadow Dragon and the Blade of Light / Shadow Dragon"]=1, ["Gaiden"]=1, ["Gaiden / Echoes: Shadows of Valentia"]=1, ["Mystery of the Emblem / New Mystery of the Emblem"]=1, ["Genealogy of the Holy War"]=2, ["Thracia 776"]=2, ["The Binding Blade"]=3, ["The Blazing Blade"]=4, ["The Sacred Stones"]=5, ["Path of Radiance"]=6, ["Radiant Dawn"]=6, ["Awakening"]=7, ["Fates"]=8, ["Heroes"]=9 }

function p.createCombinedTable(frame) --local cyl2UnitCounts = HashUtil.from_ipairs(		cargo.query(			"Units=u,CharacterSortValues=csv",			"u.Person=personwikiname,COUNT(DISTINCT u._pageName)=unitcount",			{				join = "u.Person=csv.name",				where = "(Origin=csv.title OR u.Properties HOLDS 'enemy') AND u.AdditionDate < '2018-01-29'",				groupBy="u.Person",				limit=9000			}		), function(v,k) return v.personwikiname,v.unitcount end	) local event = frame.args[1] local entries = {} -- entires[combineid..name] = {{"name"]=, ["image"]=, ["combinedVotes"]=, ["titles"]={}, ["units"]=} local query = cargo.query("CYLResults","Name,WikiName,Image,Title,Votes",{	where = "Event='".. event .. "'",limit=9000 })

for _,v in ipairs(query) do local k = TITLES[v.Title] .. v.Name if not entries[k] then entries[k] = {name = v.Name, image = nil, combinedVotes = 0, titles= {}} end

if entries[k].image == nil then entries[k].image = v.Image else entries[k].image = false end entries[k].combinedVotes = entries[k].combinedVotes + v.Votes table.insert(entries[k].titles, v.Title) end query = nil

local tbl = mw.html.create("table") :addClass("wikitable") :addClass("sortable") :css("text-align","center") tbl:tag("th"):wikitext("Image"):addClass("unsortable") tbl:tag("th"):wikitext("Rank") tbl:tag("th"):wikitext("Name") tbl:tag("th"):wikitext("Titles") tbl:tag("th"):wikitext("Votes") local rank = 1 for _,v in HashUtil.sorted_pairs(entries, function (v1, v2) return v1.combinedVotes > v2.combinedVotes end) do		local tr = tbl:tag("tr") tr:tag("td"):wikitext((v.image and ("") or nil)) tr:tag("td"):wikitext(rank) tr:tag("td"):wikitext(v.name) local gameList = tr:tag("td"):tag("ul") tr:tag("td"):wikitext(v.combinedVotes) for _,game in ipairs(v.titles) do			gameList:tag("li"):wikitext(game) end rank = rank + 1 end return tbl end

return p