diff --git a/main.go b/main.go index 9113608..e1b7f42 100644 --- a/main.go +++ b/main.go @@ -8,42 +8,27 @@ import ( ) func main() { - // log.Println("entering main") var ( mapDB string ) flag.StringVar(&mapDB, "map", "Aardwolf.db", "name of aardwolf map file") - // log.Println("parsing flags?") flag.Parse() - // log.Println("flags parsed") am, err := NewMapper(mapDB) if err != nil { log.Fatalf("error creating AardMapper: %s\n", err) } - log.Printf("databse opened: %#+v\n", am) + // log.Printf("databse opened: %#+v\n", am) - areas, err := am.Areas() - if err != nil { - log.Fatalf("error fetching Areas: %s\n", err) - } - log.Printf("found %d areas\n", len(areas)) - for k, v := range areas { - log.Printf("found Area: %s\n", k) - log.Printf("%#+v\n", v) - break - } - - rooms, err := am.Rooms() + exits, err := am.Exits() if err != nil { log.Fatalf("error fetching Rooms: %s\n", err) } - log.Printf("found %d rooms\n", len(rooms)) - for k, v := range rooms { - log.Printf("found Room: (%s,%s)[%s]\n", v.Name, v.Area, k) - log.Printf("%#+v\n", v) + for _, v := range exits { + // log.Printf("Area: %#+v\n", v) + log.Printf("RoomExit found: %s\n", v) break } diff --git a/mapper.go b/mapper.go index 516877a..094bafb 100644 --- a/mapper.go +++ b/mapper.go @@ -1,19 +1,17 @@ package main import ( + "database/sql" "fmt" + // "log" - "gorm.io/driver/sqlite" - "gorm.io/gorm" + _ "github.com/mattn/go-sqlite3" ) func NewMapper(fn string) (AardMapper, error) { var am AardMapper - db, err := gorm.Open(sqlite.Open(fn), &gorm.Config{}) - db.AutoMigrate(&Area{}) - db.AutoMigrate(&Room{}) - db.AutoMigrate(&Exit{}) + db, err := sql.Open("sqlite3", fn) if err != nil { return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err) @@ -23,33 +21,47 @@ func NewMapper(fn string) (AardMapper, error) { } type AardMapper struct { - DB *gorm.DB + DB *sql.DB } func (am AardMapper) Areas() (map[string]Area, error) { areas := make(map[string]Area) - x := make([]Area, 0) - tx := am.DB.Find(&x) + rows, err := am.DB.Query("select uid, name, texture, color, flags from areas;") + defer rows.Close() - if tx.Error != nil { - return nil, fmt.Errorf("error fetching areas: %w", tx.Error) + if err != nil { + return nil, fmt.Errorf("error selecting areas: %w", err) } - for _, a := range x { - areas[a.Uid] = a + for rows.Next() { + a := Area{} + err := rows.Scan(&a.Uid, &a.Name, &a.Texture, &a.Color, &a.Flags) + if err != nil { + return nil, fmt.Errorf("error scanning Areas: %d", err) + } + + areas[a.Uid.String] = a } return areas, nil } -func (am AardMapper) Rooms() (map[string]Room, error) { - rooms := make(map[string]Room) - x := make([]Room, 0) - tx := am.DB.Find(&x) +func (am AardMapper) Exits() ([]RoomExit, error) { + exits := make([]RoomExit, 0) + rows, err := am.DB.Query("select r.uid,r.name,r.building,r.info,r.notes,r.flags,r.area, r.norecall,r.noportal,e.dir,e.fromuid,e.touid,e.level from rooms r INNER JOIN exits e ON r.uid = e.fromuid;") + // rows, err := am.DB.Query("select e.dir,e.fromuid,e.touid,e.level from rooms r INNER JOIN exits e ON r.uid = e.fromuid;") + defer rows.Close() - if tx.Error != nil { - return nil, fmt.Errorf("error fetching areas: %w", tx.Error) + if err != nil { + return nil, fmt.Errorf("error selecting areas: %w", err) } - for _, a := range x { - rooms[a.Uid] = a + for rows.Next() { + e := RoomExit{} + // err := rows.Scan(&e.Dir, &e.Fromuid, &e.Touid, &e.Level) + err := rows.Scan(&e.Uid, &e.Name, &e.Building, &e.Info, &e.Notes, &e.Flags, &e.AreaName, &e.Norecall, &e.Noportal, &e.Dir, &e.Fromuid, &e.Touid, &e.Level) + if err != nil { + return nil, fmt.Errorf("error scanning Areas: %d", err) + } + + exits = append(exits, e) } - return rooms, nil + return exits, nil } diff --git a/models.go b/models.go index 558a5d0..f5afff4 100644 --- a/models.go +++ b/models.go @@ -1,27 +1,34 @@ package main import ( - "gorm.io/gorm" + "database/sql" + "fmt" ) type Area struct { - gorm.Model - Uid string - Name, Flags string + Uid, Name, Flags, Color, Texture sql.NullString } type Room struct { - gorm.Model - Uid string - Name, Area string - Building string - Info, Notes, Flags string - X, Y, Z int - Norecall, Noportal int - Exits []Exit `gorm:"foreignKey:Fromuid"` + Uid, Name, Building, Info, Notes, Flags sql.NullString + Area Area + AreaName sql.NullString + Norecall, Noportal sql.NullInt64 + Exits []Exit } type Exit struct { - gorm.Model - Dir, Fromuid, Touid, Level string + Dir sql.NullString + Fromuid, Touid sql.NullString + Level int +} + +type RoomExit struct { + Room + Exit +} + +func (re RoomExit) String() string { + + return fmt.Sprintf("[%s(%s) |%s> %s]", re.Name.String, re.Uid.String, re.Dir.String, re.Touid.String) } diff --git a/walk.go b/walk.go index 1833d72..629bd70 100644 --- a/walk.go +++ b/walk.go @@ -6,7 +6,7 @@ import ( ) func walk(r Room, parent *cgraph.Node, g *cgraph.Graph) error { - _, err := g.CreateNode(r.Uid) + _, err := g.CreateNode(r.Uid.String) if err != nil { return err }