refactored for less code duplication
This commit is contained in:
@@ -233,13 +233,7 @@ func ExportSkills(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch skills: %w", err)
|
return fmt.Errorf("failed to fetch skills: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all sources for mapping
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all skill category difficulties
|
// Get all skill category difficulties
|
||||||
var scds []models.SkillCategoryDifficulty
|
var scds []models.SkillCategoryDifficulty
|
||||||
@@ -285,35 +279,9 @@ func ImportSkills(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all sources for mapping
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
categoryMap := buildCategoryMap()
|
||||||
database.DB.Find(&sources)
|
difficultyMap := buildDifficultyMap()
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all categories for mapping
|
|
||||||
var categories []models.SkillCategory
|
|
||||||
database.DB.Find(&categories)
|
|
||||||
categoryMap := make(map[string]map[string]uint) // game_system -> name -> id
|
|
||||||
for _, c := range categories {
|
|
||||||
if categoryMap[c.GameSystem] == nil {
|
|
||||||
categoryMap[c.GameSystem] = make(map[string]uint)
|
|
||||||
}
|
|
||||||
categoryMap[c.GameSystem][c.Name] = c.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all difficulties for mapping
|
|
||||||
var difficulties []models.SkillDifficulty
|
|
||||||
database.DB.Find(&difficulties)
|
|
||||||
difficultyMap := make(map[string]map[string]uint) // game_system -> name -> id
|
|
||||||
for _, d := range difficulties {
|
|
||||||
if difficultyMap[d.GameSystem] == nil {
|
|
||||||
difficultyMap[d.GameSystem] = make(map[string]uint)
|
|
||||||
}
|
|
||||||
difficultyMap[d.GameSystem][d.Name] = d.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var skill models.Skill
|
var skill models.Skill
|
||||||
@@ -426,43 +394,21 @@ func ImportSources(inputDir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var source models.Source
|
source := models.Source{
|
||||||
result := database.DB.Where("code = ?", exp.Code).First(&source)
|
Code: exp.Code,
|
||||||
|
Name: exp.Name,
|
||||||
|
FullName: exp.FullName,
|
||||||
|
Edition: exp.Edition,
|
||||||
|
Publisher: exp.Publisher,
|
||||||
|
PublishYear: exp.PublishYear,
|
||||||
|
Description: exp.Description,
|
||||||
|
IsCore: exp.IsCore,
|
||||||
|
IsActive: exp.IsActive,
|
||||||
|
GameSystem: exp.GameSystem,
|
||||||
|
}
|
||||||
|
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
if err := findOrCreateByCode(exp.Code, &source, "source"); err != nil {
|
||||||
// Create new source
|
return err
|
||||||
source = models.Source{
|
|
||||||
Code: exp.Code,
|
|
||||||
Name: exp.Name,
|
|
||||||
FullName: exp.FullName,
|
|
||||||
Edition: exp.Edition,
|
|
||||||
Publisher: exp.Publisher,
|
|
||||||
PublishYear: exp.PublishYear,
|
|
||||||
Description: exp.Description,
|
|
||||||
IsCore: exp.IsCore,
|
|
||||||
IsActive: exp.IsActive,
|
|
||||||
GameSystem: exp.GameSystem,
|
|
||||||
}
|
|
||||||
if err := database.DB.Create(&source).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to create source %s: %w", exp.Code, err)
|
|
||||||
}
|
|
||||||
} else if result.Error != nil {
|
|
||||||
return fmt.Errorf("failed to query source %s: %w", exp.Code, result.Error)
|
|
||||||
} else {
|
|
||||||
// Update existing source
|
|
||||||
source.Name = exp.Name
|
|
||||||
source.FullName = exp.FullName
|
|
||||||
source.Edition = exp.Edition
|
|
||||||
source.Publisher = exp.Publisher
|
|
||||||
source.PublishYear = exp.PublishYear
|
|
||||||
source.Description = exp.Description
|
|
||||||
source.IsCore = exp.IsCore
|
|
||||||
source.IsActive = exp.IsActive
|
|
||||||
source.GameSystem = exp.GameSystem
|
|
||||||
|
|
||||||
if err := database.DB.Save(&source).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to update source %s: %w", exp.Code, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -600,13 +546,7 @@ func ExportSkillCategories(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch skill categories: %w", err)
|
return fmt.Errorf("failed to fetch skill categories: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableSkillCategory, len(categories))
|
exportable := make([]ExportableSkillCategory, len(categories))
|
||||||
for i, cat := range categories {
|
for i, cat := range categories {
|
||||||
@@ -627,36 +567,17 @@ func ImportSkillCategories(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var category models.SkillCategory
|
category := models.SkillCategory{
|
||||||
result := database.DB.Where("name = ? AND game_system = ?", exp.Name, exp.GameSystem).First(&category)
|
Name: exp.Name,
|
||||||
|
GameSystem: exp.GameSystem,
|
||||||
|
SourceID: sourceMap[exp.SourceCode],
|
||||||
|
}
|
||||||
|
|
||||||
sourceID := sourceMap[exp.SourceCode]
|
if err := findOrCreateByNameAndSystem(exp.Name, exp.GameSystem, &category, "skill category"); err != nil {
|
||||||
|
return err
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
|
||||||
category = models.SkillCategory{
|
|
||||||
Name: exp.Name,
|
|
||||||
GameSystem: exp.GameSystem,
|
|
||||||
SourceID: sourceID,
|
|
||||||
}
|
|
||||||
if err := database.DB.Create(&category).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to create category %s: %w", exp.Name, err)
|
|
||||||
}
|
|
||||||
} else if result.Error != nil {
|
|
||||||
return fmt.Errorf("failed to query category %s: %w", exp.Name, result.Error)
|
|
||||||
} else {
|
|
||||||
category.SourceID = sourceID
|
|
||||||
if err := database.DB.Save(&category).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to update category %s: %w", exp.Name, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -689,21 +610,14 @@ func ImportSkillDifficulties(inputDir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var difficulty models.SkillDifficulty
|
difficulty := models.SkillDifficulty{
|
||||||
result := database.DB.Where("name = ? AND game_system = ?", exp.Name, exp.GameSystem).First(&difficulty)
|
Name: exp.Name,
|
||||||
|
GameSystem: exp.GameSystem,
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
}
|
||||||
difficulty = models.SkillDifficulty{
|
|
||||||
Name: exp.Name,
|
if err := findOrCreateByNameAndSystem(exp.Name, exp.GameSystem, &difficulty, "skill difficulty"); err != nil {
|
||||||
GameSystem: exp.GameSystem,
|
return err
|
||||||
}
|
|
||||||
if err := database.DB.Create(&difficulty).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to create difficulty %s: %w", exp.Name, err)
|
|
||||||
}
|
|
||||||
} else if result.Error != nil {
|
|
||||||
return fmt.Errorf("failed to query difficulty %s: %w", exp.Name, result.Error)
|
|
||||||
}
|
}
|
||||||
// No update needed for difficulties (only name and game_system)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -716,13 +630,7 @@ func ExportSpells(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch spells: %w", err)
|
return fmt.Errorf("failed to fetch spells: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableSpell, len(spells))
|
exportable := make([]ExportableSpell, len(spells))
|
||||||
for i, spell := range spells {
|
for i, spell := range spells {
|
||||||
@@ -757,13 +665,7 @@ func ImportSpells(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var spell models.Spell
|
var spell models.Spell
|
||||||
@@ -830,13 +732,7 @@ func ExportCharacterClasses(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch character classes: %w", err)
|
return fmt.Errorf("failed to fetch character classes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableCharacterClass, len(classes))
|
exportable := make([]ExportableCharacterClass, len(classes))
|
||||||
for i, class := range classes {
|
for i, class := range classes {
|
||||||
@@ -859,42 +755,19 @@ func ImportCharacterClasses(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var class models.CharacterClass
|
class := models.CharacterClass{
|
||||||
result := database.DB.Where("code = ?", exp.Code).First(&class)
|
Code: exp.Code,
|
||||||
|
Name: exp.Name,
|
||||||
|
Description: exp.Description,
|
||||||
|
SourceID: sourceMap[exp.SourceCode],
|
||||||
|
GameSystem: exp.GameSystem,
|
||||||
|
}
|
||||||
|
|
||||||
sourceID := sourceMap[exp.SourceCode]
|
if err := findOrCreateByCode(exp.Code, &class, "character class"); err != nil {
|
||||||
|
return err
|
||||||
if result.Error == gorm.ErrRecordNotFound {
|
|
||||||
class = models.CharacterClass{
|
|
||||||
Code: exp.Code,
|
|
||||||
Name: exp.Name,
|
|
||||||
Description: exp.Description,
|
|
||||||
SourceID: sourceID,
|
|
||||||
GameSystem: exp.GameSystem,
|
|
||||||
}
|
|
||||||
if err := database.DB.Create(&class).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to create character class %s: %w", exp.Code, err)
|
|
||||||
}
|
|
||||||
} else if result.Error != nil {
|
|
||||||
return fmt.Errorf("failed to query character class %s: %w", exp.Code, result.Error)
|
|
||||||
} else {
|
|
||||||
class.Name = exp.Name
|
|
||||||
class.Description = exp.Description
|
|
||||||
class.SourceID = sourceID
|
|
||||||
class.GameSystem = exp.GameSystem
|
|
||||||
|
|
||||||
if err := database.DB.Save(&class).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to update character class %s: %w", exp.Code, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -908,13 +781,7 @@ func ExportSpellSchools(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch spell schools: %w", err)
|
return fmt.Errorf("failed to fetch spell schools: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableSpellSchool, len(schools))
|
exportable := make([]ExportableSpellSchool, len(schools))
|
||||||
for i, school := range schools {
|
for i, school := range schools {
|
||||||
@@ -986,6 +853,7 @@ func ExportClassCategoryEPCosts(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch class category EP costs: %w", err)
|
return fmt.Errorf("failed to fetch class category EP costs: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build reverse maps for export (ID -> Code/Name)
|
||||||
var classes []models.CharacterClass
|
var classes []models.CharacterClass
|
||||||
database.DB.Find(&classes)
|
database.DB.Find(&classes)
|
||||||
classMap := make(map[uint]string)
|
classMap := make(map[uint]string)
|
||||||
@@ -1019,13 +887,9 @@ func ImportClassCategoryEPCosts(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var classes []models.CharacterClass
|
classMap := buildCharacterClassMap()
|
||||||
database.DB.Find(&classes)
|
|
||||||
classMap := make(map[string]uint)
|
|
||||||
for _, c := range classes {
|
|
||||||
classMap[c.Code] = c.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Build category map (name -> id) - need to aggregate by name since we don't have game_system in the exportable
|
||||||
var categories []models.SkillCategory
|
var categories []models.SkillCategory
|
||||||
database.DB.Find(&categories)
|
database.DB.Find(&categories)
|
||||||
categoryMap := make(map[string]uint)
|
categoryMap := make(map[string]uint)
|
||||||
@@ -1073,6 +937,7 @@ func ExportClassSpellSchoolEPCosts(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch class spell school EP costs: %w", err)
|
return fmt.Errorf("failed to fetch class spell school EP costs: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build reverse maps for export
|
||||||
var classes []models.CharacterClass
|
var classes []models.CharacterClass
|
||||||
database.DB.Find(&classes)
|
database.DB.Find(&classes)
|
||||||
classMap := make(map[uint]string)
|
classMap := make(map[uint]string)
|
||||||
@@ -1106,13 +971,9 @@ func ImportClassSpellSchoolEPCosts(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var classes []models.CharacterClass
|
classMap := buildCharacterClassMap()
|
||||||
database.DB.Find(&classes)
|
|
||||||
classMap := make(map[string]uint)
|
|
||||||
for _, c := range classes {
|
|
||||||
classMap[c.Code] = c.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Build spell school map (name -> id)
|
||||||
var schools []models.SpellSchool
|
var schools []models.SpellSchool
|
||||||
database.DB.Find(&schools)
|
database.DB.Find(&schools)
|
||||||
schoolMap := make(map[string]uint)
|
schoolMap := make(map[string]uint)
|
||||||
|
|||||||
@@ -12,59 +12,32 @@ import (
|
|||||||
// ExportSkillImprovementCosts exports all skill improvement costs to a JSON file
|
// ExportSkillImprovementCosts exports all skill improvement costs to a JSON file
|
||||||
func ExportSkillImprovementCosts(outputDir string) error {
|
func ExportSkillImprovementCosts(outputDir string) error {
|
||||||
var costs []models.SkillImprovementCost
|
var costs []models.SkillImprovementCost
|
||||||
if err := database.DB.Find(&costs).Error; err != nil {
|
if err := database.DB.Preload("SkillCategoryDifficulty.Skill").
|
||||||
|
Preload("SkillCategoryDifficulty.SkillCategory").
|
||||||
|
Preload("SkillCategoryDifficulty.SkillDifficulty").
|
||||||
|
Find(&costs).Error; err != nil {
|
||||||
return fmt.Errorf("failed to fetch skill improvement costs: %w", err)
|
return fmt.Errorf("failed to fetch skill improvement costs: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build maps for skill category difficulties
|
exportable := make([]ExportableSkillImprovementCost, 0, len(costs))
|
||||||
var scds []models.SkillCategoryDifficulty
|
for _, cost := range costs {
|
||||||
database.DB.Find(&scds)
|
// Skip records with incomplete relationships
|
||||||
scdMap := make(map[uint]models.SkillCategoryDifficulty)
|
if cost.SkillCategoryDifficulty.Skill.Name == "" ||
|
||||||
for _, scd := range scds {
|
cost.SkillCategoryDifficulty.SkillCategory.Name == "" ||
|
||||||
scdMap[scd.ID] = scd
|
cost.SkillCategoryDifficulty.SkillDifficulty.Name == "" {
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Get skills
|
exportable = append(exportable, ExportableSkillImprovementCost{
|
||||||
var skills []models.Skill
|
SkillName: cost.SkillCategoryDifficulty.Skill.Name,
|
||||||
database.DB.Find(&skills)
|
SkillSystem: cost.SkillCategoryDifficulty.Skill.GameSystem,
|
||||||
skillMap := make(map[uint]models.Skill)
|
CategoryName: cost.SkillCategoryDifficulty.SkillCategory.Name,
|
||||||
for _, s := range skills {
|
CategorySystem: cost.SkillCategoryDifficulty.SkillCategory.GameSystem,
|
||||||
skillMap[s.ID] = s
|
DifficultyName: cost.SkillCategoryDifficulty.SkillDifficulty.Name,
|
||||||
}
|
DifficultySystem: cost.SkillCategoryDifficulty.SkillDifficulty.GameSystem,
|
||||||
|
|
||||||
// Get categories
|
|
||||||
var categories []models.SkillCategory
|
|
||||||
database.DB.Find(&categories)
|
|
||||||
categoryMap := make(map[uint]models.SkillCategory)
|
|
||||||
for _, c := range categories {
|
|
||||||
categoryMap[c.ID] = c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get difficulties
|
|
||||||
var difficulties []models.SkillDifficulty
|
|
||||||
database.DB.Find(&difficulties)
|
|
||||||
difficultyMap := make(map[uint]models.SkillDifficulty)
|
|
||||||
for _, d := range difficulties {
|
|
||||||
difficultyMap[d.ID] = d
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableSkillImprovementCost, len(costs))
|
|
||||||
for i, cost := range costs {
|
|
||||||
scd := scdMap[cost.SkillCategoryDifficultyID]
|
|
||||||
skill := skillMap[scd.SkillID]
|
|
||||||
category := categoryMap[scd.SkillCategoryID]
|
|
||||||
difficulty := difficultyMap[scd.SkillDifficultyID]
|
|
||||||
|
|
||||||
exportable[i] = ExportableSkillImprovementCost{
|
|
||||||
SkillName: skill.Name,
|
|
||||||
SkillSystem: skill.GameSystem,
|
|
||||||
CategoryName: category.Name,
|
|
||||||
CategorySystem: category.GameSystem,
|
|
||||||
DifficultyName: difficulty.Name,
|
|
||||||
DifficultySystem: difficulty.GameSystem,
|
|
||||||
CurrentLevel: cost.CurrentLevel,
|
CurrentLevel: cost.CurrentLevel,
|
||||||
TERequired: cost.TERequired,
|
TERequired: cost.TERequired,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return writeJSON(filepath.Join(outputDir, "skill_improvement_costs.json"), exportable)
|
return writeJSON(filepath.Join(outputDir, "skill_improvement_costs.json"), exportable)
|
||||||
@@ -77,30 +50,36 @@ func ImportSkillImprovementCosts(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build lookup maps using helpers
|
||||||
|
skillMap := buildSkillMap()
|
||||||
|
categoryMap := buildCategoryMap()
|
||||||
|
difficultyMap := buildDifficultyMap()
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
// Find skill
|
// Find skill ID
|
||||||
var skill models.Skill
|
skillID, ok := skillMap[exp.SkillSystem][exp.SkillName]
|
||||||
if err := database.DB.Where("name = ? AND game_system = ?", exp.SkillName, exp.SkillSystem).First(&skill).Error; err != nil {
|
if !ok {
|
||||||
return fmt.Errorf("skill not found: %s: %w", exp.SkillName, err)
|
return fmt.Errorf("skill not found: %s (%s)", exp.SkillName, exp.SkillSystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find category
|
// Find category ID
|
||||||
var category models.SkillCategory
|
categoryID, ok := categoryMap[exp.CategorySystem][exp.CategoryName]
|
||||||
if err := database.DB.Where("name = ? AND game_system = ?", exp.CategoryName, exp.CategorySystem).First(&category).Error; err != nil {
|
if !ok {
|
||||||
return fmt.Errorf("category not found: %s: %w", exp.CategoryName, err)
|
return fmt.Errorf("category not found: %s (%s)", exp.CategoryName, exp.CategorySystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find difficulty
|
// Find difficulty ID
|
||||||
var difficulty models.SkillDifficulty
|
difficultyID, ok := difficultyMap[exp.DifficultySystem][exp.DifficultyName]
|
||||||
if err := database.DB.Where("name = ? AND game_system = ?", exp.DifficultyName, exp.DifficultySystem).First(&difficulty).Error; err != nil {
|
if !ok {
|
||||||
return fmt.Errorf("difficulty not found: %s: %w", exp.DifficultyName, err)
|
return fmt.Errorf("difficulty not found: %s (%s)", exp.DifficultyName, exp.DifficultySystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find SkillCategoryDifficulty
|
// Find SkillCategoryDifficulty
|
||||||
var scd models.SkillCategoryDifficulty
|
var scd models.SkillCategoryDifficulty
|
||||||
if err := database.DB.Where("skill_id = ? AND skill_category_id = ? AND skill_difficulty_id = ?",
|
if err := database.DB.Where("skill_id = ? AND skill_category_id = ? AND skill_difficulty_id = ?",
|
||||||
skill.ID, category.ID, difficulty.ID).First(&scd).Error; err != nil {
|
skillID, categoryID, difficultyID).First(&scd).Error; err != nil {
|
||||||
return fmt.Errorf("skill category difficulty not found: %w", err)
|
return fmt.Errorf("skill category difficulty not found for %s/%s/%s: %w",
|
||||||
|
exp.SkillName, exp.CategoryName, exp.DifficultyName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find or create SkillImprovementCost
|
// Find or create SkillImprovementCost
|
||||||
@@ -137,13 +116,7 @@ func ExportWeaponSkills(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch weapon skills: %w", err)
|
return fmt.Errorf("failed to fetch weapon skills: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableWeaponSkill, len(skills))
|
exportable := make([]ExportableWeaponSkill, len(skills))
|
||||||
for i, skill := range skills {
|
for i, skill := range skills {
|
||||||
@@ -173,13 +146,7 @@ func ImportWeaponSkills(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var skill models.WeaponSkill
|
var skill models.WeaponSkill
|
||||||
@@ -237,13 +204,7 @@ func ExportEquipment(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch equipment: %w", err)
|
return fmt.Errorf("failed to fetch equipment: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableEquipment, len(equipment))
|
exportable := make([]ExportableEquipment, len(equipment))
|
||||||
for i, eq := range equipment {
|
for i, eq := range equipment {
|
||||||
@@ -269,13 +230,7 @@ func ImportEquipment(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var eq models.Equipment
|
var eq models.Equipment
|
||||||
@@ -323,13 +278,7 @@ func ExportWeapons(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch weapons: %w", err)
|
return fmt.Errorf("failed to fetch weapons: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableWeapon, len(weapons))
|
exportable := make([]ExportableWeapon, len(weapons))
|
||||||
for i, weapon := range weapons {
|
for i, weapon := range weapons {
|
||||||
@@ -360,13 +309,7 @@ func ImportWeapons(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var weapon models.Weapon
|
var weapon models.Weapon
|
||||||
@@ -426,13 +369,7 @@ func ExportContainers(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch containers: %w", err)
|
return fmt.Errorf("failed to fetch containers: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableContainer, len(containers))
|
exportable := make([]ExportableContainer, len(containers))
|
||||||
for i, container := range containers {
|
for i, container := range containers {
|
||||||
@@ -460,13 +397,7 @@ func ImportContainers(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var container models.Container
|
var container models.Container
|
||||||
@@ -520,13 +451,7 @@ func ExportTransportation(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch transportation: %w", err)
|
return fmt.Errorf("failed to fetch transportation: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableTransportation, len(transportation))
|
exportable := make([]ExportableTransportation, len(transportation))
|
||||||
for i, trans := range transportation {
|
for i, trans := range transportation {
|
||||||
@@ -554,13 +479,7 @@ func ImportTransportation(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var trans models.Transportation
|
var trans models.Transportation
|
||||||
@@ -616,13 +535,7 @@ func ExportBelieves(outputDir string) error {
|
|||||||
return fmt.Errorf("failed to fetch believes: %w", err)
|
return fmt.Errorf("failed to fetch believes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMap()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[uint]string)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.ID] = s.Code
|
|
||||||
}
|
|
||||||
|
|
||||||
exportable := make([]ExportableBelieve, len(believes))
|
exportable := make([]ExportableBelieve, len(believes))
|
||||||
for i, believe := range believes {
|
for i, believe := range believes {
|
||||||
@@ -645,13 +558,7 @@ func ImportBelieves(inputDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get source map
|
sourceMap := buildSourceMapReverse()
|
||||||
var sources []models.Source
|
|
||||||
database.DB.Find(&sources)
|
|
||||||
sourceMap := make(map[string]uint)
|
|
||||||
for _, s := range sources {
|
|
||||||
sourceMap[s.Code] = s.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, exp := range exportable {
|
for _, exp := range exportable {
|
||||||
var believe models.Believe
|
var believe models.Believe
|
||||||
|
|||||||
@@ -0,0 +1,167 @@
|
|||||||
|
package gsmaster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bamort/database"
|
||||||
|
"bamort/models"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LookupMap builders - reusable functions to build ID<->Code/Name maps
|
||||||
|
|
||||||
|
// buildSourceMap creates a map from source ID to source code
|
||||||
|
func buildSourceMap() map[uint]string {
|
||||||
|
var sources []models.Source
|
||||||
|
database.DB.Find(&sources)
|
||||||
|
sourceMap := make(map[uint]string)
|
||||||
|
for _, s := range sources {
|
||||||
|
sourceMap[s.ID] = s.Code
|
||||||
|
}
|
||||||
|
return sourceMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildSourceMapReverse creates a map from source code to source ID
|
||||||
|
func buildSourceMapReverse() map[string]uint {
|
||||||
|
var sources []models.Source
|
||||||
|
database.DB.Find(&sources)
|
||||||
|
sourceMap := make(map[string]uint)
|
||||||
|
for _, s := range sources {
|
||||||
|
sourceMap[s.Code] = s.ID
|
||||||
|
}
|
||||||
|
return sourceMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildCategoryMap creates a nested map: game_system -> name -> id
|
||||||
|
func buildCategoryMap() map[string]map[string]uint {
|
||||||
|
var categories []models.SkillCategory
|
||||||
|
database.DB.Find(&categories)
|
||||||
|
categoryMap := make(map[string]map[string]uint)
|
||||||
|
for _, c := range categories {
|
||||||
|
if categoryMap[c.GameSystem] == nil {
|
||||||
|
categoryMap[c.GameSystem] = make(map[string]uint)
|
||||||
|
}
|
||||||
|
categoryMap[c.GameSystem][c.Name] = c.ID
|
||||||
|
}
|
||||||
|
return categoryMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildDifficultyMap creates a nested map: game_system -> name -> id
|
||||||
|
func buildDifficultyMap() map[string]map[string]uint {
|
||||||
|
var difficulties []models.SkillDifficulty
|
||||||
|
database.DB.Find(&difficulties)
|
||||||
|
difficultyMap := make(map[string]map[string]uint)
|
||||||
|
for _, d := range difficulties {
|
||||||
|
if difficultyMap[d.GameSystem] == nil {
|
||||||
|
difficultyMap[d.GameSystem] = make(map[string]uint)
|
||||||
|
}
|
||||||
|
difficultyMap[d.GameSystem][d.Name] = d.ID
|
||||||
|
}
|
||||||
|
return difficultyMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildCharacterClassMap creates a map from character class code to ID
|
||||||
|
func buildCharacterClassMap() map[string]uint {
|
||||||
|
var classes []models.CharacterClass
|
||||||
|
database.DB.Find(&classes)
|
||||||
|
classMap := make(map[string]uint)
|
||||||
|
for _, c := range classes {
|
||||||
|
classMap[c.Code] = c.ID
|
||||||
|
}
|
||||||
|
return classMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildSpellSchoolMap creates a nested map: game_system -> name -> id
|
||||||
|
func buildSpellSchoolMap() map[string]map[string]uint {
|
||||||
|
var schools []models.SpellSchool
|
||||||
|
database.DB.Find(&schools)
|
||||||
|
schoolMap := make(map[string]map[string]uint)
|
||||||
|
for _, s := range schools {
|
||||||
|
if schoolMap[s.GameSystem] == nil {
|
||||||
|
schoolMap[s.GameSystem] = make(map[string]uint)
|
||||||
|
}
|
||||||
|
schoolMap[s.GameSystem][s.Name] = s.ID
|
||||||
|
}
|
||||||
|
return schoolMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildSkillMap creates a nested map: game_system -> name -> id
|
||||||
|
func buildSkillMap() map[string]map[string]uint {
|
||||||
|
var skills []models.Skill
|
||||||
|
database.DB.Find(&skills)
|
||||||
|
skillMap := make(map[string]map[string]uint)
|
||||||
|
for _, s := range skills {
|
||||||
|
if skillMap[s.GameSystem] == nil {
|
||||||
|
skillMap[s.GameSystem] = make(map[string]uint)
|
||||||
|
}
|
||||||
|
skillMap[s.GameSystem][s.Name] = s.ID
|
||||||
|
}
|
||||||
|
return skillMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildWeaponSkillMap creates a nested map: game_system -> name -> id
|
||||||
|
func buildWeaponSkillMap() map[string]map[string]uint {
|
||||||
|
var weaponSkills []models.WeaponSkill
|
||||||
|
database.DB.Find(&weaponSkills)
|
||||||
|
weaponSkillMap := make(map[string]map[string]uint)
|
||||||
|
for _, ws := range weaponSkills {
|
||||||
|
if weaponSkillMap[ws.GameSystem] == nil {
|
||||||
|
weaponSkillMap[ws.GameSystem] = make(map[string]uint)
|
||||||
|
}
|
||||||
|
weaponSkillMap[ws.GameSystem][ws.Name] = ws.ID
|
||||||
|
}
|
||||||
|
return weaponSkillMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic import helper for entities with name + game_system natural key
|
||||||
|
type ImportConfig struct {
|
||||||
|
EntityName string // For error messages, e.g., "skill category"
|
||||||
|
}
|
||||||
|
|
||||||
|
// findOrCreateByNameAndSystem is a helper for import operations
|
||||||
|
// It looks up an entity by name and game_system, creates if not found
|
||||||
|
func findOrCreateByNameAndSystem(
|
||||||
|
name string,
|
||||||
|
gameSystem string,
|
||||||
|
model interface{},
|
||||||
|
entityName string,
|
||||||
|
) error {
|
||||||
|
result := database.DB.Where("name = ? AND game_system = ?", name, gameSystem).First(model)
|
||||||
|
|
||||||
|
if result.Error == gorm.ErrRecordNotFound {
|
||||||
|
if err := database.DB.Create(model).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to create %s %s: %w", entityName, name, err)
|
||||||
|
}
|
||||||
|
} else if result.Error != nil {
|
||||||
|
return fmt.Errorf("failed to query %s %s: %w", entityName, name, result.Error)
|
||||||
|
} else {
|
||||||
|
if err := database.DB.Save(model).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to update %s %s: %w", entityName, name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// findOrCreateByCode is a helper for import operations with code as natural key
|
||||||
|
func findOrCreateByCode(
|
||||||
|
code string,
|
||||||
|
model interface{},
|
||||||
|
entityName string,
|
||||||
|
) error {
|
||||||
|
result := database.DB.Where("code = ?", code).First(model)
|
||||||
|
|
||||||
|
if result.Error == gorm.ErrRecordNotFound {
|
||||||
|
if err := database.DB.Create(model).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to create %s %s: %w", entityName, code, err)
|
||||||
|
}
|
||||||
|
} else if result.Error != nil {
|
||||||
|
return fmt.Errorf("failed to query %s %s: %w", entityName, code, result.Error)
|
||||||
|
} else {
|
||||||
|
if err := database.DB.Save(model).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to update %s %s: %w", entityName, code, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user