Exemplo n.º 1
0
        private void SaveHero_Button(object sender, RoutedEventArgs e)
        {
            var currentHero = (Hero)DataContext;

            stsBarTextBlock.Foreground = new SolidColorBrush(Colors.MediumVioletRed);
            if (HeroNameBox.Text == null || HeroNameBox.Text.Length < 3 || HeroNameBox.Text.Length > 60)
            {
                stsBarTextBlock.Text = "Hero name length allowed: 3-60!";
                HeroNameBox.Focus();
                HeroNameBox.SelectAll();
                return;
            }
            if (GoldBox.Text == null || GoldBox.Text == "" || decimal.Parse(GoldBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Gold cannot be negative or null!";
                GoldBox.Focus();
                GoldBox.SelectAll();
                return;
            }
            if (HpBox.Text == null || HpBox.Text == "" || double.Parse(HpBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Hp cannot be negative or null!";
                HpBox.Focus();
                HpBox.SelectAll();
                return;
            }
            if (ManaBox.Text == null || ManaBox.Text == "" || double.Parse(ManaBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Mana cannot be negative or null!";
                ManaBox.Focus();
                ManaBox.SelectAll();
                return;
            }
            if (DefenceBox.Text == null || DefenceBox.Text == "" || double.Parse(DefenceBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Defence cannot be negative or null!";
                DefenceBox.Focus();
                DefenceBox.SelectAll();
                return;
            }
            if (AttackPowerBox.Text == null || AttackPowerBox.Text == "" || double.Parse(AttackPowerBox.Text) < 0)
            {
                stsBarTextBlock.Text = "AttackPower cannot be negative or null!";
                AttackPowerBox.Focus();
                AttackPowerBox.SelectAll();
                return;
            }

            var hero = context.Heroes.FirstOrDefault(h => h.Id == currentHero.Id);

            hero.Name                  = HeroNameBox.Text;
            hero.Gold                  = decimal.Parse(GoldBox.Text);
            hero.HeroStats.Hp          = double.Parse(HpBox.Text);
            hero.HeroStats.Mana        = double.Parse(ManaBox.Text);
            hero.HeroStats.Defence     = double.Parse(DefenceBox.Text);
            hero.HeroStats.AttackPower = double.Parse(AttackPowerBox.Text);


            context.SaveChanges();
            stsBarTextBlock.Foreground = new SolidColorBrush(Colors.Green);
            stsBarTextBlock.Text       = "Success!";
        }
Exemplo n.º 2
0
        private void SaveHero_Button(object sender, RoutedEventArgs e)
        {
            stsBarTextBlock.Foreground = new SolidColorBrush(Colors.MediumVioletRed);
            if (HeroNameBox.Text == null || HeroNameBox.Text.Length < 3 || HeroNameBox.Text.Length > 60)
            {
                stsBarTextBlock.Text = "Hero name length allowed: 3-60!";
                HeroNameBox.Focus();
                HeroNameBox.SelectAll();
                return;
            }
            if (GoldBox.Text == null || GoldBox.Text == "" || decimal.Parse(GoldBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Gold cannot be negative or null!";
                GoldBox.Focus();
                GoldBox.SelectAll();
                return;
            }
            if (HpBox.Text == null || HpBox.Text == "" || double.Parse(HpBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Hp cannot be negative or null!";
                HpBox.Focus();
                HpBox.SelectAll();
                return;
            }
            if (ManaBox.Text == null || ManaBox.Text == "" || double.Parse(ManaBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Mana cannot be negative or null!";
                ManaBox.Focus();
                ManaBox.SelectAll();
                return;
            }
            if (DefenceBox.Text == null || DefenceBox.Text == "" || double.Parse(DefenceBox.Text) < 0)
            {
                stsBarTextBlock.Text = "Defence cannot be negative or null!";
                DefenceBox.Focus();
                DefenceBox.SelectAll();
                return;
            }
            if (AttackPowerBox.Text == null || AttackPowerBox.Text == "" || double.Parse(AttackPowerBox.Text) < 0)
            {
                stsBarTextBlock.Text = "AttackPower cannot be negative or null!";
                AttackPowerBox.Focus();
                AttackPowerBox.SelectAll();
                return;
            }

            var user = AuthenticationService.GetCurrentUser();

            var newHero = new Hero()
            {
                Name   = HeroNameBox.Text,
                Gold   = decimal.Parse(GoldBox.Text),
                UserId = user.Id
            };

            var newHeroStats = new HeroStats()
            {
                Hero        = newHero,
                Hp          = double.Parse(HpBox.Text),
                Mana        = double.Parse(ManaBox.Text),
                Defence     = double.Parse(DefenceBox.Text),
                AttackPower = double.Parse(AttackPowerBox.Text)
            };

            newHero.HeroStats = newHeroStats;

            context.Heroes.Add(newHero);
            context.HeroStatistics.Add(newHeroStats);
            context.SaveChanges();
            stsBarTextBlock.Foreground = new SolidColorBrush(Colors.Green);
            stsBarTextBlock.Text       = "Success!";
        }