コード例 #1
0
        public virtual void Cancel()
        {
            lock (this)
            {
                _process     = false;
                _gameStarted = false;

                foreach (object o in _players)
                {
                    ServerPlayer p = (ServerPlayer)o;
                    if (p.Connected)
                    {
                        p.Send(new GoodbyeMessage());
                        p.Disconnect();
                    }
                }
                _rulesServer.Stop();

                _connServer.Stop();

                _shutdown = true;
            }
        }
コード例 #2
0
 private void handleHeartBeatMessage(HeartBeatMessage m, ServerPlayer p)
 {
 }
コード例 #3
0
 private void handleMessage(Message m, ServerPlayer p)
 {
     lock (this)
     {
         try
         {
             if (m == null)
             {
                 return;
             }
             if (m.GetType().Equals(typeof(HeartBeatMessage)))
             {
                 handleHeartBeatMessage((HeartBeatMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(SystemMessage)))
             {
                 handleSystemMessage((SystemMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(ChatMessage)))
             {
                 handleChatMessage((ChatMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(ReadyMessage)))
             {
                 handleReadyMessage((ReadyMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(ChangeNameMessage)))
             {
                 handleChangeNameMessage((ChangeNameMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(PlayOnGroupMessage)))
             {
                 handlePlayOnGroupMessage((PlayOnGroupMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(MeldMessage)))
             {
                 handleMeldMessage((MeldMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(GetTopDiscardMessage)))
             {
                 handleGetTopDiscardMessage((GetTopDiscardMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(GetTopDeckCardMessage)))
             {
                 handleGetTopDeckCardMessage((GetTopDeckCardMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(DiscardSkipMessage)))
             {
                 handleDiscardSkipMessage((DiscardSkipMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(DiscardMessage)))
             {
                 handleDiscardMessage((DiscardMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(RequestHandMessage)))
             {
                 handleRequestHandMessage((RequestHandMessage)m, p);
             }
             else if (m.GetType().Equals(typeof(RequestTableMessage)))
             {
                 handleRequestTableMessage((RequestTableMessage)m, p);
             }
             else
             {
                 UnknownMessage um = (UnknownMessage)m;
                 PhazeXLog.LogError
                 (
                     new Exception("Unknown message from client received! [" + um.Identifier + "]")
                     , GameLibraryVersion.ProgramIdentifier
                     , 108
                 );
             }
         }
         catch (BadMessageException bme)
         {
             PhazeXLog.LogError
             (
                 bme
                 , GameLibraryVersion.ProgramIdentifier
                 , 0
             );
         }
     }
 }
コード例 #4
0
 public ServerPlayer NextPlayer(ServerPlayer p)
 {
     return((ServerPlayer)_players.NextObject(p));
 }
コード例 #5
0
 protected virtual void startTurn(ServerPlayer p)
 {
     SendAll(new TurnStartMessage(p.PlayerID));
 }
コード例 #6
0
        public void AddMessageToProcess(ServerPlayer sp, byte [] b)
        {
            if (b.Length < 1)
            {
                PhazeXLog.LogError
                (
                    new Exception("Attempted to process 0 byte message!")
                    , GameLibraryVersion.ProgramIdentifier
                    , 0
                );
            }
            byte start = b[0];

            try
            {
                Message msg = null;
                switch (start)
                {
                case (byte)pxMessages.Heartbeat:
                    msg = new HeartBeatMessage(b);
                    break;

                case (byte)pxMessages.SystemMessage:
                    msg = new SystemMessage(b);
                    break;

                case (byte)pxMessages.Chat:
                    msg = new ChatMessage(b, this.getIDs());
                    break;

                case (byte)pxMessages.Ready:
                    msg = new ReadyMessage(b, this.getIDs());
                    break;

                case (byte)pxMessages.ChangeName:
                    msg = new ChangeNameMessage(b, this.getIDs());
                    break;

                case (byte)pxMessages.PlayOnGroup:
                    msg = new PlayOnGroupMessage(b, this.rules);
                    break;

                case (byte)pxMessages.Meld:
                    msg = new MeldMessage(b, this.rules);
                    break;

                case (byte)pxMessages.GetTopDiscard:
                    msg = new GetTopDiscardMessage(b);
                    break;

                case (byte)pxMessages.GetTopDeckCard:
                    msg = new GetTopDeckCardMessage(b);
                    break;

                case (byte)pxMessages.DiscardSkip:
                    msg = new DiscardSkipMessage(b, this.getIDs(), this.rules);
                    break;

                case (byte)pxMessages.Discard:
                    msg = new DiscardMessage(b, this.rules);
                    break;

                case (byte)pxMessages.RequestHand:
                    msg = new RequestHandMessage(b);
                    break;

                case (byte)pxMessages.RequestTable:
                    msg = new RequestTableMessage(b);
                    break;

                default:
                    msg = new UnknownMessage(b);
                    break;
                }
                lock (this)
                {
                    _messagesToProcess.Add(new PlayerMessage(sp, msg));
                }
            }
            catch (BadMessageException bme)
            {
                PhazeXLog.LogError
                (
                    bme
                    , GameLibraryVersion.ProgramIdentifier
                    , 109
                );

                string info = "";
                foreach (byte tmp in b)
                {
                    info += ((int)tmp).ToString() + " ";
                }
                PhazeXLog.LogInformation(info, pid);
            }
        }
コード例 #7
0
 public void AddPlayer(ServerPlayer p)
 {
     _players.Add(p);
 }
コード例 #8
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();
        }
コード例 #9
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);
        }
コード例 #10
0
 private void handleRequestTableMessage(RequestTableMessage m, ServerPlayer p)
 {
     p.Send(new TableMessage(_table));
 }
コード例 #11
0
 private void handleRequestHandMessage(RequestHandMessage m, ServerPlayer p)
 {
     p.Send(new HandMessage(p.GetHand(), true));
 }
コード例 #12
0
        private void handleReadyMessage(ReadyMessage m, ServerPlayer p)
        {
            if (_gameStarted)
            {
                return;                           //client sent invalid request!
            }
            lock (this)
            {
                p.ReadyToPlay = m.Ready;
            }

            //notify everybody
            SendAll(new ReadyMessage(p.PlayerID, p.ReadyToPlay));

            int          ready_players = 0;
            int          total_players = 0;
            ServerPlayer p2;

            lock (this)
            {
                foreach (object o in _players)
                {
                    p2 = (ServerPlayer)o;
                    if (p2.Connected)
                    {
                        total_players++;
                        if (p2.ReadyToPlay)
                        {
                            ready_players++;
                        }
                    }
                }
            }


            if ((ready_players == total_players) &&
                (total_players > 2) &&
                (!_gameStarted) &&
                (!_startGameTimerStarted))
            {
                lock (this)
                {
                    _startGameTimerSeconds = 0;
                    _startGameTimerStarted = true;
                }
                // StartGame();
            }
            if ((ready_players > 1) &&
                (total_players > 1) &&
                (!_gameStarted) &&
                (!_startGameTimerStarted))
            {
                //Thread t = new Thread(new ThreadStart(this.WaitStartGame));
                //t.IsBackground = true;
                //t.Name = "Waiting to start the game thread";
                //lock(this)
                //{
                //    _startGameTimerStarted = true;
                //}
                //t.Start();
            }
        }