public AchievementsForm(User user)
        {
            me = user;
            InitializeComponent();
            AllAchievements = DataApiController <List <Achievement> > .GetData(ChessUtils.IPAddressWithPort + "GetAllAchievements");

            // At least 1 achievement earned
            UsersAchievements = DataApiController <List <Achievement> > .GetData(ChessUtils.IPAddressWithPort + me.Username);

            if (UsersAchievements != null)
            {
                foreach (Achievement achievement in AllAchievements)
                {
                    bool achieved = UsersAchievements.Where(x => x.AchievementID == achievement.AchievementID).Any();
                    dgvAchievements.Rows.Add(achievement.AchievementID, achievement.Title, achievement.Difficulty, achieved);
                    if (achieved)
                    {
                        dgvAchievements.Rows[dgvAchievements.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LightGreen;
                    }
                }
            }

            // No achievements
            else
            {
                foreach (Achievement achievement in AllAchievements)
                {
                    dgvAchievements.Rows.Add(achievement.AchievementID, achievement.Title, achievement.Difficulty, false);
                }
            }
        }
Example #2
0
        // Add user request
        bool AddUser(User user)
        {
            bool success = DataApiController <User> .PostData(ChessUtils.IPAddressWithPort + "AddUser", user);

            if (!success)
            {
                MessageBox.Show("Error while adding user to database.", "Error");
            }

            return(success);
        }
Example #3
0
        private void GetGames()
        {
            gameList = DataApiController <List <CustomGame> > .GetData(ChessUtils.IPAddressWithPort + "GetCustomGamesForUser/" + me.Username);

            if (gameList != null)
            {
                foreach (CustomGame game in gameList)
                {
                    AddGameToList(game);
                }
            }
            else
            {
                totalTime.Text     =
                    turnTime.Text  =
                        color.Text = "No data available";
            }
        }
Example #4
0
        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (gamesList.SelectedRows.Count > 0)
            {
                // Confirm that the user wants to delete the selected custom game!
                DialogResult result = MessageBox.Show("Are you sure you would like to delete the game \"" +
                                                      (string)gamesList.SelectedRows[0].Cells[1].Value + "\"?", "Warning - Delete custom game", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                {
                    return;
                }

                // The user has selected to delete the custom game, continue...
                int gameId = (int)gamesList.SelectedRows[0].Cells[0].Value;

                DataApiController <List <CustomGame> > .PostData(ChessUtils.IPAddressWithPort + "DeleteCustomGame/" + gameId.ToString(), null);

                // Delete the game from the data grid view
                gamesList.Rows.RemoveAt(gamesList.SelectedRows[0].Index);
            }
        }
Example #5
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 #6
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();
                }
            }
        }
        private bool Verify_Login(string username, string password)
        {
            User user = new User(username, password);

            return(DataApiController <User> .PostData(ChessUtils.IPAddressWithPort + "AuthenticateUser", user));
        }
        private void saveAndClose_Click(object sender, EventArgs e)
        {
            bool isTurnSelected  = false;
            bool isNameEmpty     = true;
            bool hasBothKings    = false;
            bool areKingsInCheck = true;

            /// Verify form has all valid fields and no kings are in check (turn times are automatically taken care of)
            // Check if the custom game has a name
            isNameEmpty = gameNameBox.Text == "";
            if (isNameEmpty)
            {
                ShowErrorMessage("You must enter a name for this game mode.");
                return;
            }

            // Check if the turns are selected
            isTurnSelected = whiteToMove.Checked || blackToMove.Checked;
            if (!isTurnSelected)
            {
                ShowErrorMessage("You must first select which color moves first on game startup.");
                return;
            }

            // Check to make sure there are both black and white kings on the board
            bool hasWhiteKing = false, hasBlackKing = false;

            foreach (Button square in board)
            {
                if ((string)square.Tag == TagStrings[(int)PieceEnum.WhiteKing])
                {
                    hasWhiteKing = true;
                    if (hasBlackKing)
                    {
                        // Both kings found, exit loop
                        break;
                    }
                }
                else if ((string)square.Tag == TagStrings[(int)PieceEnum.BlackKing])
                {
                    hasBlackKing = true;
                    if (hasWhiteKing)
                    {
                        // Both kings found, exit loop
                        break;
                    }
                }
            }
            hasBothKings = hasBlackKing && hasWhiteKing;
            if (!hasBothKings)
            {
                string missingKings = "";
                if (!hasBlackKing)
                {
                    missingKings += "You are missing the black king";
                    // Both kings missing
                    if (!hasWhiteKing)
                    {
                        missingKings += " and the white king";
                    }
                }
                else if (!hasWhiteKing)
                {
                    missingKings += "You are missing the white king";
                }
                missingKings += ".";
                ShowErrorMessage(missingKings);
                return;
            }

            //Verify no kings are in check
            CheckLogic logic = new CheckLogic(board);

            areKingsInCheck = logic.GetCustomGameCheckState() == ChessUtils.CheckState.Check;
            if (areKingsInCheck)
            {
                ShowErrorMessage("One of your kings is in check!");
                return;
            }

            // Add the settings to the database
            List <Piece> pieces = new List <Piece>();

            foreach (Button square in board)
            {
                Piece       piece = new Piece();
                Coordinates c     = ChessUtils.Settings.GetCoordinatesOfButton(square);
                string      name  = square.Tag.ToString();
                piece.Coordinates = c;
                piece.Name        = name;

                pieces.Add(piece);
            }

            int gameTimerSeconds = IsPlayerTotalTime.Checked ?
                                   ((int)totalMins.Value * 60) + ((int)totalSecs.Value) : 0;

            int moveTimerSeconds = IsPlayerTurnTime.Checked ?
                                   ((int)turnMins.Value * 60) + ((int)turnSecs.Value) : 0;

            CustomGame game = new CustomGame
            {
                Username        = me.Username,
                GameTimer       = gameTimerSeconds,
                MoveTimer       = moveTimerSeconds,
                WhiteMovesFirst = whiteToMove.Checked,
                CustomGameName  = gameNameBox.Text,
                Pieces          = pieces
            };

            bool success = DataApiController <CustomGame> .PostData(ChessUtils.IPAddressWithPort + "AddCustomGame", game);

            if (!success)
            {
                ShowErrorMessage("Error while adding custom game to database.");
            }
            else
            {
                Close();
            }
        }
Example #9
0
        bool IsUserInDb(User user)
        {
            bool success = DataApiController <User> .PostData(ChessUtils.IPAddressWithPort + "CheckUserAvailability/" + user.Username, null);

            return(success);
        }
Example #10
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            // Update the local settings
            ChessUtils.Settings.Color.darkSquareColor  = darkColor.BackColor;
            ChessUtils.Settings.Color.lightSquareColor = lightColor.BackColor;

            redLightComponent   = lightColor.BackColor.R;
            greenLightComponent = lightColor.BackColor.G;
            blueLightComponent  = lightColor.BackColor.B;
            redDarkComponent    = darkColor.BackColor.R;
            greenDarkComponent  = darkColor.BackColor.G;
            blueDarkComponent   = darkColor.BackColor.B;

            ChessUtils.Settings.Image.BlackKing   = BlackKing;
            ChessUtils.Settings.Image.BlackQueen  = BlackQueen;
            ChessUtils.Settings.Image.BlackRook   = BlackRook;
            ChessUtils.Settings.Image.BlackBishop = BlackBishop;
            ChessUtils.Settings.Image.BlackKnight = BlackKnight;
            ChessUtils.Settings.Image.BlackPawn   = BlackPawn;
            ChessUtils.Settings.Image.WhiteKing   = WhiteKing;
            ChessUtils.Settings.Image.WhiteQueen  = WhiteQueen;
            ChessUtils.Settings.Image.WhiteRook   = WhiteRook;
            ChessUtils.Settings.Image.WhiteBishop = WhiteBishop;
            ChessUtils.Settings.Image.WhiteKnight = WhiteKnight;
            ChessUtils.Settings.Image.WhitePawn   = WhitePawn;

            // Send the settings to the database
            SquareColorSettings settings = new SquareColorSettings()
            {
                Username = me.Username,
                Red1     = redDarkComponent,
                Green1   = greenDarkComponent,
                Blue1    = blueDarkComponent,
                Red2     = redLightComponent,
                Green2   = greenLightComponent,
                Blue2    = blueLightComponent
            };

            DataApiController <SquareColorSettings> .PostData(ChessUtils.IPAddressWithPort + "EditCustomChessboard", settings);

            // Send the piece images to the database
            List <PieceImageSettings> images = new List <PieceImageSettings>()
            {
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wKing",
                    Image     = ImageToByteArray(WhiteKing)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wQueen",
                    Image     = ImageToByteArray(WhiteQueen)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wRook",
                    Image     = ImageToByteArray(WhiteRook)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wBishop",
                    Image     = ImageToByteArray(WhiteBishop)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wKnight",
                    Image     = ImageToByteArray(WhiteKnight)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "wPawn",
                    Image     = ImageToByteArray(WhitePawn)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bKing",
                    Image     = ImageToByteArray(BlackKing)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bQueen",
                    Image     = ImageToByteArray(BlackQueen)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bRook",
                    Image     = ImageToByteArray(BlackRook)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bBishop",
                    Image     = ImageToByteArray(BlackBishop)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bKnight",
                    Image     = ImageToByteArray(BlackKnight)
                },
                new PieceImageSettings()
                {
                    Username  = me.Username,
                    PieceName = "bPawn",
                    Image     = ImageToByteArray(BlackPawn)
                }
            };

            DataApiController <object> .PostData(ChessUtils.IPAddressWithPort + "DeleteUsersCustomPieceImages/" + me.Username, null);

            DataApiController <List <PieceImageSettings> > .PostData(ChessUtils.IPAddressWithPort + "AddCustomPieceImages", images);

            // Close this form
            Close();
        }
Example #11
0
        private void SetupSettings()
        {
            SquareColorSettings colors = DataApiController <SquareColorSettings> .GetData(ChessUtils.IPAddressWithPort + "GetCustomChessboardForUser/" + me.Username);

            ChessUtils.Settings.Color.darkSquareColor  = Color.FromArgb(colors.Red1, colors.Green1, colors.Blue1);
            ChessUtils.Settings.Color.lightSquareColor = Color.FromArgb(colors.Red2, colors.Green2, colors.Blue2);

            List <PieceImageSettings> images = DataApiController <List <PieceImageSettings> > .GetData(ChessUtils.IPAddressWithPort + "GetCustomPieceImagesForUser/" + me.Username);

            if (images != null)
            {
                foreach (PieceImageSettings image in images)
                {
                    switch (image.PieceName)
                    {
                    case "wKing":
                        ChessUtils.Settings.Image.WhiteKing = GetImageFromByteArray(image.Image);
                        break;

                    case "wQueen":
                        ChessUtils.Settings.Image.WhiteQueen = GetImageFromByteArray(image.Image);
                        break;

                    case "wRook":
                        ChessUtils.Settings.Image.WhiteRook = GetImageFromByteArray(image.Image);
                        break;

                    case "wBishop":
                        ChessUtils.Settings.Image.WhiteBishop = GetImageFromByteArray(image.Image);
                        break;

                    case "wKnight":
                        ChessUtils.Settings.Image.WhiteKnight = GetImageFromByteArray(image.Image);
                        break;

                    case "wPawn":
                        ChessUtils.Settings.Image.WhitePawn = GetImageFromByteArray(image.Image);
                        break;

                    case "bKing":
                        ChessUtils.Settings.Image.BlackKing = GetImageFromByteArray(image.Image);
                        break;

                    case "bQueen":
                        ChessUtils.Settings.Image.BlackQueen = GetImageFromByteArray(image.Image);
                        break;

                    case "bRook":
                        ChessUtils.Settings.Image.BlackRook = GetImageFromByteArray(image.Image);
                        break;

                    case "bBishop":
                        ChessUtils.Settings.Image.BlackBishop = GetImageFromByteArray(image.Image);
                        break;

                    case "bKnight":
                        ChessUtils.Settings.Image.BlackKnight = GetImageFromByteArray(image.Image);
                        break;

                    case "bPawn":
                        ChessUtils.Settings.Image.BlackPawn = GetImageFromByteArray(image.Image);
                        break;
                    }
                }
            }
        }