///<exception cref="ServiceException">ServiceException</exception> public IGameSession Create(SessionGamePlayer sessionPlayer1, SessionGamePlayer sessionPlayer2 = null) { if (sessionPlayer2 == null) { sessionPlayer2 = this.GetRandomSessionPlayer2(sessionPlayer1); } var newSession = new GameSession(sessionPlayer1, sessionPlayer2); var existSession = this.sessionRepository.Exist(s => s.State == SessionState.Active && s.Name == newSession.Name); if (existSession) { var errorMessage = string.Format("There is already an active session for players {0} and {1}", sessionPlayer1.Information.Name, sessionPlayer2.Information.Name); throw new ServiceException(errorMessage); } try { this.sessionRepository.Create(newSession); } catch (DataException gameDataEx) { var errorMessage = string.Format("An error occured when creating the session {0}", newSession.Name); throw new ServiceException(errorMessage, gameDataEx); } return newSession; }
public void UT_When_HandleMove_Then_Success() { var sessionName = "player1-vs-player2"; var player1 = new TestSessionPlayer() { SessionName = sessionName, PendingToMove = false, Information = new User { DisplayName = "Player 1", Name = "player1" } }; var player2 = new TestSessionPlayer() { SessionName = sessionName, PendingToMove = true, Information = new User { DisplayName = "Player 2", Name = "player2" } }; var session = new GameSession(player1, player2); var testMoveObject = new TestMoveObject { Answer = "Test Answer" }; var testMove = new TestMove(testMoveObject); var moveResponse = new TestResponse(new TestResponseObject { IsCorrect = true }) { IsWin = false }; var sessionServiceMock = new Mock<ISessionService>(); sessionServiceMock .Setup(s => s.GetByName(It.Is<string>(x => x == sessionName))) .Returns(session) .Verifiable(); var moveProcessorMock = new Mock<IMoveProcessor<TestMoveObject, TestResponseObject>>(); moveProcessorMock .Setup(p => p.Process(It.Is<SessionGamePlayer>(x => x == player2), It.Is<IGameMove<TestMoveObject>>(m => m == testMove))) .Returns(moveResponse) .Verifiable(); this.moveService = new MoveService<TestMoveObject, TestResponseObject>(sessionServiceMock.Object, moveProcessorMock.Object); var response = this.moveService.Handle(sessionName, player1.Information.Name, testMove); sessionServiceMock.VerifyAll(); moveProcessorMock.VerifyAll(); Assert.IsNotNull(response); Assert.AreEqual(moveResponse, response); }
public void Initialize() { this.serializer = new JsonSerializer(); var player1s1 = new TestSessionPlayer() { SessionName = this.session1Name, PendingToMove = false, Information = new User { DisplayName = "Player 1", Name = "player1" } }; var player2s1 = new TestSessionPlayer() { SessionName = this.session1Name, PendingToMove = true, Information = new User { DisplayName = "Player 2", Name = "player2" } }; var player1s2 = new TestSessionPlayer() { SessionName = this.session2Name, PendingToMove = false, Information = new User { DisplayName = "Player 1", Name = "player1" } }; var player3s2 = new TestSessionPlayer() { SessionName = this.session2Name, PendingToMove = false, Information = new User { DisplayName = "Player 3", Name = "player3" } }; var session1 = new GameSession(player1s1, player2s1); var session2 = new GameSession(player1s2, player3s2); this.sessions = new List<IGameSession>(); this.sessions.Add(session1); this.sessions.Add(session2); this.sessionServiceMock = new Mock<ISessionService>(); this.sessionHistoryServiceMock = new Mock<ISessionHistoryService<TestMoveObject, TestResponseObject>>(); this.notificationServiceMock = new Mock<INotificationService>(); }