--[[
	lua/autorun/client/OubHack/oubhack_ttt.lua
	Oubliette | (STEAM_0:1:34029133)
	===DStream===
]]

/******************************************************************************
 .d88888b.  888     888 888888b.   888    888        d8888  .d8888b.  888    d8P  
d88P" "Y88b 888     888 888  "88b  888    888       d88888 d88P  Y88b 888   d8P   
888     888 888     888 888  .88P  888    888      d88P888 888    888 888  d8P    
888     888 888     888 8888888K.  8888888888     d88P 888 888        888d88K     
888     888 888     888 888  "Y88b 888    888    d88P  888 888        8888888b    
888     888 888     888 888    888 888    888   d88P   888 888    888 888  Y88b   
Y88b. .d88P Y88b. .d88P 888   d88P 888    888  d8888888888 Y88b  d88P 888   Y88b  
 "Y88888P"   "Y88888P"  8888888P"  888    888 d88P     888  "Y8888P"  888    Y88b 
 By Oubliete and UnknownAlias
*******************************************************************************/
-- TTT 

local pmeta = table.Copy(_R.Player,{})
local ttt = {
	GetWeapons = FindMetaTable("Player").GetWeapons
}
function oubhack:TTTIsTraitor(ply)
	if not ply:IsPlayer() then return false end;
	if not ttt.GetWeapons(ply) then return false end;
	for k,v in pairs(ttt.GetWeapons(ply)) do
		if v.CanBuy then	
			if table.HasValue(v.CanBuy,ROLE_TRAITOR) then		
				return "TRAITOR";
			end
		end 
	end
end


    local ttt = {
            weps = {
                    "weapon_ttt_sipistol",
                    "weapon_ttt_flaregun",
                    "weapon_ttt_phammer",
                    "weapon_ttt_knife",
                    "weapon_ttt_push",
                    "weapon_ttt_radio",
                    "weapon_ttt_teleport",
            },
            GetWeapons = FindMetaTable("Player").GetWeapons,
    }
    local function TestTraitor(v)
            for kw,vw in pairs(ttt.GetWeapons(v) or {}) do
                    for i = 1, #ttt.weps do
                            if vw:GetClass() == ttt.weps[i] then
                                    if vw.bb_used_traitor_weapon ~= v then
                                            chat.AddText(
                                                    Color(100, 100, 100), "[DW] ",
                                                    Color(255, 0, 0), v:Name(),
                                                    vw.bb_used_traitor_weapon and Color(100, 255, 100) or Color(255, 100, 0),
                                                            " HAS "..(vw.bb_initial_traited and "USED " or "").."TRAITOR WEAPON: ",
                                                    Color(255, 255, 0), vw:GetTable().PrintName,
                                                    Color(255, 0, 0), "!"
                                            )
                                            table.insert(terroristadverts, { time = CurTime(), color = vw.bb_used_traitor_weapon and Color(100, 255, 100) or Color(255, 100, 0),
                                                    text = v:Name() .. " HAS " .. (vw.bb_initial_traited and "USED " or "").."TRAITOR WEAPON: "..vw:GetTable().PrintName.."!"}
                                            )
                                            if not vw.bb_used_traitor_weapon then
                                                    v.bb_traitor = true
                                                    v.bb_spotted = CurTime()
                                                    vw.bb_initial_traited = true
                                            end
                                            vw.bb_used_traitor_weapon = v -- So you know when they buy /another/ traitor weapon
                                    end
                                    break
                            end
                    end
            end
    end
    local hastt_timer = false
    local function TestTraitors()
            if WIN_TRAITOR == nil then return end
            hastt_timer = true
            for k,v in pairs(player.GetAll()) do
                    TestTraitor(v)
            end
            timer.Simple(1, TestTraitors)
    end
    timer.Simple(1, TestTraitors)


local oldincoming = usermessage.IncomingMessage
usermessage.IncomingMessage = function(msgn, umsg)
			local size = "??"
			if UmsgSIZE then
					size = UmsgSIZE(umsg)
			end
			oubhack:Print( "umsg incoming: " .. msgn .. " size: " .. tostring(size) )
	if msgn == "DSPacket" && logds:GetBool() then
					local id = umsg:ReadLong()
					local data = umsg:ReadString()
					umsg:Reset()
					oubhack:Print("DSPacket" .. data)
	elseif msgn == "ttt_role" then
			for k,v in pairs(player.GetAll()) do
					v.bb_traitor = false
			end
			if hastt_timer == false then
					TestTraitors()
			end
	end
   
	oldincoming(msgn, umsg)
end