--------------------- Polite Notice ---------------------
-- YaLB was created by Daniel Astbury (jnz)            --
-- The source is avaliable to anyone, however          --
-- please do not take it can claim it yours.           --
-- Please follow my example and release your own.      --
-----------------------Thank you-------------------------


Map = Get_Current_Map()

function db_PlayerCountry_Callback(userdata, argc, data, column)
	Console_Input(string.format("msg %s is connecting from %s", userdata, data[1]))
end


function Output_Chat(PlayerId, Type, Message, Target)
	local teamColour = Get_TeamIRCColour(Get_Team(PlayerId))
	local mute = Players[PlayerId].Muted
	if mute == 0 then
		mute = ""
	else
		mute = " (Muted)"
	end
	
	if Type == 1 then
		IRC_Sendc("a", "%s11[Team] %s%.2d%s%s: %s%s", string.char(3), string.char(3), teamColour, Player_Name_With_Status(PlayerId), mute, Message, string.char(3))
	end
	if Type == 0 then
		IRC_Sendc("p", "%s%.2d%s%s:%s %s", string.char(3), teamColour, Player_Name_With_Status(PlayerId), mute, string.char(3), Message)
	end
end

function Output_PlayerJoin(PlayerId, Nick)
	local teamColour = Get_TeamIRCColour(Get_Team(PlayerId))
	
	IRC_Sendc("p", "%s11[Player]: %s%.2d%s has joined the game, fighting for team %s", string.char(3), string.char(3), teamColour, Nick, Get_Team_Name(Get_Team(PlayerId)))
	
	local IP = IPToLong(string.match(Get_IP_Address(PlayerId), "(%d+).(%d+).(%d+).(%d+)"))
	db:Query(Nick, "db_PlayerCountry_Callback", string.format("SELECT Country FROM IpCountry WHERE IPFrom <= %s and IPTO >= %s;", IP, IP))
end

function Output_PlayerLeave(PlayerId)
	local Nick = Player_Name_With_Status(PlayerId)
	local teamColour = Get_TeamIRCColour(Get_Team(PlayerId))
	
	IRC_Sendc("p", "%s11[Player]: %s%.2d%s has left the game.", string.char(3), string.char(3), teamColour, Nick)
end

function Output_HostMessage(PlayerId, Type, Message)
	if Type == 0 then
		IRC_Sendc("p", "%s11[Host]: %s%s", string.char(3), string.char(3), Message)
	end
end

function Output_MapLoad()
	Map = Get_Current_Map()
	IRC_Sendc("p", "%s11[Map]:%s %s has loaded.", string.char(3), string.char(3), string.match(Map, "C&C_([^.]+).mix"))
end

function Output_MapEnd()
	IRC_Sendc("p", "%s11[Map]:%s %s has ended. Next map will be %s", string.char(3), string.char(3), string.match(Map, "C&C_([^.]+).mix"), string.match(Get_Current_Map(), "C&C_([^.]+).mix"))
end

function Output_Purchase(Purchaser, Cost, Preset, PurchaserRet)
	if PurchaserRet == 0 then
		local Team = Get_Team(Get_Player_ID(Purchaser))
		
		IRC_Sendc("a", "%s11[Purchase]:%s%.2d %s purchased a %s.", string.char(3), string.char(3), Get_TeamIRCColour(Team), Player_Name_With_Status(Get_Player_ID(Purchaser)), Get_Translated_String(Team, Preset))
	end
end

AutoRotateClock = tClock()
local AutoRotateit = 1
function AutoRotateProc()
	if tClock() - AutoRotateClock > AutoRotateTimeout then
		if AutoRotate[AutoRotateit] == nil then
			AutoRotateit = 1
		end
		if Get_Player_Count() > 0 then	
			Console_Input(string.format("msg %s", AutoRotate[AutoRotateit]))
		end
		AutoRotateit = AutoRotateit + 1
		AutoRotateClock = tClock()
	end
end

