diff --git a/components/components.go b/components/components.go index 794c3dd..a28e60b 100644 --- a/components/components.go +++ b/components/components.go @@ -6,11 +6,11 @@ type ID struct { type Location struct { X, Y, Z int64 - World uint64 + World uint64 // this is being stored as an int in the db, overflow risk Exits []uint64 } -type LocationLink struct { +type Exit struct { To, From uint64 } @@ -23,10 +23,14 @@ type Inventory struct { Items []uint64 } -type HP struct { - Max, Current, Regen int64 -} +type PoolType uint64 + +const ( + Health = iota + Mana +) -type Mana struct { +type Pool struct { Max, Current, Regen int64 + Type PoolType } diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..cd5bbb8 --- /dev/null +++ b/db/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package db + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/db/models.go b/db/models.go new file mode 100644 index 0000000..481ea98 --- /dev/null +++ b/db/models.go @@ -0,0 +1,38 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package db + +import ( + "database/sql" +) + +type Exit struct { + ID int64 + To sql.NullInt64 + From sql.NullInt64 +} + +type Inventory struct { + ID int64 +} + +type Location struct { + ID int64 + X int64 + Y int64 + Z int64 + World int64 +} + +type Observable struct { + ID int64 + Name string + Description string +} + +type Pool struct { + ID int64 + Type int64 +} diff --git a/db/query.sql.go b/db/query.sql.go new file mode 100644 index 0000000..4c67bd6 --- /dev/null +++ b/db/query.sql.go @@ -0,0 +1,82 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 +// source: query.sql + +package db + +import ( + "context" + "database/sql" +) + +const getLocation = `-- name: GetLocation :one +select id, x, y, z, world from locations where id = ? LIMIT 1 +` + +func (q *Queries) GetLocation(ctx context.Context, id int64) (Location, error) { + row := q.db.QueryRowContext(ctx, getLocation, id) + var i Location + err := row.Scan( + &i.ID, + &i.X, + &i.Y, + &i.Z, + &i.World, + ) + return i, err +} + +const getLocationExitsFrom = `-- name: GetLocationExitsFrom :many +select id, "to", "from" from exits where from = ? LIMIT 1 +` + +func (q *Queries) GetLocationExitsFrom(ctx context.Context, from sql.NullInt64) ([]Exit, error) { + rows, err := q.db.QueryContext(ctx, getLocationExitsFrom, from) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Exit + for rows.Next() { + var i Exit + if err := rows.Scan(&i.ID, &i.To, &i.From); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getLocationExitsTo = `-- name: GetLocationExitsTo :many +select id, "to", "from" from exits where to = ? LIMIT 1 +` + +func (q *Queries) GetLocationExitsTo(ctx context.Context, to sql.NullInt64) ([]Exit, error) { + rows, err := q.db.QueryContext(ctx, getLocationExitsTo, to) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Exit + for rows.Next() { + var i Exit + if err := rows.Scan(&i.ID, &i.To, &i.From); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/query.sql b/query.sql new file mode 100644 index 0000000..4038480 --- /dev/null +++ b/query.sql @@ -0,0 +1,8 @@ +-- name: GetLocation :one +select * from locations where id = ? LIMIT 1; + +-- name: GetLocationExitsTo :many +select * from exits where to = ? LIMIT 1; + +-- name: GetLocationExitsFrom :many +select * from exits where from = ? LIMIT 1; diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..6e4025f --- /dev/null +++ b/schema.sql @@ -0,0 +1,28 @@ +create TABLE locations ( + id INTEGER PRIMARY KEY, + x INTEGER NOT NULL, + y INTEGER NOT NULL, + z INTEGER NOT NULL, + world INTEGER NOT NULL +) + +create TABLE exits ( + id INTEGER PRIMARY KEY, + to INTEGER REFERENCES locations(id), + from INTEGER REFERENCES locations(id) +) + +create TABLE observables ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + description TEXT NOT NULL +) + +create TABLE inventories ( + id INTEGER PRIMARY KEY +) + +create TABLE pools ( + id INTEGER PRIMARY KEY, + type INTEGER NOT NULL +) diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 0000000..10aef0c --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,9 @@ +version: "2" +sql: + - engine: "sqlite" + queries: "query.sql" + schema: "schema.sql" + gen: + go: + package: "db" + out: "db"