Beispiel #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 { }));
        }
        public async Task FireCannon_Ok()
        {
            P2PFireCannonDto dto = new P2PFireCannonDto()
            {
                Code = "12345", ConnectionId = "c2", CellId = 1
            };

            var p2pSvc = new Mock <IPeerToPeerGameService>();

            p2pSvc.Setup(x => x.FindActiveSessionAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new PeerToPeerSessionState()
            {
                Code = "12345", HostConnectionId = "c1",
            });

            var controller = new PeerToPeerGameController(p2pSvc.Object, _mapper, _signalRHub.Object, _logger.Object);

            var output = await controller.FireCannon(dto);

            _signalRHub.VerifyGet(p => p.Clients, Times.Once);

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