Module:Compatibility status counter PS3

From PS4 Developer wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Compatibility status counter PS3/doc

-- This module counts table rows in wikitext 

local p = {}
local getArgs

function p.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	return p._main(getArgs(frame, {wrappers = 'Template:Table row counter'}))
end

function p._main(args)
	-- Get the title object
	local titleObj
	do
		local success
		success, titleObj = pcall(mw.title.new, args.page)
		if not success or not titleObj then
			titleObj = mw.title.getCurrentTitle()
		end
	end

	-- Get the page content
	local content = titleObj:getContent()
	if not content then
		return nil
	end

	-- Find the wikitables on that page
	local wikitables = {}
	do
		local iWikitable = 0
		local s1 = content:match('^({|.-\n|})')
		if s1 then
			iWikitable = iWikitable + 1
			wikitables[iWikitable] = s1
		end
		for s in content:gmatch('\n({|.-\n|})') do
			iWikitable = iWikitable + 1
			wikitables[iWikitable] = s
		end
	end

    local totalcount = 0
    local psnowcount = 0
    local unplayablecount = 0
    local majorcount = 0
    local minorcount = 0
    local playablecount = 0

    -- Iterate all tables on the calling page
    for i, wikitable in ipairs(wikitables) do
        -- Count rows depending on playable status
        for row in wikitable:gmatch("|%-%s*\n(.-)\n") do
            row = string.lower(row)
            totalcount = totalcount + 1
            if row:find('{{playstationnow}}') then
                psnowcount = psnowcount + 1
            elseif row:find('<center>PlayStation Now</center>') then
                psnowcount = psnowcount + 1
            elseif row:find('{{unplayable}}') then
                unplayablecount = unplayablecount + 1
            elseif row:find('{{majorissues}}') then
                majorcount = majorcount + 1
            elseif row:find('{{minorissues}}') then
                minorcount = minorcount  + 1
            elseif row:find('<center>Minor Issues</center>') then
                minorcount = minorcount  + 1
            elseif row:find('{{playable}}') then
                playablecount = playablecount + 1
            else
                totalcount = totalcount - 1
            end
        end
    end

    return '<span style="font-size:85%"><div align="right"><b>Total Games = ' .. totalcount .. ':</b> <i>' .. playablecount .. ' Games are "Playable" <span style="font-weight:bold;">&middot;</span> ' .. minorcount .. ' Games have "Minor Issues" <span style="font-weight:bold;">&middot;</span> ' .. majorcount .. ' Games have "Major Issues" <span style="font-weight:bold;">&middot;</span> ' .. unplayablecount .. ' Games are "Unplayable" <span style="font-weight:bold;">&middot;</span> ' .. psnowcount .. ' Games are available through [[PlayStation Now]]</i></div></span>'

end

return p