Module:Combined award boxes

From IFWiki

Events
XYZZY Awards 2007
Winner: Best Game, Best Individual NPC, Best Individual PC, Best Writing
XYZZY Awards 2007
Finalist: Best NPCs, Best Puzzles, Best Setting, Best Use of Medium
IFComp 2007
1st of 27
Events
XYZZY Awards 2018
Winner: Best Technological Development
Events
ParserComp 2015
3rd place: Best Technical
ParserComp 2015
1st place: Best Overall, 1st place: Best Story, 1st place: Best Use of Theme, 1st place: Best Writing
ParserComp 2015
2nd place: Best Puzzles
XYZZY Awards 2015
Finalist: Best Puzzles, Best Setting

Displays "Events" header and award boxes in an infobox row.

For example (in relation to the above pages):

{| class="wikitable ifwiki-infobox"
{{#invoke:Combined award boxes|displayAwardBoxes|game=Lost Pig}}
|-
{{#invoke:Combined award boxes|displayAwardBoxes|game=Dialog}}
|-
{{#invoke:Combined award boxes|displayAwardBoxes|game=Chlorophyll}}
|}

local p = {}
local cargo = mw.ext.cargo

function p.displayAwardBoxes(frame, game)
	local gf = require('Module:Game functions')
	output = {}

	-- Needed if invoked from wikitext
	if type(echo) ~= "function" then
		function echo(text)
			table.insert(output, text)
		end
	end
	if not game or game=='' then
		if frame.args['game'] then
			game = frame.args['game']
		else 
			return ''
		end
	end
	
	-- Get Events data from the Events and Awards tables
	local cargo = mw.ext.cargo
    local tables = 'Events, Awards'
    local fields = 'Events._pageName=Event, Part_of_series, Total, Award_name, Notes'
    local args = {
    	join = 'Events._pageID=Awards._pageID',
    	where = 'Game="' .. game .. '"'
    }
    local events = cargo.query( tables, fields, args )
    
	function add(ribbonName)
		-- Function for grouping awards by Event and Ribbon
	    awardRibbons[event] = awardRibbons[event] or {} 
	    awardRibbons[event][ribbonName] = awardRibbons[event][ribbonName] or '' 
	    if awardRibbons[event][ribbonName] ~= '' then
	        awardRibbons[event][ribbonName] = awardRibbons[event][ribbonName] .. ', '
	    end
	    
        -- Turn 1 into 1st etc
		if award:match("^[0-9]+$") ~= nil then
			award = gf.ordinalNumber(award)
		end
		-- Turn 1st into 1st of total
		if award:match("^[0-9]+%a%a$") ~= nil and total and total~='' then
			award = award .. ' of ' .. total
		end

	    awardRibbons[event][ribbonName] = awardRibbons[event][ribbonName] .. award
	end

	-- Only show infobox event/award row if needed
	if events and #events>0 then
	
		-- Associate ribbon types with filenames
		compIcon = {
		    ["IFComp blue"] = "blue IF comp ribbon",
		    ["IFComp red"] = "red IF comp ribbon",
		    ["IFComp white"] = "white IF comp ribbon",
		    ["IFComp green"] = "green IF comp ribbon",
		    ["XYZZY winner"] = "trophy small",
		    ["XYZZY finalist"] = "red comp ribbon",
		    ["Blue"] = "blue comp ribbon",
		    ["Red"] = "red comp ribbon",
		    ["White"] = "white comp ribbon",
		    ["Green"] = "green comp ribbon"
		}    
		awardRibbons = {}
	    
		-- Start infobox row
		echo('<tr><th>Events</th><td>')
	
		-- Group similar Awards together, by Event and by Ribbon
		-- Ideally would order too
		if #events~=0 then
			for e = 1, #events do
		        event = events[e]['Event']
		        award = events[e]['Award_name']
		        total = events[e]['Total']
	        	if event:sub(1,6)=='IFComp' then
	        		-- IFComp
		        	if award=='1' or award=='1st' then
		        		add('IFComp blue')
		        	elseif award=='2' or award=='2nd' then
		        		add('IFComp red')
		        	elseif award=='3' or award=='3rd' then
		        		add('IFComp white')
		        	else
		        		add('IFComp green')
		        	end
		        elseif event:sub(1,5)=='XYZZY' then
		        	-- XYZZY
		        	if award:sub(1,6)=='Winner' then
						add('XYZZY winner')
		        	else 
						add('XYZZY finalist')
		        	end
				else		
					-- Everything else
		        	if string.match(award, "^1[^%d]") --Begins with 1 (e.g. "1st" but not "10th")
		        	or award=='1'
		        	or award:sub(1,4)=='Best' 
		        	or award:sub(1,6)=='Winner'
		        	or award=='Commended'
		        	or award=='Audience Choice' 
		        	or award=="Alumni's Choice" 
		        	then
		        		add('Blue')
		        	elseif string.match(award, "^2[^%d]") 
		        	or award=='2'
		        	or award:sub(1,8)=='Finalist'
		        	then
		         		add('Red')
		    		elseif string.match(award, "^3[^%d]") 
		        	or award=='3'
		        	then
		         		add('White')
		       		else
		        		add('Green')
		        	end
		        end
			end --for
		end --if #events	        
	
		-- Display the combined Award boxes
		for event, ribbons in pairs(awardRibbons) do
			for ribbon, awards in pairs (ribbons) do
				if event:sub(1,5)=='XYZZY' then
					if ribbon=='XYZZY winner' then
						awards = 'Winner: ' .. awards.gsub(awards, "Winner: ", "") 
					else 
						awards = 'Finalist: ' .. awards.gsub(awards, "Finalist: ", "") 
					end
				end
				echo(frame:expandTemplate{ title='Award box', 
			    	args={ icon=compIcon[ribbon], event='[['..event..']]', comment=awards } } )
			end
		end
		echo "</td></tr>"
	end

    output = table.concat(output)
    return output
end

return p