Promotion_Interval = 0.1 // Time in hours
Warning_Penalty = 0 // Time in minutes 

// Place the groups you want promoted here, in decending order(Highest group to Lowest)
groups_to_promote = { 
 [1] = "superadmin",
 [1] = "admin",
 [1] = "moderator",
 [1] = "colonel",
 [2] = "lieutenantcolonel",
 [3] = "major", 
 [4] = "captain",
 [5] = "firstlieutenant", 
 [6] = "secondlieutenant",
 [7] = "sergeantmajor", 
 [8] = "firstsergeant", 
 [9] = "gunnerysergeant", 
 [10] = "staffsergeant", 
 [11] = "sergeant", 
 [12] = "corporal", 
 [13] = "lancecorporal", 
 [14] = "privatefirstclass", 
 [15] = "user" }

// Place what you want each group promoted to
promote_to = { 
["user"] = "privatefirstclass", 
["privatefirstclass"] = "lancecorporal", 
["lancecorporal"] = "corporal", 
["corporal"] = "sergeant", 
["sergeant"] = "staffsergeant", 
["staffsergeant"] = "gunnerysergeant", 
["gunnerysergeant"] = "firstsergeant", 
["firstsergeant"] = "sergeantmajor", 
["sergeantmajor"] = "secondlieutenant", 
["secondlieutenant"] = "firstlieutenant", 
["firstlieutenant"] = "captain", 
["captain"] = "major", 
["major"] = "lieutenantcolonel", 
["lieutenantcolonel"] = "colonel", 
["colonel"] = "HighestLevel" }

// Do you have Umotd?
HasUmotd = true




//===================================END OF CONFIGURATION====================================//

players = { ["players"] = {}}
player_promotions = {}
// Create the file if it doesn't exist
if SERVER then
	if !file.Exists("player_times.txt") then
		players = { players = {}} 
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	else
		players = ULib.parseKeyValues ( file.Read("player_times.txt"))
	end
	if !file.Exists("player_promotions.txt") then
		player_promotions = { promotions = {} }
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	else
		player_promotions = ULib.parseKeyValues ( file.Read("player_promotions.txt"))
		for k,v in pairs( player_promotions.promotions ) do
			player_promotions.promotions[tonumber(k)]=v
		end
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	end	
end
//timer.Create( "UpdatePromotionsFile", 60, 0, Promotions_Update )
function promotions_init( ply )
	if !(tobool(players.players[ply:Nick()])) then 
		players.players[ply:Nick()] = { time = 0, promote = "none", lvl = 1 } 
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	end
		timer.Create(ply:Nick().. "GroupCheck", 1, 0, CheckGroup, ply)
		timer.Create(ply:Nick().. "PromotionTimer", 60, 0, SetTime, ply)
		for k, v in ipairs( groups_to_promote ) do
			if ply:IsUserGroup( v ) then
			ply.grouplvl = 0
			ply.promote = promote_to[v]
			file.Write( "player_times.txt", ULib.makeKeyValues(players))
			return
			end
		end
end
hook.Add( "PlayerInitialSpawn", "Promotions_Init", promotions_init)

function SetTime( ply )
	players.players[ply:Nick()].promote = ply.promote 
	players.players[ply:Nick()].lvl = ply.grouplvl
	if ply.warn == 0 then
		players.players[ply:Nick()].time = players.players[ply:Nick()].time + 1
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
		if (players.players[ply:Nick()].time > (Promotion_Interval * 60 * (((table.maxn(groups_to_promote)) - ply.grouplvl + 1)))) then
			Promote( ply )
		end
	else
		if ply.warn >= 5 then
			if !(players.players[ply:Nick()].time - (ply.warn * 10) < 0) then
				players.players[ply:Nick()].time = players.players[ply:Nick()].time - (ply.warn * 10)
				file.Write( "player_times.txt", ULib.makeKeyValues(players))
			else
				players.players[ply:Nick()].time = 0
				file.Write( "player_times.txt", ULib.makeKeyValues(players))
			end
		end
	end
end
function Promote( ply )
	if !(ply.grouplvl == 1) then
		players.players[ply:Nick()].time = 0
		//local topromote = string.Explode( " ", ply:Nick())
		//local topromote = string.sub( topromote[1], -math.abs((string.len( topromote[1] )/2)))
		local topromote = string.format( "%q", ply:Nick() )
		Msg( "ulx adduser " ..topromote.. " " ..players.players[ply:Nick()].promote.. "\n" )
//		ULib.ucl.addUser(ply:Nick(), "steamid", ply:SteamID(), ply.promotegroup)
		game.ConsoleCommand( "ulx adduserid " ..topromote.. " " ..players.players[ply:Nick()].promote.. " " ..ply:SteamID().. "\n")
		game.ConsoleCommand( "ulx csay Player: " ..ply:Nick().. " has been promoted to " ..players.players[ply:Nick()].promote.. " for exceptional behaviour \n")
		table.insert( player_promotions.promotions,"<li> Player: " ..ply:Nick().. " has been promoted to " ..players.players[ply:Nick()].promote.. " for exceptional behaviour" )
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
		ply.grouplvl = ply.grouplvl - 1
		ply.promote = promote_to[groups_to_promote[ply.grouplvl]]
		players.players[ply:Nick()].promote = ply.promote
		players.players[ply:Nick()].lvl = ply.grouplvl
		file.Write( "player_times.txt", ULib.makeKeyValues(players))
	end
end

function RemoveTimers( ply )
	if timer.IsTimer( ply:Nick().. "PromotionTimer" ) then
		timer.Destroy( ply:Nick().. "PromotionTimer" )
	end
	if timer.IsTimer( ply:Nick().. "GroupCheck" ) then
		timer.Destroy( ply:Nick().. "GroupCheck" )
	end
end
hook.Add( "PlayerDisconnected", "RemoveTimersHook", RemoveTimers )
function Player_Promotes()	
	local LimitTable = table.Copy(player_promotions)
	while table.Count(LimitTable.promotions) > 3 do
		table.remove( LimitTable.promotions, 1)
	end
	if table.Count(LimitTable.promotions) <= 3 then
		promotesout = table.concat( LimitTable.promotions, " ")
		if string.find( promotesout, "<ol>", 1, true) then
			string.gsub( promotesout, "<ol>", " ")
			file.Write( "latestpromotes.txt", "<ol> " ..promotesout.. " </ol>")
			return promotesout
		else
			file.Write( "latestpromotes.txt", "<ol> " ..promotesout.. " </ol>")
			return promotesout
		end
	end
end
timer.Create("Player_Promotes_Timer", 1, 0, Player_Promotes)

function CheckGroup( ply )
	for k, v in ipairs(groups_to_promote) do
		if ply:IsUserGroup( v ) then
		ply.grouplvl = k
		ply.promote = promote_to[v]
		return
		end
	end
end	

if file.Exists( "Umotd/*.*" ) or HasUmotd then
	Umotd_dynamic_variables["%%promotions%%"] = function() return Player_Promotes() end
else
	Msg("You should get Umotd, it's Great! \n")
end	

function Promotions_Update()
	if !file.Exists("player_promotions.txt") then
		player_promotions = { promotions = {} }
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	else
		player_promotions = ULib.parseKeyValues ( file.Read("player_promotions.txt"))
		file.Write( "player_promotions.txt", ULib.makeKeyValues(player_promotions))
	end
end



		



