public async Task IntegrationTestSetCardDiscarded() { // CHANGE GUIDS // Arrange NationRepository repository = new NationRepository(DevelopmentStorageAccountConnectionString); SessionRepository sessionRepository = new SessionRepository(DevelopmentStorageAccountConnectionString); Guid validGuid = new Guid("C6EA373B-63E8-481D-894C-9051F6710771"); Guid dummyRegionGuid = new Guid("7D194EC9-59ED-494F-8F77-9ED4B26F75FA"); String dummyUserId = "DummyUserId"; await sessionRepository.SetupSession(validGuid, dummyUserId); CardTableEntry ownedCard = await sessionRepository.SetupAddCard(validGuid, dummyRegionGuid, CardTableEntry.State.Owned, dummyUserId, 3); var dataTable = SessionRepository.GetTableForSessionData(TableClient, validGuid); // Act using (IBatchOperationHandle batchOperation = new BatchOperationHandle(sessionRepository.GetTableForSessionData(validGuid))) { repository.SetCardDiscarded(batchOperation, validGuid, dummyRegionGuid, ownedCard.CurrentEtag); } // Assert var operation = TableOperation.Retrieve <CardTableEntry>(validGuid.ToString(), "Card_" + dummyRegionGuid); var result = await dataTable.ExecuteAsync(operation); CardTableEntry resultStronglyTyped = result.Result as CardTableEntry; Assert.AreEqual(CardTableEntry.State.Discarded, resultStronglyTyped.OwnerState); Assert.AreEqual(dummyUserId, resultStronglyTyped.OwnerId); }
public async Task PostCards(Guid sessionId, IEnumerable <Guid> cards) { ISession session = await SessionRepository.GetSessionOrThrow(sessionId) .IsUserIdJoinedOrThrow(NationRepository, User.Identity.GetUserId()) .IsPhaseTypeOrThrow(SessionPhase.Reinforcements); Task <INationData> nationDataTask = NationRepository.GetNationOrThrow(sessionId, User.Identity.GetUserId()); Task <IEnumerable <ICardData> > playerCardsTask = NationRepository.GetCards(sessionId, User.Identity.GetUserId()); if (cards == null) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "Invalid card(s) specified" }); } HashSet <Guid> cardRegionIds = new HashSet <Guid>(); foreach (Guid cardRegionId in cards) { if (cardRegionIds.Contains(cardRegionId)) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "Duplicate card(s) specified" }); } else if (cardRegionIds.Count >= 3) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "Too many card(s) specified" }); } else { cardRegionIds.Add(cardRegionId); } } if (cardRegionIds.Count != 3) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "Too few card(s) specified" }); } using (IBatchOperationHandle batchOperation = SessionRepository.StartBatchOperation(sessionId)) { IEnumerable <ICardData> playerCards = await playerCardsTask; HashSet <UInt32> seenValues = new HashSet <UInt32>(); foreach (ICardData card in playerCards) { if (cardRegionIds.Contains(card.RegionId)) { NationRepository.SetCardDiscarded(batchOperation, sessionId, card.RegionId, card.CurrentEtag); seenValues.Add(card.Value); } } UInt32 additionalReinforcements = 0; if (seenValues.Count == 1) { additionalReinforcements = seenValues.First(); } else if (seenValues.Count == 3) { additionalReinforcements = 9; } else { await batchOperation.Abort(); throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "Invalid combination of card(s) specified" }); } INationData nation = await nationDataTask; NationRepository.SetAvailableReinforcements(batchOperation, sessionId, nation.UserId, nation.CurrentEtag, nation.AvailableReinforcements + additionalReinforcements); await batchOperation.CommitBatch(); } }