fix session ending bug

trunk
vilmibm 2022-12-29 23:42:54 -05:00
parent 03e3aa866d
commit 3715a2f841
3 changed files with 22 additions and 104 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"errors"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -145,8 +144,7 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec
} }
if !ok || sc == nil { if !ok || sc == nil {
sc, err = witch.NewScriptContext(serverAPI) if sc, err = witch.NewScriptContext(serverAPI); err != nil {
if err != nil {
return err return err
} }
@ -171,31 +169,26 @@ func (s *gameWorldServer) HandleCmd(verb, rest string, sender *db.Object) {
// TODO // TODO
} }
func (s *gameWorldServer) endSession(sid string) error { func (s *gameWorldServer) endSession(sid string) {
var err error
var avatar *db.Object
log.Printf("ending session %s", sid) log.Printf("ending session %s", sid)
errors := []string{} avatar, err = s.db.AvatarBySessionID(sid)
err := s.db.EndSession(sid)
if err != nil { if err != nil {
errors = append(errors, err.Error()) log.Printf("error while ending session %s: %s", sid, err.Error())
}
s.msgRouter[sid] = nil
avatar, err := s.db.AvatarBySessionID(sid)
if err != nil {
errors = append(errors, err.Error())
} else { } else {
s.scriptsMutex.Lock() s.scriptsMutex.Lock()
s.scripts[avatar.ID] = nil delete(s.scripts, avatar.ID)
s.scriptsMutex.Unlock() s.scriptsMutex.Unlock()
} }
if len(errors) > 0 { if err = s.db.EndSession(sid); err != nil {
return fmt.Errorf("error(s) encountered trying to end session %s: %v", sid, errors) log.Printf("error while ending session %s: %s", sid, err.Error())
} }
return nil delete(s.msgRouter, sid)
} }
func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error { func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error {
@ -217,16 +210,15 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error
defer s.endSession(sid) defer s.endSession(sid)
} }
err = stream.Send(&proto.CommandAck{ if err = stream.Send(&proto.CommandAck{
Acked: true, Acked: true,
}) }); err != nil {
if err != nil {
log.Printf("unable to ack command in session %s", sid) log.Printf("unable to ack command in session %s", sid)
return err return err
} }
if send == nil { if send == nil {
log.Printf("saving a send fn for session %s", sid)
send = s.msgRouter[sid] send = s.msgRouter[sid]
} }
@ -272,8 +264,6 @@ 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) { func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
var account *db.Account var account *db.Account
account, err = s.db.CreateAccount(auth.Username, auth.Password) account, err = s.db.CreateAccount(auth.Username, auth.Password)
@ -293,18 +283,6 @@ func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (s
return nil, fmt.Errorf("failed to find avatar for %s: %w", sessionID, 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, fmt.Errorf("failed to find bedroom for %s: %w", sessionID, err)
}
err = s.db.MoveInto(*av, *bedroom)
if err != nil {
return nil, fmt.Errorf("failed to move %d into %d: %w", av.ID, bedroom.ID, err)
}
*/
foyer, err := s.db.GetObject("system", "foyer") foyer, err := s.db.GetObject("system", "foyer")
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to find foyer: %w", err) return nil, fmt.Errorf("failed to find foyer: %w", err)
@ -339,14 +317,13 @@ func (s *gameWorldServer) Login(ctx context.Context, auth *proto.AuthInfo) (si *
return nil, fmt.Errorf("failed to find avatar for %s: %w", sessionID, err) return nil, fmt.Errorf("failed to find avatar for %s: %w", sessionID, err)
} }
bedroom, err := s.db.BedroomBySessionID(sessionID) foyer, err := s.db.GetObject("system", "foyer")
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to find bedroom for %s: %w", sessionID, err) return nil, fmt.Errorf("failed to find foyer: %w", err)
} }
err = s.db.MoveInto(*av, *bedroom) if err = s.db.MoveInto(*av, *foyer); err != nil {
if err != nil { return nil, fmt.Errorf("failed to move %d into %d: %w", av.ID, foyer.ID, err)
return nil, fmt.Errorf("failed to move %d into %d: %w", av.ID, bedroom.ID, err)
} }
si = &proto.SessionInfo{SessionID: sessionID} si = &proto.SessionInfo{SessionID: sessionID}
@ -354,62 +331,6 @@ func (s *gameWorldServer) Login(ctx context.Context, auth *proto.AuthInfo) (si *
return return
} }
func (s *gameWorldServer) HandleSay(sender *db.Object, msg string) error {
name := sender.Data["name"]
if name == "" {
// TODO determine this based on a hash or something
name = "a mysterious figure"
}
heard, err := s.db.Earshot(*sender)
if err != nil {
log.Println(err.Error())
return err
}
log.Printf("found %#v in earshot of %#v\n", heard, sender)
as, err := s.db.ActiveSessions()
if err != nil {
return err
}
sendErrs := []error{}
// TODO figure out pointer shit
for _, h := range heard {
s.verbHandler("hears", msg, *sender, h)
// TODO once we have a script engine, deliver the HEARS event
for _, sess := range as {
if sess.AccountID == h.OwnerID {
cm := proto.ClientMessage{
Type: proto.ClientMessage_OVERHEARD,
Text: msg,
Speaker: &name,
}
err = s.msgRouter[sess.ID](&cm)
if err != nil {
sendErrs = append(sendErrs, err)
}
}
}
}
if len(sendErrs) > 0 {
errMsg := "send errors: "
for i, err := range sendErrs {
errMsg += err.Error()
if i < len(sendErrs)-1 {
errMsg += ", "
}
}
return errors.New(errMsg)
}
return nil
}
func (s *gameWorldServer) HandleError(send func(*proto.ClientMessage) error, err error) error { func (s *gameWorldServer) HandleError(send func(*proto.ClientMessage) error, err error) error {
log.Printf("error: %s", err.Error()) log.Printf("error: %s", err.Error())
msg := &proto.ClientMessage{ msg := &proto.ClientMessage{

View File

@ -1,15 +1,15 @@
package witch package witch
import ( import (
"log"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
func witchHas(l *lua.LState) int { func witchHas(l *lua.LState) int {
lv := l.ToTable(1)
log.Println(lv)
// TODO // TODO
/*
lv := l.ToTable(1)
log.Println(lv)
*/
return 0 return 0
} }
@ -20,7 +20,6 @@ func witchHears(l *lua.LState) int {
} }
func witchSees(l *lua.LState) int { func witchSees(l *lua.LState) int {
log.Println("adding handler for emote")
return addHandler(l, "emote") return addHandler(l, "emote")
} }

View File

@ -73,7 +73,6 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) {
})) }))
l.SetGlobal("showMe", l.NewFunction(func(l *lua.LState) int { l.SetGlobal("showMe", l.NewFunction(func(l *lua.LState) int {
log.Println("showMe called")
sender := l.GetGlobal("sender").(*lua.LTable) sender := l.GetGlobal("sender").(*lua.LTable)
senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) senderID := int(lua.LVAsNumber(sender.RawGetString("ID")))
sc.serverAPI.Show(senderID, l.ToString(1)) sc.serverAPI.Show(senderID, l.ToString(1))
@ -114,6 +113,5 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) {
} }
func (sc *ScriptContext) Handle(vc VerbContext) { func (sc *ScriptContext) Handle(vc VerbContext) {
log.Printf("%#v", sc)
sc.incoming <- vc sc.incoming <- vc
} }