Example #1
0
        private Task Timer_Elapsed()
        {
            Action action;

            Invoke(action = new Action(() =>
            {
                if (moveLogic.myTurn)
                {
                    totalTimeRemaining--;
                    turnTimeRemaining--;

                    myTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
                    myTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);
                }
                else
                {
                    eTotalTimeRemaining--;
                    eTurnTimeRemaining--;

                    enemyTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(eTotalTimeRemaining);
                    enemyTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(eTurnTimeRemaining);
                }
            }));
            return(new Task(action));
        }
Example #2
0
        private void AddGameToLobbyTable(int sessionId, bool customCheck, string username, int turnTime, int gameTime, int gameId)
        {
            string turnString, gameString;

            turnString = ChessUtils.ConvertSecondsToTimeString(turnTime);
            gameString = ChessUtils.ConvertSecondsToTimeString(gameTime);
            lobbyTable.Rows.Add(sessionId, customCheck, username, turnString, gameString, gameId);

            // Send a signal to other clients to add this game to the list
        }
Example #3
0
        private void gamesList_SelectionChanged(object sender, EventArgs e)
        {
            // Update the stuff on the right of the form
            if (gamesList.SelectedRows.Count > 0)
            {
                // Get the game ID from the row
                int gameId = (int)gamesList.SelectedRows[0].Cells[0].Value;

                // Match the game ID with the appropriate CustomGame in the list
                CustomGame game = gameList.Single(x => x.GameID == gameId);

                totalTime.Text = ChessUtils.ConvertSecondsToTimeString(game.GameTimer);
                turnTime.Text  = ChessUtils.ConvertSecondsToTimeString(game.MoveTimer);
                color.Text     = game.WhiteMovesFirst ?
                                 "White" :
                                 "Black";
            }
        }
Example #4
0
        private void viewDetailsButton_Click(object sender, EventArgs e)
        {
            if (gamesList.SelectedRows.Count > 0)
            {
                ViewGameDetailsForm vgdf = new ViewGameDetailsForm();

                int gameId = (int)gamesList.SelectedRows[0].Cells[0].Value;

                List <CustomGame> games = DataApiController <List <CustomGame> > .GetData(ChessUtils.IPAddressWithPort + "GetCustomGamesForUser/" + me.Username);

                CustomGame game = games.Single(x => x.GameID == gameId);
                if (game != null)
                {
                    vgdf.SetGameTime(ChessUtils.ConvertSecondsToTimeString(game.GameTimer));
                    vgdf.SetTurnTime(ChessUtils.ConvertSecondsToTimeString(game.MoveTimer));
                    vgdf.SetOpponentName(me.Username);
                    vgdf.SetToMove(game.WhiteMovesFirst ? "White" : "Black");
                    vgdf.SetBoard(game.Pieces);
                    vgdf.Show();
                }
            }
        }
Example #5
0
        private void details_Click(object sender, EventArgs e)
        {
            if (lobbyTable.SelectedRows.Count > 0)
            {
                ViewGameDetailsForm vgdf = new ViewGameDetailsForm();

                bool isCustomGame = (bool)lobbyTable.SelectedRows[0].Cells[1].Value;
                if (isCustomGame)
                {
                    string hostUsername = (string)lobbyTable.SelectedRows[0].Cells[2].Value;
                    int    gameId       = (int)lobbyTable.SelectedRows[0].Cells[5].Value;

                    List <CustomGame> games = DataApiController <List <CustomGame> > .GetData(ChessUtils.IPAddressWithPort + "GetCustomGamesForUser/" + hostUsername);

                    CustomGame game = games.Single(x => x.GameID == gameId);
                    if (game != null)
                    {
                        vgdf.SetGameTime(ChessUtils.ConvertSecondsToTimeString(game.GameTimer));
                        vgdf.SetTurnTime(ChessUtils.ConvertSecondsToTimeString(game.MoveTimer));
                        vgdf.SetOpponentName(hostUsername);
                        vgdf.SetToMove(game.WhiteMovesFirst ? "White" : "Black");
                        vgdf.SetBoard(game.Pieces);
                        vgdf.Show();
                    }
                }
                else
                {
                    string gameTime = (string)lobbyTable.SelectedRows[0].Cells[4].Value;
                    string turnTime = (string)lobbyTable.SelectedRows[0].Cells[3].Value;
                    string host     = (string)lobbyTable.SelectedRows[0].Cells[2].Value;
                    vgdf.SetGameTime(gameTime);
                    vgdf.SetTurnTime(turnTime);
                    vgdf.SetOpponentName(host);
                    vgdf.SetToMove("White");
                    vgdf.Show();
                }
            }
        }
Example #6
0
        public GameSession(User Me, User Them, NetworkStream networkStream, Session session, LobbyForm Lobby, MoveLogic ml, CustomGame game = null)
        {
            me = Me;
            int squareSize = 65;
            int offset     = 50;

            InitializeComponent();
            lobby     = Lobby;
            moveLogic = ml;

            // Create timers
            timer          = new System.Timers.Timer(1000);
            timer.Elapsed += async(sender, e) => await Timer_Elapsed();

            stream      = networkStream;
            sessionInfo = session;

            originalTotalTimeRemaining = eTotalTimeRemaining = totalTimeRemaining = sessionInfo.GameTimerSeconds;
            originalTimeRemaining      = eTotalTimeRemaining = turnTimeRemaining = sessionInfo.MoveTimerSeconds;

            myTotalTimeRemaining.Text    = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
            myTimeRemaining.Text         = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);
            enemyTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
            enemyTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);

            myName       = myUsername.Text = me.Username;
            opponentName = enemyUsername.Text = Them.Username;
            customGame   = game;
            // set up the array of buttons (chess grid) into an array
            board = new Button[, ]
            {
                { square00, square01, square02, square03, square04, square05, square06, square07 },
                { square10, square11, square12, square13, square14, square15, square16, square17 },
                { square20, square21, square22, square23, square24, square25, square26, square27 },
                { square30, square31, square32, square33, square34, square35, square36, square37 },
                { square40, square41, square42, square43, square44, square45, square46, square47 },
                { square50, square51, square52, square53, square54, square55, square56, square57 },
                { square60, square61, square62, square63, square64, square65, square66, square67 },
                { square70, square71, square72, square73, square74, square75, square76, square77 }
            };

            // Set up the board to look pretty
            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < 8; ++j)
                {
                    Point       p = new Point(j * squareSize + offset, i * squareSize + (offset * 2));
                    Size        s = new Size(squareSize, squareSize);
                    Coordinates c = ChessUtils.Settings.GetCoordinatesOfButton(board[i, j]);

                    board[i, j].Size     = s;
                    board[i, j].Location = p;
                }
            }

            ChessUtils.Settings.Image.UpdateBoardImages(board);
            ChessUtils.Settings.Color.UpdateChessBoardColors(board);

            UpdatePlayerPieces(whitePieces, blackPieces);
            moveLogic.UpdateAttackedSquares(board);

            SetUpButtons();
            moveLogic.ClearChessBoardColors(board);

            // I am the guest, both people are in the game, now set the game to started
            if (Me.Username != "" && Them.Username != "")
            {
                moveLogic.gameStarted = true;
            }

            checkLabel.Hide();

            gameWorker.RunWorkerAsync();
        }
Example #7
0
        private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!gameWorker.CancellationPending)
            {
                if (opponentName == "")
                {
                    byte[] data = new byte[65535];
                    try
                    {
                        stream.Read(data, 0, data.Length);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    string    json   = ASCIIEncoding.ASCII.GetString(data).Replace("\0", "");
                    TCPSignal signal = (TCPSignal)JsonConvert.DeserializeObject(json, typeof(TCPSignal));
                    if (signal.SignalType == Signal.StartSession)
                    {
                        // Host
                        Invoke(new Action(() =>
                        {
                            moveLogic.gameStarted = true;
                            //TODO add timers, etc
                            opponentName = enemyUsername.Text = signal.NewSession.GuestPlayer;
                        }));
                    }
                }
                else
                {
                    // TODO: Perform server move logic, timers, etc.

                    // Guest
                    Invoke(new Action(() =>
                    {
                        enemyUsername.Text = opponentName;
                        if (moveLogic.myTurn)
                        {
                            timer.Start();
                        }
                    }));
                    byte[] data = new byte[65535];

                    try
                    {
                        stream.Read(data, 0, data.Length);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    string    json   = ASCIIEncoding.ASCII.GetString(data).Replace("\0", "");
                    TCPSignal signal = (TCPSignal)JsonConvert.DeserializeObject(json, typeof(TCPSignal));

                    if (signal.SignalType == Signal.MakeAMove)
                    {
                        Invoke(new Action(() =>
                        {
                            Coordinates destination = signal.PlayerMove.Destination;
                            Piece pieceMoved        = signal.PlayerMove.Source;

                            string pieceTag = pieceMoved.Name;
                            Coordinates c   = pieceMoved.Coordinates;

                            Button originSquare = moveLogic.GetButtonOn(c.X, c.Y, board);
                            Button newSquare    = moveLogic.GetButtonOn(destination.X, destination.Y, board);

                            originSquare.Tag   = "NoPiece";
                            originSquare.Image = null;
                            newSquare.Tag      = pieceTag;
                            newSquare.Image    = ChessUtils.Settings.Image.GetImageForTag(pieceTag);

                            // Update check logic...
                            checkLogic = new CheckLogic(board);
                            ChessUtils.CheckState checkState = checkLogic.CetCheckState();

                            if (checkState == ChessUtils.CheckState.Check)
                            {
                                checkLabel.Show();
                            }

                            else if (checkState == ChessUtils.CheckState.NoCheck)
                            {
                                checkLabel.Hide();
                            }

                            else if (checkState == ChessUtils.CheckState.Checkmate)
                            {
                                // I lost, send the lose signal
                                // They won, send the win signal
                            }

                            else if (checkState == ChessUtils.CheckState.Stalemate)
                            {
                                // We tied, send tie signal for both
                            }

                            // It is now my turn
                            moveLogic.myTurn = !moveLogic.myTurn;

                            if (totalTimeRemaining > originalTimeRemaining)
                            {
                                turnTimeRemaining = originalTimeRemaining;
                            }
                            else
                            {
                                turnTimeRemaining = totalTimeRemaining;
                            }
                            myTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);
                            myTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
                        }));
                    }
                }
            }
        }