Пример #1
0
        public void Test_Referencial_Entities_Are_Saved()
        {
            MapSeeding();
            var player = new Player()
            {
                Name       = "Ioann The Terrible",
                CurrentMap = _dbContext.Set <Map>().FirstOrDefault()
            };

            _dbContext.Add(player);
            _dbContext.SaveChanges();
            Assert.Pass();
        }
Пример #2
0
        public IActionResult SetShips(int?id, [FromBody] List <Position> list)
        {
            Boolean result = Collision.CheckShipsCollisions(list);

            if (result)
            {
                Game game = _context.Game.Find(id);
                foreach (Position pos in list)
                {
                    Ship ship = new Ship();
                    ship.playerShip = true;
                    ship.shipNumber = pos.shipNumber;
                    ship.x          = pos.key;
                    ship.y          = pos.value;
                    game.ships.Add(ship);
                }
                _context.Game.Update(game);
                _context.SaveChanges();

                Dictionary <string, int> shipSettings   = GetShipSettings();
                List <Position>          enemyPositions = new List <Position>();
                int shipNumber = 0;
                foreach (KeyValuePair <string, int> entry in shipSettings)
                {
                    int value = entry.Value;
                    while (value > 0)
                    {
                        List <Position> shipPosition = new List <Position>();
                        int             key          = Int32.Parse(entry.Key);
                        Random          rand         = new Random();
                        bool            randBool     = (rand.Next(2) == 0);
                        int             randX        = rand.Next(10 - key);
                        int             randY        = rand.Next(10 - key);
                        for (int i = 0; i < key; i++)
                        {
                            int x = randX;
                            int y = randY;
                            if (randBool)
                            {
                                randY = randY + 1;
                            }
                            else
                            {
                                randX = randX + 1;
                            }
                            Position position = new Position(shipNumber, randX, randY);
                            shipPosition.Add(position);
                        }
                        enemyPositions.AddRange(shipPosition);
                        if (Collision.CheckShipsCollisions(enemyPositions))
                        {
                            shipNumber++;
                            value--;
                        }
                        else
                        {
                            foreach (var pos2 in shipPosition)
                            {
                                enemyPositions.Remove(pos2);
                            }
                        }
                    }
                }

                foreach (Position pos in enemyPositions)
                {
                    Ship ship = new Ship();
                    ship.playerShip = false;
                    ship.shipNumber = pos.shipNumber;
                    ship.x          = pos.key;
                    ship.y          = pos.value;
                    game.ships.Add(ship);
                }
                _context.Game.Update(game);
                _context.SaveChanges();
            }
            return(Json(result));
        }
        /// <summary>
        /// Add account - Async for no need to wait on, no risk for compromising data for only adding
        /// </summary>
        /// <param name="userName">UserName</param>
        /// <param name="password">Password</param>
        /// <param name="email">Email</param>
        public static void NewAccount(string userName, string password, string email)
        {
            Account alreadyExisting = _context.Accounts.Where(a => a.UserName == userName || a.Email == email).FirstOrDefault();

            if (alreadyExisting != null)
            {
                if (alreadyExisting.Email == email)
                {
                    throw new Exception("A account is already made with this email! If you not remember username and/or password, use forgot password function.");
                }
                else
                {
                    throw new Exception("Username are already in use! If you not remember password, use forgot password function.");
                }
            }

            Account account = new Account
            {
                UserName = userName,
                Password = password,
                Email    = email
            };

            _context.Accounts.Add(account);
            _context.SaveChanges();
        }