
--------------------- 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-------------------------

dofile("YaLB\\YaLB_Settings.Lua")
dofile("YaLB\\YaLB_Permissions.Lua")
dofile("YaLB\\YaLB_IRC.Lua")
dofile("YaLB\\YaLB_Functions.Lua")
dofile("YaLB\\YaLB_Commands.Lua")
dofile("YaLB\\YaLB_Players.Lua")
dofile("YaLB\\YaLB_Output.Lua")
dofile("YaLB\\YaLB_Game.Lua")
dofile("YaLB\\YaLB_Scripts.Lua")
dofile("YaLB\\YaLB_Ranks.Lua")


db = sqlite("YaLB\\YaLB.db")
LUA_NAME = "Yet Another Lua Bot"
LUA_VERSION = "0.0.1"

function OnLoad()
	math.randomseed(tClock())
	Load_Command_Settings()
	Load_Moderators()
	Load_IRC()
	Game_First_Load()
	Load_Ranks()
	Load_Scripts()
end

function OnUnload()
	Disconnect(IRC_Client)
	IRC_Client = -1
end

function OnChat(PlayerId, Type, Message, Target)
	if string.find(Message, "\n") ~= nil then
		return 1
	end

	if Type == 2 then
		if Players[PlayerId] ~= nil and Players[PlayerId].ShowPM == true then
			IRC_Sendc('a', "%s%.2d%s%s -> %s%.2d%s%s: %s", string.char(3), Get_TeamIRCColour(Get_Team(PlayerId)), Get_Player_Name_By_ID(PlayerId), string.char(3), string.char(3), Get_TeamIRCColour(Get_Team(Target)), Get_Player_Name_By_ID(Target), string.char(3), Message)
			return 1
		end
	end 
	Output_Chat(PlayerId, Type, Message, Target)
	return ChatMessage(PlayerId, Type, Message, Target)
end

function OnPlayerJoin(PlayerId, Nick)
	Output_PlayerJoin(PlayerId, Nick)
	Add_Player(PlayerId)
	RequestSerial(PlayerId)
	IRC_Sendc("a", "%s's IP address is %s\n", Nick, Get_IP_Address(PlayerId))
end

function OnPlayerLeave(PlayerId)
	Output_PlayerLeave(PlayerId)
	Remove_Player(PlayerId)
end

function OnHostMessage(PlayerId, Type, Message)
	Output_HostMessage(PlayerId, Type, Message)
end

function OnLevelLoaded()
	Output_MapLoad()
	Load_Game()
	
	Attach_Script_Building("YaLB_Killed", "", 2)
	Attach_Script_Building("YaLB_Building_Sounds", "", 2)
	Attach_Script_Building("YaLB_Building_Damage", "", 2)
end

function OnGameOver()
	Output_MapEnd()
end

function OnConsoleOutput(Message)
	
end

function OnObjectCreate(Object)
	Attach_Script_Once(Object, "YaLB_Killed", "")
end

function OnPlayerVersion(PlayerId, Version)
	Player_Version(PlayerId, Version)
end

function OnCharacterPurchase(Purchaser, Cost, Preset, PurchaserRet)
	Output_Purchase(Purchaser, Cost, Preset, PurchaserRet)
end

function OnVehiclePurchase(Purchaser, Cost, Preset, PurchaserRet) 
	Output_Purchase(Purchaser, Cost, Preset, PurchaserRet)
end


function OnPowerupPurchase(Purchaser, Cost, Preset, PurchaserRet) 
	Output_Purchase(Purchaser, Cost, Preset, PurchaserRet)
end

function OnDDE(Message)

end

function OnThink()
	coroutine.resume(Main_Think_CoRoutine)
	AutoRotateProc()
	Bans_Think()
end

function sql_Serial_Callback(userdata, argc, data, column)
	if data[1] == "0" then
		local PlayerId = tonumber(userdata)
		local a,b,c,d = string.match(Players[PlayerId].IP, "(%d+).(%d+).(%d+).(%d+)")
		db:Query("", "", string.format("INSERT INTO PlayerLog(Nick, Serial, IP, Host, Date) VALUES('%s', '%s', '%s', '%s', '%d');", Players[PlayerId].Nick, Players[PlayerId].Serial, IPToLong(a,b,c,d), Players[PlayerId].Host, os.time(os.date("*t"))))
	end
end

function Serial_Hook(PlayerId, Serial)
	Players[PlayerId].Serial = Serial
	IRC_Sendc("a", "%s's Serial hash is %s\n", Get_Player_Name_By_ID(PlayerId), Serial)
	local a,b,c,d = string.match(Players[PlayerId].IP, "(%d+).(%d+).(%d+).(%d+)")
	db:Query(tostring(PlayerId), "sql_Serial_Callback", string.format("SELECT COUNT(*) FROM PlayerLog WHERE Nick = '%s' AND Serial = '%s' AND IP = '%s' AND Host = '%s';", Players[PlayerId].Nick, Players[PlayerId].Serial, IPToLong(a, b, c, d), Players[PlayerId].Host))	
	local bID = Get_Ban_ID(PlayerId)
	if bID > 0 then
		local info = Get_Ban_Infomation(bID)
		Attach_Script_Once(Get_GameObj(PlayerId), "YaLB_Boot", string.format("You were banned by %s for %s", info[2], info[3]))
	end
end

function Loading_Hook(PlayerId, Loading)

end

function Damage_Hook(PlayerId, Damager, Target, Damage, Warhead)
	Vet_Damaged(Get_GameObj(PlayerId), Target, Damage) 
	return 1
end

function Ping_Hook(PlayerId, Ping)

end

function Suicide_Hook(PlayerId)
	return 1
end

function Radio_Hook(Team, PlayerId, a, RadioId, b)
	local mute = Players[PlayerId].Muted
	if mute == 0 then
		mute = 1
	else
		mute = 0
	end
	return mute
end

function OnError(Error)
	if IRC_Sendc == nil then
		print(Error)
	else
		IRC_Sendc("p", "%s", Error)
	end
end