Exemplo n.º 1
0
        public void betBlinds()
        {
            players[currentSmall].Tokens -= currentBlindBet / 2;
            GameLog.logLine(
                gameId,
                GameLog.Actions.Action_Bet,
                players[currentSmall].name,
                (currentBlindBet / 2).ToString(),
                currentSmall.ToString());

            players[currentSmall].TokensInBet = currentBlindBet / 2;

            players[currentBig].Tokens -= currentBlindBet;
            GameLog.logLine(
                gameId,
                GameLog.Actions.Action_Bet,
                players[currentBig].name,
                currentBlindBet.ToString(),
                currentBig.ToString());

            players[currentBig].TokensInBet = currentBlindBet;

            var sum = ((currentBlindBet + (currentBlindBet / 2)));

            pot += sum;
            GameLog.logLine(gameId, GameLog.Actions.Pot_Changed, sum.ToString(), pot.ToString());
        }
Exemplo n.º 2
0
 public void playGame()
 {
     GameLog.logLine(gameId, GameLog.Actions.Round_Start);
     InitializeGame();
     gameStatesObserver.Update(this);
     spectateObserver.Update(this);
 }
Exemplo n.º 3
0
        public ReturnMessage joinSpectate(SystemUser user)
        {
            ReturnMessage m = gamePreferences.canPerformUserActions(this, user, "spectate");

            if (!m.success)
            {
                return(m);
            }

            //check that the user is not playing
            foreach (Player p in players)
            {
                if (p != null && p.systemUserID == user.id)
                {
                    return(new ReturnMessage(false, "Couldn't spectate the game because the user is already playing the game."));
                }
            }

            //check that the user is not spectating
            foreach (SystemUser spectateUser in spectators)
            {
                if (spectateUser.id == user.id)
                {
                    return(new ReturnMessage(false, "Couldn't spectate the game because the user is already spectating the game."));
                }
            }

            spectators.Add(user);
            GameLog.logLine(gameId, GameLog.Actions.Spectate_Join, user.name);

            return(new ReturnMessage(true, ""));
        }
Exemplo n.º 4
0
        public ReturnMessage removeUser(int userId)
        {
            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && players[i].systemUserID == userId)
                {
                    // Buy in handle
                    int buyIn = 50;
                    BuyInPolicyDecPref buyInPref = (BuyInPolicyDecPref)gamePreferences.getOptionalPref(new BuyInPolicyDecPref(0, null));
                    if (buyInPref != null)
                    {
                        buyIn = buyInPref.buyInPolicy;
                    }

                    // Starting chips amount policy
                    int moneyToAdd    = 0;
                    int startingChips = 1000;
                    StartingAmountChipsCedPref startingChipsPref = (StartingAmountChipsCedPref)gamePreferences.getOptionalPref(new StartingAmountChipsCedPref(0, null));
                    if (startingChipsPref != null)
                    {
                        int policy = startingChipsPref.startingChipsPolicy;
                        if (policy == 0)
                        {
                            startingChips = buyIn;
                            moneyToAdd   += players[i].Tokens;
                        }
                        else
                        {
                            startingChips = policy;
                        }
                    }

                    // updates the rank and money of the user.
                    rankMoneyUpdateCallback(new int[] { userId, players[i].Tokens - startingChips, moneyToAdd });

                    GameLog.logLine(gameId, GameLog.Actions.Player_Left, players[i].name, i.ToString());

                    players[i] = null;

                    gameStatesObserver.Update(this);
                    spectateObserver.Update(this);
                    return(new ReturnMessage(true, ""));
                }
            }
            foreach (SystemUser u in spectators)
            {
                if (u.id == userId)
                {
                    spectators.Remove(u);
                    u.spectatingGame.Remove(this);
                    return(new ReturnMessage(true, ""));
                }
            }
            return(new ReturnMessage(true, ""));
        }
Exemplo n.º 5
0
 public void leaveGamePlayer(Player p)
 {
     for (int i = 0; i < maxPlayers; i++)
     {
         if (players[i] != null && players[i].systemUserID == p.systemUserID)
         {
             players[i] = null;
             GameLog.logLine(gameId, GameLog.Actions.Player_Left, p.name, i.ToString());
             break;
         }
     }
 }
Exemplo n.º 6
0
        public int getNextPlayer(int current)
        {
            int i = (current + 1) % maxPlayers;

            while (i != current)
            {
                if (players[i] != null && (players[i].playerState == Player.PlayerState.in_round || players[i].playerState.Equals(Player.PlayerState.my_turn)))
                {
                    GameLog.logLine(gameId, GameLog.Actions.Small_Blind, players[i].name);
                    return(i);
                }
                i = (i + 1) % maxPlayers;
            }
            throw new ArgumentException("cannot locate next player");
        }
Exemplo n.º 7
0
        public TexasHoldemGame(SystemUser user, MustPreferences gamePreferences, Action <int[]> rankMoneyUpdateCallback, Action <int[]> leaderBoardUpdateCallback)
        {
            firstJoin            = true;
            gameCreatorUserId    = user.id;
            this.gamePreferences = gamePreferences;
            pot                = 0;
            active             = true;
            deck               = new Deck();
            spectators         = new List <SystemUser>();
            gameStatesObserver = new GameObserver();
            spectateObserver   = new GameObserver();
            //setting the players array according to the max players pref if entered else 9 players is the max.
            maxPlayers = 9;
            MaxPlayersDecPref maxPlayersDec = (MaxPlayersDecPref)gamePreferences.getOptionalPref(new MaxPlayersDecPref(0, null));

            if (maxPlayersDec != null)
            {
                maxPlayers = maxPlayersDec.maxPlayers;
            }
            players        = new Player[maxPlayers];
            playersStats   = new LeaderboardsStats[maxPlayers];
            availableSeats = maxPlayers - 1;
            this.rankMoneyUpdateCallback = rankMoneyUpdateCallback;
            flop            = null;
            currentBlindBet = 20;

            gameOnChips = true;
            StartingAmountChipsCedPref startingChipsPref = (StartingAmountChipsCedPref)gamePreferences.getOptionalPref(new StartingAmountChipsCedPref(0, null));

            if (startingChipsPref != null)
            {
                if (startingChipsPref.startingChipsPolicy == 0)
                {
                    gameOnChips = false;
                }
            }

            gameId = getNextId();
            GameLog.setLog(gameId, DateTime.Now);
            GameLog.logLine(gameId, GameLog.Actions.Game_Create, gameId.ToString(), user.name, DateTime.Now.ToString());
            for (int i = 1; i < maxPlayers; i++)
            {
                players[i] = null;
            }
        }
Exemplo n.º 8
0
        public ReturnMessage check(Player p)
        {
            GameLog.logLine(
                gameId,
                GameLog.Actions.Action_Check,
                p.name);
            int turn = checkWhosTurnIs();

            players[turn].playerState             = PlayerState.in_round;
            players[nextToSeat(turn)].playerState = PlayerState.my_turn;
            minNumberOfPlayerRounds--;
            if (minNumberOfPlayerRounds <= 0)
            {
                continueGame();
            }
            gameStatesObserver.Update(this);
            spectateObserver.Update(this);
            return(new ReturnMessage(true, ""));
        }
Exemplo n.º 9
0
        public void dealCards()
        {
            int index = nextToSeat(currentDealer);

            //deal first card
            for (int i = 0; i < maxPlayers; i++)
            {
                if (players[index] != null)
                {
                    if (players[index].playerCards.Count >= 2)
                    {
                        players[index].playerCards.Clear();
                    }

                    Card newCard   = deck.Top();
                    var  cardIndex = players[index].playerCards.Count;
                    players[index].playerCards.Add(newCard);
                    GameLog.logLine(gameId, GameLog.Actions.Deal_Card, cardIndex.ToString(), newCard.Type.ToString(), newCard.Value.ToString(), players[index].name, index.ToString());
                }
                index = (index + 1) % maxPlayers;
            }

            index = nextToSeat(currentDealer);
            //deal second card
            for (int i = 0; i < maxPlayers; i++)
            {
                if (players[index] != null)
                {
                    Card newCard   = deck.Top();
                    var  cardIndex = players[index].playerCards.Count;
                    players[index].playerCards.Add(newCard);
                    GameLog.logLine(gameId, GameLog.Actions.Deal_Card, cardIndex.ToString(), newCard.Type.ToString(), newCard.Value.ToString(), players[index].name, index.ToString());
                }
                index = (index + 1) % maxPlayers;
            }
            flop  = null;
            river = null;
            turn  = null;
        }
Exemplo n.º 10
0
        private void checkWhoWins()
        {
            int highRank    = -1;
            int winnerIndex = -1;

            for (int i = 0; i < players.Length - 1; i++)
            {
                if (players[i].playerState.Equals(Player.PlayerState.in_round) || players[i].playerState.Equals(Player.PlayerState.my_turn))
                {
                    if ((int)players[i].handRank > highRank)
                    {
                        winnerIndex = i;
                        highRank    = (int)players[i].handRank;
                    }
                }
            }

            players[winnerIndex].playerState = PlayerState.winner;
            GameLog.logLine(gameId, GameLog.Actions.Player_Winner, players[winnerIndex].name);
            players[winnerIndex].Tokens += pot;
            playersStats[winnerIndex].totalGrossProfit += pot;
            if (playersStats[winnerIndex].highetsCashInAGame < players[winnerIndex].Tokens)
            {
                playersStats[winnerIndex].highetsCashInAGame = players[winnerIndex].Tokens;
            }

            for (int i = 0; i < playersStats.Length; i++)
            {
                if (players[i] != null && playersStats[i] != null)
                {
                    leaderBoardUpdateCallback(new int[] { players[i].systemUserID, playersStats[i].highetsCashInAGame, playersStats[i].totalGrossProfit });
                }
            }


            gameStatesObserver.Update(this);
            spectateObserver.Update(this);
        }
Exemplo n.º 11
0
 private void addToPot(int sum)
 {
     pot += sum;
     GameLog.logLine(gameId, GameLog.Actions.Pot_Changed, sum.ToString(), pot.ToString());
 }
Exemplo n.º 12
0
        public ReturnMessage fold(Player p)
        {
            int fold = -1;

            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState.Equals(Player.PlayerState.my_turn)) && players[i].Tokens != 0)
                {
                    fold = players[i].TokensInBet;
                    break;
                }
            }

            bool isFoldOver = true;

            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState.Equals(Player.PlayerState.my_turn)) && players[i].TokensInBet != fold && players[i].Tokens != 0)
                {
                    isFoldOver = false;
                }
            }
            int turn = checkWhosTurnIs();

            players[turn].playerState = PlayerState.folded;
            int nextPlayer = nextToSeat(turn);

            players[nextPlayer].playerState = PlayerState.my_turn;
            if (numbersOfPlayersInRound() == 1 && !checkIfAnyPlayerIsWinner())
            {
                players[nextPlayer].playerState = PlayerState.winner;
                GameLog.logLine(gameId, GameLog.Actions.Player_Winner, players[nextPlayer].name);
                players[nextPlayer].Tokens += pot;
                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && players[i].Tokens <= 0)
                    {
                        removeUser(players[i].systemUserID);
                    }
                }
                if (numbersOfPlayersInRound() == 1)
                {
                    rankMoneyUpdateCallback(new int[] { p.systemUserID, p.Tokens, moneyInGame });
                }
                gameStatesObserver.Update(this);
                spectateObserver.Update(this);
                return(new ReturnMessage(true, ""));
            }
            if (isFoldOver)
            {
                continueGame();
            }
            gameStatesObserver.Update(this);
            spectateObserver.Update(this);
            GameLog.logLine(
                gameId,
                GameLog.Actions.Action_Fold,
                p.name,
                turn.ToString());

            return(new ReturnMessage(true, ""));
        }
Exemplo n.º 13
0
        public ReturnMessage bet(Player p, int amount)
        {
            ReturnMessage ans;

            ans = gamePreferences.canPerformGameActions(this, amount, "Bet");
            if (!ans.success)
            {
                return(ans);
            }
            if (p.Tokens - amount < 0)
            {
                return(new ReturnMessage(false, "not enough coins"));
            }
            if (currentBet > amount && amount != p.Tokens)
            {
                return(new ReturnMessage(false, "need to bet more"));
            }

            p.Tokens -= amount;

            GameLog.logLine(
                gameId,
                GameLog.Actions.Action_Bet,
                p.name,
                amount.ToString());

            pot += amount;
            GameLog.logLine(gameId, GameLog.Actions.Pot_Changed, amount.ToString(), pot.ToString());
            int turn = checkWhosTurnIs();

            players[turn].TokensInBet += amount;
            int bet = -1;

            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState.Equals(Player.PlayerState.my_turn)) && players[i].Tokens != 0)
                {
                    bet = players[i].TokensInBet;
                    break;
                }
            }
            bool isBetOver = true;

            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState.Equals(Player.PlayerState.my_turn)) && players[i].TokensInBet != bet)// && players[i].Tokens != 0)
                {
                    isBetOver = false;
                }
            }


            players[turn].playerState             = PlayerState.in_round;
            players[nextToSeat(turn)].playerState = PlayerState.my_turn;

            if (isBetOver)
            {
                continueGame();
            }
            gameStatesObserver.Update(this);
            spectateObserver.Update(this);
            // TODO: Gili, you need to send the message to the other players
            return(new ReturnMessage(true, ""));
        }
Exemplo n.º 14
0
        private void continueGame()
        {
            switch (gameState)
            {
            case GameState.bFlop:
                //reveal flop
                flop = new List <Card>();
                for (int i = 0; i < 3; i++)
                {
                    Card flopCard = deck.Top();
                    flop.Add(flopCard);
                    GameLog.logLine(gameId, GameLog.Actions.Flop, i.ToString(), flopCard.Type.ToString(), flopCard.Value.ToString());
                }
                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState == PlayerState.my_turn))
                    {
                        players[i].TokensInBet = 0;
                    }
                }
                minNumberOfPlayerRounds = numbersOfPlayersInRound();
                gameState++;
                break;

            case GameState.bTurn:
                turn = deck.Top();
                GameLog.logLine(
                    gameId,
                    GameLog.Actions.Turn,
                    turn.Type.ToString(), turn.Value.ToString());
                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState == PlayerState.my_turn))
                    {
                        players[i].TokensInBet = 0;
                    }
                }
                minNumberOfPlayerRounds = numbersOfPlayersInRound();
                gameState++;
                break;

            case GameState.bRiver:
                river = deck.Top();
                GameLog.logLine(
                    gameId,
                    GameLog.Actions.River,
                    river.Type.ToString(), river.Value.ToString());
                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && (players[i].playerState == PlayerState.in_round || players[i].playerState == PlayerState.my_turn))
                    {
                        players[i].TokensInBet = 0;
                    }
                }
                minNumberOfPlayerRounds = numbersOfPlayersInRound();
                gameState++;
                break;

            case GameState.aRiver:
                gameState++;
                Player p = decideWhoWon();
                isGameIsOver  = true;
                p.Tokens     += pot;
                p.playerState = PlayerState.winner;

                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && players[i].Tokens <= 0)
                    {
                        removeUser(players[i].systemUserID);
                    }
                }
                if (numbersOfPlayersInRound() == 1)
                {
                    rankMoneyUpdateCallback(new int[] { p.systemUserID, p.Tokens, moneyInGame });
                }
                gameStatesObserver.Update(this);
                spectateObserver.Update(this);
                break;
            }
        }
Exemplo n.º 15
0
 public void leaveGameSpectator(SystemUser user)
 {
     GameLog.logLine(gameId, GameLog.Actions.Spectate_Left, user.name);
     spectators.Remove(user);
 }
Exemplo n.º 16
0
        public ReturnMessage joinGame(SystemUser user, int seatIndex)
        {
            ReturnMessage m = gamePreferences.canPerformUserActions(this, user, "join");

            if (!m.success)
            {
                return(m);
            }

            if (seatIndex > players.Length)
            {
                return(new ReturnMessage(false, "cannot seat here"));
            }
            if (players[seatIndex] != null)
            {
                return(new ReturnMessage(false, "seat is taken"));
            }

            // Buy in handle
            int buyIn = 50;
            BuyInPolicyDecPref buyInPref = (BuyInPolicyDecPref)gamePreferences.getOptionalPref(new BuyInPolicyDecPref(0, null));

            if (buyInPref != null)
            {
                buyIn = buyInPref.buyInPolicy;
            }

            // Starting chips amount policy
            int startingChips = 1000;
            StartingAmountChipsCedPref startingChipsPref = (StartingAmountChipsCedPref)gamePreferences.getOptionalPref(new StartingAmountChipsCedPref(0, null));

            if (startingChipsPref != null)
            {
                int policy = startingChipsPref.startingChipsPolicy;
                if (policy == 0)
                {
                    startingChips = buyIn;
                }
                else
                {
                    startingChips = policy;
                }
            }

            // The user pay to enter the game.
            //user.money -= buyIn;
            rankMoneyUpdateCallback(new int[] { user.id, 0, -buyIn });

            Player p = new Player(user.id, user.name, startingChips, user.rank, user.userImageByteArray);

            players[seatIndex] = p;

            playersStats[seatIndex] = new LeaderboardsStats();

            if (firstJoin)
            {
                currentDealer = seatIndex;
                firstJoin     = false;
            }

            GameLog.logLine(gameId, GameLog.Actions.Player_Join, user.name, seatIndex.ToString(), startingChips.ToString());

            spectateObserver.Update(this);
            gameStatesObserver.Update(this);
            moneyInGame += buyIn;
            return(new ReturnMessage(true, ""));
        }