Exemple #1
0
        public async Task <IActionResult> FireCannon([FromBody] P2PFireCannonDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.ConnectionId))
            {
                return(BadRequest());
            }

            if (string.IsNullOrWhiteSpace(dto.Code))
            {
                return(BadRequest());
            }

            PeerToPeerSessionState session = await _p2pSvc.FindActiveSessionAsync(dto.Code, dto.ConnectionId);

            if (session == null)
            {
                return(BadRequest());
            }

            var connectionId = dto.ConnectionId == session.HostConnectionId ? session.FriendConnectionId : session.HostConnectionId;

            FireCannonDto srDto = new FireCannonDto
            {
                GameId = session.GameId,
                CellId = dto.CellId
            };

            await _gameHubContext.Clients.Client(connectionId).SendAsync("MakeFireFromServer", srDto);

            return(Ok(new { }));
        }
        //Called by client when its move
        public async Task <IActionResult> FireCannon([FromBody] FireCannonDto dto)
        {
            FireResult res = await _gameSvc.FireCannon(dto.GameId, dto.CellId);

            if (res == null)
            {
                return(BadRequest());
            }

            FireCannonResultDto respDto = _mapper.Map <FireCannonResultDto>(res);

            respDto.CellId = dto.CellId;
            respDto.GameId = dto.GameId;

            return(Ok(respDto));
        }
        public async Task FireCannon_BadRequest()
        {
            FireResult res         = null;
            var        gameService = new Mock <IGameService>();

            gameService.Setup(x => x.FireCannon(It.IsAny <string>(), It.IsAny <int>()))
            .ReturnsAsync(res);

            var inputDto = new FireCannonDto {
                CellId = 1, GameId = ""
            };

            var controller = new GameController(gameService.Object, _mapper, _statisticsSvcMock.Object);
            var output     = await controller.FireCannon(inputDto);

            Assert.AreEqual(output.GetType(), typeof(BadRequestResult));
        }
Exemple #4
0
        private async Task MakeMove(GameState game)
        {
            int cellId = await _gameSvc.FiredCannonFromServer(game.GameId);

            if (cellId < 0)
            {
                return;
            }

            FireCannonDto dto = new FireCannonDto
            {
                GameId = game.GameId,
                CellId = cellId
            };

            _logger.LogDebug($"BackgroundPlayerService: Sent to Connection id: {game.ConnectionId}. Game id: {dto.GameId}. Cell id {dto.CellId}.");

            await _gameHubContext.Clients.Client(game.ConnectionId).SendAsync("MakeFireFromServer", dto);
        }
        public async Task FireCannon_Ok()
        {
            var gameService = new Mock <IGameService>();

            gameService.Setup(x => x.FireCannon(It.IsAny <string>(), It.IsAny <int>()))
            .ReturnsAsync(new FireResult()
            {
                IsHit = true
            });

            var inputDto = new FireCannonDto {
                CellId = 1, GameId = ""
            };

            var controller = new GameController(gameService.Object, _mapper, _statisticsSvcMock.Object);
            var output     = await controller.FireCannon(inputDto);

            Assert.AreEqual(output.GetType(), typeof(OkObjectResult));
            Assert.AreEqual(((OkObjectResult)output).Value.GetType(), typeof(FireCannonResultDto));
        }