Module:Sandbox/Vozul

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
require('strict')

local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local americanFootballPositions = require('Module:American football positions')
local collegeFootballConference = require('Module:College football conference')

local function hsvToHex(h, s, v)
    local c = v * s
    local x = c * (1 - math.abs((h/60) % 2 - 1))
    local m = v - c
    
    local rgb
    if h < 60 then rgb = {r=c, g=x, b=0}
    elseif h < 120 then rgb = {r=x, g=c, b=0}
    elseif h < 180 then rgb = {r=0, g=c, b=x}
    elseif h < 240 then rgb = {r=0, g=x, b=c}
    elseif h < 300 then rgb = {r=x, g=0, b=c}
    else rgb = {r=c, g=0, b=x}
    end
    
    rgb = {r=(rgb.r + m) * 255, g=(rgb.g + m) * 255, b=(rgb.b + m) * 255}

    return string.format('%02x%02x%02x', rgb.r, rgb.g, rgb.b)
end

local function getRowRoundColor(round, numRounds)
    if not round then
        mw.addWarning('round not provided')
        return 'ffffff'
    elseif (round <= 0 or round > numRounds) then
        mw.addWarning('round should be positive and less than numberofrounds')
        return 'ffffff'
    end

    local hueSteps = 360.0/numRounds
    local hue = math.floor((round - 1) * hueSteps)
    return hsvToHex(hue, 1.0, 1.0)
end

function p.main(frame)
    local args = getArgs(frame, {
		wrappers = 'Template:NFLDraft-row',
		valueFunc = function(key, value)
			if key == 'numberofrounds' then
				if value == nil or value:find('^%s*$') ~= nil then return 7
				else return tonumber(value)
				end
			elseif key == 'dab' then
				if value ~= nil and value:find('^%s*$') ~= nil then return nil end
				return value
			elseif key == 'round' or key == 'picknum' then
				return tonumber(value)
            elseif key == 'playerpageexists' then
                return yesno(value) or true
			else
				return value
			end
		end
	})
	
    return p._main(args)
end

function p._main(args)
    local row = {'|-'}
    local rowHasDifferntBg = true

    if yesno(args.compensatory) then table.insert(row, 'bgcolor="#d9e9f9"')
    elseif yesno(args.supplemental) then table.insert(row, 'bgcolor="#ffdddd"')
    elseif yesno(args.expansion) then table.insert(row, 'bgcolor="#a9f5d0"')
    elseif yesno(args.resolution) then table.insert(row, 'bgcolor="#ccf9cc"')
    else rowHasDifferntBg = false
    end
    
    if not yesno(args.undrafted, false) then
        table.insert(row, '\n|')
        local numerofrounds = tonumber(args.numerofrounds)
        if not numerofrounds then
            numerofrounds = 7
        end

        table.insert(row, 'bgcolor="#')
        table.insert(row, getRowRoundColor(args.round, args.numberofrounds))
        table.insert(row, '"|&nbsp;')

        -- round column
        table.insert(row, '\n!scope=row style="text-align:center; font-weight:bold;')
        if rowHasDifferntBg then
            table.insert(row, ' background-color:unset;')
        end

        table.insert(row, '"') --closing quote for style

        if yesno(args.firstpickinround) then
            table.insert(row, 'id="Round_')
            table.insert(row, tostring(args.round))
        end

        table.insert(row, '|')
        table.insert(row, tostring(args.round))

        if yesno(args.compensatory) then table.insert(row, '*')
        elseif yesno(args.supplemental) then table.insert(row, '^"')
        elseif yesno(args.expansion) then table.insert(row, '¤')
        elseif yesno(args.resolution) then table.insert(row, '×"')
        end

        -- num pick column
        table.insert(row, '\n!scope=row style="text-align:center; font-weight:bold;')

        if rowHasDifferntBg then
            table.insert(row, 'background-color:unset;')
        end

        table.insert(row, '" id=Pick_')
        table.insert(row, tostring(args.picknum))
        table.insert(row, '|')

        if args.picknum == 1 then
            table.insert(row, '[[List of first overall National Football League Draft picks|1]]')
        elseif args.picknum == 2 then
            table.insert(row, '[[List of second overall National Football League Draft picks|2]]')
        elseif yesno(args.lastpick) then
            table.insert(row, '[[Mr. Irrelevant|')
            table.insert(row, tostring(args.picknum))
            table.insert(row, ']]')
        else
            table.insert(row, tostring(args.picknum))
        end
    end

 -- team column
    table.insert(row, '\n|[[')
    
    if args.draftyear then
        table.insert(row, tostring(args.draftyear))
        table.insert(row, ' ')

        if args.teamdab then
            table.insert(row, args.teamdab)
        else
            table.insert(row, args.team)
        end

        table.insert(row, ' season|')
    elseif args.teamdab then
        table.insert(row, args.teamdab)
        table.insert(row, '|')
    end

    table.insert(row, args.team)
    table.insert(row, ']]')

    -- Player column
    table.insert(row, '\n|')
    if args.last then
        if yesno(args.hof) then table.insert(row, 'bgcolor="#ffcc00" ')
        elseif yesno(args.probowl) then table.insert(row, 'bgcolor="#faecc8" ')
        end

        table.insert(row, 'data-sort-value="')
        table.insert(row, string.lower(args.last))
        table.insert(row, ', ')
        table.insert(row, string.lower(args.first))
        table.insert(row, '"|')

        if yesno(args.playerpageexists, true) then
            table.insert(row, '[[')

            if args.dab then
                table.insert(row, args.dab)
                table.insert(row, '|')
            end
        end

        table.insert(row, args.first)
        table.insert(row, ' ')
        table.insert(row, args.last)

        if yesno(args.playerpageexists, true) then
            table.insert(row, ']]')
        end

        if yesno(args.probowl) then table.insert(row, '<sup>†</sup>') end
        if yesno(args.hof) then table.insert(row, '<sup>‡</sup>') end
    else
        table.insert(row, '&nbsp;')
    end

    -- positon column
    table.insert(row, '\n| ')
    if args.position then
        table.insert(row, americanFootballPositions._main(args.position, false))
    
        if args.position2 then
            table.insert(row, '/')
            table.insert(row, americanFootballPositions._main(args.position2, false))
        end
    else
        table.insert(row, '&nbsp;')
    end

    table.insert(row, '\n|')

    -- college col
    if args.college then
        local collegeLink = args.collegelink
        if not args.collegelink then
            collegeLink = args.college
        end

        if yesno(args['cfb page exists']) then
            table.insert(row, '[[')
            table.insert(row, args.collegeyear)
            table.insert(row, ' ')
            table.insert(row, args.collegeteam)
            table.insert(row, ' football team|')
            table.insert(row, collegeLink)
            table.insert(row, ']]')
        else
            table.insert(row, mw.getCurrentFrame():expandTemplate{
                title='cfb link', args = {
                    year = args.collegeyear,
                    team = args.collegeteam,
                    school=args.college,
                    link=collegeLink
                }
            })
        end

        -- conference column
        table.insert(row, '\n|')
        if args.conference then
            table.insert(row, args.conference)
        else
            table.insert(row, collegeFootballConference._main(args.collegeyear, args.college))
        end
    end

    -- note column
    table.insert(row, '\n|')

    if args.note then
        table.insert(row, '<small>')
        table.insert(row, args.note)
        table.insert(row, '</small>')
    end

    return table.concat(row)
end

return p