//ayo thanks Xian
if GetNumberList == nil then
	rawset(_G, "GetNumberList",
	--From CobaltBW. Thank you my dude!
	--This is used to turn a level header variable into a table
	--via detecting a "," as a separator.
	---@param str string
	function(str)
		local t = {}
		while str do
			local sep = string.find(str,'%,')
			if sep != nil then
				local arg = string.sub(str,0,sep-1)
				local tag = tonumber(arg)
				if tag then
					table.insert(t, tag)
				elseif tag != 0 then
					print('Invalid argument '..arg)
					break
				end
				str = string.sub($,sep+1)
			else
				local tag = tonumber(str)
				if tag then
					table.insert(t, tag)
				end
				break
			end
		end
		return t
	end)
end

addHook("PlayerSpawn", function(player)
	player.scorerank = player.score
	player.finalscore = 0
	player.rank = "E" --reset all our stuff
end)

addHook("PlayerThink", function(p)
	if p.exiting then --time to calculate bonuses
		local tb = p.realtime / TICRATE
		local tb2 = 0
		local gb2 = 0
		local rb = p.rings
		local rb2 = 0
		if mapheaderinfo[gamemap].timerankings != nil then --if our map supports it
			local tr = GetNumberList(mapheaderinfo[gamemap].timerankings) --grab level specific time bonuses
			if tb < tr[1] then --Max Time bonus
				tb2 = 7500
			elseif tb < tr[2] then
				tb2 = 5000
			elseif tb < tr[3] then
				tb2 = 2500
			elseif tb < tr[3] then
				tb2 = 1000
			elseif tb < tr[4] then
				tb2 = 500
			elseif tb < tr[5] then
				tb2 = 250
			elseif tb < tr[6] then
				tb2 = 100
			else tb2 = 0 end --no time bonus				
		else --our map doesn't support it, grab a default value instead
			if (tb <  30) then /*   :30 */ tb2 = 7500
			elseif (tb <  60) then /*  1:00 */ tb2 = 5000
			elseif (tb <  90) then /*  1:30 */ tb2 = 2500
			elseif (tb < 120) then /*  2:00 */ tb2 = 1000
			elseif (tb < 180) then /*  3:00 */ tb2 = 500
			elseif (tb < 240) then /*  4:00 */ tb2 = 250
			elseif (tb < 300) then /*  5:00 */ tb2 = 100
			else  /* TIME TAKEN: TOO LONG */ tb2 = 0 end
		end
		if p.timeshit == 0 then --how many times did we take damage this run
			gb2 = 7500
		elseif p.timeshit == 1 then
			gb2 = 2500
		elseif p.timeshit == 2 then
			gb2 = 1000
		elseif p.timeshit == 3 then
			gb2 = 500
		else
			gb2 = 0
		end
		if rb <= 500 then
			rb2 = rb * 25
		else
			rb2 = 5000
		end
		p.finalscore = (p.score - p.scorerank) + (tb2 + gb2 + rb2) --calculate our final rank score
		if mapheaderinfo[gamemap].levelrankings != nil then --if our level supports it
			local lr = GetNumberList(mapheaderinfo[gamemap].levelrankings)
			if p.finalscore > lr[1] then
				p.rank = "S"
			elseif p.finalscore > lr[2] then
				p.rank = "A"
			elseif p.finalscore > lr[3] then
				p.rank = "B"
			elseif p.finalscore > lr[4] then
				p.rank = "C"
			elseif p.finalscore > lr[5] then
				p.rank = "D"
			else
				p.rank = "E"
			end
		else --grab the default values instead
			if p.finalscore >= 50000 then
				p.rank = "S"
			elseif p.finalscore >= 40000 then
				p.rank = "A"
			elseif p.finalscore >= 30000 then
				p.rank = "B"
			elseif p.finalscore >= 20000 then
				p.rank = "C"
			elseif p.finalscore >= 10000 then
				p.rank = "D"
			else
				p.rank = "E"
			end
		end
	end
end)

local function ranking(v, player) 
	local player = consoleplayer
	local rank = player.rank
	if player.rank == "E" then
		rank = "E"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_BLACK, SKINCOLOR_BLACK)
	elseif player.rank == "D" then
		rank = "D"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_WHITE, SKINCOLOR_WHITE)
    elseif player.rank == "C"
		rank = "C"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_YELLOW, SKINCOLOR_YELLOW)
	elseif player.rank == "B"
		rank = "B"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_GREEN, SKINCOLOR_GREEN)
	elseif player.rank == "A"
		rank = "A"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_RED, SKINCOLOR_RED)
	elseif player.rank == "S"
		rank = "S"
		v.drawNameTag(270, 80, ""+rank+"", SKINCOLOR_BLUE, SKINCOLOR_BLUE)
	end
end

hud.add(ranking,"intermission")

COM_AddCommand("test", function(player)
	CONS_Printf(player, ""+rank+"")
end)