floor egg. put stuff into foyer

trunk
vilmibm 2022-12-22 22:19:25 -08:00
parent 1664d678cb
commit ec67b3169d
1 changed files with 48 additions and 6 deletions

View File

@ -24,6 +24,7 @@ type DB interface {
GetAccount(string) (*Account, error) GetAccount(string) (*Account, error)
StartSession(Account) (string, error) StartSession(Account) (string, error)
EndSession(string) error EndSession(string) error
ActiveSessions() ([]Session, error)
ClearSessions() error ClearSessions() error
// General // General
@ -33,7 +34,6 @@ type DB interface {
Ensure() error Ensure() error
// Presence // Presence
ActiveSessions() ([]Session, error)
AvatarBySessionID(string) (*Object, error) AvatarBySessionID(string) (*Object, error)
BedroomBySessionID(string) (*Object, error) BedroomBySessionID(string) (*Object, error)
MoveInto(toMove Object, container Object) error MoveInto(toMove Object, container Object) error
@ -91,13 +91,13 @@ func (db *pgDB) Ensure() error {
} }
} }
log.Printf("%#v", sysAcc) foyer, err := db.GetObject("system", "foyer")
if err != nil {
if _, err := db.GetObject("system", "foyer"); err != nil { // TODO actually check error. for now assuming it means does not exist
data := map[string]string{} data := map[string]string{}
data["name"] = "foyer" data["name"] = "foyer"
data["description"] = "a big room. the ceiling is painted with constellations." data["description"] = "a big room. the ceiling is painted with constellations."
foyer := &Object{ foyer = &Object{
Data: data, Data: data,
Script: "", Script: "",
// TODO default room script // TODO default room script
@ -107,6 +107,30 @@ func (db *pgDB) Ensure() error {
} }
} }
egg, err := db.GetObject("system", "floor egg")
if err != nil {
// TODO actually check error. for now assuming it means does not exist
data := map[string]string{}
data["name"] = "floor egg"
data["description"] = "it's an egg and it's on the floor."
egg = &Object{
Data: data,
Script: "",
// TODO default room script
}
if err = db.CreateObject(sysAcc, egg); err != nil {
return err
}
}
sysAva, err := db.GetAccountAvatar(*sysAcc)
if err != nil {
return fmt.Errorf("could not find avatar for system account: %w", err)
}
db.MoveInto(*sysAva, *foyer)
db.MoveInto(*egg, *foyer)
return nil return nil
} }
@ -343,11 +367,29 @@ func (db *pgDB) GetObject(owner, name string) (obj *Object, err error) {
WHERE owner = $1 AND data['name'] = $2` WHERE owner = $1 AND data['name'] = $2`
err = db.pool.QueryRow(ctx, stmt, owner, fmt.Sprintf(`"%s"`, name)).Scan( err = db.pool.QueryRow(ctx, stmt, owner, fmt.Sprintf(`"%s"`, name)).Scan(
&obj.ID, &obj.Avatar, &obj.Data, &obj.OwnerID, &obj.Script) &obj.ID, &obj.Avatar, &obj.Data, &obj.OwnerID, &obj.Script)
// TODO i think the escaping here is going to create a sadness ^
return return
} }
func (db *pgDB) GetAccountAvatar(account Account) (*Object, error) {
ctx := context.Background()
obj := &Object{
OwnerID: account.ID,
Avatar: true,
}
stmt := `
SELECT id, data, script
FROM objects
WHERE owner = $1 AND avatar IS true`
err := db.pool.QueryRow(ctx, stmt, account.ID).Scan(
&obj.ID, &obj.Data, &obj.Script)
if err != nil {
return nil, err
}
return obj, nil
}
func (db *pgDB) ActiveSessions() (out []Session, err error) { func (db *pgDB) ActiveSessions() (out []Session, err error) {
stmt := `SELECT id, account FROM sessions` stmt := `SELECT id, account FROM sessions`
rows, err := db.pool.Query(context.Background(), stmt) rows, err := db.pool.Query(context.Background(), stmt)