Example #1
0
        /// <summary>
        /// it returns a hero from another db through api
        /// searching by id
        /// </summary>
        /// <returns></returns>
        public static async Task <SuperHeroById.HeroById> GetHeroById(int id)
        {
            try
            {
                var baseAddress = new Uri(ConfigurationManager.AppSettings["apiUrl"].ToString());
                using (var httpClient = new HttpClient {
                    BaseAddress = baseAddress
                })
                {
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
                    using (var response = await httpClient.GetAsync($"{id}"))
                    {
                        var    hero     = new SuperHeroById.HeroById();
                        string jsonFile = await response.Content.ReadAsStringAsync();

                        hero = JsonConvert.DeserializeObject <SuperHeroById.HeroById>(jsonFile);

                        return(hero);
                    }
                }
            }
            catch (Exception)
            {
                throw new ApiNotFoundException("Api temporarily unavailable!");
            }
        }
        /// <summary>
        /// mapping a hero that we get from api,
        /// to a new favourite superhero, because the hero
        /// insn't in our db
        /// </summary>
        /// <param name="hero"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static FavouriteSuperHero MappingNewFavouriteHero(SuperHeroById.HeroById hero)
        {
            var newFavouriteHero = new FavouriteSuperHero();

            //mapping for our db from api
            newFavouriteHero.ApiId        = Convert.ToInt32(hero.ApiId);
            newFavouriteHero.Name         = hero.Name;
            newFavouriteHero.RealName     = hero.Biography.Full_Name;
            newFavouriteHero.ImgUrl       = hero.Image.Url;
            newFavouriteHero.Intelligence = MappingExtensions.StatStringToInt(hero.Powerstats.Intelligence);
            newFavouriteHero.Strength     = MappingExtensions.StatStringToInt(hero.Powerstats.Strength);
            newFavouriteHero.Speed        = MappingExtensions.StatStringToInt(hero.Powerstats.Speed);
            newFavouriteHero.Durability   = MappingExtensions.StatStringToInt(hero.Powerstats.Durability);
            newFavouriteHero.Power        = MappingExtensions.StatStringToInt(hero.Powerstats.Power);
            newFavouriteHero.Combat       = MappingExtensions.StatStringToInt(hero.Powerstats.Combat);

            return(newFavouriteHero);
        }
Example #3
0
        /// <summary>
        /// modelling a random enemy hero when api
        /// not working
        /// </summary>
        /// <param name="randomOpponentHero"></param>
        /// <returns></returns>
        internal static FavouriteSuperHero MappingWhenApiNotWorking(SuperHeroById.HeroById randomOpponentHero)
        {
            var opponentHero = new FavouriteSuperHero();

            try
            {
                opponentHero.ApiId        = 0;
                opponentHero.Name         = "Temporarily Unavailable!";
                opponentHero.RealName     = "Temporarily Unavailable!";
                opponentHero.ImgUrl       = "../../../img/pictureNA.jpg";
                opponentHero.Intelligence = 0;
                opponentHero.Strength     = 0;
                opponentHero.Speed        = 0;
                opponentHero.Durability   = 0;
                opponentHero.Power        = 0;
                opponentHero.Combat       = 0;

                return(opponentHero);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #4
0
        /// <summary>
        /// help modelling the data we get from the api
        /// </summary>
        /// <param name="randomOpponentHero"></param>
        /// <returns></returns>
        internal static FavouriteSuperHero Mapping(SuperHeroById.HeroById randomOpponentHero)
        {
            var opponentHero = new FavouriteSuperHero();

            try
            {
                opponentHero.ApiId        = Convert.ToInt32(randomOpponentHero.ApiId);
                opponentHero.Name         = randomOpponentHero.Name;
                opponentHero.RealName     = randomOpponentHero.Biography.Full_Name;
                opponentHero.ImgUrl       = randomOpponentHero.Image.Url;
                opponentHero.Intelligence = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Intelligence);
                opponentHero.Strength     = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Strength);
                opponentHero.Speed        = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Speed);
                opponentHero.Durability   = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Durability);
                opponentHero.Power        = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Power);
                opponentHero.Combat       = MappingExtensions.StatStringToInt(randomOpponentHero.Powerstats.Combat);

                return(opponentHero);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #5
0
 public FavouriteSuperHero MappingWhenApiNotWorking(SuperHeroById.HeroById randomOpponentHero)
 {
     return(BattleMapping.MappingWhenApiNotWorking(randomOpponentHero));
 }
Example #6
0
 /// <summary>
 /// mapping a hero that we get from api,
 /// to a new favourite superhero, because the hero
 /// insn't in our db
 /// </summary>
 /// <param name="hero"></param>
 /// <param name="user"></param>
 /// <returns></returns>
 public FavouriteSuperHero MappingNewFavouriteHero(SuperHeroById.HeroById hero)
 {
     return(DetailedHeroMapping.MappingNewFavouriteHero(hero));
 }
Example #7
0
 /// <summary>
 /// mappging the data from our api model
 /// </summary>
 /// <param name="model"></param>
 /// <param name="hero"></param>
 /// <returns></returns>
 public DetailedHeroViewModel MappingFromApi(DetailedHeroViewModel model, SuperHeroById.HeroById hero)
 {
     return(DetailedHeroMapping.MappingFromApi(model, hero));
 }
        /// <summary>
        /// mappging the data from our api model
        /// </summary>
        /// <param name="model"></param>
        /// <param name="hero"></param>
        /// <returns></returns>
        internal static DetailedHeroViewModel MappingFromApi(DetailedHeroViewModel model, SuperHeroById.HeroById hero)
        {
            try
            {
                model.ApiId    = hero.ApiId;
                model.Name     = hero.Name;
                model.ImageUrl = hero.Image.Url;
                model.BiographyData.Full_Name      = hero.Biography.Full_Name;
                model.BiographyData.Alignment      = hero.Biography.Alignment;
                model.BiographyData.Place_Of_Birth = hero.Biography.Place_Of_Birth;
                model.BiographyData.Publisher      = hero.Biography.Publisher;
                model.AppearanceValues.Gender      = hero.Appearance.Gender;
                model.AppearanceValues.Race        = hero.Appearance.Race;
                model.Powerstat.Intelligence       = hero.Powerstats.Intelligence;
                model.Powerstat.Strength           = hero.Powerstats.Strength;
                model.Powerstat.Speed      = hero.Powerstats.Speed;
                model.Powerstat.Durability = hero.Powerstats.Durability;
                model.Powerstat.Power      = hero.Powerstats.Power;
                model.Powerstat.Combat     = hero.Powerstats.Combat;

                return(model);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #9
0
        public async Task <ActionResult> BattleRandom(int?leftHeroApiId, int?rightHeroApiId)
        {
            try
            {
                #region Validation
                //if get nulls for heroes
                if (rightHeroApiId == null || leftHeroApiId == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                var userId            = User.Identity.GetUserId();
                var userHeroApiId     = (int)leftHeroApiId;
                var opponentHeroApiId = (int)rightHeroApiId;
                var model             = new BattleViewModel();

                //if something went wrong
                var userHeroApiIdList = objBs.FavouriteSuperHeroBLL.GetUserFavouriteHeroIdList(userId);
                if (!userHeroApiIdList.Contains(userHeroApiId))
                {
                    return(RedirectToAction("ChooseHero", "ChooseHeroView"));
                }
                #endregion

                var apiIdList = objBs.FavouriteSuperHeroBLL.GetFavouriteHeroIdList();
                var userHero  = objBs.FavouriteSuperHeroBLL.GetUserFavouriteHeroById(userHeroApiId, userId);
                model.UserHero = userHero;

                //the opponent hero is in our db, so don't need to call the API
                //just get the hero from the db
                if (apiIdList.Contains(opponentHeroApiId))
                {
                    var opponentHero = objBs.FavouriteSuperHeroBLL.GetFavouriteHeroById(opponentHeroApiId);
                    model.OpponentHero = opponentHero;

                    return(View("Battle", model));
                }

                //if hero don't exist in our db yet
                //then we call the API, and get the required data
                var randomOpponentHero = await ApiCall.GetHeroById(opponentHeroApiId);

                model.OpponentHero = objBs.battleBLL.Mapping(randomOpponentHero);

                return(View("Battle", model));
            }
            catch (ApiNotFoundException)
            {
                //mapping user's hero
                var userId        = User.Identity.GetUserId();
                var userHeroApiId = (int)leftHeroApiId;
                var model         = new BattleViewModel();
                var userHero      = objBs.FavouriteSuperHeroBLL.GetUserFavouriteHeroById(userHeroApiId, userId);
                model.UserHero = userHero;

                //mapping enemy hero
                var randomOpponentHero = new SuperHeroById.HeroById();
                model.OpponentHero = objBs.battleBLL.MappingWhenApiNotWorking(randomOpponentHero);

                return(View("Battle", model));
            }
            catch (Exception)
            {
                throw;
            }
        }