Пример #1
0
        private async Task InitializeSkillDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            // külön DTO class-ba parse-oljuk a JSON-ból
            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <MagusTtkSkillDefinitionDto[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(MagusTtkSkillDefinitionDto)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = dataContext.SkillDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Skill code in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal skill definition
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Skill code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                var newEntity = new MagusTtkSkillDefinition()
                {
                    Code  = entity.Code,
                    Group = entity.Group,
                    DisplayOrderInGroup = entity.DisplayOrderInGroup,
                    AbilityBase         = await GetValidatedSkillAbilityBase(entity, dataContext.AbilityDefinitions, cancellationToken),
                    AbilityBaseDependsOnSpecialization = entity.AbilityBaseDependsOnSpecialization,
                    IsSecret = entity.IsSecret,
                    RequiresSpecialization       = entity.RequiresSpecialization,
                    SupportsUniqueSpecialization = entity.SupportsUniqueSpecialization,
                    Specializations      = GetValidatedSkillSpecializations(entity),
                    Category             = await GetValidatedSkillCategory(entity, dataContext.SkillCategoryDefinitions, cancellationToken),
                    SkillClassDefinition = await GetValidatedSkillClassDefinition(entity, dataContext.SkillClassDefinitions, cancellationToken)
                };

                await editableRepo.Add(newEntity, cancellationToken);
            }
        }
Пример #2
0
        public async Task InitializeWeaponDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <MagusTtkWeaponDefinition[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(MagusTtkWeaponDefinition)}' from Json file '{jsonFilePath}'.");
            }

            var editableRepo = dataContext.WeaponDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Weapon code in file '{jsonFilePath}' is empty.");
                }

                // ha már van ilyen kóddal már fegyver
                if (await dataContext.WeaponDefinitions.ExistsByCode(entity.Code, cancellationToken))
                {
                    throw new ArgumentException($"Weapon code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                // ha nincs megadva egy fegyver kategória sem
                if ((entity.CategoryCodes == null) || (entity.CategoryCodes.Length == 0))
                {
                    throw new ArgumentException($"CategoryCodes are missing for weapon code '{entity.Code}' in file '{jsonFilePath}'.");
                }

                foreach (var weaponCategoryCode in entity.CategoryCodes)
                {
                    // ha nem valid fegyver kategória van megadva
                    if (!await dataContext.WeaponCategoryDefinitions.TryGetByCode(weaponCategoryCode, out var weaponCategory, cancellationToken))
                    {
                        throw new ArgumentException($"Weapon category code '{weaponCategoryCode}' in weapon definition '{entity.Code}' is unknown.");
                    }
                }

                // végül megpróbáljuk hozzáadni (elszáll, ha már lenne ilyen kóddal)
                await editableRepo.Add(entity, cancellationToken);
            }
        }
Пример #3
0
        private async Task InitializeBackgroundDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            // külön DTO class-ba parse-oljuk a JSON-ból
            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <Background <CodeOnlyAttribute>[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(Background<CodeOnlyAttribute>)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = dataContext.OriginDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Background code in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Background code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                var newEntity = new Background <CodeOnlyAttribute>()
                {
                    Code          = entity.Code,
                    Name          = entity.Name,
                    Advantages    = await GetValidatedSkillCodeReferences(entity, entity.Advantages, dataContext.SkillDefinitions, cancellationToken),
                    Disadvantages = await GetValidatedSkillCodeReferences(entity, entity.Disadvantages, dataContext.SkillDefinitions, cancellationToken)
                };

                await editableRepo.Add(newEntity, cancellationToken);
            }
        }
Пример #4
0
        private async Task InitializeTraitDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <TraitDefinition[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(TraitDefinition)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = dataContext.TraitDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Skill code in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal skill definition
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Skill code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                var newEntity = new TraitDefinition()
                {
                    Code     = entity.Code,
                    MaxValue = 3,
                    MinValue = 1
                };

                await editableRepo.Add(newEntity, cancellationToken);
            }
        }
Пример #5
0
        private async Task InitializeInMemoryRepositoryFromJson <TEntity>(MagusTtkContext dataContext, IReadOnlyRepository <TEntity> repo, string jsonFilePath, CancellationToken cancellationToken = default)
            where TEntity : IHasUniqueCode
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <TEntity[]>(jsonString);

            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(TEntity)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = repo.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Unique code of '{typeof(TEntity)}' in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                await editableRepo.Add(entity, cancellationToken);
            }
        }
Пример #6
0
        public async Task InitializeRaceDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <MagusTtkRaceDefinition[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(MagusTtkRaceDefinition)}' from Json file '{jsonFilePath}'.");
            }

            var editableRepo = dataContext.RaceDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Race code in file '{jsonFilePath}' is empty.");
                }

                // ha már van ilyen kóddal már faj
                if (await dataContext.RaceDefinitions.ExistsByCode(entity.Code, cancellationToken))
                {
                    throw new ArgumentException($"Race code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                // ha nincs megadva egy korkategória sem
                if ((entity.Aging == null) || (entity.Aging.Length == 0))
                {
                    throw new ArgumentException($"Aging categories are missing for race code '{entity.Code}' in file '{jsonFilePath}'.");
                }
                else
                {
                    AgingCategory previousAgingCategory = null;
                    foreach (var agingCategory in entity.Aging)
                    {
                        // ha negatív FromAge van megadva
                        if (agingCategory.FromAge < 0)
                        {
                            throw new ArgumentException($"FromAge value '{agingCategory.FromAge}' of aging category '{agingCategory.Name}' in file '{jsonFilePath}' cannot be negative.");
                        }
                        // ha a ToAge nem nagyobb, mint a FromAge
                        if ((agingCategory.ToAge != null) && (agingCategory.ToAge.Value <= agingCategory.FromAge))
                        {
                            throw new ArgumentException($"ToAge value '{agingCategory.ToAge.Value}' of aging category '{agingCategory.ToAge.Value}' in file '{jsonFilePath}' cannot be smaller than the FromAge '{agingCategory.FromAge}'.");
                        }
                        // ha negatív SpPerYear van megadva
                        if (agingCategory.SpPerYear < 0)
                        {
                            throw new ArgumentException($"SpPerYear value '{agingCategory.SpPerYear}' of aging category '{agingCategory.Name}' in file '{jsonFilePath}' cannot be negative.");
                        }
                        // ha negatív AgingFactor van megadva
                        if ((agingCategory.AgingFactor != null) && (agingCategory.AgingFactor.Value < 0))
                        {
                            throw new ArgumentException($"AgingFactor value '{agingCategory.AgingFactor.Value}' of aging category '{agingCategory.Name}' in file '{jsonFilePath}' cannot be negative.");
                        }

                        if (previousAgingCategory != null)
                        {
                            previousAgingCategory.ToAge = agingCategory.FromAge - 1;
                        }

                        previousAgingCategory = agingCategory;
                    }
                }

                // ha vannak tulajdonság módosítók
                if (entity.AbilityModifiers != null)
                {
                    foreach (var abilityModifier in entity.AbilityModifiers)
                    {
                        // ha nem létező tulajdonság van megadva
                        if (!await dataContext.AbilityDefinitions.TryGetByCode(abilityModifier.Key, out var abilityDef, cancellationToken))
                        {
                            throw new ArgumentException($"Ability modifier code '{abilityModifier.Key}' in race definition '{entity.Code}' is unknown.");
                        }
                    }
                }

                // végül megpróbáljuk hozzáadni (elszáll, ha már lenne ilyen kóddal)
                await editableRepo.Add(entity, cancellationToken);
            }
        }
Пример #7
0
        private async Task InitializeCharacterClassDefinitionInMemoryRepositoryFromJson(MagusTtkContext dataContext, string jsonFilePath, CancellationToken cancellationToken = default)
        {
            string jsonString = await fileContentResolver.ReadFileAsTextAsync(jsonFilePath, cancellationToken);

            if (string.IsNullOrWhiteSpace(jsonString))
            {
                throw new ArgumentException($"Json file '{jsonFilePath}' not found.");
            }

            // külön DTO class-ba parse-oljuk a JSON-ból
            var entitiesFromJson = JsonDeserializer.DeserializeFromJsonString <MagusTtkCharacterClassDefinitionDto[]>(jsonString);

            // JSON séma hiba
            if ((entitiesFromJson == null) || (entitiesFromJson.Length == 0))
            {
                throw new ArgumentException($"Could not parse entities of type '{typeof(MagusTtkCharacterClassDefinitionDto)}' from Json file '{jsonFilePath}'.");
            }

            HashSet <string> codeHashes = new HashSet <string>();
            var editableRepo            = dataContext.CharacterClassDefinitions.AsEditableRepository();

            foreach (var entity in entitiesFromJson)
            {
                // hiányzó Code
                if (string.IsNullOrWhiteSpace(entity.Code))
                {
                    throw new ArgumentException($"Character class code in file '{jsonFilePath}' is empty.");
                }

                // ha nem tudja hozzáadni, akkor már volt ilyen kóddal
                if (!codeHashes.Add(entity.Code))
                {
                    throw new ArgumentException($"Character class code '{entity.Code}' in file '{jsonFilePath}' is a duplicate.");
                }

                await ValidateCharacterClassAbilities(entity, entity.Abilities, dataContext.AbilityDefinitions, cancellationToken);

                ValidateCharacterClassLevelUpBonus(entity, entity.LevelUpBonus);

                // ha meg van adva, hogy melyik másik kasztból származik ez a kaszt definíció
                if (!string.IsNullOrWhiteSpace(entity.ParentClassCode))
                {
                    // de a hivatkozott kaszt nem ismert
                    if (!codeHashes.Contains(entity.ParentClassCode))
                    {
                        throw new ArgumentException($"ParentClassCode '{entity.ParentClassCode}' of character class code '{entity.Code}' in file '{jsonFilePath}' is unknown.");
                    }
                }

                var newEntity = new MagusTtkCharacterClassDefinition()
                {
                    Code            = entity.Code,
                    Name            = entity.Name,
                    Abilities       = entity.Abilities,
                    LevelUpBonus    = entity.LevelUpBonus,
                    ParentClassCode = entity.ParentClassCode,
                    Skills          = await GetValidatedCharacterClassSkills(entity, entity.Skills, dataContext.SkillDefinitions, cancellationToken),
                    Talents         = await GetValidatedCharacterClassTalents(entity, entity.Talents, dataContext.TalentDefinitions, cancellationToken)
                };

                await editableRepo.Add(newEntity, cancellationToken);
            }
        }