예제 #1
0
        private void SendResultToPlayers()
        {
            playerManager.ForEach((player) =>
            {
                BlackjackPlayer bPlayer = player as BlackjackPlayer;
                if (bPlayer.status == PlayerStatus.Playing)
                {
                    BlackjackDeckResults deckResults = new BlackjackDeckResults();
                    deckResults.deckResults          = new List <BlackjackResult>();
                    int moneyDelta = 0;
                    for (int i = 0; i < playerCards[bPlayer].Count; i++)
                    {
                        BlackjackResult result = GetGameResult(bPlayer, i, playerBetsDic[player].initialBet);
                        deckResults.deckResults.Add(result);
                        moneyDelta += result.Money;
                    }

                    GameResultResponse response = new GameResultResponse();
                    response.deckResults        = PacketHelper.Serialize <BlackjackDeckResults>(deckResults);
                    response.BetMoney           = playerBetsDic[player].initialBet;
                    response.DealerCards        = PacketHelper.Serialize <CardSet>(dealerCards);

                    bPlayer.peer.SendOperationResponse(new OperationResponse(CommonOperationCode.BaccaratGameResult, response), new SendParameters());
                    bPlayer.money += moneyDelta;
                    //WebHelper.UpdatePlayerMoney(player.name, moneyDelta);
                }
                else
                {
                    GameResultResponse response = new GameResultResponse();
                    response.BetMoney           = 0;
                    response.DealerCards        = PacketHelper.Serialize <CardSet>(dealerCards);
                    bPlayer.peer.SendOperationResponse(new OperationResponse(CommonOperationCode.BaccaratGameResult, response), new SendParameters());
                }
            });
        }
예제 #2
0
        public Card PlayerHit(BlackjackPlayer player, int deckIndex)
        {
            Card card = PickRandomCard(new System.Random());

            playerCards[player][deckIndex].AddCard(card);
            return(card);
        }
예제 #3
0
        protected override Player MakePlayer(GamePeer peer, PlayerInfo info)
        {
            //Get Info from webserver
            BlackjackPlayer newPlayer = new BlackjackPlayer(peer, info);

            return newPlayer;
        }
예제 #4
0
        protected override Player MakePlayer(GamePeer peer, PlayerInfo info)
        {
            //Get Info from webserver
            BlackjackPlayer newPlayer = new BlackjackPlayer(peer, info);

            return(newPlayer);
        }
예제 #5
0
 private bool CanAddBet(BlackjackPlayer player, int betAdd)
 {
     if (playerBetsDic.ContainsKey(player))
     {
         return(player.money >= betAdd);
     }
     return(false);
 }
예제 #6
0
 private bool CanBet(BlackjackPlayer player, BlackjackBet bet)
 {
     if (bet.initialBet >= minimumBet && bet.initialBet <= maximumBet)
     {
         return(player.money >= bet.totalBet);
     }
     return(false);
 }
예제 #7
0
 private void FillActionEventDeck(ref ActionEvent eventData, BlackjackPlayer player, int deckIndex, int splitIndex = -1)
 {
     eventData.ActionCardSet  = PacketHelper.Serialize <BlackjackCardSet>(playerCards[player][deckIndex]);
     eventData.SplitDeckIndex = splitIndex;
     if (splitIndex != -1)
     {
         eventData.SplitCardSet = PacketHelper.Serialize <BlackjackCardSet>(playerCards[player][splitIndex]);
     }
 }
예제 #8
0
        protected override void OnJoin(GamePeer peer)
        {
            BlackjackPlayer player = playerManager.GetPlayer(peer) as BlackjackPlayer;

            SitInEmptySeat(player);

            NewPlayerJoinEvent newJoin = new NewPlayerJoinEvent();

            newJoin.Actor = player.key.ID;
            newJoin.Money = player.money;
            newJoin.Name  = player.name;
            newJoin.Seat  = player.seat;

            EventData eventData = new EventData(EventCode.PlayerJoin, newJoin);

            BroadcastMessage(peer, eventData, new SendParameters());

            ConfirmJoinResponse joinRes = new ConfirmJoinResponse();

            joinRes.Actor = player.key.ID;
            joinRes.Seat  = player.seat;
            joinRes.Money = player.money;
            ExistingPlayerInfos infos;

            infos.infos = new List <ExistingPlayerInfo>();

            playerManager.ForEach((roomPlayer) =>
            {
                var bRoomPlayer = roomPlayer as BlackjackPlayer;
                if (bRoomPlayer.key.ID == player.key.ID)
                {
                    return;
                }
                ExistingPlayerInfo pInfo;
                pInfo.ID     = bRoomPlayer.key.ID;
                pInfo.Name   = bRoomPlayer.name;
                pInfo.Seat   = bRoomPlayer.seat;
                pInfo.Money  = bRoomPlayer.money;
                pInfo.Bet    = playerBetsDic.ContainsKey(bRoomPlayer) ? playerBetsDic[bRoomPlayer] : new BlackjackBet();
                pInfo.Decks  = player.status == PlayerStatus.Playing ? PacketHelper.Serialize <List <BlackjackCardSet> >(playerCards[bRoomPlayer]) : new byte[] { };
                pInfo.Status = bRoomPlayer.status;

                infos.infos.Add(pInfo);
            });
            joinRes.OtherPlayerInfos = PacketHelper.Serialize <ExistingPlayerInfos>(infos);

            var response = new OperationResponse(CommonOperationCode.ConfirmJoin, joinRes);

            peer.SendOperationResponse(response, new SendParameters());

            if (playerManager.Count == 1)
            {
                ClearGame();
                StartGame();
            }
        }
예제 #9
0
 private bool AddBet(BlackjackPlayer player, int betAdd)
 {
     if (CanAddBet(player, betAdd))
     {
         BlackjackBet bet = playerBetsDic[player];
         bet.totalBet         += betAdd;
         playerBetsDic[player] = bet;
         return(true);
     }
     return(false);
 }
예제 #10
0
 private void SitInEmptySeat(BlackjackPlayer player)
 {
     foreach (int seatNum in seatPriority)
     {
         if (seatsDic.ContainsKey(seatNum) == false)
         {
             player.seat = seatNum;
             seatsDic.Add(seatNum, player);
             return;
         }
     }
 }
예제 #11
0
        public override void RemovePlayer(GamePeer peer, ExitRequest exitReq, SendParameters sendParameters)
        {
            BlackjackPlayer player = playerManager.GetPlayer(peer) as BlackjackPlayer;

            playerBetsDic.Remove(player);
            playerCards.Remove(player);
            seatsDic.Remove(player.seat);

            base.RemovePlayer(peer, exitReq, sendParameters);

            CheckAllBet(false);
        }
예제 #12
0
 public void PlayerActionDone(BlackjackPlayer player, int deckIndex)
 {
     if (IsDoubleDowned(player, deckIndex) || IsPlayer21(player, deckIndex))
     {
         Finish(player, deckIndex);
         CheckAllPlayerFinished();
     }
     else if (IsPlayerBusted(player, deckIndex))
     {
         Finish(player, deckIndex);
         CheckAllPlayerFinished();
     }
 }
예제 #13
0
        private bool IsSplittable(BlackjackPlayer player, int deckIndex)
        {
            if (playerCards[player][deckIndex].Cards.Count != 2)
            {
                return(false);
            }

            if (playerCards[player][deckIndex].Cards[0].Is10JQK() && playerCards[player][deckIndex].Cards[1].Is10JQK())
            {
                return(true);
            }

            return(playerCards[player][deckIndex].Cards[0].Rank == playerCards[player][deckIndex].Cards[1].Rank);
        }
예제 #14
0
        public bool IsPlayer21(BlackjackPlayer player, int deckIndex)
        {
            List <int> scores = GetPossibleScores(playerCards[player][deckIndex].Cards);

            foreach (int score in scores)
            {
                if (score == 21)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #15
0
        public void Bet(GamePeer peer, SendBetRequest sendBet)
        {
            BlackjackPlayer player = playerManager.GetPlayer(peer) as BlackjackPlayer;
            BlackjackBet    playerBet;

            playerBet.initialBet = sendBet.BetMoney;
            playerBet.totalBet   = sendBet.BetMoney;

            if (HasBet(player) == false && CanBet(player, playerBet))
            {
                playerBetsDic.Add(player, playerBet);

                BroadcastBetDone(player);
            }

            CheckAllBet(false);
        }
예제 #16
0
        public void BroadcastBetDone(BlackjackPlayer player)
        {
            BlackjackBet bet = playerBetsDic[player];

            BetDoneEvent betDone = new BetDoneEvent();

            betDone.Actor    = player.key.ID;
            betDone.BetMoney = bet.totalBet;
            var eventData = new EventData(EventCode.BlackjackBetDone, betDone);

            BroadcastMessage(player.peer, eventData, new SendParameters());

            var response = new OperationResponse(CommonOperationCode.BlackjackBet, new Dictionary <byte, object> {
                { (byte)CommonParameterKey.Success, true }
            });

            player.peer.SendOperationResponse(response, new SendParameters());
        }
예제 #17
0
        public void Split(BlackjackPlayer player, int deckIndex, out Card card1, out Card card2)
        {
            //if (!IsSplittable(player)) return;

            Card        splitted     = playerCards[player][deckIndex].Cards[1];
            List <Card> splittedList = new List <Card>();

            splittedList.Add(splitted);
            playerCards[player].Add(new BlackjackCardSet(splittedList));
            playerCards[player][deckIndex].RemoveCardAt(1);

            var random = new System.Random();

            card1 = PickRandomCard(random);
            card2 = PickRandomCard(random);

            playerCards[player][deckIndex].AddCard(card1);
            playerCards[player][playerCards[player].Count - 1].AddCard(card2);
        }
예제 #18
0
        private void FirstDraw()
        {
            // Pick 2 cards for each
            var random = new System.Random();

            dealerCards.cards.Add(PickRandomCard(random));
            dealerCards.cards.Add(PickRandomCard(random));

            playerManager.ForEach((player) => {
                BlackjackPlayer bPlayer = player as BlackjackPlayer;
                if (bPlayer.status == PlayerStatus.Waiting)
                {
                    return;
                }

                playerCards[bPlayer].Add(new BlackjackCardSet());;
                BlackjackCardSet pCardSet = playerCards[bPlayer][0];
                pCardSet.AddCard(PickRandomCard(random));
                pCardSet.AddCard(PickRandomCard(random));
            });
        }
예제 #19
0
        public void CheckAllPlayerFinished()
        {
            bool isFinished = true;

            playerManager.ForEach((player) =>
            {
                BlackjackPlayer bPlayer = player as BlackjackPlayer;
                for (int i = 0; i < playerCards[bPlayer].Count; i++)
                {
                    if (bPlayer.status == PlayerStatus.Playing && !IsFinished(bPlayer, i))
                    {
                        isFinished = false;
                        return;
                    }
                }
            });

            if (isFinished)
            {
                DealerDraw();
                EndGame();
            }
        }
예제 #20
0
        public void CheckAllBet(bool forceStart)
        {
            bool allBet = true;

            playerManager.ForEach((player) =>
            {
                BlackjackPlayer bPlayer = player as BlackjackPlayer;
                if (playerBetsDic.ContainsKey(bPlayer))
                {
                    bPlayer.status = PlayerStatus.Playing;
                    SitInEmptySeat(bPlayer);
                }
                else
                {
                    allBet         = false;
                    bPlayer.status = PlayerStatus.Waiting;
                }
            });

            if (allBet || (forceStart && playerBetsDic.Count >= 1))
            {
                StartBlackjack();
            }
        }
예제 #21
0
        private BlackjackResult GetGameResult(BlackjackPlayer player, int deckIndex, int betMoney)
        {
            BlackjackResult result = new BlackjackResult();
            var             money  = 0;

            //TODO: use blackjack result
            if (IsDoubleDowned(player, deckIndex))
            {
                betMoney *= 2;
            }

            if (IsDealerBlackjack() && IsPlayerBlackjack(player, deckIndex))
            {
                money       = 0;
                result.Type = BlackjackResultType.Push;
            }
            else if (IsDealerBlackjack())
            {
                money       = -betMoney;
                result.Type = BlackjackResultType.Lose;
            }
            else if (IsPlayerBlackjack(player, deckIndex))
            {
                money       = betMoney * 3 / 2;
                result.Type = BlackjackResultType.Blackjack;
            }
            else if (IsPlayerBusted(player, deckIndex))
            {
                money       = -betMoney;
                result.Type = BlackjackResultType.Lose;
            }
            else if (IsDealerBusted())
            {
                money       = betMoney;
                result.Type = BlackjackResultType.Win;
            }
            else
            {
                int dealerBestScore = GetDealerBestScore();
                int playerBestScore = GetPlayerBestScore(player, deckIndex);

                if (dealerBestScore < playerBestScore)
                {
                    money       = betMoney;
                    result.Type = BlackjackResultType.Win;
                }
                else if (dealerBestScore > playerBestScore)
                {
                    money       = -betMoney;
                    result.Type = BlackjackResultType.Lose;
                }
                else
                {
                    money       = 0;
                    result.Type = BlackjackResultType.Push;
                }
            }

            result.Money    = money;
            result.BetMoney = betMoney;
            return(result);
        }
예제 #22
0
 public bool IsPlayerBlackjack(BlackjackPlayer player, int deckIndex)
 {
     return(IsBlackjack(playerCards[player][deckIndex].Cards));
 }
예제 #23
0
 public void DoubleDown(BlackjackPlayer player, int deckIndex)
 {
     playerCards[player][deckIndex].DoubleDown();
 }
예제 #24
0
 public int GetPlayerBestScore(BlackjackPlayer player, int deckIndex)
 {
     return(GetBestScore(playerCards[player][deckIndex].Cards));
 }
예제 #25
0
        public void OnPlayerAction(GamePeer peer, ActionRequest action)
        {
            if (peer.ValidateOperation(action, new SendParameters()) == false)
            {
                return;
            }

            BlackjackPlayer player = playerManager.GetPlayer(peer) as BlackjackPlayer;

            if (player.status != PlayerStatus.Playing)
            {
                return;
            }

            if (playerCards[player][action.DeckIndex].Finished)
            {
                return;
            }

            BlackjackBet bet = playerBetsDic[player];

            ActionEvent actionEvent = new ActionEvent();

            actionEvent.Actor      = player.key.ID;
            actionEvent.ActionType = action.ActionType;
            actionEvent.DeckIndex  = action.DeckIndex;

            switch (action.ActionType)
            {
            case BlackjackActionType.Hit:
                PlayerHit(player, action.DeckIndex);
                FillActionEventDeck(ref actionEvent, player, action.DeckIndex);
                break;

            case BlackjackActionType.Split:
                if (AddBet(player, bet.initialBet) == false)
                {
                    return;
                }
                Card originalDeckCard;
                Card newDeckCard;
                Split(player, action.DeckIndex, out originalDeckCard, out newDeckCard);
                FillActionEventDeck(ref actionEvent, player, action.DeckIndex, playerCards[player].Count - 1);
                break;

            case BlackjackActionType.Stand:
                Finish(player, action.DeckIndex);
                FillActionEventDeck(ref actionEvent, player, action.DeckIndex);
                break;

            case BlackjackActionType.DoubleDown:
                if (AddBet(player, bet.initialBet) == false)
                {
                    return;
                }
                DoubleDown(player, action.DeckIndex);
                PlayerHit(player, action.DeckIndex);
                FillActionEventDeck(ref actionEvent, player, action.DeckIndex);
                break;
            }

            EventData eventData = new EventData(EventCode.BlackjackAction, actionEvent);

            BroadcastMessage(peer, eventData, new SendParameters());

            var response = new OperationResponse(CommonOperationCode.BlackjackAction, actionEvent);

            peer.SendOperationResponse(response, new SendParameters());

            PlayerActionDone(player, action.DeckIndex);
        }
예제 #26
0
 public bool IsFinished(BlackjackPlayer player, int deckIndex)
 {
     return(playerCards[player][deckIndex].Finished);
 }
예제 #27
0
 public void Finish(BlackjackPlayer player, int deckIndex)
 {
     playerCards[player][deckIndex].Finish();
 }
예제 #28
0
 public bool HasBet(BlackjackPlayer player)
 {
     return(playerBetsDic.ContainsKey(player));
 }
예제 #29
0
 private bool IsDoubleDowned(BlackjackPlayer player, int deckIndex)
 {
     return(playerCards[player][deckIndex].DoubleDowned);
 }