יחידה:הצבעת אירוויזיון

מתוך ויקיפדיה, האנציקלופדיה החופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:הצבעת אירוויזיון/תיעוד

local p = {}

local flagIcons = {
    ['אוסטריה'] = 'Flag of Austria.svg',
    ['אוסטרליה'] = 'Flag of Australia.svg',
    ['אוקראינה'] = 'Flag of Ukraine.svg',
    ['אזרבייג\'ן'] = 'Flag of Azerbaijan.svg',
    ['איטליה'] = 'Flag of Italy.svg',
    ['איסלנד'] = 'Flag of Iceland.svg',
    ['אירלנד'] = 'Flag of Ireland.svg',
    ['אלבניה'] = 'Flag of Albania.svg',
    ['אסטוניה'] = 'Flag of Estonia.svg',
    ['ארמניה'] = 'Flag of Armenia.svg',
    ['בלגיה'] = 'Flag of Belgium (civil).svg',
    ['גאורגיה'] = 'Flag of Georgia.svg',
    ['גרמניה'] = 'Flag of Germany.svg',
    ['דנמרק'] = 'Flag of Denmark.svg',
    ['הולנד'] = 'Flag of the Netherlands.svg',
    ['הממלכה המאוחדת'] = 'Flag of the United Kingdom.svg',
    ['יוון'] = 'Flag of Greece.svg',
    ['ישראל'] = 'Flag of Israel.svg',
    ['לוקסמבורג'] = 'Flag of Luxembourg.svg',
    ['לטביה'] = 'Flag of Latvia.svg',
    ['ליטא'] = 'Flag of Lithuania.svg',
    ['מולדובה'] = 'Flag of Moldova.svg',
    ['מלטה'] = 'Flag of Malta.svg',
    ['נורווגיה'] = 'Flag of Norway.svg',
    ['סלובניה'] = 'Flag of Slovenia.svg',
    ['סן מרינו'] = 'Flag of San Marino.svg',
    ['ספרד'] = 'Flag of Spain.svg',
    ['סרביה'] = 'Flag of Serbia.svg',
    ['פולין'] = 'Flag of Poland.svg',
    ['פורטוגל'] = 'Flag of Portugal.svg',
    ['פינלנד'] = 'Flag of Finland.svg',
    [\'כיה'] = 'Flag of the Czech Republic.svg',
    ['צרפת'] = 'Flag of France (1794–1815, 1830–1974, 2020–present).svg',
    ['קפריסין'] = 'Flag of Cyprus.svg',
    ['קרואטיה'] = 'Flag of Croatia.svg',
    ['רומניה'] = 'Flag of Romania.svg',
    ['שוודיה'] = 'Flag of Sweden.svg',
    ['שווייץ'] = 'Flag of Switzerland.svg',
    ['אנדורה'] = 'Flag of Andorra.svg',
    ['בולגריה'] = 'Flag of Bulgaria.svg',
    ['בוסניה והרצגובינה'] = 'Flag of Bosnia and Herzegovina.svg',
    ['הונגריה'] = 'Flag of Hungary.svg',
    ['טורקיה'] = 'Flag of Turkey.svg',
    ['מונטנגרו'] = 'Flag of Montenegro.svg',
    ['מונקו'] = 'Flag of Monaco.svg',
    ['מקדוניה הצפונית'] = 'Flag of North Macedonia.svg',
    ['מרוקו'] = 'Flag of Morocco.svg',
    ['סלובקיה'] = 'Flag of Slovakia.svg',
    ['בלארוס'] = 'Flag of Belarus.svg',
    ['רוסיה'] = 'Flag of Russia.svg',
    ['יוגוסלביה'] = 'Flag of Yugoslavia (1946–1992).svg',
    ['סרביה ומונטנגרו'] = 'Flag of Serbia and Montenegro.svg',
    ['העולם'] = 'Earth Day Flag 2.png'
}

local background = {
	['1'] = '',
	['2'] = '',
	['3'] = '',
	['4'] = '',
	['5'] = '',
	['6'] = '',
	['7'] = '',
	['8'] = '',
	['9'] = '',
	['10'] = '',
	['12'] = ''
}

local function UsingFlagTemplate(country_code)
	if (country_code == 'העולם') then
		return "[[File:" .. flagIcons[country_code] .. "|22x16px]] " .. country_code
	else
    	return "[[File:" .. flagIcons[country_code] .. "|22x16px]] [[" .. country_code .. " באירוויזיון|" .. country_code .. "]]"
    end
end

-- Split the comma-separated lists into tables and sort them
local function splitString(inputstr, sep)
    local sep, fields = sep or ",", {}
    local pattern = string.format("([^%s]+)", sep)
    inputstr:gsub(pattern, function(c) fields[#fields + 1] = c end)
    return fields
end

local function trim(s)
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local function stringToBoolean(str)
	if str == "true" then
	    return true
	elseif str == "false" then
	    return false
	else
	    -- Handle invalid input
	    return nil
	end
end

-- Define the function to generate the points awarded table
function p.generatePointsTable(frame)
	year = tonumber(frame.args.year) or nil
	title = frame.args.title or nil
	reference = frame.args.reference or nil
	enableSum = stringToBoolean(frame.args.enableSum) or nil
    one = frame.args.one or ""
    two = frame.args.two or ""
    three = frame.args.three or ""
    four = frame.args.four or ""
    five = frame.args.five or ""
    six = frame.args.six or ""
    seven = frame.args.seven or ""
    eight = frame.args.eight or ""
    nine = frame.args.nine or ""
    ten = frame.args.ten or ""
    twelve = frame.args.twelve or ""
    
    local one_countries = splitString(one, ",")
    local two_countries = splitString(two, ",")
    local three_countries = splitString(three, ",")
    local four_countries = splitString(four, ",")
    local five_countries = splitString(five, ",")
    local six_countries = splitString(six, ",")
    local seven_countries = splitString(seven, ",")
    local eight_countries = splitString(eight, ",")
    local nine_countries = splitString(nine, ",")
    local ten_countries = splitString(ten, ",")
    local twelve_countries = splitString(twelve, ",")
    
    -- Remove leading and trailing spaces from each country code
    for i, country in ipairs(one_countries) do one_countries[i] = trim(country) end
    for i, country in ipairs(two_countries) do two_countries[i] = trim(country) end
    for i, country in ipairs(three_countries) do three_countries[i] = trim(country) end
    for i, country in ipairs(four_countries) do four_countries[i] = trim(country) end
    for i, country in ipairs(five_countries) do five_countries[i] = trim(country) end
    for i, country in ipairs(six_countries) do six_countries[i] = trim(country) end
    for i, country in ipairs(seven_countries) do seven_countries[i] = trim(country) end
    for i, country in ipairs(eight_countries) do eight_countries[i] = trim(country) end
    for i, country in ipairs(nine_countries) do nine_countries[i] = trim(country) end
    for i, country in ipairs(ten_countries) do ten_countries[i] = trim(country) end
    for i, country in ipairs(twelve_countries) do twelve_countries[i] = trim(country) end
    
    -- Sort the points and their corresponding countries
    local points_sorted
    local points_countries
    if year >= 1975 then
    	points_sorted = {"12", "10", "8", "7", "6", "5", "4", "3", "2", "1"}
    	points_countries = {
	        ["1"] = one_countries,
	        ["2"] = two_countries,
	        ["3"] = three_countries,
	        ["4"] = four_countries,
	        ["5"] = five_countries,
	        ["6"] = six_countries,
	        ["7"] = seven_countries,
	        ["8"] = eight_countries,
	        ["10"] = ten_countries,
	        ["12"] = twelve_countries
    	}
    	background['8'] = '#CC9966'
    	background['10'] = 'silver'
    	background['12'] = 'gold'
    else
    	if year == 1974 or ( year <= 1970 and year >= 1967 ) or ( year <= 1961 and year >= 1956 ) then
	    	points_sorted = {"10", "9", "8", "7", "6", "5", "4", "3", "2", "1"}
	    	points_countries = {
		        ["1"] = one_countries,
		        ["2"] = two_countries,
		        ["3"] = three_countries,
		        ["4"] = four_countries,
		        ["5"] = five_countries,
		        ["6"] = six_countries,
		        ["7"] = seven_countries,
		        ["8"] = eight_countries,
		        ["9"] = nine_countries,
		        ["10"] = ten_countries
	    	}
	    	background['8'] = '#CC9966'
	    	background['9'] = 'silver'
	    	background['10'] = 'gold'
    	elseif year <= 1973 and year >= 1971 then
			points_sorted = {"10", "9", "8", "7", "6", "5", "4", "3", "2"}
	    	points_countries = {
		        ["2"] = two_countries,
		        ["3"] = three_countries,
		        ["4"] = four_countries,
		        ["5"] = five_countries,
		        ["6"] = six_countries,
		        ["7"] = seven_countries,
		        ["8"] = eight_countries,
		        ["9"] = nine_countries,
		        ["10"] = ten_countries
	    	}
	    	background['8'] = '#CC9966'
	    	background['9'] = 'silver'
	    	background['10'] = 'gold'
	    elseif year <= 1966 and year >= 1964 then
    		points_sorted = {"9", "6", "5", "3", "1"}
	    	points_countries = {
		        ["1"] = one_countries,
		        ["3"] = three_countries,
		        ["5"] = five_countries,
		        ["6"] = six_countries,
		        ["9"] = nine_countries,
	    	}
	    	-- first option (only three songs)
	    	background['1'] = '#CC9966'
	    	background['3'] = 'silver'
		    background['5'] = 'gold'
		    
		    -- second option (only two songs)
		    background['3'] = 'silver'
		    background['6'] = 'gold'
		    
		    -- third option (only one song)
		    background['9'] = 'gold'
    	elseif year == 1963 then
    		points_sorted = {"5", "4", "3", "2", "1"}
	    	points_countries = {
		        ["1"] = one_countries,
		        ["2"] = two_countries,
		        ["3"] = three_countries,
		        ["4"] = four_countries,
		        ["5"] = five_countries
	    	}
	    	background['3'] = '#CC9966'
	    	background['4'] = 'silver'
	    	background['5'] = 'gold'
	    elseif year == 1962 then
    		points_sorted = {"3", "2", "1"}
	    	points_countries = {
		        ["1"] = one_countries,
		        ["2"] = two_countries,
		        ["3"] = three_countries
	    	}
	    	background['1'] = '#CC9966'
	    	background['2'] = 'silver'
	    	background['3'] = 'gold'
    	end
    end
    -- Construct the table in the desired format
    local table_text = "{| class=\"wikitable\"\n"

    if enableSum then
    	table_text = table_text .. "|-\n!colspan=3|" .. title .. reference .. "\n"
    	table_text = table_text .. "|-\n! ציון !! מדינות !! סך הכל\n"
    else
    	table_text = table_text .. "|-\n!colspan=2|" .. title .. reference .. "\n"
    	table_text = table_text .. "|-\n! ציון !! מדינות\n"
    end
    
	local sum_points = 0
	local sum_countries = 0
	local wasROTW = 0
	
    for _, points in ipairs(points_sorted) do
        local countries = points_countries[points]
        local countries_formatted = ""
        local lsum = 0
        for _, country_code in ipairs(countries) do
        	sum_points = sum_points + points
        	if (country_code == 'העולם') then
        		wasROTW = 1
        	else
        		sum_countries = sum_countries + 1
        	end
        	
        	lsum = lsum + points
            countries_formatted = countries_formatted .. UsingFlagTemplate(country_code) .. ",&nbsp;"
        end
        
        countries_formatted = countries_formatted:sub(1, -8)  -- Remove the trailing comma and space
        table_text = table_text .. "|-\n| align=center bgcolor=" .. background[points] .. "| '''"
        
        if (points == "1") then
        	table_text = table_text .. "נקודה אחת'''"
        else
        	table_text = table_text .. points .. " נקודות'''"
        end
        
        table_text = table_text .. "\n| " .. countries_formatted
        
        if enableSum then
	        if (lsum == 1) then
	        	table_text = table_text .. " || " .. "נקודה אחת"
	        else
	        	table_text = table_text .. " || " .. lsum .. " נקודות"
	        end
        end
        table_text = table_text .. "\n"
    end
	
	local countries_text = sum_countries .. " מדינות"
	if (wasROTW == 1) then
		countries_text = countries_text .. ' ושאר העולם<sup>1</sup>'
	end
	table_text = table_text .. "|-\n! סך הכל || " .. countries_text
	
	if enableSum then
		if (sum_points == 1) then
			table_text = table_text .. " || " .. "נקודה אחת"
		else
			table_text = table_text .. " || " .. sum_points .. " נקודות"
		end
	end
	table_text = table_text .. "\n"
	
	if (wasROTW == 1) then
		table_text = table_text .. '|-\n|colspan=3| <sup>1</sup><small>החל משנת 2023 גם המדינות שלא משתתפות יכולות להצביע, והצבעותיהן נספרות כקבוצה אחת ששמה הוא "שאר העולם" (באנגלית: Rest of the world; בראשי תיבות: ROTW).</small>\n'
	end
	
    table_text = table_text .. "|}"
    
    return table_text
end

return p