예제 #1
0
        public void Movement(ChessPiece piece)
        {
            ChessPieceServer pieceServer = new ChessPieceServer(piece.Color, piece.Type);

            pieceServer.key            = piece.parentTile.Key.ToUpper();
            pieceServer.previousKey    = piece.previousKey.ToUpper();
            pieceServer.movementString = piece.movementString;
            sendToServer(pieceServer, ClientServerCommunication.CommunicationType.Move);
        }
예제 #2
0
        public void opponentsMovement(ChessPieceServer piece)
        {
            string previousKey = null;
            string currentKey  = null;

            cb.turn    = cb.turn == ChessPiece.PieceColor.Black ? ChessPiece.PieceColor.White : ChessPiece.PieceColor.Black;
            currentKey = piece.key;
            ChessPiece found = null;

            foreach (var item in cb.boardTiles)
            {
                ChessPiece currentPiece = item.Value.Piece;
                if (currentPiece != null)
                {
                    if (currentPiece.Type == piece.Type && currentPiece.Color == piece.Color && item.Key == piece.previousKey)
                    {
                        found = currentPiece;
                    }
                }
            }
            bool pnlEnable = true;

            if (cb.boardTiles[currentKey].Piece != null)
            {
                if (cb.boardTiles[currentKey].Piece.Type == ChessPiece.PieceType.King)
                {
                    pnlEnable = false;
                }
            }
            cb.boardTiles[currentKey].Piece = found;
            ChessBoardTile cbt = cb.boardTiles[piece.previousKey];

            found.parentTile    = cb.boardTiles[currentKey];
            cbt.Piece           = null;
            cb.pnlBoard.Enabled = pnlEnable;
            getMovements(found.movementString);
            var player = new System.Media.SoundPlayer("../../effects/sound.wav");

            player.Play();
            updateTurn();
        }
예제 #3
0
        private void listenFromServer()
        {
            try
            {
                stream = clientSocket.GetStream();

                while (true)
                {
                    ClientServerCommunication csc = obtainFromServer();

                    switch (csc.Type)
                    {
                    case ClientServerCommunication.CommunicationType.Login:
                        UserViewModel user = csc.Content as UserViewModel;

                        if (user == null)
                        {
                            MessageBox.Show("Failed to login");
                        }
                        else
                        {
                            CurrentUser = user;
                            BeginInvoke((Action) delegate
                            {
                                cf = new ClientForm(this);
                                cf.Show();
                            });
                            Hide();
                        }
                        break;

                    case ClientServerCommunication.CommunicationType.InSuspence:
                        MessageBox.Show("Your account is in suspense.");
                        break;

                    case ClientServerCommunication.CommunicationType.FriendGotOnline:
                        UserViewModel onFriend = csc.Content as UserViewModel;
                        cf.BeginInvoke((Action) delegate
                        {
                            cf.friendGotOnline(onFriend);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.FriendGotOffline:
                        UserViewModel ofFriend = csc.Content as UserViewModel;
                        cf.BeginInvoke((Action) delegate
                        {
                            cf.friendGotOffline(ofFriend);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.SendMessage:
                        MessageViewModel msg = csc.Content as MessageViewModel;

                        cf.BeginInvoke((Action) delegate
                        {
                            cf.getMessage(msg);
                        });

                        break;

                    case ClientServerCommunication.CommunicationType.CreateARoom:
                        if (csc.Content == null)
                        {
                            cf.BeginInvoke((Action) delegate
                            {
                                cf.FailedToCreate();
                            });
                        }
                        else
                        {
                            currentSession = (GameSession)csc.Content;
                            cf.BeginInvoke((Action) delegate
                            {
                                cf.roomCreated(currentSession);
                            });
                        }
                        break;

                    case ClientServerCommunication.CommunicationType.GetRooms:
                        List <GameSession> sessions = (List <GameSession>)csc.Content;

                        cf.BeginInvoke((Action) delegate
                        {
                            cf.ListRooms(sessions);
                        });
                        break;

                    case ClientServerCommunication.CommunicationType.JoinRoom:
                        currentSession = csc.Content as GameSession;
                        if (currentSession == null)
                        {
                            MessageBox.Show("You can't join the session. You are in a game or the lobby is full.");
                        }
                        else
                        {
                            gf = null;
                            BeginInvoke((Action) delegate
                            {
                                gf = new GameForm(currentSession, this);
                                gf.Show();
                            });
                        }

                        break;

                    case ClientServerCommunication.CommunicationType.Move:
                        ChessPieceServer piece = (ChessPieceServer)csc.Content;
                        gf.BeginInvoke((Action) delegate
                        {
                            gf.opponentsMovement(piece);
                        });

                        break;

                    case ClientServerCommunication.CommunicationType.GameStart:
                        currentSession = (GameSession)csc.Content;
                        BeginInvoke((Action) delegate
                        {
                            gf = new GameForm(currentSession, this);
                            gf.Show();
                        });
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }