Esempio n. 1
0
        public async Task <Guid> PostAttack(Guid sessionId, Guid regionId, uint numberOfTroops, Guid targetRegionId)
        {
            IRegionData sourceRegion = await RegionRepository.GetRegionOrThrow(sessionId, regionId)
                                       .IsRegionOwnerOrThrow(User.Identity.GetUserId());

            ISession session = await SessionRepository.GetSessionOrThrow(sourceRegion)
                               .IsUserIdJoinedOrThrow(NationRepository, User.Identity.GetUserId())
                               .IsPhaseTypeOrThrow(SessionPhase.CombatOrders);

            IRegionData targetRegion = await RegionRepository.GetRegionOrThrow(session.GameId, targetRegionId)
                                       .IsNotRegionOwnerOrThrow(User.Identity.GetUserId())
                                       .IsRegionConnectedOrThrow(sourceRegion.RegionId);

            if (sourceRegion.TroopCount <= (sourceRegion.TroopsCommittedToPhase + numberOfTroops))
            {
                throw new HttpResponseException(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "You do not have that many troops available to attack with"
                });
            }
            else if (numberOfTroops <= 0)
            {
                throw new HttpResponseException(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "You can not attack with less than 1 troop"
                });
            }
            else
            {
                Guid orderGuid;
                using (IBatchOperationHandle batchOperation = SessionRepository.StartBatchOperation(session.GameId))
                {
                    var queueAttackTask = CommandQueue.OrderAttack(batchOperation, session.GameId, session.PhaseId, sourceRegion.RegionId, sourceRegion.CurrentEtag, targetRegion.RegionId, numberOfTroops);
                    RegionRepository.CommitTroopsToPhase(batchOperation, sourceRegion, numberOfTroops);
                    await batchOperation.CommitBatch();

                    orderGuid = await queueAttackTask;
                }

                return(orderGuid);
            }
        }
Esempio n. 2
0
        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();
            }
        }