示例#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);
            }
        }