add quitting

trunk
vilmibm 2022-07-27 21:05:48 -05:00
parent 4a6c6541fd
commit 747ac09cc2
4 changed files with 25 additions and 5 deletions

View File

@ -63,7 +63,9 @@ func (cs *ClientState) HandleInput(input string) error {
input = input[1:] input = input[1:]
parts := strings.SplitN(input, " ", 1) parts := strings.SplitN(input, " ", 1)
verb = parts[0] verb = parts[0]
rest = parts[1] if len(parts) > 1 {
rest = parts[1]
}
} else { } else {
verb = "say" verb = "say"
} }
@ -73,7 +75,14 @@ func (cs *ClientState) HandleInput(input string) error {
Rest: rest, Rest: rest,
} }
// TODO I'm punting on handling CommandAcks for now but it will be a nice UX thing later for showing connectivity problems // 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 { func (cs *ClientState) InitCommandStream() error {

View File

@ -86,6 +86,8 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error
for { for {
cmd, err := stream.Recv() cmd, err := stream.Recv()
if err == io.EOF { 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) return s.db.EndSession(sid)
} }
if err != nil { if err != nil {
@ -93,6 +95,14 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error
} }
sid = cmd.SessionInfo.SessionID 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] send := s.msgRouter[sid]
msg := &proto.ClientMessage{ msg := &proto.ClientMessage{
@ -145,6 +155,7 @@ func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (s
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("started session for %s", a.Name)
si = &proto.SessionInfo{SessionID: sessionID} si = &proto.SessionInfo{SessionID: sessionID}

View File

@ -109,7 +109,7 @@ func (db *pgDB) StartSession(a Account) (sessionID string, err error) {
sessionID = uuid.New().String() 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 return
} }
@ -125,7 +125,7 @@ func (db *pgDB) EndSession(sid string) error {
return err 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 return err
} }

View File

@ -6,7 +6,7 @@ CREATE TABLE accounts (
); );
CREATE TABLE sessions ( CREATE TABLE sessions (
session_id varchar(100) PRIMARY KEY, id varchar(100) PRIMARY KEY,
account integer references accounts ON DELETE CASCADE account integer references accounts ON DELETE CASCADE
); );