You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
// "log"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func NewMapper(fn string) (AardMapper, error) {
|
|
var am AardMapper
|
|
|
|
db, err := sql.Open("sqlite3", fn)
|
|
if err != nil {
|
|
|
|
return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err)
|
|
}
|
|
am.DB = db
|
|
return am, nil
|
|
}
|
|
|
|
type AardMapper struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
func (am AardMapper) Areas() (map[string]Area, error) {
|
|
areas := make(map[string]Area)
|
|
rows, err := am.DB.Query("select uid, name, texture, color, flags from areas;")
|
|
defer rows.Close()
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error selecting areas: %w", err)
|
|
}
|
|
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) 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 err != nil {
|
|
return nil, fmt.Errorf("error selecting areas: %w", err)
|
|
}
|
|
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 exits, nil
|
|
}
|