fix perms relation; complete moving avatar into bedroom

trunk
vilmibm 2022-08-01 18:00:43 +02:00
parent 043790c015
commit 3db69fd8fb
3 changed files with 42 additions and 36 deletions

View File

@ -143,6 +143,8 @@ func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld
}
}
// TODO make sure the Foyer is created as part of initial setup / migration
func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
var account *db.Account
account, err = s.db.CreateAccount(auth.Username, auth.Password)
@ -153,23 +155,23 @@ func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (s
var sessionID string
sessionID, err = s.db.StartSession(*account)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to start session for %d: %w", account.ID, err)
}
log.Printf("started session for %s", account.Name)
av, err := s.db.AvatarBySessionID(sessionID)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to find avatar for %s: %w", sessionID, err)
}
bedroom, err := s.db.BedroomBySessionID(sessionID)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to find bedroom for %s: %w", sessionID, err)
}
err = s.db.MoveInto(*av, *bedroom)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to move %d into %d: %w", av.ID, bedroom.ID, err)
}
// TODO send room info, avatar info to client (need to figure this out and update proto)
@ -200,6 +202,7 @@ func (s *gameWorldServer) Login(ctx context.Context, auth *proto.AuthInfo) (si *
// TODO other server functions
func main() {
// TODO at some point during startup clear out sessions
err := _main()
if err != nil {
log.Fatal(err.Error())

View File

@ -81,13 +81,6 @@ func (db *pgDB) CreateAccount(name, password string) (account *Account, err erro
return
}
var pid int
stmt = "INSERT INTO permissions DEFAULT VALUES RETURNING id"
err = tx.QueryRow(ctx, stmt).Scan(&pid)
if err != nil {
return
}
data := map[string]string{}
data["name"] = account.Name
data["description"] = fmt.Sprintf("a gaseous form. it smells faintly of %s.", randSmell())
@ -96,24 +89,34 @@ func (db *pgDB) CreateAccount(name, password string) (account *Account, err erro
Data: data,
}
stmt = "INSERT INTO objects ( avatar, data, perms, owner ) VALUES ( $1, $2, $3, $4 ) RETURNING id"
err = tx.QueryRow(ctx, stmt, av.Avatar, av.Data, pid, account.ID).Scan(&av.ID)
stmt = "INSERT INTO objects ( avatar, data, owner ) VALUES ( $1, $2, $3 ) RETURNING id"
err = tx.QueryRow(ctx, stmt, av.Avatar, av.Data, account.ID).Scan(&av.ID)
if err != nil {
return
}
stmt = "INSERT INTO permissions DEFAULT VALUES RETURNING id"
err = tx.QueryRow(ctx, stmt).Scan(&pid)
stmt = "INSERT INTO permissions (object) VALUES ( $1 )"
_, err = tx.Exec(ctx, stmt, av.ID)
if err != nil {
return
}
data = map[string]string{}
data["name"] = "your private bedroom"
bedroom := &Object{
Bedroom: true,
Data: data,
}
stmt = "INSERT INTO objects ( bedroom, data, perms, owner ) VALUES ( $1, $2, $3, $4 )"
_, err = tx.Exec(ctx, stmt, bedroom.Bedroom, bedroom.Data, pid, account.ID)
stmt = "INSERT INTO objects ( bedroom, data, owner ) VALUES ( $1, $2, $3 ) RETURNING id"
err = tx.QueryRow(ctx, stmt, bedroom.Bedroom, bedroom.Data, account.ID).Scan(&bedroom.ID)
if err != nil {
return
}
stmt = "INSERT INTO permissions (object) VALUES ( $1 )"
_, err = tx.Exec(ctx, stmt, bedroom.ID)
if err != nil {
return
}
@ -191,11 +194,11 @@ func (db *pgDB) AvatarBySessionID(sid string) (avatar *Object, err error) {
// TODO subquery
stmt := `
SELECT (id,avatar,bedroom,data)
SELECT id, avatar, data
FROM objects WHERE avatar = true AND owner = (
SELECT a.id FROM sessions s JOIN accounts a ON s.account = a.id WHERE s.id = $1)`
err = db.pool.QueryRow(context.Background(), stmt, sid).Scan(
&avatar.ID, &avatar.Avatar, &avatar.Bedroom, &avatar.Data)
&avatar.ID, &avatar.Avatar, &avatar.Data)
return
}
@ -204,11 +207,11 @@ func (db *pgDB) BedroomBySessionID(sid string) (bedroom *Object, err error) {
// TODO subquery
stmt := `
SELECT (id,avatar,bedroom,data)
SELECT id, bedroom, data
FROM objects WHERE bedroom = true AND owner = (
SELECT a.id FROM sessions s JOIN accounts a ON s.account = a.id WHERE s.id = $1)`
err = db.pool.QueryRow(context.Background(), stmt, sid).Scan(
&bedroom.ID, &bedroom.Avatar, &bedroom.Bedroom, &bedroom.Data)
&bedroom.ID, &bedroom.Bedroom, &bedroom.Data)
return
}
@ -226,7 +229,7 @@ func (db *pgDB) MoveInto(toMove Object, container Object) error {
return err
}
stmt = "INSERT INTO contains (contained, container) VALUES ($1, $1)"
stmt = "INSERT INTO contains (contained, container) VALUES ($1, $2)"
_, err = tx.Exec(ctx, stmt, toMove.ID, container.ID)
if err != nil {
return err

View File

@ -7,32 +7,32 @@ CREATE TABLE accounts (
CREATE TABLE sessions (
id varchar(100) PRIMARY KEY,
account integer references accounts ON DELETE CASCADE
account integer REFERENCES accounts ON DELETE CASCADE
);
CREATE TYPE perm AS ENUM ('owner', 'world');
-- owner = 1, world = 2
CREATE TABLE permissions (
id serial PRIMARY KEY,
read perm NOT NULL DEFAULT 'world',
write perm NOT NULL DEFAULT 'owner',
carry perm NOT NULL DEFAULT 'world',
exec perm NOT NULL DEFAULT 'world'
);
CREATE TABLE objects (
id serial PRIMARY KEY,
avatar boolean NOT NULL DEFAULT FALSE,
bedroom boolean NOT NULL DEFAULT FALSE,
data jsonb NOT NULL,
perms integer references permissions,
owner integer references accounts
owner integer REFERENCES accounts ON DELETE RESTRICT
);
-- owner = 1, world = 2
CREATE TABLE permissions (
id serial PRIMARY KEY,
read perm NOT NULL DEFAULT 'world',
write perm NOT NULL DEFAULT 'owner',
carry perm NOT NULL DEFAULT 'world',
exec perm NOT NULL DEFAULT 'world',
object integer REFERENCES objects ON DELETE CASCADE
);
CREATE TABLE contains (
container integer references objects ON DELETE RESTRICT,
contained integer references objects ON DELETE CASCADE
container integer REFERENCES objects ON DELETE RESTRICT,
contained integer REFERENCES objects ON DELETE CASCADE
);