Exemple #1
0
        private void handlePlayOnGroupMessage(PlayOnGroupMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                p.Send(new ErrorMessage("It is not your turn to play on the table!"));
                p.Send(new HandMessage(p.GetHand(), false));
                return;                 //client sent invalid request!
            }

            if (p.GetHand().RemoveCardWithId(m.CardObj.Id) == null)
            {
                //invalid request
                //send cards back, biotches!
                //todo:: error message
                p.Send(new ErrorMessage("You do not have that card in your hand to play on the table. Returning card. [" + m.CardObj.Id + "]"));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }

            if (p.GetHand().Count == 0)
            {
                //invalid request!
                p.Send(new ErrorMessage("You must discard your last card! Returning card."));
                p.GetHand().Add(m.CardObj);
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }

            //find group on table with correct ID
            Group g = _table.FindGroup(m.GroupId);

            if (g == null)             //invalid request
            {
                //send cards back, biotches!
                p.GetHand().Add(m.CardObj);
                p.Send(new ErrorMessage("Group ID does not exist. Returning card."));
                p.Send(new HandMessage(p.GetHand(), true));
                p.Send(new TableMessage(_table));
            }
            else if (!g.CheckWith(m.CardObj))
            {
                //send cards back, biotches!
                p.GetHand().Add(m.CardObj);
                p.Send(new ErrorMessage("Card (" + m.CardObj.ToString() + ") does not fit into group (" + g.Id + "). Returning card."));
                p.Send(new HandMessage(p.GetHand(), false));
            }
            else
            {
                _table.PlayCard(m.CardObj, m.GroupId);
                SendAll(new PlayedCardOnTableMessage(p.PlayerID, _table.ReadGroup(m.GroupId), m.CardObj));
                SendAll(new TableMessage(_table));
                updateClientsStatuses();
            }
        }
Exemple #2
0
        private void handleGetTopDeckCardMessage(GetTopDeckCardMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                return;                        //client sent invalid request!
            }
            //remove card from deck
            Card c = _deck.RemoveCard();

            //check to see if top deck card is null!
            if (_deck.IsTopCardNull())
            {
                _deck.ShuffleAndAddUnused(_discard.RemoveAllButTopCard());
            }

            //add to hand of client
            p.PickedUpCard = true;
            p.GetHand().Add(c);
            CardCollection cc = new CardCollection(1);

            cc.Add(c);
            p.Send(new GotCardsMessage(cc));

            //notify the users
            SendAll(new GotDeckCardMessage(p.PlayerID));
            updateClientsStatuses();
        }
Exemple #3
0
        private void handleGetTopDiscardMessage(GetTopDiscardMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                p.Send(new ErrorMessage("ERROR: It is not your turn to get the top discard!"));
                return; //client sent invalid request!
            }
            if (_discard.ReadTopCard() == null)
            {
                SendAll(new ErrorMessage("Internal server error"));
                return;
            }
            if ((_discard.ReadTopCard().Number == CardNumber.Skip) && (!_firstDiscard))//skip card
            {
                p.Send(new ErrorMessage("ERROR: You asked for a skip card. This is an invalid move, and should have been caught by your client. Upgrade!"));
                return;                 //client sent invalid request
            }

            //remove card from discard
            Card c = _discard.RemoveTopCard();

            p.PickedUpCard = true;
            p.GetHand().Add(c);

            CardCollection cc = new CardCollection(1);

            cc.Add(c);
            p.Send(new GotCardsMessage(cc));
            SendAll(new UpdateDiscardMessage(_discard.ReadTopCard(), p, false));
            SendAll(new GotDiscardMessage(p.PlayerID, c));
            updateClientsStatuses();
        }
Exemple #4
0
        public void PlayerDisconnected(ServerPlayer p)
        {
            if (!_gameStarted)
            {
                SendAll(new LogOffMessage(p.PlayerID, true));
                lock (this)
                {
                    _players.Remove(p);
                }
                return;
            }

            if (!_gameOver)
            {
                SendAll(new LogOffMessage(p.PlayerID, false));

                lock (this)
                {
                    //put players cards back in deck
                    Hand h = p.GetHand();
                    if (h != null)
                    {
                        if (h.Count > 0)
                        {
                            List <Card> addBack = new List <Card>();
                            foreach (Card c in h)
                            {
                                h.Remove(c);
                                addBack.Add(c);
                            }
                            _deck.ShuffleAndAddUnused(addBack);
                        }
                    }



                    //check to see how many players are left
                    int ctr = 0;
                    foreach (object o in _players)
                    {
                        if (((ServerPlayer)o).Connected)
                        {
                            ctr++;
                        }
                    }
                    //if only 1 player is left (and the game is started) end the game
                    if ((ctr == 1) && (_gameStarted))
                    {
                        EndGame();
                    }
                    p.RemovedFromGame();
                }
                if (p.MyTurn)
                {
                    //TODO: add better logic!
                    this.endTurn(p, true);
                }
            }
        }
Exemple #5
0
        private void handleDiscardMessage(DiscardMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                //invalid request!
                p.Send(new ErrorMessage("It is not your turn to discard!"));
                return;                 //client sent invalid request!
            }

            if (!p.PickedUpCard)
            {
                //invalid request
                p.Send(new ErrorMessage("You did not yet pick up a card from the discard pile or deck!"));
                return;
            }

            if (p.GetHand().RemoveCardWithId(m.CardObj.Id) == null)
            {
                //invalid request
                //send cards back, biotches!
                p.AddHandCard(m.CardObj);
                p.Send(new ErrorMessage("You do not have the discard in your hand. Returning card. [" + m.CardObj.Id + "]"));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }

            //make sure we can't pick any skip cards up
            _firstDiscard = false;

            if (p.GetHand().Count == 0)
            {
                endTurn(p, false);
                SendAll(new WentOutMessage(p.PlayerID));
                EndHand(p, m.CardObj);
                return;
            }

            _discard.AddTopCard(m.CardObj);
            SendAll(new UpdateDiscardMessage(m.CardObj, p, true));
            updateClientsStatuses();
            Thread.Sleep(TimingDefinitions.pxcpArtificalWait);

            endTurn(p, true);
        }
Exemple #6
0
        private void handleMeldMessage(MeldMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                p.Send(new ErrorMessage("It is not your turn to meld! Returning cards."));
                p.Send(new HandMessage(p.GetHand(), true));
                return;                 //client sent invalid request!
            }
            if (p.CompletedPhaze)
            {
                p.Send(new ErrorMessage("You have already completed your phaze! Returning cards."));
                p.Send(new HandMessage(p.GetHand(), true));
                return;                 //client sent invalid request!
            }

            List <Group> lg = m.Groups;

            //check that this doesn't use ALL of the player's cards!
            int num_cards = 0;

            foreach (Group g in lg)
            {
                num_cards += g.Count;
            }
            if (num_cards > this.rules.HandCards)
            {
                //invalid request!
                p.Send(new ErrorMessage("You cannot meld with ALL of the cards in your hand! You must keep one card to discard with!"));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }


            //check that all cards are from the user's hand
            //string card_data = "";
            bool okay    = true;
            bool correct = true;

            foreach (Group g in lg)
            {
                foreach (Card c in g)
                {
                    if (!p.GetHand().HasCardWithId(c.Id))
                    {
                        okay = false;
                    }
                    if (!g.Check())
                    {
                        correct = false;
                    }
                    if ((!okay) || (!correct))
                    {
                        break;
                    }
                }
                if ((!okay) || (!correct))
                {
                    break;
                }
            }
            if (!okay)
            {
                //invalid request
                p.Send(new ErrorMessage("You do not own any of the cards you are trying to play! Returning all cards."));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }
            if (!correct)
            {
                //invalid request

                p.Send(new ErrorMessage("Meld is invalid! Returning all cards."));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }


            //if we've reached here, we can officially remove all cards
            //and add the groups to the table
            foreach (Group g in lg)
            {
                foreach (Card c in g)
                {
                    p.GetHand().RemoveCardWithId(c.Id);
                }
                _table.AddGroup(g);
            }

            p.CompletedPhaze = true;
            p.Send(new HandMessage(p.GetHand(), false));
            SendAll(new CompletedPhazeMessage(p.PlayerID));
            SendAll(new TableMessage(_table));
            updateClientsStatuses();
        }
Exemple #7
0
        private void handleDiscardSkipMessage(DiscardSkipMessage m, ServerPlayer p)
        {
            if (!p.MyTurn)
            {
                //invalid request
                p.Send(new ErrorMessage("It is not your turn to discard a skip card!"));
                return;
            }

            if (!p.PickedUpCard)
            {
                //invalid request
                p.Send(new ErrorMessage("You did not yet pick up a card from the discard pile or deck!"));
                return;
            }

            if (p.GetHand().RemoveCardWithId(m.CardObj.Id) == null)
            {
                //invalid request
                //send cards back, biotches!
                p.Send(new ErrorMessage("You do not have the skip card in your hand. Returning card. [" + m.CardObj.Id + "]"));
                p.Send(new HandMessage(p.GetHand(), true));
                return;
            }

            if (p.GetHand().Count == 0)
            {
                endTurn(p, false);
                SendAll(new WentOutMessage(p.PlayerID));
                EndHand(p, m.CardObj);
                return;
            }

            ServerPlayer playerToSkip = findPlayer(m.PlayerId);

            if (playerToSkip == null)
            {
                //logError("No player with specified ID!");
                p.Send(new ErrorMessage("No player with specified ID! (" + m.PlayerId + ")"));
                p.AddHandCard(m.CardObj);
                p.Send(new HandMessage(p.GetHand(), false));
                return;                 //invalid request!
            }
            if (!playerToSkip.Connected)
            {
                p.AddHandCard(m.CardObj);
                p.Send(new ErrorMessage("Unable to skip player; player is no longer playing."));
                p.Send(new HandMessage(p.GetHand(), false));
                return;                 //invalid request
            }

            //make sure we can't pick any skip cards up
            _firstDiscard = false;

            _discard.AddTopCard(m.CardObj);
            SendAll(new UpdateDiscardMessage(m.CardObj, p, true));


            playerToSkip.SkippedBy(p);
            p.Skip(playerToSkip);
            SendAll(new DiscardSkipMessage(m.CardObj, playerToSkip.PlayerID));
            updateClientsStatuses();
            //update scoreboards!
            updateClientsScoreboards();

            Thread.Sleep(TimingDefinitions.pxcpArtificalWait);
            endTurn(p, true);
        }
Exemple #8
0
 private void handleRequestHandMessage(RequestHandMessage m, ServerPlayer p)
 {
     p.Send(new HandMessage(p.GetHand(), true));
 }