if not SERVER then return end

--[[
CREDIT FOR THIS VERSION OF AUTOPROMOTE GOES TO:
HOLOGRAPHICpizza
Major_Pain
Smithy
Jay209015
PRETTY SELF EXLANATORY
HOURS  IS THE POINT OF WHERE THE PLAYER WILL GET PROMOTED TO THE GROUP
NAME IS THE NAME THAT WILL GET DISPLAYED WHEN SOMEONE GETS PROMOTED
]]
promotes = {
		{ hours = 1, group = "player", name = "Player" },
		{ hours = 2, group = "regular", name = "Regular" },
		{ hours = 5, group = "respected", name = "Respected" },
		{ hours = 10, group = "highrespected", name = "Highly Respected" },
		{ hours = 25, group = "silverplayer", name = "Silver Player" },
		{ hours = 50, group = "goldplayer", name = "Gold Player" }
}

---------IF A PLAYER IS A MEMBER OF THESE GROUPS THEY WONT GET PROMOTED.......EVER
excludes = { "donator", "moderator", "admin", "superadmin" }

---------Chat announcement settings, msgOn  - if true then it will announce promotions(false if you want it disabled), msgcol - color of the chat announcements
settings = {
		{ msgOn = true, msgcol = Color(255,225,0), effOn = true }
}

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------- DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING---------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
if settings[1].effOn then
	resource.AddFile( "sound/autopromote/promote.wav" )
end



function checkExcludes( ply )
	for id, eX in pairs (excludes) do
		if ply:IsUserGroup(eX) then
			return true
		end
	end
	return false
end

function hoursToPromote( group )
	for id,_ in ipairs( promotes ) do
		if group == promotes[id]["group"] then
			return promotes[id]["hours"]
		end
	end
end

function getPromoteName( group )
	for id,_ in ipairs( promotes ) do
		if group == promotes[id]["group"] then
			return promotes[id]["name"]
		end
	end
end
function groupSet( ply )
	ply.plhrs = math.floor((ply:GetUTime() + CurTime() - ply:GetUTimeStart())/60/60)
	ply.promote = promotes[1]["group"]
	for id,_ in ipairs(promotes) do
		if ply:IsUserGroup(promotes[id]["group"]) and id < table.Count(promotes) then
			ply.promote = promotes[id + 1]["group"]
		end
	end
	if not(ply:IsUserGroup(ply.promote)) and (ply.plhrs >= hoursToPromote( ply.promote )) then
		return true
	end
	return false
end

function promotePlayer( ply )
	if not checkExcludes( ply ) then
		if  groupSet( ply ) then
			if settings[1].msgOn then
				for ud, blank in pairs( player.GetAll()) do
					ULib.tsayColor(player.GetAll()[ud],false,settings[1].msgcol,ply:Nick().." has been promoted to '"..getPromoteName(ply.promote).."'.")
				end
			end
			if settings[1].effOn then
				effectS( ply )
				WorldSound( "autopromote/promote.wav", ply:GetPos( 0, 0, 0 ), 160, 100 )
			end
			ulx.adduser(nil, ply, ply.promote)
		end
	end
	if not timer.IsTimer("Promotion-" ..tostring(ply:SteamID())) then
		timer.Create("Promotion-" ..tostring(ply:SteamID()), 10, 0, promotePlayer, ply)
	end
end

function effectS( ply )
local ed = EffectData()
ed:SetEntity(ply)
util.Effect("autopromotion",ed, true, true)
local vPoint = ply:GetPos()
local effectdata = EffectData()
local r = math.random(20, 255)
local g = math.random(20, 255)
local b = math.random(20, 255)
effectdata:SetStart( Vector( r, g, b ) )
effectdata:SetOrigin( vPoint )
effectdata:SetScale( 1 )
util.Effect( "autopromotion2", effectdata )
timer.Simple( 1, function()
r = math.random(20, 255)
g = math.random(20, 255)
b = math.random(20, 255)
effectdata:SetStart( Vector( r, g, b ) )
local vPoint = ply:GetPos()
effectdata:SetOrigin( vPoint )
util.Effect( "autopromotion2", effectdata )
end)
timer.Simple( 2, function()
r = math.random(20, 255)
g = math.random(20, 255)
b = math.random(20, 255)
effectdata:SetStart( Vector( r, g, b ) )
local vPoint = ply:GetPos()
effectdata:SetOrigin( vPoint )
util.Effect( "autopromotion2", effectdata )
end)
timer.Simple( 3, function()
r = math.random(20, 255)
g = math.random(20, 255)
b = math.random(20, 255)
effectdata:SetStart( Vector( r, g, b ) )
local vPoint = ply:GetPos()
effectdata:SetOrigin( vPoint )
util.Effect( "autopromotion2", effectdata )
end)
timer.Simple( 4, function()
r = math.random(20, 255)
g = math.random(20, 255)
b = math.random(20, 255)
effectdata:SetStart( Vector( r, g, b ) )
local vPoint = ply:GetPos()
effectdata:SetOrigin( vPoint )
util.Effect( "autopromotion2", effectdata )
end)

end

function timerStart( ply )
	timer.Create("Promotion-" ..tostring(ply:SteamID()), 10, 0, promotePlayer, ply)
end
hook.Add( "PlayerInitialSpawn", "timerstarting", timerStart)

function destroyTimers(ply)
	if timer.IsTimer("Promotion-" ..tostring(ply:SteamID())) then
		timer.Stop("Promotion-" ..tostring(ply:SteamID()))
		timer.Destroy("Promotion-" ..tostring(ply:SteamID()))
	end
end
hook.Add( "PlayerDisconnected", "PromotionCleanUP", destroyTimers )