Example #1
0
        public static async Task <Member> CreateFrom(string teamId, string modelProfileId)
        {
            ModelProfile declinaison = KTContext.Db.ModelProfiles
                                       .Where(e => e.Id == modelProfileId)
                                       .Include(m => m.CostOverrides)
                                       .Include(m => m.LevelCosts)
                                       .Include(m => m.WarGearOptions)
                                       .Include(m => m.ModelProfileWeapons)
                                       .ThenInclude(sn => sn.Weapon)
                                       .Include(m => m.Psychics)
                                       .Include(m => m.Model.ModelWeapons)
                                       .ThenInclude(sn => sn.Weapon)
                                       .AsNoTracking()
                                       .First();


            Team   equipe     = KTContext.Db.Teams.Find(teamId);
            int    nbSameName = equipe.Members.Where(m => m.Name.Contains(declinaison.Name)).Count();
            string membreNom  = declinaison.Name;

            if (nbSameName != 0)
            {
                int i = 1;
                while (equipe.Members.Where(m => m.Name == membreNom).Count() == 1)
                {
                    membreNom = declinaison.Name + " " + i;
                    i++;
                }
            }


            Member membre = new Member();

            membre.Id           = Guid.NewGuid().ToString();
            membre.Name         = membreNom;
            membre.ModelProfile = declinaison;
            membre.Level        = 1;
            membre.TeamId       = teamId;
            membre.Position     = KTContext.Db.Members
                                  .Where(m => m.TeamId == teamId)
                                  .Select(a => a.Position)
                                  .DefaultIfEmpty(0)
                                  .Max() + 1;

            KTContext.Db.Entry(membre).State = EntityState.Added;

            foreach (Weapon arme in membre.GetDefaultWeapons())
            {
                MemberWeapon membreArme = new MemberWeapon();
                membreArme.Id       = Guid.NewGuid().ToString();
                membreArme.Weapon   = arme;
                membreArme.WeaponId = arme.Id;
                membreArme.MemberId = membre.Id;
                KTContext.Db.Entry(membreArme).State = EntityState.Added;
            }
            membre.UpdateCost();

            if (declinaison.NumberOfPsychicsManifestationPerRound > 0)
            {
                Psychic       psybolt = KTContext.Db.Psychics.Find("1");
                MemberPsychic mp      = new MemberPsychic()
                {
                    Psychic = psybolt, PsychicId = psybolt.Id, Member = membre, MemberId = membre.Id
                };
                KTContext.Db.Entry(mp).State = EntityState.Added;

                foreach (Psychic psy in KTContext.Db.Psychics.Where(p => p.ModelProfileId == declinaison.Id).Take(declinaison.NumberOfKnownPsychics))
                {
                    MemberPsychic mp2 = new MemberPsychic()
                    {
                        Psychic = psy, PsychicId = psy.Id, Member = membre, MemberId = membre.Id
                    };
                    KTContext.Db.Entry(mp2).State = EntityState.Added;
                }
            }

            await KTContext.Db.SaveChangesAsync();

            return(membre);
        }
Example #2
0
        public static async Task <Member> DuplicateFrom(string memberId, string teamId = null)
        {
            Member duplicateMembre = duplicateMembre = KTContext.Db.Members
                                                       .Where(m => m.Id == memberId)
                                                       .Include(e => e.MemberPowers)
                                                       .ThenInclude(e => e.Power)
                                                       .Include(m => m.Specialist)
                                                       .ThenInclude(e => e.Tactics)
                                                       .Include(e => e.Specialist.Tactics)
                                                       .Include(e => e.Specialist.Powers)
                                                       .Include(e => e.MemberWeapons)
                                                       .ThenInclude(e => e.Weapon.WeaponProfiles)
                                                       .ThenInclude(e => e.WeaponType)
                                                       .Include(e => e.ModelProfile)
                                                       .ThenInclude(e => e.Abilities)
                                                       .ThenInclude(a => a.Details)
                                                       .Include(e => e.ModelProfile.Model)
                                                       .ThenInclude(e => e.Abilities)
                                                       .ThenInclude(a => a.Details)
                                                       .Include(e => e.ModelProfile.Tactics)
                                                       .Include(m => m.MemberTraits)
                                                       .ThenInclude(ma => ma.Trait)
                                                       .Include(m => m.ModelProfile.CostOverrides)
                                                       .Include(m => m.ModelProfile.LevelCosts)
                                                       .Include(m => m.MemberPsychics)
                                                       .ThenInclude(ma => ma.Psychic)
                                                       .AsNoTracking()
                                                       .First();

            Team equipe = new Team();

            if (teamId == null)
            {
                equipe = KTContext.Db.Teams
                         .AsNoTracking()
                         .Where(e => e.Id == duplicateMembre.TeamId)
                         .Include(e => e.Faction.Tactics)
                         .Include(e => e.Faction.Abilities)
                         .ThenInclude(a => a.Details)
                         .Include(e => e.Members)
                         .First();
            }

            Member membre = new Member();

            membre.Id            = Guid.NewGuid().ToString();
            membre.Name          = duplicateMembre.Name;
            membre.ModelProfile  = duplicateMembre.ModelProfile;
            membre.Level         = duplicateMembre.Level;
            membre.Specialist    = duplicateMembre.Specialist;
            membre.TeamId        = teamId != null ? teamId : duplicateMembre.TeamId;
            membre.Position      = teamId != null ? duplicateMembre.Position : equipe.Members.Count();
            membre.Xp            = duplicateMembre.Xp;
            membre.FleshWounds   = duplicateMembre.FleshWounds;
            membre.Convalescence = duplicateMembre.Convalescence;
            membre.Recruit       = duplicateMembre.Recruit;
            membre.Selected      = duplicateMembre.Selected;

            KTContext.Db.Entry(membre).State = EntityState.Added;

            foreach (Weapon arme in duplicateMembre.MemberWeapons.Select(a => a.Weapon))
            {
                MemberWeapon membreArme = new MemberWeapon();
                membreArme.Id       = Guid.NewGuid().ToString();
                membreArme.Weapon   = arme;
                membreArme.WeaponId = arme.Id;
                membreArme.MemberId = membre.Id;
                KTContext.Db.Entry(membreArme).State = EntityState.Added;
            }
            foreach (Trait trait in duplicateMembre.MemberTraits.Select(a => a.Trait))
            {
                MemberTrait membreTrait = new MemberTrait();
                membreTrait.Id       = Guid.NewGuid().ToString();
                membreTrait.Trait    = trait;
                membreTrait.TraitId  = trait.Id;
                membreTrait.MemberId = membre.Id;
                KTContext.Db.Entry(membreTrait).State = EntityState.Added;
            }

            foreach (Power pouvoir in duplicateMembre.MemberPowers.Select(a => a.Power))
            {
                MemberPower membrePouvoir = new MemberPower();
                membrePouvoir.Id        = Guid.NewGuid().ToString();
                membrePouvoir.Power     = pouvoir;
                membrePouvoir.PowerId   = pouvoir.Id;
                membrePouvoir.MembrerId = membre.Id;
                KTContext.Db.Entry(membrePouvoir).State = EntityState.Added;
            }
            foreach (Psychic psychique in duplicateMembre.MemberPsychics.Select(a => a.Psychic))
            {
                MemberPsychic membrePsychique = new MemberPsychic();
                membrePsychique.Id        = Guid.NewGuid().ToString();
                membrePsychique.Psychic   = psychique;
                membrePsychique.PsychicId = psychique.Id;
                membrePsychique.MemberId  = membre.Id;
                KTContext.Db.Entry(membrePsychique).State = EntityState.Added;
            }
            foreach (WarGearOption remplacement in duplicateMembre.MemberWarGearOptions.Select(a => a.WarGearOption))
            {
                MemberWarGearOption membreRemplacement = new MemberWarGearOption();
                membreRemplacement.Id                        = Guid.NewGuid().ToString();
                membreRemplacement.WarGearOption             = remplacement;
                membreRemplacement.WarGearOptionId           = remplacement.Id;
                membreRemplacement.MemberId                  = membre.Id;
                KTContext.Db.Entry(membreRemplacement).State = EntityState.Added;
            }

            membre.UpdateCost();

            if (teamId == null)
            {
                int    nbSameName = equipe.Members.Where(m => m.Name.Contains(membre.Name)).Count();
                string membreNom  = membre.Name;
                if (nbSameName != 0)
                {
                    int i = 1;
                    while (equipe.Members.Where(m => m.Name == membreNom).Count() == 1)
                    {
                        membreNom = membre.Name + " - " + Resx.Translate.Copier + " (" + i + ")";
                        i++;
                    }
                }
                membre.Name = membreNom;
                equipe.Members.Add(membre);
            }
            await KTContext.Db.SaveChangesAsync();

            return(membre);
        }