private void SetFromPlan(CharacterPlan plan)
        {
            EnsureTwentyLevels(plan);

            CharacterName = plan.Name;
            Alignment     = plan.Alignment;

            Race  = plan.Race;
            Speed = plan.Speed;

            Age          = plan.Age;
            HeightFeet   = plan.HeightFeet;
            HeightInches = plan.HeightInches;
            Weight       = plan.Weight;
            EyeColor     = plan.EyeColor;
            HairColor    = plan.HairColor;
            SkinColor    = plan.SkinColor;

            CharacterBackground = plan.Background;

            ClassPlan.ClassName = plan.ClassPlan.ClassName;

            ClassPlan.ArmorProficiencies  = string.Join(", ", plan.ClassPlan.ArmorProficiencies ?? new string[0]);
            ClassPlan.WeaponProficiencies = string.Join(", ", plan.ClassPlan.WeaponProficiencies ?? new string[0]);
            ClassPlan.ToolProficiencies   = string.Join(", ", plan.ClassPlan.ToolProficiencies ?? new string[0]);

            var saveProfs = plan.ClassPlan.SaveProficiencies ?? new string[0];

            foreach (var saveProfVm in ClassPlan.SaveProficiencies)
            {
                saveProfVm.IsProficient = saveProfs.Contains(saveProfVm.Ability.Abbreviation);
            }

            var skillProfs = plan.ClassPlan.SkillProficiencies ?? new string[0];

            foreach (var skillProfVm in ClassPlan.SkillProficiencies)
            {
                skillProfVm.IsProficient = skillProfs.Contains(skillProfVm.Skill.SkillName);
            }

            LevelPlans.Clear();
            foreach (var lp in plan.LevelPlans)
            {
                var levelPlanVm = new LevelPlanViewModel
                {
                    Level = lp.Level
                };
                levelPlanVm.AbilityScoreImprovements.AddRange(
                    Ability.All.Select(a => new AbilityScoreImprovementViewModel
                {
                    Ability = a,
                }));


                foreach (var kvp in lp.AbilityScoreImprovements ?? new Dictionary <string, int>())
                {
                    var asiVm = levelPlanVm.AbilityScoreImprovements.First(asivm => asivm.Ability.Abbreviation == kvp.Key);
                    asiVm.Improvement = kvp.Value;
                }

                foreach (var feature in lp.FeaturePlans ?? new FeaturePlan[0])
                {
                    var featureVm = new FeaturePlanViewModel()
                    {
                        FeatureName = feature.Name,
                        Description = feature.Description,
                    };

                    levelPlanVm.NewFeatures.Add(featureVm);
                }

                LevelPlans.Add(levelPlanVm);
            }

            Armor.Clear();
            foreach (var armor in plan.Armor ?? new Armor[0])
            {
                Armor.Add(new ArmorViewModel()
                {
                    ArmorName                = armor.Name,
                    ArmorClass               = armor.ArmorClass,
                    ProficiencyGroup         = armor.ProficiencyGroup,
                    MaximumDexterityModifier = armor.MaximumDexterityModifier
                });
            }

            Weapons.Clear();
            foreach (var weapon in plan.Weapons ?? new Weapon[0])
            {
                Weapons.Add(new WeaponViewModel
                {
                    Name             = weapon.Name,
                    ProficiencyGroup = weapon.ProficiencyGroup,
                    DamageDice       = weapon.DamageDice,
                    DamageType       = weapon.DamageType,

                    NormalRange  = weapon.NormalRange,
                    MaximumRange = weapon.MaximumRange,

                    HasAmmunition = weapon.HasAmmunition,
                    IsLight       = weapon.IsLight
                });
            }

            // Set snapshot level last so that the SnapshotMarkdown property is
            // set correctly. Otherwise it will try to render at a level that
            // isn't in the list yet.
            SnapshotLevel = plan.SnapshotLevel;
        }
Esempio n. 2
0
        // TODO: :question: Consider moving ToSnapshot into an extension method.
        public PlayerCharacter ToSnapshot(int level)
        {
            var applicableLevels = LevelPlans.Where(l => l.Level <= level).ToList();

            var snapshot = new PlayerCharacter
            {
                CharacterLevel = level,

                Name         = Name,
                Race         = Race,
                Speed        = Speed,
                Alignment    = Alignment,
                Background   = Background,
                Age          = Age,
                HeightFeet   = HeightFeet,
                HeightInches = HeightInches,
                Weight       = Weight,
                EyeColor     = EyeColor,
                HairColor    = HairColor,
                SkinColor    = SkinColor,

                ClassName = ClassPlan.ClassName
            };

            foreach (var prof in ClassPlan.ArmorProficiencies ?? new string[0])
            {
                snapshot.ArmorProficiencies.Add(prof);
            }

            foreach (var prof in ClassPlan.WeaponProficiencies ?? new string[0])
            {
                snapshot.WeaponProficiencies.Add(prof);
            }

            foreach (var prof in ClassPlan.ToolProficiencies ?? new string[0])
            {
                snapshot.ToolProficiencies.Add(prof);
            }

            foreach (var savingThrowKey in ClassPlan.SaveProficiencies ?? new string[0])
            {
                snapshot.SavingThrows[savingThrowKey].IsProficient = true;
            }

            foreach (var skillName in ClassPlan.SkillProficiencies ?? new string[0])
            {
                snapshot.Skills[skillName].IsProficient = true;
            }

            foreach (var plan in applicableLevels)
            {
                foreach (var kvp in plan.AbilityScoreImprovements ?? new Dictionary <string, int>())
                {
                    snapshot.Abilities[kvp.Key].Score += kvp.Value;
                }

                foreach (var feature in plan.FeaturePlans ?? new FeaturePlan[0])
                {
                    snapshot.Features.Add(new FeatureSnapshot()
                    {
                        Name        = feature.Name,
                        Description = feature.Description
                    });
                }
            }

            foreach (var armor in Armor ?? new Armor[0])
            {
                snapshot.Armor.Add(new InventoryArmor(snapshot, armor));
            }

            foreach (var weapon in Weapons ?? new Weapon[0])
            {
                snapshot.Weapons.Add(new InventoryWeapon(snapshot, weapon));
            }

            return(snapshot);
        }
        private CharacterPlan GetPlan()
        {
            var plan = new CharacterPlan
            {
                SnapshotLevel = SnapshotLevel,

                Name      = CharacterName,
                Race      = Race,
                Speed     = Speed,
                Alignment = Alignment,

                Age          = Age,
                HeightFeet   = HeightFeet,
                HeightInches = HeightInches,
                Weight       = Weight,

                EyeColor  = EyeColor,
                HairColor = HairColor,
                SkinColor = SkinColor,

                Background = CharacterBackground,

                ClassPlan = new ClassPlan
                {
                    ClassName = ClassPlan.ClassName,

                    ArmorProficiencies  = ClassPlan.ArmorProficiencies.Split(',').Select(s => s.Trim()).ToArray(),
                    WeaponProficiencies = ClassPlan.WeaponProficiencies.Split(',').Select(s => s.Trim()).ToArray(),
                    ToolProficiencies   = ClassPlan.ToolProficiencies.Split(',').Select(s => s.Trim()).ToArray(),

                    SaveProficiencies = ClassPlan.SaveProficiencies
                                        .Where(s => s.IsProficient)
                                        .Select(s => s.Ability.Abbreviation).ToArray(),

                    SkillProficiencies = ClassPlan.SkillProficiencies
                                         .Where(s => s.IsProficient)
                                         .Select(s => s.Skill.SkillName).ToArray(),
                },
            };

            plan.LevelPlans = LevelPlans.Select(view => new LevelPlan
            {
                Level = view.Level,

                AbilityScoreImprovements = view.AbilityScoreImprovements
                                           .Where(asi => asi.Ability != null)
                                           .ToDictionary(asi => asi.Ability.Abbreviation, asi => asi.Improvement),

                FeaturePlans = view.NewFeatures
                               .Where(f => !string.IsNullOrWhiteSpace(f.FeatureName))
                               .Select(f => new FeaturePlan
                {
                    Name        = f.FeatureName,
                    Description = f.Description
                }).ToArray(),
            }).ToList();

            plan.Armor = Armor.Select(view => new Armor
            {
                Name                     = view.ArmorName,
                ArmorClass               = view.ArmorClass,
                ProficiencyGroup         = view.ProficiencyGroup,
                MaximumDexterityModifier = view.MaximumDexterityModifier
            }).ToList();

            plan.Weapons = Weapons.Select(view => new Weapon
            {
                Name             = view.Name,
                ProficiencyGroup = view.ProficiencyGroup,
                DamageDice       = view.DamageDice,
                DamageType       = view.DamageType,

                NormalRange  = view.NormalRange,
                MaximumRange = view.MaximumRange,

                HasAmmunition = view.HasAmmunition,
                IsLight       = view.IsLight
            }).ToList();

            return(plan);
        }