﻿var $deck; // $deck represents a deck object
var $hand; // $hand represents a $hand of 5 cards
var $wager;

function fbPoker()
{
    //this.NewGame();
}

fbPoker.prototype.NewGame = function()
{
   $deck = new Deck(); 
   $hand = new Array(5);
   $wager = 0;
   
   $deck.Shuffle();
}

fbPoker.prototype.SetWager = function(wager)
{
    $wager = wager
}

fbPoker.prototype.DealCards = function(cardsNeeded)
{
    var c;

    if(cardsNeeded != null)
    {
        if(cardsNeeded.length > 0 && cardsNeeded.length <= 5)
        {
            for(i=0; i<cardsNeeded.length; i++)
            {
                // Only add a new card to the $hand ($hand) at the position specified in cardsNeeded.
                $hand[cardsNeeded[i]] = $deck.Deal();
            }
            return $hand;
        }
        else
        {
            alert('Too many cards needed.');
        }
    }
    
    return null;  

}













































































fbPoker.prototype._SCORE_ROYALFLUSH = 250;
fbPoker.prototype._SCORE_STRAIGHTFLUSH = 50;
fbPoker.prototype._SCORE_FOUROFAKIND = 25;
fbPoker.prototype._SCORE_FULLHOUSE = 8;
fbPoker.prototype._SCORE_FLUSH = 5;
fbPoker.prototype._SCORE_STRAIGHT = 4;
fbPoker.prototype._SCORE_THREEOFAKIND = 3;
fbPoker.prototype._SCORE_TWOPAIR =2;
fbPoker.prototype._SCORE_PAIR = 1;

fbPoker.prototype._score;
fbPoker.prototype._multiplier;

fbPoker.prototype.Score = function()
{
    return this._score;
}

fbPoker.prototype.Multiplier = function()
{
    return this._multiplier;
}


fbPoker.prototype.sortByRank = function(a,b)
{
    return a._rank - b._rank;
}

fbPoker.prototype.Checkhand = function()
{   
    if($hand != null)
    {
        $hand.sort(this.sortByRank);
        
        if(this._isFlush($hand))
        {
            this._multiplier = this._SCORE_FLUSH;
            this._score = 'Flush';
        }
        else if(this._isStraight($hand))
        {
            this._multiplier = this._SCORE_STRAIGHT;
            this._score = 'Straight';
        }
        else if(this._isRoyalFlush($hand))
        {
            this._multiplier = this._SCORE_ROYALFLUSH;
            this._score = 'Royal Flush';
        }
        else if(this._isStraightFlush($hand))
        {
            this._multiplier = this._SCORE_STRAIGHTFLUSH;
            this.__score = 'Straight Flush';
        }
        else if (this._isFourOfAKind($hand))
        {
            this._multiplier = this._SCORE_FOUROFAKIND;
            this._score = 'Four Of A Kind';
        }
        else if (this._isFullHouse($hand))
        {
            this._multiplier = this._SCORE_FULLHOUSE;
            this._score = 'Full House';
        }
        else if (this._isThreeOfAKind($hand))
        {
            this._multiplier = this._SCORE_THREEOFAKIND;
            this._score = 'Three Of A Kind';
        }
        else if (this._isTwoPair($hand))
        {
            this._multiplier =this._SCORE_TWOPAIR;
            this._score = 'Two Pair';
        }
        else if (this._isHighPair($hand))
        {
            this._multiplier = this._SCORE_PAIR;
            this._score = 'Jacks Or Better';
        }
        else
        {
            this._multiplier = 0;
            this._score = 'Nothing';
        }
    }
}



fbPoker.prototype._isFlush = function($hand)
{    
    if ($hand[0].Suit() == $hand[1].Suit() && $hand[1].Suit() == $hand[2].Suit() && $hand[2].Suit() == $hand[3].Suit() && $hand[3].Suit() == $hand[4].Suit())
    {
        return true;
    }
    
    return false;
}

fbPoker.prototype._isStraight = function($hand)
{
    if ($hand[0].Rank() == $hand[1].Rank() - 1 && $hand[1].Rank() == $hand[2].Rank() - 1 && $hand[2].Rank() == $hand[3].Rank() - 1 && $hand[3].Rank() == $hand[4].Rank() - 1)
    {
        return true;
    }

    if ($hand[1].Rank == 10 && $hand[2].Rank == 11 && $hand[3].Rank == 12 && $hand[4].Rank == 13 && $hand[0].Rank == 1)
    {
        return true;
    }
    return false;
}

fbPoker.prototype._isRoyalFlush = function($hand)
{
    if (this._isStraight($hand) && this._isFlush($hand) && $hand[0].Rank() == 1 && $hand[1].Rank() == 10 && $hand[2].Rank() == 11 && $hand[3].Rank() == 12 && $hand[4].Rank() == 13)
    {
        return true;
    }
    return false;
}

fbPoker.prototype._isStraightFlush = function($hand)
{
    if (this._isStraight($hand) && this._isFlush($hand))
    {
        return true;
    }
    return false;
}

fbPoker.prototype._isFourOfAKind = function($hand)
{
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[1].Rank() == $hand[2].Rank() && $hand[2].Rank() == $hand[3].Rank() || $hand[1].Rank() == $hand[2].Rank() && $hand[2].Rank() == $hand[3].Rank() && $hand[3].Rank() == $hand[4].Rank())
    {
        return true;
    }

    return false;
}

fbPoker.prototype._isFullHouse = function($hand)
{
    // See if first two cards and last three cards are the same
    // OR
    // See if first three cards and last two cards are the same
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[2].Rank() == $hand[3].Rank() && $hand[3].Rank() == $hand[4].Rank() || $hand[0].Rank() == $hand[1].Rank() && $hand[1].Rank() == $hand[2].Rank() && $hand[3].Rank() == $hand[4].Rank())
    {
        return true;
    }

    return false;
}

fbPoker.prototype._isThreeOfAKind = function($hand)
{
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[1].Rank() == $hand[2].Rank())
    {
        return true;
    }
    if ($hand[1].Rank() == $hand[2].Rank() && $hand[2].Rank() == $hand[3].Rank())
    {
        return true;
    }
    if ($hand[2].Rank() == $hand[3].Rank() && $hand[3].Rank() == $hand[4].Rank())
    {
        return true;
    }
    return false;
}

fbPoker.prototype._isTwoPair = function($hand)
{
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[2].Rank() == $hand[3].Rank())
    {
        return true;
    }
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[3].Rank() == $hand[4].Rank())
    {
        return true;
    }
    if ($hand[1].Rank() == $hand[2].Rank() && $hand[3].Rank() == $hand[4].Rank())
    {
        return true;
    }
    return false;
}

fbPoker.prototype._isHighPair = function($hand)
{
    if ($hand[0].Rank() == $hand[1].Rank() && $hand[0].isJacksOrBetter())
    {
        return true;
    }
    if ($hand[1].Rank() == $hand[2].Rank() && $hand[1].isJacksOrBetter())
    {
        return true;
    }
    if ($hand[2].Rank() == $hand[3].Rank() && $hand[2].isJacksOrBetter())
    {
        return true;
    }
    if ($hand[3].Rank() == $hand[4].Rank() && $hand[3].isJacksOrBetter())
    {
        return true;
    }
    return false;
}
