Home > Back-end >  New items are not saved to character class when logged out and logged back in
New items are not saved to character class when logged out and logged back in

Time:11-13

My "Hero" created character class has this code:

        public virtual User User { get; set; }


    public override List<Weapon> WeaponSack { get; set; } = new() { Weapon.ElectricityWand, Weapon.ElementalStaff };

Whenever I log back into a character, these items overwrite the weapons added through gameplay.

My looting class has this code which is supposed to add and save looted weapons to the character. The weapons are added successfully and I can use them until I log out and they get overwritten. I am able to modify player attributes like health and experience and save those by logging out and in, but I cannot see why the weapons won't stick:

                    {
                    var CurrentCharacter = SelectCharacter.CurrentHero;
                    //Damage player for a percentage of their health
                    var damage = CurrentCharacter.Health - (CurrentCharacter.Health * 0.70);
                    context.User.Update(user);
                    CurrentCharacter.LoseHealth(CurrentCharacter, CurrentCharacter.Health, (int)damage);
                    context.SaveChanges();
                    Console.WriteLine($"You have lost {damage} health.");
                    Console.WriteLine($"You have {CurrentCharacter.Health} health left.");
                    Console.WriteLine("Press any key to collect your loot!");
                    Console.ReadKey();
                    //Generate a random weapon
                    Random random = new Random();
                    var weapon = new Weapon();
                    var weaponName = new List<string> { "Sword", "Axe", "Mace", "Dagger", "Bow", "Staff", "Spear", "Wand", "Hammer", "Sickle" };
                    var weaponType = new List<string> { "Heavy", "Defensive", "Offensive", "Light", "Damaged", "Reinforced", "Magic", "Wooden", "Metallic", "Colorful" };
                    var weaponLevel = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                    var weaponDamage = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
                    var weaponRarity = new List<string> { "Common", "Uncommon", "Rare", "Epic", "Legendary" };
                    var weaponRarityChance = new List<int> { 70, 15, 10, 4, 1 };
                    var weaponRarityChanceTotal = weaponRarityChance.Sum();
                    var weaponRarityChanceRandom = random.Next(1, weaponRarityChanceTotal   1);
                    var weaponRarityChanceIndex = 0;
                    for (var i = 0; i < weaponRarityChance.Count; i  )
                    {
                        weaponRarityChanceIndex  = weaponRarityChance[i];
                        if (weaponRarityChanceRandom <= weaponRarityChanceIndex)
                        {
                            weapon.Rarity = weaponRarity[i];
                            break;
                        }
                    }
                    weapon.Name = weaponName[random.Next(0, weaponName.Count)];
                    weapon.Degradation = random.Next(1, 15);
                    weapon.Type = weaponType[random.Next(0, weaponType.Count)];
                    weapon.Damage = weaponDamage[random.Next(0, weaponDamage.Count)];
                    weapon.Level = weaponLevel[random.Next(0, weaponLevel.Count)];
                    context.Hero.Update(CurrentCharacter);
                    CurrentCharacter.WeaponSack.Add(weapon);
                    context.SaveChanges();
                    Console.WriteLine($"You have found a [level {weapon.Level}] [{weapon.Rarity}] ({weapon.Type} {weapon.Name})! It has {weapon.Degradation} uses left.");
                    Console.WriteLine("Press any key to continue your adventure!");
                    Console.ReadKey();
                    Adventure.AdventureStart(user, hero);
                    break;
                }

When weapons are looted, they are tied to the UserId in the database, but they won't stay in the Hero's weapon list. So I have a whole table of weapons tied to Id's that are inaccessible.

Is the public override of list in the Hero class my problem? I tried to find a way to retrieve the weapons that are tied to the userId and populate that in but I could not figure it out.

Classes as requested:

    namespace HeroModels
{

    public class Weapon
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public int Damage { get; set; }
    public int Level { get; set; }
    public string Rarity { get; set; }
    public string Type { get; set; }
    public int Degradation { get; set; }


    public Weapon(string name, int damage, int level, string rarity, string type, int degradation)
    {
        Name = name;
        Damage = damage;
        Level = level;
        Rarity = rarity;
        Type = type;
        Degradation = 10;
    }

    public Weapon()
    {
    }

    /*Magic weapons*/
    public static Weapon ElectricityWand =
        new(name: "Electricity Wand", damage: 5, level: 1, rarity: "Common", type: "Magic", degradation: 10);

    public static Weapon ElementalStaff =
        new(name: "Elemental Staff", damage: 30, level: 5, rarity: "Common", type: "Magic", degradation: 10);

    public int CalculateDamage(Weapon weapon)
    {

        Random random = new();
        var damage = random.Next(weapon.Damage);

        return damage;
    }


    }
}

Hero class :

   using HeroModels;
   using SolutionItems;
   using System.ComponentModel.DataAnnotations;
    
    namespace UserRegistration
    {
    public class Hero : IHero
    {
        public Hero()
        {
        }
        [Key]
        public new int Id { get; set; }
        public override string? Name { get; set; } = "Harry Potter";
        public override string Class { get; set; } = "Mage";

        public override int Health { get; set; } = 100;

        public override int Damage { get; set; } = 10;

        public override int Armor
        { get; set; } = 5;

        public override int Level
        { get; set; } = 1;

        public override int Experience
        { get; set; } = 0;

        public override int Gold
        { get; set; } = 0;

        public override int Strength
        { get; set; } = 10;

        public virtual User User { get; set; }


        public virtual List<Weapon> WeaponSack { get; set; } = new() { Weapon.ElectricityWand, Weapon.ElementalStaff };
        public override List<Food> FoodSack { get; set; } = new() { Food.trout, Food.salmon };

        public override string Attack(IHero hero)
        {
            return "";
        }

        public override int GainExperience(IHero hero, int experience)
        {
            return hero.Experience  = experience;
        }

        public override int GainGold(IHero hero, int gold)
        {
            return hero.Gold  = gold;
        }

        public override string GainHealth(IHero hero, int health)
        {
            hero.Health  = health;
            if (hero.Health >= 100)
            {
                hero.Health = 100;
            }
            return $"Your health is now at {hero.Health}!";
        }
        public override string LoseHealth(IHero hero, int health, int damage)
        {
            hero.Health -= damage;
            if (hero.Health <= 0)
            {
                Environment.Exit(1);
                return "You have died.";
            }
            return $"You have taken {damage} damage. Your health is now at {hero.Health}!";
        }

        public override string LevelUp(IHero hero)
        {
            hero.Level  = 1;
            var level = hero.Level;
            return $"Congratulations! You are now level {Level}";
        }
    }
}

How I load

    private DbContext LogIn()
{
    Console.WriteLine("Please enter your username");
    string username = Console.ReadLine();
    Console.WriteLine("Please enter your password");
    string password = Console.ReadLine();
    using var context = new HeroesDBContext(_optionsBuilder.Options);
    var usercompare = new HeroesDBContext().User.Where(u => u.Username == username && u.Password == password);

    if (!usercompare.Any())
    {
        Console.WriteLine("User not found");
        WelcomeScreen();
    }
    CurrentHero = context.Hero.FirstOrDefault(h => h.User.Username == username);
    CurrentUser = context.User.Where(u => u.Username == username && u.Password == password).FirstOrDefault();
    context.SaveChanges();
    Adventure.AdventureStart(CurrentUser, CurrentHero);

    return context;
}

CodePudding user response:

in the LogIn method you can load your weapon by eager Loading as following

CurrentHero = context.Hero.Include(h=>h.WeaponSack).FirstOrDefault(h => h.User.Username == username);
  • Related