Пример #1
0
        public JsonResult CheckForHit(db_ShipLocation shipLocation)
        {
            // Check to see if there is a ship at the target location
            var ship = _shipLocationRepo.CheckLocation(shipLocation); // ship found at the row/col combination on the board id

            // Create the new shot
            var shot = new db_Shot
            {
                Board_Id = shipLocation.Board_Id,
                Col      = shipLocation.Col,
                Row      = shipLocation.Row,
                Is_Hit   = (ship != null) ? 1 : 0
            };

            var newShotReturn = _shotRepo.CreateNewShot(shot);

            // Check to make sure the shot was made.
            if (newShotReturn != null)
            {
                if (!newShotReturn[0].Equals("Shot made"))
                {
                    return(Json(new
                    {
                        errMsg = "Error creating shot, please try again.",
                        err = "An error creating your shot has occured.",
                        invalidToken = false
                    }));
                }

                // Return the win if the repo says you won
                if (newShotReturn[1].Equals("Win"))
                {
                    return(Json(new
                    {
                        shotMade = true,
                        hit = shot.Is_Hit,
                        win = true,
                        winningBoard = shot.Board_Id
                    }));
                }

                // Return without the win if there is no win condition
                return(Json(new
                {
                    shotMade = true,
                    win = false
                }));
            }
            return(Json(new
            {
                errMsg = "Error creating shot, please try again.",
                err = "An error creating your shot has occured.",
                invalidToken = false
            }));
        }
Пример #2
0
        /// <summary>
        /// Creates a new shot in the DB.
        /// Switches whos turn it is in the DB.
        /// </summary>
        /// <param name="shot"></param>
        /// <returns>ArrayList</returns>
        public ArrayList CreateNewShot(db_Shot shot)
        {
            try
            {
                // Insert the new shot into the DB
                _context.MySqlDb.Query <db_Shot>("INSERT INTO shot (board_id, row, col, is_hit) VALUES (" + shot.Board_Id + ", " + shot.Row + ", " + shot.Col + ", " + shot.Is_Hit + ");",
                                                 commandType: CommandType.Text);

                // Get the game that we just made a shot in
                var game = _context.MySqlDb.Query <db_Game>("SELECT * FROM game WHERE player_1_board_id = " + shot.Board_Id + " " +
                                                            "OR player_2_board_id = " + shot.Board_Id + ";",
                                                            commandType: CommandType.Text).FirstOrDefault();

                // Switch the turn in the game
                var nextTurn = shot.Board_Id == game.Player_1_Board_Id ? game.Player_1_Id : game.Player_2_Id;

                // Update the game with the turn
                _context.MySqlDb.Query <db_Game>("UPDATE game " +
                                                 "SET turn = " + nextTurn + " " +
                                                 "WHERE game_id = " + game.Game_Id + ";",
                                                 commandType: CommandType.Text);

                var win = false;

                // If the shot was a hit, get all of the hits for the board
                if (shot.Is_Hit == 1)
                {
                    var hitsForBoard          = GetAllHitsForBoard(shot.Board_Id);
                    var shipLocationsForBoard = _context.MySqlDb.Query <db_ShipLocation>("SELECT * FROM ship_location WHERE board_id = " + shot.Board_Id + ";",
                                                                                         commandType: CommandType.Text);

                    // Checking for the win condition
                    if (hitsForBoard.Count() == shipLocationsForBoard.Count())
                    {
                        var validWin          = false;
                        var requiredValidHits = shipLocationsForBoard.Count();
                        var validHits         = 0;

                        foreach (var hit in hitsForBoard)
                        {
                            var hitRow = hit.Row;
                            var hitCol = hit.Col;

                            foreach (var shipLocation in shipLocationsForBoard)
                            {
                                var shipLocationRow = shipLocation.Row;
                                var shipLocationCol = shipLocation.Col;

                                if (shipLocationRow == hitRow)
                                {
                                    if (shipLocationCol == hitCol)
                                    {
                                        validHits++;
                                    }
                                }
                            }
                        }

                        if (validHits == requiredValidHits)
                        {
                            win = true;
                            _context.MySqlDb.Query <db_ShipLocation>("UPDATE game" +
                                                                     "SET complete = 1  " +
                                                                     "WHERE board_id = " + shot.Board_Id + ";",
                                                                     commandType: CommandType.Text);
                        }
                    }
                }

                var returnVal = new ArrayList();
                returnVal.Add("Shot made");
                if (win)
                {
                    returnVal.Add("Win");
                }
                else
                {
                    returnVal.Add("No win");
                }

                return(returnVal);
            }
            catch (MySqlException mysqlex)
            {
                Debug.WriteLine("MYSQL EXCEPTION IN CreateNewShot");
                Debug.WriteLine(mysqlex.InnerException);
                Debug.WriteLine(mysqlex.StackTrace);
                return(null);
            }
            catch (InvalidOperationException ioe)
            {
                Debug.WriteLine("INVALID OPERATION EXCEPTION IN CreateNewShot");
                Debug.WriteLine(ioe.InnerException);
                return(null);
            }
            catch (Exception e)
            {
                Debug.WriteLine("EXCEPTION IN CreateNewShot");
                Debug.WriteLine(e.InnerException);
                return(null);
            }
        }