Exemplo n.º 1
0
        public async Task <IActionResult> CreateBulletForBulletPage([FromBody] BulletDTO bulletDTO, [FromRoute] int bulletPageId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (bulletDTO.Id != 0 || bulletPageId == 0)
            {
                return(BadRequest());
            }

            if (!_context.BulletPages.Any(bp => bp.Id == bulletPageId))
            {
                return(NotFound());
            }

            var bulletPage = _context.BulletPages.FirstOrDefault(bp => bp.Id == bulletPageId);
            var bullet     = _mapper.Map <BulletDTO, Bullet>(bulletDTO);

            bulletPage.Bullets.Add(bullet);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (BulletExists(bulletDTO.Id))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
            }
            return(Ok(bulletDTO));
        }
Exemplo n.º 2
0
        public bool playerGotShot(BulletDTO bullet)
        {
            methodCallCounter();
            bool result = false;

            if (Opponents.ContainsKey(Clients.CallerState.UserName))
            {
                PlayerDTO player = Opponents[Clients.CallerState.UserName];
                if (Bullets.ContainsKey(bullet.ID) && Bullets.TryRemove(bullet.ID, out BulletDTO bulletDTO))
                {
                    Clients.Others.removeBullet(bulletDTO);
                    player.Health -= bulletDTO.Damage;
                    result         = true;
                    if (player.Health < 1)
                    {
                        if (Opponents.TryRemove(player.Name, out PlayerDTO deadPlayer))
                        {
                            Clients.Caller.playerDied(bulletDTO.PlayerID);
                            Clients.Others.removeDeadOpponent(deadPlayer.Name);
                        }
                        else
                        {
                            Console.WriteLine($"playerGotShot Error, Could not remove dead player '{deadPlayer.Name}' from List of Opponents");
                        }
                    }
                    else
                    {
                        Clients.Caller.updateHealth(player.Health);
                    }
                }
            }
            return(result);
        }
        private static void opponentShoots(BulletDTO bullet)
        {
            Bullet newBullet = BulletAdapter.renderBullet(bullet);

            if (GameController.Bullets.TryAdd(newBullet.ID, newBullet))
            {
                BulletAdapter.addBulletToCanvas(newBullet);
            }
        }
Exemplo n.º 4
0
        public static Bullet renderBullet(BulletDTO bullet)
        {
            IRenderable renderable = new Renderable(bulletBorderColor, bulletColor, bulletLineThickness,
                                                    bulletWidth, bulletHight, bulletVertices);
            IShape    shape      = new Shape(bullet.ID, bullet.Ray, renderable, new RenderStrategy());
            IMoveable bulletShip = new Moveable(bulletLineMaxVelocity - 1, bulletLineMaxVelocity, 0, 0, shape, new MoveStrategy());

            return(new Bullet(bullet.ID, bulletShip, bullet.Damage, bullet.PlayerID));
        }
Exemplo n.º 5
0
        public static BulletDTO bulletToDTO(IBullet bullet)
        {
            BulletDTO bulletDTO = new BulletDTO()
            {
                ID       = bullet.ID,
                PlayerID = bullet.PlayerID,
                Ray      = (Ray)bullet.BulletShip.Shape.Ray,
                Damage   = bullet.Damage
            };

            return(bulletDTO);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Put(int bulletId, [FromBody] BulletDTO bulletDTO)
        {
            if (!BulletExists(bulletId))
            {
                return(NotFound());
            }
            if (bulletId == 0)
            {
                return(BadRequest());
            }
            var bullet = _mapper.Map <BulletDTO, Bullet>(bulletDTO);

            _context.Update(bullet);
            await _context.SaveChangesAsync();

            return(Ok(bullet));
        }
Exemplo n.º 7
0
        public async Task <bool> playerShoots(int damage)
        {
            methodCallCounter();
            bool      result         = false;
            PlayerDTO shootingPlayer = Opponents[Clients.CallerState.UserName];
            await Task.Factory.StartNew(() => {
                if (shootingPlayer != null)
                {
                    BulletDTO bullet = BulletFactory.generateBullet(damage, shootingPlayer);
                    bool addBullet   = Bullets.TryAdd(bullet.ID, bullet);
                    if (addBullet)
                    {
                        result = true;
                        Clients.All.opponentShoots(bullet);
                    }
                }
            });

            return(result);
        }
 private static void removeBullet(BulletDTO bullet)
 {
     BulletAdapter.removeBulletFromCanvas(bullet.ID);
 }
Exemplo n.º 9
0
 public async Task <bool> playerGotShot(BulletDTO bullet)
 {
     return(await hubProxy.Invoke <bool>("playerGotShot", bullet));
 }