public async Task StartNewGame_BothConnected_Ok() { P2PNewGametDto dto = new P2PNewGametDto() { Code = "12345", ConnectionId = "c2", Ships = new ClientShipDto[10] }; var p2pSvc = new Mock <IPeerToPeerGameService>(); p2pSvc.Setup(x => x.AddPeerToSession(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <ShipInfo> >())) .ReturnsAsync(new PeerToPeerSessionState() { Code = "12345", HostConnectionId = "c1", FriendConnectionId = "c2", GameStartedCount = 2 }); p2pSvc.Setup(x => x.StartNewGameAsync(It.IsAny <PeerToPeerSessionState>())) .ReturnsAsync(new PeerToPeerSessionState() { Code = "12345", HostConnectionId = "c1", FriendConnectionId = "c2", GameStartedCount = 2, GameId = "7890" }); var controller = new PeerToPeerGameController(p2pSvc.Object, _mapper, _signalRHub.Object, _logger.Object); var output = await controller.StartNewGame(dto); p2pSvc.Verify(p => p.StartNewGameAsync(It.IsAny <PeerToPeerSessionState>()), Times.Once); _signalRHub.VerifyGet(p => p.Clients, Times.Exactly(2)); Assert.AreEqual(output.GetType(), typeof(OkObjectResult)); }
public async Task <IActionResult> StartNewGame([FromBody] P2PNewGametDto dto) { if (string.IsNullOrWhiteSpace(dto.ConnectionId)) { return(BadRequest()); } if (string.IsNullOrWhiteSpace(dto.Code)) { return(BadRequest()); } if (dto.Ships == null || dto.Ships.Length != 10) { return(BadRequest()); } PeerToPeerSessionState session = null; try { session = await _p2pSvc.AddPeerToSession(dto.Code, dto.ConnectionId, _mapper.Map <IEnumerable <ShipInfo> >(dto.Ships)); } catch (Exception e) { _logger.LogError(e, e.Message); } if (session == null) { return(BadRequest()); } if (session.HostConnectionId == session.FriendConnectionId) { return(BadRequest()); } if (session.GameStartedCount == 2) { //TODO: Implement add board to Game and track battle history session = await _p2pSvc.StartNewGameAsync(session); var connectionId = dto.ConnectionId == session.HostConnectionId ? session.FriendConnectionId : session.HostConnectionId; await _gameHubContext.Clients.Client(connectionId).SendAsync("GameStartedYourMove", new P2PNewGameResultDto { GameId = session.GameId, YourMove = true }); await _gameHubContext.Clients.Client(dto.ConnectionId).SendAsync("GameStartedFriendsMove", new P2PNewGameResultDto { GameId = session.GameId, YourMove = false }); } else { var connectionId = dto.ConnectionId == session.HostConnectionId ? session.FriendConnectionId : session.HostConnectionId; await _gameHubContext.Clients.Client(connectionId).SendAsync("FriendStartedGame"); await _gameHubContext.Clients.Client(dto.ConnectionId).SendAsync("YouStartedGame"); } return(Ok(new { })); }