コード例 #1
0
ファイル: Sauvegarde.cs プロジェクト: tilteffect/KTManagerApp
        public static void SetSerializedData(IKTContext db, string data, bool deleteFirst = true, Dictionary <string, Dictionary <string, string> > replacements = default)
        {
            SaveModel saveModel = JsonConvert.DeserializeObject <SaveModel>(DecompressString(data));

            if (deleteFirst)
            {
                DeleteUserData(db);
            }

            if (replacements != null && replacements.Count != 0)
            {
                saveModel = ReplaceData(saveModel, replacements);
            }

            var existing_options = db.Set <WarGearOption>()
                                   .Where(wgo => saveModel.Teams
                                          .SelectMany(t => t.Members)
                                          .SelectMany(m => m.MemberWarGearOptions)
                                          .Select(m => m.WarGearOptionId)
                                          .Contains(wgo.Id)
                                          )
                                   .Select(wgo => wgo.Id)
                                   .ToList();

            // import everything
            List <Team> equipes = saveModel.Teams;

            foreach (Team equipe in equipes)
            {
                db.Entry(equipe).State = EntityState.Added;
                foreach (Member membre in equipe.Members)
                {
                    db.Entry(membre).State = EntityState.Added;
                    foreach (MemberTrait MembreTrait in membre.MemberTraits)
                    {
                        db.Entry(MembreTrait).State = EntityState.Added;
                    }
                    foreach (MemberPsychic MembrePsychique in membre.MemberPsychics)
                    {
                        db.Entry(MembrePsychique).State = EntityState.Added;
                    }
                    foreach (MemberWeapon MembreArme in membre.MemberWeapons)
                    {
                        db.Entry(MembreArme).State = EntityState.Added;
                    }
                    foreach (MemberPower MembrePouvoir in membre.MemberPowers)
                    {
                        db.Entry(MembrePouvoir).State = EntityState.Added;
                    }
                    foreach (MemberWarGearOption MembreRemplacement in membre.MemberWarGearOptions)
                    {
                        // if the id still exists add it, otherwise skip
                        if (existing_options.Contains(MembreRemplacement.WarGearOptionId))
                        {
                            db.Entry(MembreRemplacement).State = EntityState.Added;
                        }
                    }
                }
            }

            db.SaveChanges();
        }
コード例 #2
0
ファイル: Sauvegarde.cs プロジェクト: sklarsa/KTManagerApp
        public static void SetSerializedData(
            IKTContext db,
            string data,
            bool deleteFirst = true,
            Dictionary <string, Dictionary <string, string> > replacements = default,
            Action <float?, string> callback = null
            )
        {
            callback?.Invoke(0, "Reading backup");
            SaveModel saveModel = JsonConvert.DeserializeObject <SaveModel>(DecompressString(data));
            int       size      = saveModel.Size;

            if (deleteFirst)
            {
                DeleteUserData(db);
            }

            callback?.Invoke(0, "Cleaning up old data");
            if (replacements != null && replacements.Count != 0)
            {
                saveModel = ReplaceData(saveModel, replacements);
            }

            var existing_options = db.Set <WarGearOption>()
                                   .Where(wgo => saveModel.Teams
                                          .SelectMany(t => t.Members)
                                          .SelectMany(m => m.MemberWarGearOptions)
                                          .Select(m => m.WarGearOptionId)
                                          .Contains(wgo.Id)
                                          )
                                   .Select(wgo => wgo.Id)
                                   .ToList();

            // import everything
            int         progress = 0;
            List <Team> equipes  = saveModel.Teams;

            foreach (Team equipe in equipes)
            {
                db.Entry(equipe).State = EntityState.Added;
                foreach (Member membre in equipe.Members)
                {
                    float percent = (float)progress / size;
                    callback?.Invoke(percent, $"Adding {membre.Name} from {equipe.Name}");
                    progress += 1;

                    db.Entry(membre).State = EntityState.Added;
                    foreach (MemberTrait MembreTrait in membre.MemberTraits)
                    {
                        db.Entry(MembreTrait).State = EntityState.Added;
                    }
                    foreach (MemberPsychic MembrePsychique in membre.MemberPsychics)
                    {
                        db.Entry(MembrePsychique).State = EntityState.Added;
                    }
                    foreach (MemberWeapon MembreArme in membre.MemberWeapons)
                    {
                        db.Entry(MembreArme).State = EntityState.Added;
                    }
                    foreach (MemberPower MembrePouvoir in membre.MemberPowers)
                    {
                        db.Entry(MembrePouvoir).State = EntityState.Added;
                    }
                    foreach (MemberWarGearOption MembreRemplacement in membre.MemberWarGearOptions)
                    {
                        // if the id still exists add it, otherwise skip
                        if (existing_options.Contains(MembreRemplacement.WarGearOptionId))
                        {
                            db.Entry(MembreRemplacement).State = EntityState.Added;
                        }
                    }
                }
            }

            db.SaveChanges();
        }