Exemplo n.º 1
0
        public HttpResponseMessage PutUpdateHero(HeroUpdateModel heroModel,
                                                 [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                var context = new StoreContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentOutOfRangeException("Invalid session key!");
                    }

                    var hero = user.Hero;
                    if (hero == null)
                    {
                        throw new ArgumentNullException("hero", "Cannot update non-existing hero!");
                    }

                    UpdateHero(hero, heroModel, context);
                    hero.Items = hero.Items;
                    context.SaveChanges();

                    var response =
                        this.Request.CreateResponse(HttpStatusCode.OK, new LoggedUserModel());
                    return(response);
                }
            });

            return(responseMsg);
        }
Exemplo n.º 2
0
        private void UpdateHero(Hero hero, HeroUpdateModel model, StoreContext context)
        {
            if (model.Name != null)
            {
                hero.Name = model.Name;
            }

            if (model.Items != null)
            {
                //foreach (var i in hero.Items)
                //{
                //    i.Heroes.Remove(hero);
                //}

                while (hero.Items.Count > 0)
                {
                    hero.Items.Remove(hero.Items.First());
                }
                foreach (var i in model.Items)
                {
                    var item = context.Items.FirstOrDefault(x => x.ItemId == i.ItemId);
                    if (item == null)
                    {
                        throw new ArgumentNullException("item", "Cannot use invalid item!");
                    }

                    hero.Items.Add(item);
                }
            }

            if (model.Id <= 0)
            {
                throw new ArgumentNullException("HeroId", "Invalid hero id!");
            }

            hero.MagicAttack  = model.MagicAttack;
            hero.MagicDefense = model.MagicDefense;
            hero.MeleAttack   = model.MeleAttack;
            hero.MeleDefense  = model.MeleDefense;
            hero.Experience   = model.Experience;
            hero.MaxHP        = model.MaxHP;
            hero.HP           = model.HP;
            hero.MaxMP        = model.MaxMP;
            hero.MP           = model.MP;
            hero.Level        = model.Level;
        }