Jump to content

Module:SportsRankings

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jts1882 (talk | contribs) at 10:51, 7 November 2018 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals');

local p = {} 

local error_msg = '<span style=\"font-size:100%\" class=\"error\"><code style=\"color:inherit; border:inherit; padding:inherit;\">&#124;_template=</code> missing or empty</span>';

-- data for various rankings held in module subpages, e.g. "Module:SportsRankings/data/FIFA World Rankings"
local data = {}      --[[ parameters containing data help in three tables
						data.source = {}     -- parameters for using in cite web (title, url, website)
						data.updated = {}    -- date of latest update (month, day, year)
						data.rankings = {}   -- the rankings list (country code, ranking, movement)
					    data.alias = {}      -- alias list (country code, country name [=key])
					    
					--]]

local  templateArgs = {} -- contains arguments from template involking module


local function getArgs(frame)
	local parents = mw.getCurrentFrame():getParent()
		
	for k,v in pairs(parents.args) do
		--check content
		if v and v ~= "" then
			templateArgs[k]=v --parents.args[k]
		end
	end

end

local function loadData(frame)
    
    local source = frame.args[1] -- source of rankings e.g. FIFA World Rankings
    data = require('Module:SportsRankings/data/'.. source);
    
end

local function getDate()
   
   if templateArgs['mdy'] and templateArgs['mdy'] ~= "" then
   	   return data.updated['month'] .. " " .. data.updated['day'] .. ", " .. data.updated['year']
   else
   	   return data.updated['day'] .. " " .. data.updated['month'] .. " " .. data.updated['year']
   end
end

local function addCiteWeb(frame)  -- use cite web template
	
	return frame:expandTemplate{ title = 'cite web' , args = {
    		url = data.source['url'],            --"https://www.fifa.com/fifa-world-ranking/ranking-table/men/index.html", 
			title = data.source['title'],        -- "The FIFA/Coca-Cola World Ranking",
			website = data.source['website'],    --"FIFA",
			['date'] = getDate(),
			['access-date'] = getDate()
			}}
end
local function addReference(frame)
	
	local text = ""
	if data.source['text'] then text = data.source['text'] end
	
	return frame:expandTemplate{ title = 'refn' , args = {
		name=frame.args[1],                                 --ranking used, e.g. "FIFA World Rankings",
	    text .. addCiteWeb(frame)
	}}

end

--[[ the main function returning rankings 
]]
function p.main(frame)
	
    getArgs(frame) -- returns args table having checked for content
    loadData(frame)
    local outputString = ""
    local country = templateArgs[1] -- country name or county code passed as parameter
    local rank, move
    for _,u in pairs(data.alias) do
    	if u[1]==country then 
       		country = u[2]    -- if alias (country code) then use country name as key
       		break
       	end
    end    
    for _,v in pairs(data.rankings) do
    	if v[1]==country then 
       		rank = v[2]    -- get rank
       		move = v[3]    -- get move from last ranking
       		break
       	end
    end
    if not rank then rank = 'NR' end
	
	if rank ~= 'NR' then
		outputString = outputString .. ' ' .. rank .. ' '
	    if move == 0 then                                    -- if no change in ranking
	    	outputString = outputString .. frame:expandTemplate{ title = 'steady' } 
	    elseif move < 0 then                                 --  if ranking down
	    	outputString = outputString .. frame:expandTemplate{ title = 'decrease' } .. ' ' .. math.abs(move)
	    elseif move > 0 then                                 -- if ranking up
	    	outputString = outputString .. frame:expandTemplate{ title = 'increase' } .. ' ' .. move
	    end	
    else
    	outputString = outputString .. frame:expandTemplate{ title = 'Abbr', args = { "NR", "Not ranked"}  }
    	--	{{Abbr|NR|Not ranked}} 
	end
	outputString = outputString .. ' <small>(' .. getDate() .. ')</small>'
	outputString = outputString .. addReference(frame)
    
    return outputString
	
end
local function table(frame, ranking, first,last)

    local styleString = ""
    if templateArgs['style'] and templateArgs['style'] ~= "" then styleString = templateArgs['style'] end
    


    local outputString = '{| class="wikitable" style="text-align:center;' .. styleString .. '"'
    outputString = outputString ..	'\n|+' .. ranking .. ' as of ' .. getDate() .. '.' .. addReference(frame)
    outputString = outputString ..	'\n|-\n!Rank\n!Change\n!Team\n!Points '
    local change,code = '', ''
    --while i<last do 
    for k,v in pairs(data.rankings) do
	
	   if v[2] >= first and v[2] <= last then 
		   outputString = outputString .. '\n|-\n|' .. v[2]  -- rank
		   local move = v[3]
		   if move == 0 then                                    -- if no change in ranking
		    	change = frame:expandTemplate{ title = 'steady' } 
		    elseif move < 0 then                                 --  if ranking down
		    	change = frame:expandTemplate{ title = 'decrease' } .. ' ' .. math.abs(move)
		    elseif move > 0 then                                 -- if ranking up
		    	change = frame:expandTemplate{ title = 'increase' } .. ' ' .. move
		    end	
		   outputString = outputString .. '||' .. change
		   
		   for _,u in pairs(data.alias) do
		    	if u[2]==v[1] then 
		       		code = u[1]    -- if alias (country code) then use country name as key
		       		break
		       	end
		    end   
		   
		   outputString = outputString .. '\n|style="text-align:left"|' .. frame:expandTemplate{ title = 'fb', args = {code} }    -- country
		   outputString = outputString .. '||' .. v[4]       -- country for now, later points
	   end
	end
    outputString = outputString .. "\n|}"

    return outputString
	
end

function p.list(frame)

    getArgs(frame) -- returns args table having checked for content
    loadData(frame)	
    local ranking = frame.args[1]
    local first, last = 1,10
    first = tonumber(frame.args['2'])
    last = tonumber(frame.args['3'])
    
    return table(frame, ranking, first,last)
end
function p.list2(frame)

    getArgs(frame) -- returns args table having checked for content
    loadData(frame)	
    local ranking = frame.args[1]
    local first, last = 1,10
    local country = frame.args[2]
    
    if string.len(country) == 3 then  -- if three letter country code
    		for _,u in pairs(data.alias) do
		    	if u[1]==country then 
		       		country = u[2]    -- if country code then use country name 
		       		break
		       	end
		    end   
    end
    for k,v in pairs(data.rankings) do
       if v[1] == country then
       	  first = v[2]-frame.args[3]
       	  last = v[2]+frame.args[3]
       end
    end
    
    return table(frame, ranking, first,last)
end

return p