From 747ac09cc289180df3345cb7f8a75dd328214b80 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 27 Jul 2022 21:05:48 -0500 Subject: [PATCH] add quitting --- client/cmd/main.go | 13 +++++++++++-- server/cmd/main.go | 11 +++++++++++ server/db/db.go | 4 ++-- server/db/schema.sql | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/client/cmd/main.go b/client/cmd/main.go index b6338fc..ac96e47 100644 --- a/client/cmd/main.go +++ b/client/cmd/main.go @@ -63,7 +63,9 @@ func (cs *ClientState) HandleInput(input string) error { input = input[1:] parts := strings.SplitN(input, " ", 1) verb = parts[0] - rest = parts[1] + if len(parts) > 1 { + rest = parts[1] + } } else { verb = "say" } @@ -73,7 +75,14 @@ func (cs *ClientState) HandleInput(input string) error { Rest: rest, } // TODO I'm punting on handling CommandAcks for now but it will be a nice UX thing later for showing connectivity problems - return cs.cmdStream.Send(cmd) + err := cs.cmdStream.Send(cmd) + if err != nil { + return err + } + if verb == "quit" || verb == "q" { + cs.App.Stop() + } + return nil } func (cs *ClientState) InitCommandStream() error { diff --git a/server/cmd/main.go b/server/cmd/main.go index eb1812a..b52a547 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -86,6 +86,8 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error for { cmd, err := stream.Recv() if err == io.EOF { + // TODO this doesn't really do anything. if a client + // disconnects without warning there's no EOF. return s.db.EndSession(sid) } if err != nil { @@ -93,6 +95,14 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error } sid = cmd.SessionInfo.SessionID + + log.Printf("verb %s in session %s", cmd.Verb, sid) + + if cmd.Verb == "quit" || cmd.Verb == "q" { + s.msgRouter[sid] = nil + log.Printf("ending session %s", sid) + return s.db.EndSession(sid) + } send := s.msgRouter[sid] msg := &proto.ClientMessage{ @@ -145,6 +155,7 @@ func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (s if err != nil { return nil, err } + log.Printf("started session for %s", a.Name) si = &proto.SessionInfo{SessionID: sessionID} diff --git a/server/db/db.go b/server/db/db.go index 552812c..f44de6f 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -109,7 +109,7 @@ func (db *pgDB) StartSession(a Account) (sessionID string, err error) { sessionID = uuid.New().String() - _, err = conn.Exec(context.Background(), "INSERT INTO sessions (session_id, account) VALUES ( $1, $2 )", sessionID, a.ID) + _, err = conn.Exec(context.Background(), "INSERT INTO sessions (id, account) VALUES ( $1, $2 )", sessionID, a.ID) return } @@ -125,7 +125,7 @@ func (db *pgDB) EndSession(sid string) error { return err } - _, err = conn.Exec(context.Background(), "DELETE FROM sessions WHERE id = ?", sid) + _, err = conn.Exec(context.Background(), "DELETE FROM sessions WHERE id = $1", sid) return err } diff --git a/server/db/schema.sql b/server/db/schema.sql index 6705692..2d01b62 100644 --- a/server/db/schema.sql +++ b/server/db/schema.sql @@ -6,7 +6,7 @@ CREATE TABLE accounts ( ); CREATE TABLE sessions ( - session_id varchar(100) PRIMARY KEY, + id varchar(100) PRIMARY KEY, account integer references accounts ON DELETE CASCADE );