-- functions


local function XPForLevel(level)
	return math.floor(300 + (level/100)^4 * 300)
end

local function UpdateLevels(player)
	local playerData	= PLAYER_DATA:FindFirstChild(player.Name)
	if playerData then
		local ranking		= playerData.Ranking
		local battlePass	= playerData.BattlePass
		if ranking.CurrentSeason.Value == CURRENT_SEASON then
			-- update level from xp
			local lastLevel	= ranking.Level.Value
			repeat
				local requiredXP	= XPForLevel(ranking.Level.Value + 1)
				if ranking.LevelXP.Value >= requiredXP then
					ranking.LevelXP.Value	= ranking.LevelXP.Value - requiredXP
					ranking.Level.Value		= ranking.Level.Value + 1
					script.Parent.BattlePassScript.LevelUp:Fire(player, ranking.Level.Value)
				end
			until ranking.LevelXP.Value < requiredXP
			
			if ranking.Level.Value > lastLevel then
				REMOTES.LevelUp:FireClient(player)
			end
		else
			-- transfer level to rank
			ranking.Rank.Value	= ranking.Rank.Value + ranking.Level.Value
			-- reset level and level xp
			ranking.Level.Value		= 0
			ranking.LevelXP.Value	= 0
			-- reset xp boost
			battlePass.XPBoost.Value	= 0
			-- update to current season
			ranking.CurrentSeason.Value	= CURRENT_SEASON
		end
	end
end

local function SkipTier(player)
	local playerData	= PLAYER_DATA:FindFirstChild(player.Name)
	if playerData then
		if playerData.Ranking.Level.Value < 100 then
			playerData.Ranking.Level.Value	= playerData.Ranking.Level.Value + 1
			script.Parent.BattlePassScript.LevelUp:Fire(player, playerData.Ranking.Level.Value)
			REMOTES.LevelUp:FireClient(player)
			return true
		end
	end
	return false
end
