@model models/keycard/keycard.mdl
@name SystemShocks Realistic Black Jack
@persist Deck:array PlayerOne:entity DealerHand:number PlayerOneHand:number PlayerOneAce:number DealerAce:number

function void shuffleDeck(){
    for(X = 1, 256, 1){
    Spot = randint(52) #Get a random spot in the deck
    GotNum = Deck:number(Spot) #Find what card is there
    Deck:remove(Spot) #Remove that card from the deck
    Deck:unshiftNumber(GotNum) #add it to the start of the deck
    }
}

function entity getPlayer(){
    PlayerOne = ranger(100):entity()
    ##debug statement
    #print(PlayerOne:name())
    
}

function void initialiseHands(){
    DealerHand = 0
    PlayerOneHand = 0
}



#########End of functions

if(first())
    {
Deck = array(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,
                5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,
                10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)
##Declaring the deck

initialiseHands()
######## Initialising hand variables.

shuffleDeck()
######### Shuffling the Deck

print("Welcome to SystemShocks realistic Blackjack E2.
Type -help for a simple list of commands.")
print("Type -detailed for a detailed(shock horror) list of commands.
Type -credits for, wait for it, the credits.")
print("!Be warned, Card counting WILL work against this E2!")
}
#########End of first

runOnChat(1)

if(chatClk(owner())) {
    A=owner():lastSaid()
    A = A:lower()
    if(A=="-dealerhit")
    {
        hideChat(1)
        timer("dealerHit", 700)
    }
    elseif(A=="-new")
    {
     hideChat(1)
     initialiseHands()
     getPlayer()   
     timer("deal", 700) 
    }
    elseif(A=="-onestand")
    {
        hideChat(1)
        timer("oneStands", 700)
        
    }
    elseif(A=="-onehit")
    {
        hideChat(1)
        timer("oneHits", 700)
    }
    elseif(A=="-turn")
    {
        hideChat(1)
        timer("turn", 700)
    }
    elseif(A=="-dumpdebug")
    {
        
        print("Debug Command : Do not use in live games")
        for(I = 1, 52, 1){
        print(Deck:number(I))
    }
    }
    elseif(A=="-shuffle")
    {
        hideChat(1)
        timer("shuffle", 700)
    }
    
    
    
    
    
    ##########################################################################
    #Fluff chat commands
    if(A=="-help"){
        hideChat(1)
        print("")
        print("")
        print("Type -new, to register a new round")
        print("Type -[playernumber]hit to hit a player with a card.")
        print("Type -[playernumber]stand to have a player stand.")
        print("I.e -onehit to player one, -standone to stand.")
        print("-turn, will turn over the dealers hidden card.")
        print("-dealerhit, will hit the dealer, he cannot stand.")
        print("-shuffle, will... shuffle the deck")
    }
    elseif(A=="-detailed"){
        print("To do, system's too lazy")
    }
    elseif(A=="-credits"){
        hideChat(1)
        print("")
        print("")
        print("")
        print("")
        print("")
        print("This E2 was made by SystemShock, do not redistribute.")
        print("")
        print("")
    }
}

if(clk("dealerHit"))
    {
     Temp = Deck:number(1)
     if(Temp==1){
        DealerAce = DealerAce + 1
    
     if((DealerHand + 11) < 22){
        DealerHand = DealerHand + 11
    }
    else{
     DealerHand = DealerHand + Temp
     DealerAce = DealerAce - 1   
}
}
else
{
    DealerHand = DealerHand + Temp
}
    if((DealerHand >= PlayerOneHand) && (DealerHand < 21))
    {
        concmd("say The dealer just drew a card, it was a " + 
                    Temp + " better luck next time, " + PlayerOne:name())
                }
     elseif(DealerHand <= 21){
     concmd("say The dealer just drew a card, it was a " + 
                Temp + " His hand now has a value of " + DealerHand)
            }
    elseif(DealerHand > 21)
    {
     concmd("say The dealer drew a, " + Temp
                + " he is now bust on " + DealerHand + ".")   
    }
    
            
     Deck:shift()
     Deck:insertNumber(52, Temp)
}
elseif(clk("oneStands")){
    concmd("say " + PlayerOne:name() + " has stood on " + PlayerOneHand)
}
elseif(clk("oneHits")){
     Temp = Deck:number(1)
     if(Temp==1){
        PlayerOneAce = PlayerOneAce + 1
    
     if((PlayerOneHand + 11) < 22){
        PlayerOneHand = PlayerOneHand + 11
    }
    else{
     PlayerOneHand = PlayerOneHand + Temp
     PlayerOneAce = PlayerOneAce - 1   
}
}
else
{
    PlayerOneHand = PlayerOneHand + Temp
}

     if(PlayerOneHand <= 21){
     concmd("say " + PlayerOne:name() + " just drew a card, it was a " + 
                Temp + " His hand now has a value of " + PlayerOneHand)
            }
    else
    {
     concmd("say "  + PlayerOne:name()+ " drew a, " + Temp
                + " he is now bust on " + PlayerOneHand + ".")   
    }
     Deck:shift()
     Deck:insertNumber(52, Temp)
}
elseif(clk("deal")){
    PlayerOneFirst = Deck:number(1)
    Deck:shift()
    Deck:insertNumber(52, PlayerOneFirst) 
    
    DealerFirst = Deck:number(1)
    Deck:shift()
    Deck:insertNumber(52, DealerFirst)
    
    PlayerOneSecond = Deck:number(1)
    Deck:shift()
    Deck:insertNumber(52, PlayerOneSecond)
    
    Temp = Deck:number(1)
    Deck:shift()
    Deck:insertNumber(52, Temp)
    
    if(PlayerOneFirst==1)
    {
        PlayerOneHand = 11 + PlayerOneSecond
    }
    elseif(PlayerOneSecond==1)
    {
        PlayerOneHand = 11 + PlayerOneFirst
    }
    elseif((PlayerOneFirst==1) && (PlayerOneSecond==1))
    {
        PlayerOneHand = 12
    }
    else
    {
    PlayerOneHand = PlayerOneFirst + PlayerOneSecond
    }
    
    if(DealerFirst==1)
    {
        DealerHand = 11 + Temp
    }
    elseif(Temp==1)
    {
        DealerHand = 11 + DealerFirst
    }
    elseif((DealerFirst==1) && (Temp==1))
    {
        DealerHand = 12
    }
    else
    {
    DealerHand = DealerFirst + Temp
    }
    
    concmd("say The cards have been dealt, " + PlayerOne:name() + " has a " + PlayerOneFirst + " and "
                + PlayerOneSecond + " Totalling " + PlayerOneHand + " the dealer shows a " + DealerFirst)
    

}
elseif(clk("turn")){
    concmd("say The dealer turns his hidden card, he now has a total of " + DealerHand)
}
elseif(clk("shuffle"))
{
    concmd("Say The dealer is now shuffling the deck")
    shuffleDeck()
}
