Esempio n. 1
0
        public async Task <IHttpActionResult> Route(BattalionModel model)
        {
            var game = _gameService.GetLatestSnapshot(model.Id);

            if (game == null)
            {
                return(NotFound());
            }
            var castle            = game.Castles?.FirstOrDefault(e => e.Id == model.CastleId);
            var destinationCastle = game.Castles?.FirstOrDefault(e => e.Id == model.DestinationCastleId);

            if (castle == null || destinationCastle == null)
            {
                return(NotFound());
            }
            if (model.MoveByPercent)
            {
                model.Soldiers = await AutoSelectSoldiers(game.Id, new Guid(castle.Id), model.PercentOfSelectedSoldiers);

                if (!model.Soldiers.Any())
                {
                    return(NotFound());
                }
            }
            var route = await GetBattalionRoute(game, castle, destinationCastle, model.Soldiers);

            if (route == null)
            {
                return(InternalServerError(new Exception("Can not get route between two castle!")));
            }
            return(Ok(route));
        }
Esempio n. 2
0
 /// <summary>
 /// Get route between two castle
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='model'>
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <object> RouteAsync(this IGame operations, BattalionModel model, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.RouteWithHttpMessagesAsync(model, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Esempio n. 3
0
        public async Task <string> BattalionAsync(Guid id, BattalionModel model, RouteModel route, string userId)
        {
            var game = await Repository.GetByIdAsync(id.ToString());

            if (game == null)
            {
                throw new KeyNotFoundException("Game not found");
            }
            var startBattleEvent = new StartBattalionEvent(model.Id, new Guid(model.CastleId), new Guid(model.DestinationCastleId),
                                                           model.Soldiers,
                                                           Mapper.Map <Route>(route),
                                                           userId,
                                                           model.DateTime, model.DateTime);

            _domain.AddEvent(id, startBattleEvent);
            return(startBattleEvent.Id.ToString());
        }
Esempio n. 4
0
        public async Task <IHttpActionResult> Battalion(string id, BattalionModel model)
        {
            var game = await _gameService.Build(new Guid(id), User.Identity.GetUserId(), -1);

            if (game == null || game.HasError)
            {
                return(NotFound());
            }
            var castle            = game.Castles?.FirstOrDefault(e => e.Id == model.CastleId);
            var destinationCastle = game.Castles?.FirstOrDefault(e => e.Id == model.DestinationCastleId);

            if (castle == null || destinationCastle == null)
            {
                return(NotFound());
            }
            var userId = User.Identity.GetUserId();

            if (castle.OwnerUserId != userId)
            {
                return(BadRequest("You don't have permission to performance this action on the castle"));
            }
            if (model.MoveByPercent)
            {
                model.Soldiers = await AutoSelectSoldiers(game.Id, new Guid(castle.Id), model.PercentOfSelectedSoldiers);

                if (!model.Soldiers.Any())
                {
                    return(NotFound());
                }
            }
            // get route
            RouteModel route = await GetBattalionRoute(game, castle, destinationCastle, model.Soldiers);

            if (route == null)
            {
                return(InternalServerError(new Exception("Can not get route between two castle!")));
            }
            return(Ok(await _gameService.BattalionAsync(game.Id, model, route, User.Identity.GetUserId())));
        }
Esempio n. 5
0
 /// <summary>
 /// Get route between two castle
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='model'>
 /// </param>
 public static object Route(this IGame operations, BattalionModel model)
 {
     return(operations.RouteAsync(model).GetAwaiter().GetResult());
 }
Esempio n. 6
0
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='id'>
 /// </param>
 /// <param name='model'>
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <string> BattalionAsync(this IGame operations, string id, BattalionModel model, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BattalionWithHttpMessagesAsync(id, model, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Esempio n. 7
0
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='id'>
 /// </param>
 /// <param name='model'>
 /// </param>
 public static string Battalion(this IGame operations, string id, BattalionModel model)
 {
     return(operations.BattalionAsync(id, model).GetAwaiter().GetResult());
 }