//ATM Cl_init
//Made by: Fish
//December 17th, 2011
--
require( "datastream" )

local PersonPIN = {}
local PersonPINSelect = {}
CrackedPins = {};

function ATM( um )
datastream.StreamToServer( "CheckPIN", { "" } )
end
usermessage.Hook( "ATMCall", ATM )

local function CommaTheCash( num )
    if ( !num ) then 
        return
    end
    if ( num ) then
        local len = string.len( num )
        for i = len - 3, 1, -3 do 
            num = string.sub( num, 1, i ) .. "," .. string.sub( num, i + 1 )
        end
        return num
    end
end
standardnum = 1000;
numberat = standardnum;
pass = 1000;
LastPin = 0;
numberofpins = 0;
_HudCrackCol = 255;
digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

function PINAlreadySet( handler, id, encoded, decoded )
PersonPIN = {}
PersonPINSelect = {}

DermaPanel5 = vgui.Create( "DFrame" )
DermaPanel5:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 150 )
DermaPanel5:SetSize( 165, 250 )
DermaPanel5:SetTitle( "ATM Security" )
DermaPanel5:SetVisible( true )
DermaPanel5:SetDraggable( true )
DermaPanel5:ShowCloseButton( true )
DermaPanel5:MakePopup()

PropertySheet = vgui.Create( "DPropertySheet", DermaPanel5 )
PropertySheet:SetPos( 0, 25 )
PropertySheet:SetSize( 165, 225 )

local TabA = vgui.Create( "DPanelList", PropertySheet )
TabA:SetPos( 0, 0 )

local DLabel2 = vgui.Create( "DLabel", TabA )
DLabel2:SetPos( 45.5, 85  )
DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
DLabel2:SetFont( "TabLarge" )
DLabel2:SetText( "PIN:" )
DLabel2:SizeToContents()

myText1 = vgui.Create( "DTextEntry", TabA )
myText1:SetSize( 33.5, 15 ) //SetSize( 33.5, 15 )
myText1:SetPos( 65, 85 )
myText1:RequestFocus()
myText1.OnEnter = function()
datastream.StreamToServer( "PINValid", { myText1:GetValue(), PersonPIN[1] } )
end

local button1 = vgui.Create( "DButton", TabA )
button1:SetSize( 55, 15 )
button1:SetPos( 45.5, 105 )
button1:SetText( "Random" )
button1.DoClick = function()
datastream.StreamToServer( "PINValid", { tostring(math.random(9)..math.random(9)..math.random(9)..math.random(9)), PersonPIN[1] } )
end

concommand.Add("Endit",function() timer.Destroy("ATMCrackT"); _HudCrackCol = 0; end)

function crack()
	numberat = numberat + 1;
	pass = tostring(numberat); _HudCrackCol = 255;
	print(pass)
	datastream.StreamToServer( "PINValid", { pass, PersonPIN[1] } )
end

hook.Add("HUDPaint",012,function()
	draw.SimpleText("Bruteforcing: " .. pass,"MenuLarge",200,200,
                             Color(255,255,255,_HudCrackCol),TEXT_ALIGN_CENTER) end)

LOLHAX = vgui.Create( "DButton", TabA )
LOLHAX:SetSize( 55, 15 )
LOLHAX:SetPos(45.5, 125 )
LOLHAX:SetText( "BruteForce" )
LOLHAX.DoClick = function() timer.Create("ATMCrackT",0.1,10000,crack) end

LOLHAX2 = vgui.Create( "DButton" , TabA )
LOLHAX2:SetSize( 55, 15 )
LOLHAX2:SetPos(100, 125 )
LOLHAX2:SetText( "Reset" )
LOLHAX2.DoClick = function() numberat = standardnum; pass = 1000; 
_HudCrackCol = 255; end


LOLHAX3 = vgui.Create( "DButton" , TabA )
LOLHAX3:SetSize( 55, 15 )
LOLHAX3:SetPos(100, 105 )
LOLHAX3:SetText( "Stop" )
LOLHAX3.DoClick = function() RunConsoleCommand("Endit") end

local DLabel1 = vgui.Create( "DLabel", TabA )
DLabel1:SetPos( 33.5, 135 )
DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
DLabel1:SetFont( "TabLarge" )
DLabel1:SetText( "Select Account:" )
DLabel1:SizeToContents()

table.insert( PersonPIN, tostring( LocalPlayer() ) )

List = vgui.Create( "DMultiChoice", TabA )
List:SetPos( 25.5, 152.5 ) //SetPos( 22.5, 43.5 )
List:SetSize( 100, 20 )
List:SetText( tostring( LocalPlayer():Nick() ) )
for k, v in pairs( player.GetAll() ) do
List:AddChoice( tostring( v:Nick() ) )
end
List.OnSelect = function( panel, index, value, data )
PersonPIN = {}
for k, v in pairs( player.GetAll() ) do
if tostring( v:Nick() ) == tostring( value ) then
table.insert( PersonPIN, v )
		end
	end
end

local button2 = vgui.Create( "DButton", TabA )
button2:SetSize( 65, 25 )
button2:SetPos( 42.5, 30 )
button2:SetText( "Lost My PIN" )
button2.DoClick = function()

----

DermaPanel5:SetVisible( false )
DermaPanel5:Close()

--

DermaPanel4 = vgui.Create( "DFrame" )
DermaPanel4:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
DermaPanel4:SetSize( 150, 100 )
DermaPanel4:SetTitle( "ATM PIN SET-UP" )
DermaPanel4:SetVisible( true )
DermaPanel4:SetDraggable( false )
DermaPanel4:ShowCloseButton( true )
DermaPanel4:MakePopup()

local DLabel5 = vgui.Create( "DLabel", DermaPanel4 )
DLabel5:SetPos( 13.5, 25 )
DLabel5:SetColor( Color( 255, 255, 255, 255 ) )
DLabel5:SetFont( "TabLarge" )
DLabel5:SetText( "Please Create a PIN." )
DLabel5:SizeToContents()

local DLabel6 = vgui.Create( "DLabel", DermaPanel4 )
DLabel6:SetPos( 27.5, 39.5 )
DLabel6:SetColor( Color( 255, 0, 0, 255 ) )
DLabel6:SetFont( "TabLarge" )
DLabel6:SetText( "*4 Digits Only!*" )
DLabel6:SizeToContents()

local myText5 = vgui.Create( "DTextEntry", DermaPanel4 )
myText5:SetSize( 33.5, 18 )
myText5:SetPos( 53.5, 57.5 )
myText5:RequestFocus()

local button5 = vgui.Create( "DButton", DermaPanel4 )
button5:SetSize( 65, 18 )
button5:SetPos( 37.5, 78.5 )
button5:SetText( "Set PIN" )
button5.DoClick = function()
datastream.StreamToServer( "SetPIN", { myText5:GetValue() } )
	end
end
PropertySheet:AddSheet( "ATM", TabA, "gui/silkicons/shield", false, false, "ATM Security" )

--------------------------------------------------------------------------------------------

if LocalPlayer():IsSuperAdmin() then
local TabB = vgui.Create( "DPanelList", PropertySheet )
TabB:SetPos( 0, 0 )

List2 = vgui.Create( "DMultiChoice", TabB )
List2:SetPos( 25.5, 170 )
List2:SetSize( 100, 20 )
List2:SetText( tostring( LocalPlayer():Nick() ) )
for k, v in pairs( player.GetAll() ) do
List2:AddChoice( tostring( v:Nick() ) )
end
List2.OnSelect = function( panel, index, value, data )
PersonPINSelect = {}
for k, v in pairs( player.GetAll() ) do
if tostring( v:Nick() ) == tostring( value ) then
table.insert( PersonPINSelect, v )
		end
	end
end

table.insert( PersonPINSelect, LocalPlayer() ) 

local myText11 = vgui.Create( "DTextEntry", TabB )
myText11:SetSize( 50, 15 )
myText11:SetPos( 25.5, 92 )
myText11.OnEnter = function()
datastream.StreamToServer( "SetPINOf", { myText11:GetValue(), PersonPINSelect[1] } )
end

local button13 = vgui.Create( "DButton", TabB )
button13:SetSize( 50, 15 )
button13:SetPos( 75, 92 )
button13:SetText( "Set PIN" )
button13.DoClick = function()
datastream.StreamToServer( "SetPINOf", { myText11:GetValue(), PersonPINSelect[1] } )
end

local myText18 = vgui.Create( "DTextEntry", TabB )
myText18:SetSize( 50, 15 )
myText18:SetPos( 25.5, 124 )
myText18.OnEnter = function()
datastream.StreamToServer( "WithdrawMoneyOf", { tonumber( myText18:GetValue() ), PersonPINSelect[1] } )
end

local button9 = vgui.Create( "DButton", TabB )
button9:SetSize( 50, 15 )
button9:SetPos( 75, 124 )
button9:SetText( "Withdraw" )
button9.DoClick = function()
datastream.StreamToServer( "WithdrawMoneyOf", { tonumber( myText18:GetValue() ), PersonPINSelect[1] } )
end

local myText7 = vgui.Create( "DTextEntry", TabB )
myText7:SetSize( 50, 15 )
myText7:SetPos( 25.5, 108 )
myText7.OnEnter = function()
datastream.StreamToServer( "DepositMoneyOf", { tonumber( myText7:GetValue() ), PersonPINSelect[1] } )
end

local button10 = vgui.Create( "DButton", TabB )
button10:SetSize( 50, 15 )
button10:SetPos( 75, 108 )
button10:SetText( "Deposit" )
button10.DoClick = function()
datastream.StreamToServer( "DepositMoneyOf", { tonumber( myText7:GetValue() ), PersonPINSelect[1] } )
end

local button4 = vgui.Create( "DButton", TabB )
button4:SetSize( 100, 15 )
button4:SetPos( 25.5, 154 )
button4:SetText( "Reset PIN" )
button4.DoClick = function()
datastream.StreamToServer( "ResetPINOf", { PersonPINSelect[1] } )
end

local button5 = vgui.Create( "DButton", TabB )
button5:SetSize( 100, 15 )
button5:SetPos( 25.5, 139 )
button5:SetText( "Reset Bank Money" )
button5.DoClick = function()
datastream.StreamToServer( "ResetBankedMoneyOf", { PersonPINSelect[1] } )
end

local button7 = vgui.Create( "DButton", TabB )
button7:SetSize( 100, 15 )
button7:SetPos( 25.5, 54 )
button7:SetText( "Remove All ATMs" )
button7.DoClick = function()
LocalPlayer():ConCommand( "atm_removeall" )
end

local button11 = vgui.Create( "DButton", TabB )
button11:SetSize( 100, 15 )
button11:SetPos( 25.5, 70 )
button11:SetText( "Spawn All ATMs" )
button11.DoClick = function()
LocalPlayer():ConCommand( "atm_spawnall" )
end

local button3 = vgui.Create( "DButton", TabB )
button3:SetSize( 100, 20 )
button3:SetPos( 25, 5 )
button3:SetText( "Print PIN Codes" )
button3.DoClick = function()
LocalPlayer():ConCommand( "pincodes_send" )
end

local button8 = vgui.Create( "DButton", TabB )
button8:SetSize( 100, 20 )
button8:SetPos( 25, 25 )
button8:SetText( "Print Banked Money" )
button8.DoClick = function()
LocalPlayer():ConCommand( "money_send" )
end

PropertySheet:AddSheet( "S-Admin", TabB, "gui/silkicons/group", false, false, "ATM SuperAdmin Settings" )
	end
end
datastream.Hook( "PINAlreadySet", PINAlreadySet )

local function PINSet( handler, id, encoded, decoded )
DermaPanel4:SetVisible( false )
DermaPanel4:Close()
end
datastream.Hook( "PINSet", PINSet )

local function PINAccepted( handler, id, encoded, decoded )

DermaPanel5:SetVisible( false )
DermaPanel5:Close()

--

LastPin = pass;
timer.Destroy("ATMCrackT");
CrackedPins[numberofpins+1] = (tostring(PersonPIN[1] .. "\'s Pin: " .. LastPin))
numberofpins = numberofpins+1;
print(CrackedPins[1])

DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
DermaPanel:SetSize( 145, 205 )
DermaPanel:SetTitle( "ATM" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( false )
DermaPanel:ShowCloseButton( true )
DermaPanel:MakePopup()

gacc = 0;
downgacc = true;
hook.Add("HUDPaint",012,function()
	if(gacc>255) then downgacc = true; end
	if(gacc<0) then downgacc = false; end
	if(downgacc) then gacc = gacc - 1; end
	if(!downgacc) then gacc = gacc + 1; end
	draw.SimpleText("Successful: " .. LastPin,"MenuLarge",200,200,
                             Color(gacc,255,gacc,_HudCrackCol),TEXT_ALIGN_CENTER)
	for i = 1,numberofpins,1 do 
		draw.SimpleText(tostring(CrackedPins[i]),"MenuLarge",200,230+(i*20),
			     Color(255,25,25,_HudCrackCol),TEXT_ALIGN_CENTER) end 
	end)

local DermaPanelList = vgui.Create( "DPanelList", DermaPanel )
DermaPanelList:SetPos( 0, 22.5 )
DermaPanelList:SetSize( 145, 205 )

local DLabel7 = vgui.Create( "DLabel", DermaPanelList )
DLabel7:SetPos( 14, 5 )
DLabel7:SetColor( Color( 255, 204, 51, 255 ) )
DLabel7:SetFont( "TabLarge" )
DLabel7:SetText( "-BANK ACCOUNT INFO-" )
DLabel7:SizeToContents()

local DLabel8 = vgui.Create( "DLabel", DermaPanelList )
DLabel8:SetPos( 14, 20 )
DLabel8:SetColor( Color( 51, 204, 255, 255 ) )
DLabel8:SetFont( "TabLarge" )
for k, v in pairs( player.GetAll() ) do
if tostring( v ) == tostring( PersonPIN[1] ) then
DLabel8:SetText( v:Nick() )
	end
end
DLabel8:SizeToContents()

local DLabel9 = vgui.Create( "DLabel", DermaPanelList )
DLabel9:SetPos( 14, 35 )
DLabel9:SetColor( Color( 0, 255, 0, 255 ) )
DLabel9:SetFont( "TabLarge" )
DLabel9:SetText( "$" .. CommaTheCash( tonumber( decoded[2] ) ) )
DLabel9:SizeToContents()

local DLabel1 = vgui.Create( "DLabel", DermaPanelList )
DLabel1:SetPos( 50, 50 )
DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
DLabel1:SetFont( "TabLarge" )
DLabel1:SetText( "Deposit" )
DLabel1:SizeToContents()

local myText = vgui.Create( "DTextEntry", DermaPanelList )
myText:SetSize( 85, 20 )
myText:SetPos( 28, 65 )
myText:RequestFocus()
myText.OnEnter = function()
datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
end

local button = vgui.Create( "DButton", DermaPanelList )
button:SetSize( 50, 20 )
button:SetPos( 47.5, 90 )
button:SetText( "Deposit!" )
button.DoClick = function()
datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
end

--

local DLabel2 = vgui.Create( "DLabel", DermaPanelList )
DLabel2:SetPos( 45, 115 )
DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
DLabel2:SetFont( "TabLarge" )
DLabel2:SetText( "Withdraw" )
DLabel2:SizeToContents()

local myText2 = vgui.Create( "DTextEntry", DermaPanelList )
myText2:SetSize( 85, 20 )
myText2:SetPos( 28, 130 )
myText2.OnEnter = function()
datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
end

local button = vgui.Create( "DButton", DermaPanelList )
button:SetSize( 55, 20 )
button:SetPos( 45, 155 )
button:SetText( "Withdraw!" )
button.DoClick = function()
datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
	end
end
datastream.Hook( "PINAccepted", PINAccepted )

--


local function PINAccepted1( handler, id, encoded, decoded )

DermaPanel:SetVisible( false )
DermaPanel:Close()

--

DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
DermaPanel:SetSize( 145, 205 )
DermaPanel:SetTitle( "ATM" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( false )
DermaPanel:ShowCloseButton( true )
DermaPanel:MakePopup()

local DermaPanelList = vgui.Create( "DPanelList", DermaPanel )
DermaPanelList:SetPos( 0, 22.5 )
DermaPanelList:SetSize( 145, 205 )

local DLabel7 = vgui.Create( "DLabel", DermaPanelList )
DLabel7:SetPos( 14, 5 )
DLabel7:SetColor( Color( 255, 204, 51, 255 ) )
DLabel7:SetFont( "TabLarge" )
DLabel7:SetText( "-BANK ACCOUNT INFO-" )
DLabel7:SizeToContents()

local DLabel8 = vgui.Create( "DLabel", DermaPanelList )
DLabel8:SetPos( 14, 20 )
DLabel8:SetColor( Color( 51, 204, 255, 255 ) )
DLabel8:SetFont( "TabLarge" )
for k, v in pairs( player.GetAll() ) do
if tostring( v ) == tostring( PersonPIN[1] ) then
DLabel8:SetText( v:Nick() )
	end
end
DLabel8:SizeToContents()

local DLabel9 = vgui.Create( "DLabel", DermaPanelList )
DLabel9:SetPos( 14, 35 )
DLabel9:SetColor( Color( 0, 255, 0, 255 ) )
DLabel9:SetFont( "TabLarge" )
DLabel9:SetText( "$" .. CommaTheCash( tonumber( decoded[2] ) ) )
DLabel9:SizeToContents()

local DLabel1 = vgui.Create( "DLabel", DermaPanelList )
DLabel1:SetPos( 50, 50 )
DLabel1:SetColor( Color( 255, 255, 255, 255 ) )
DLabel1:SetFont( "TabLarge" )
DLabel1:SetText( "Deposit" )
DLabel1:SizeToContents()

local myText = vgui.Create( "DTextEntry", DermaPanelList )
myText:SetSize( 85, 20 )
myText:SetPos( 28, 65 )
if decoded[3] == "d" then
myText:RequestFocus()
end
myText.OnEnter = function()
datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
end

local button = vgui.Create( "DButton", DermaPanelList )
button:SetSize( 50, 20 )
button:SetPos( 47.5, 90 )
button:SetText( "Deposit!" )
button.DoClick = function()
datastream.StreamToServer( "DepositMoney1", { tonumber( myText:GetValue() ), PersonPIN[1] } )
end

--

local DLabel2 = vgui.Create( "DLabel", DermaPanelList )
DLabel2:SetPos( 45, 115 )
DLabel2:SetColor( Color( 255, 255, 255, 255 ) )
DLabel2:SetFont( "TabLarge" )
DLabel2:SetText( "Withdraw" )
DLabel2:SizeToContents()

local myText2 = vgui.Create( "DTextEntry", DermaPanelList )
myText2:SetSize( 85, 20 )
myText2:SetPos( 28, 130 )
if decoded[3] == "w" then
myText2:RequestFocus()
end
myText2.OnEnter = function()
datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
end

local button = vgui.Create( "DButton", DermaPanelList )
button:SetSize( 55, 20 )
button:SetPos( 45, 155 )
button:SetText( "Withdraw!" )
button.DoClick = function()
datastream.StreamToServer( "Withdraw", { tonumber( myText2:GetValue() ), PersonPIN[1] } )
	end
end
datastream.Hook( "PINAccepted1", PINAccepted1 )

local function PINSet( handler, id, encoded, decoded )
DermaPanel4:SetVisible( false )
DermaPanel4:Close()
end
datastream.Hook( "PINSet", PINSet )

-------

local function NoPINCode( handler, id, encoded, decoded )
DermaPanel4 = vgui.Create( "DFrame" )
DermaPanel4:SetPos( ScrW() / 2 - 100, ScrH() / 2 - 100 )
DermaPanel4:SetSize( 150, 100 )
DermaPanel4:SetTitle( "ATM PIN SET-UP" )
DermaPanel4:SetVisible( true )
DermaPanel4:SetDraggable( false )
DermaPanel4:ShowCloseButton( true )
DermaPanel4:MakePopup()

local DLabel5 = vgui.Create( "DLabel", DermaPanel4 )
DLabel5:SetPos( 13.5, 25 )
DLabel5:SetColor( Color( 255, 255, 255, 255 ) )
DLabel5:SetFont( "TabLarge" )
DLabel5:SetText( "Please Create a PIN." )
DLabel5:SizeToContents()

local DLabel6 = vgui.Create( "DLabel", DermaPanel4 )
DLabel6:SetPos( 27.5, 39.5 )
DLabel6:SetColor( Color( 255, 0, 0, 255 ) )
DLabel6:SetFont( "TabLarge" )
DLabel6:SetText( "*4 Digits Only!*" )
DLabel6:SizeToContents()

local myText5 = vgui.Create( "DTextEntry", DermaPanel4 )
myText5:SetSize( 33.5, 18 )
myText5:SetPos( 53.5, 57.5 )
myText5:RequestFocus()

local button5 = vgui.Create( "DButton", DermaPanel4 )
button5:SetSize( 65, 18 )
button5:SetPos( 37.5, 78.5 )
button5:SetText( "Set PIN" )
button5.DoClick = function()
datastream.StreamToServer( "SetPIN", { myText5:GetValue() } )
	end
end
datastream.Hook( "NoPIN", NoPINCode )