/// <inheritdoc/> public void EndTurn(string host, string username, CardPairDto cardPairDto) { ServiceMatch gameMatch = GetMatch(host); ServicePlayer player = gameMatch.GetPlayer(username); int indexOfPlayerWithCurrentTurn = gameMatch.GetPlayersConnectedToMatch().IndexOf(player); if (cardPairDto.BothCardsAreEqual) { player.Score += 100; } else { indexOfPlayerWithCurrentTurn = ChangeTurn(gameMatch, indexOfPlayerWithCurrentTurn); } ServicePlayer nextPlayer = gameMatch.GetPlayersConnectedToMatch()[indexOfPlayerWithCurrentTurn]; player.HasActiveTurn = false; nextPlayer.HasActiveTurn = true; IList <ServicePlayer> playersInMatch = gameMatch.GetPlayersConnectedToMatch(); foreach (var playerInMatch in playersInMatch) { playerInMatch.MatchServiceConnection.NotifyTurnHasEnded(nextPlayer.Username, cardPairDto); } if (gameMatch.TotalPairs == gameMatch.ServiceCardDeck.NumberOfPairs) { this.NotifyMatchHasEnded(host); } }
/// <inheritdoc/> public bool CanJoin(string matchHost) { ServiceMatch gameMatch = GetMatch(matchHost); if (gameMatch == null) { MatchAccessDeniedFault matchAccessDeniedFault = new MatchAccessDeniedFault() { Details = "The match does not exist." }; throw new FaultException <MatchAccessDeniedFault>(matchAccessDeniedFault); } else { ServiceLobby lobby = gameMatch.Lobby; IList <ServicePlayerInLobby> playersConnectedToLobby = lobby.GetPlayersConnectedToLobby(); int numberOfPlayersConnectedToLobby = playersConnectedToLobby.Count; int numberOfPlayersRequired = gameMatch.MaxNumberOfPlayers; bool matchHasStarted = gameMatch.HasStarted; bool ThereIsSpaceForAnotherPlayer = false; if (numberOfPlayersConnectedToLobby < numberOfPlayersRequired) { ThereIsSpaceForAnotherPlayer = true; } if (ThereIsSpaceForAnotherPlayer && !matchHasStarted) { return(true); } return(false); } }
public void TestDeuceAfterAdvantageBis() { ServiceMatch target = new ServiceMatch(); List <IMatchEvent> events = new List <IMatchEvent>() { new MatchStarted("p1", "p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2") }; Match actual = target.Replay(events); Assert.AreEqual(actual.Score[0], 3); Assert.AreEqual(actual.Score[1], 3); Assert.AreEqual(actual.Deuce, true); Assert.AreEqual(actual.AdvantagePlayer, string.Empty); }
public void TestGameAfterOneDeuceAdvantage() { ServiceMatch target = new ServiceMatch(); List <IMatchEvent> events = new List <IMatchEvent>() { new MatchStarted("p1", "p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p1") }; Match actual = target.Replay(events); Assert.AreEqual(actual.Score[0], 0); Assert.AreEqual(actual.Score[1], 0); Assert.AreEqual(actual.Score[2], 1); Assert.AreEqual(actual.Score[3], 0); }
/// <inheritdoc/> public void NotifyCardWasUncoveredd(PlayerMovementDto playerMovementDto) { string host = playerMovementDto.Host; ServiceMatch gameMatch = GetMatch(host); ServicePlayer player = gameMatch.GetPlayer(playerMovementDto.Username); if (playerMovementDto.HasFormedAPair) { player.AddUncoveredCard(playerMovementDto.CardIndex); gameMatch.TotalPairs++; } else { if (playerMovementDto.MovementsLeft == 0) { player.RemoveUncoveredCard(); } else { player.AddUncoveredCard(playerMovementDto.CardIndex); } } IList <ServicePlayer> playersInMatch = gameMatch.GetPlayersConnectedToMatch(); foreach (var playerInMatch in playersInMatch) { playerInMatch.MatchServiceConnection.UncoverCardd(playerMovementDto.CardIndex); } }
/// <inheritdoc/> public void CreateNewMatch(MatchDto gameMatchDto) { IList <ServiceCard> serviceCards = new List <ServiceCard>(); foreach (var cardDto in gameMatchDto.CardDeckDto.Cards) { ServiceCard card = new ServiceCard() { CardId = cardDto.CardId, FrontImage = cardDto.FrontImage }; serviceCards.Add(card); } ServiceCardDeck serviceCardDeck = new ServiceCardDeck() { CardDeckId = gameMatchDto.CardDeckDto.CardDeckId, NumberOfPairs = gameMatchDto.CardDeckDto.NumberOfPairs, Cards = serviceCards, BackImage = gameMatchDto.CardDeckDto.BackImage }; ServiceMatch serviceMatch = new ServiceMatch() { Host = gameMatchDto.Host, MaxNumberOfPlayers = gameMatchDto.MaxNumberOfPlayers, HasStarted = false, ServiceCardDeck = serviceCardDeck }; _matches.Add(serviceMatch); }
/// <inheritdoc/> public void NotifyMatchHasEnded(string host) { ServiceMatch gameMatch = GetMatch(host); if (host != null) { ServicePlayer playerWithBestScore = gameMatch.GetPlayerWithBestScore(); string usernameOfPlayerWithBestScore = playerWithBestScore.Username; IList <ServicePlayer> playersConnectedToMatch = gameMatch.GetPlayersConnectedToMatch(); foreach (var playerConnected in playersConnectedToMatch) { var channel = playerConnected.MatchServiceConnection; channel.ShowWinners(usernameOfPlayerWithBestScore); channel.MatchHasEnded(); } UnitOfWork unitOfWork = new UnitOfWork(new MemoryGameContext()); try { foreach (var playerInMatch in gameMatch.GetPlayersConnectedToMatch()) { unitOfWork.Players.UpdateScoreOfPlayersAfterMatch(playerInMatch.Username, playerInMatch.Score); } Player winner = unitOfWork.Players.FindPlayerByUsername(usernameOfPlayerWithBestScore); CardDeck cardDeck = unitOfWork.CardDecks.Get(gameMatch.ServiceCardDeck.CardDeckId); Match matchToBeSaved = new Match() { CardDeck = cardDeck, Winner = winner, Date = DateTime.Now }; unitOfWork.Matches.Add(matchToBeSaved); unitOfWork.Complete(); } catch (SqlException sqlException) { _logger.Fatal("MatchService.cs: An exception was thrown while trying " + "to update the score of the players or while trying to register " + " the match. The transaction could not be completed. " + "Method NotifyMatchHasEnded, line 181", sqlException); throw; } catch (EntityException entityException) { _logger.Fatal("MatchService.cs: An exception was thrown while trying to access the " + " database. It is possible that the database is corrupted or that it does not exist. " + "Method NotifyMatchHasEnded, line 181", entityException); throw; } finally { unitOfWork.Dispose(); } _matches.Remove(gameMatch); } }
/// <inheritdoc/> public IList <string> GetActivePlayersInLobby(string host) { ServiceMatch match = GetMatch(host); ServiceLobby lobby = match.Lobby; IList <string> activePlayersFromMatch = lobby.GetUsernamesOfPlayersConnectedToLobby(); return(activePlayersFromMatch); }
/// <inheritdoc/> public IList <string> GetUsernamesOfPlayersConnectedToMatch(string host) { ServiceMatch gameMatch = GetMatch(host); IList <string> playerUsername = gameMatch.GetUsernamesOfPlayersConnectedToMatch(); return(playerUsername); }
/// <inheritdoc/> public void StartGame(string host) { ServiceMatch match = GetMatch(host); if (match != null) { match.StartMatch(); } }
/// <inheritdoc/> public IList <string> GetPlayersVoted(string host, string username) { ServiceMatch gameMatch = GetMatch(host); ServicePlayer player = gameMatch.GetPlayer(username); IList <string> playersVoted = player.GetPlayersVoted(); return(playersVoted); }
public void TestMatchStart() { ServiceMatch target = new ServiceMatch(); List <IMatchEvent> events = new List <IMatchEvent>() { new MatchStarted("p1", "p2"), }; Match actual = target.Replay(events); }
/// <summary> /// Subtracts the number of total pairs formed in the match from the number of pairs formed /// by the player leaving the match. /// </summary> /// <param name="gameMatch">Match in which the operation is to be carried out.</param> /// <param name="cardsUncovered">List of index of cards flipped by the player.</param> public void RemovePairs(ServiceMatch gameMatch, IList <int> cardsUncovered) { if ((cardsUncovered.Count % 2) == 0) { gameMatch.TotalPairs = gameMatch.TotalPairs - (cardsUncovered.Count / 2); } else { gameMatch.TotalPairs = gameMatch.TotalPairs - ((cardsUncovered.Count - 1) / 2); } }
/// <inheritdoc/> public void JoinLobby(string host, string username) { ServiceMatch match = GetMatch(host); if (match != null) { ServiceLobby lobby = match.Lobby; lobby.AddPlayerToLobby(host, username); lobby.NotifyNewPlayerEnteredLobby(username); } }
/// <summary> /// Change the active turn from one player to the next in the list of connected players. /// </summary> /// <param name="gameMatch">Match in which the operation is to be carried out.</param> /// <param name="indexOfPlayerWithCurrentTurn">Index in the list of the player with active turn.</param> /// <returns>Index of the new player with active turn.</returns> public int ChangeTurn(ServiceMatch gameMatch, int indexOfPlayerWithCurrentTurn) { if (indexOfPlayerWithCurrentTurn == (gameMatch.GetPlayersConnectedToMatch().Count - 1)) { indexOfPlayerWithCurrentTurn = 0; } else { indexOfPlayerWithCurrentTurn++; } return(indexOfPlayerWithCurrentTurn); }
/// <inheritdoc/> public void ExpelPlayer(ExpelVoteDto expelVote) { string host = expelVote.Host; ServiceMatch gameMatch = GetMatch(host); string usernameOfExpelPlayer = expelVote.UsernameOfExpelPlayer; int playerExpelVotes = gameMatch.AddExpelVote(usernameOfExpelPlayer); ServicePlayer voterPlayer = gameMatch.GetPlayer(expelVote.UsernameOfVoterPlayer); voterPlayer.AddPlayerVoted(usernameOfExpelPlayer); IList <ServicePlayer> playersInMatch = gameMatch.GetPlayersConnectedToMatch(); int numOfPlayers = playersInMatch.Count; if (playerExpelVotes > (numOfPlayers / 2)) { ServicePlayer playerWithActiveTurn = gameMatch.GetPlyerWithActiveTurn(); ServicePlayer expelPlayer = gameMatch.GetPlayer(usernameOfExpelPlayer); if (playerWithActiveTurn.Username.Equals(usernameOfExpelPlayer)) { expelPlayer = playerWithActiveTurn; int indexOfPlayerWithCurrentTurn = gameMatch.GetPlayersConnectedToMatch().IndexOf(playerWithActiveTurn); indexOfPlayerWithCurrentTurn = ChangeTurn(gameMatch, indexOfPlayerWithCurrentTurn); ServicePlayer nextPlayer = gameMatch.GetPlayersConnectedToMatch()[indexOfPlayerWithCurrentTurn]; playerWithActiveTurn.HasActiveTurn = false; nextPlayer.HasActiveTurn = true; foreach (var playerInMatch in playersInMatch) { playerInMatch.MatchServiceConnection.EndTurnOfExpelPlayer(nextPlayer.Username); } } IList <int> cardsUncovered = expelPlayer.GetUncoveredCards(); foreach (var playerConnected in playersInMatch) { var channel = playerConnected.MatchServiceConnection; channel.NotifyPlayerWasExpel(usernameOfExpelPlayer, cardsUncovered); } RemovePairs(gameMatch, cardsUncovered); gameMatch.RemovePlayer(usernameOfExpelPlayer); if (playersInMatch.Count == 1) { this.NotifyMatchHasEnded(host); } } }
public void TestPoint() { ServiceMatch target = new ServiceMatch(); List <IMatchEvent> events = new List <IMatchEvent>() { new MatchStarted("p1", "p2"), new MatchPoint("p1"), }; Match actual = target.Replay(events); Assert.AreEqual(actual.Score[0], 1); Assert.AreEqual(actual.Score[1], 0); }
/// <summary> /// Gets the match that matches the username provided. /// </summary> /// <param name="host">Name of the player who created the game.</param> /// <returns>A specific matchDto object</returns> private ServiceMatch GetMatch(string host) { ServiceMatch gameMatch = null; foreach (var match in _matches) { if (match.Host.Equals(host)) { gameMatch = match; break; } } return(gameMatch); }
/// <inheritdoc/> public void LeaveLobby(string host, string username) { ServiceMatch match = GetMatch(host); if (match != null) { ServiceLobby lobby = match.Lobby; lobby.RemovePlayerFromLobby(username); if (host.Equals(username)) { lobby.NotifyPlayersMatchHasBeenDeleted(); _matches.Remove(match); } else { lobby.NotifyOnePlayerLeftLobby(username); } } }
/// <inheritdoc/> public IList <PlayerInMatchDto> GetPlayersConnectedToMatch(string host) { ServiceMatch match = GetMatch(host); IList <ServicePlayer> playersConnectedToMatch = match.GetPlayersConnectedToMatch(); IList <PlayerInMatchDto> playersToBeReturned = new List <PlayerInMatchDto>(); foreach (var player in playersConnectedToMatch) { PlayerInMatchDto playerInMatch = new PlayerInMatchDto() { Username = player.Username, Score = player.Score, HasActiveTurn = player.HasActiveTurn, ExpulsionVotes = 0 }; playersToBeReturned.Add(playerInMatch); } return(playersToBeReturned); }
/// <inheritdoc/> public void LeaveMatch(string host, string username) { ServiceMatch gameMatch = GetMatch(host); IList <ServicePlayer> playersInMatch = gameMatch.GetPlayersConnectedToMatch(); ServicePlayer playerWithActiveTurn = gameMatch.GetPlyerWithActiveTurn(); ServicePlayer leavePlayer = gameMatch.GetPlayer(username); if (playerWithActiveTurn.Username.Equals(username)) { leavePlayer = playerWithActiveTurn; int indexOfPlayerWithCurrentTurn = gameMatch.GetPlayersConnectedToMatch().IndexOf(playerWithActiveTurn); indexOfPlayerWithCurrentTurn = ChangeTurn(gameMatch, indexOfPlayerWithCurrentTurn); ServicePlayer nextPlayer = gameMatch.GetPlayersConnectedToMatch()[indexOfPlayerWithCurrentTurn]; playerWithActiveTurn.HasActiveTurn = false; nextPlayer.HasActiveTurn = true; foreach (var playerInMatch in playersInMatch) { playerInMatch.MatchServiceConnection.EndTurnOfExpelPlayer(nextPlayer.Username); } } IList <int> cardsUncovered = leavePlayer.GetUncoveredCards(); foreach (var playerConnected in playersInMatch) { var channel = playerConnected.MatchServiceConnection; channel.NotifyPlayerLeaveMatch(username, cardsUncovered); } RemovePairs(gameMatch, cardsUncovered); gameMatch.RemovePlayer(username); if (playersInMatch.Count == 1) { this.NotifyMatchHasEnded(host); } }
public void TestAdvantage() { ServiceMatch target = new ServiceMatch(); List <IMatchEvent> events = new List <IMatchEvent>() { new MatchStarted("p1", "p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1"), new MatchPoint("p2"), new MatchPoint("p1") }; Match actual = target.Replay(events); Assert.AreEqual(actual.Score[0], 3); Assert.AreEqual(actual.Score[1], 3); Assert.AreEqual(actual.Deuce, false); Assert.AreEqual(actual.AdvantagePlayer, "p1"); }
/// <inheritdoc/> public void EnterMatch(string host, string username) { ServiceMatch gameMatch = GetMatch(host); bool hasActiveTurn = false; if (host.Equals(username)) { hasActiveTurn = true; } ServicePlayer player = new ServicePlayer() { Username = username, Score = 0, HasActiveTurn = hasActiveTurn, MatchServiceConnection = OperationContext.Current.GetCallbackChannel <IMatchServiceCallback>(), ExpulsionVotes = 0 }; gameMatch.AddPlayer(player); }