diff --git a/server/cmd/main.go b/server/cmd/main.go index da6d3f8..a60ce7f 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -109,7 +109,7 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec sid, _ := s.db.SessionIDForAvatar(target) serverAPI := witch.ServerAPI{ Show: func(_ int, _ string) {}, - Tell: func(_ int, _ string) {}, + Tell: func(_ int, _ string) { log.Println("stub tell called") }, DB: func() db.DB { return s.db }, @@ -137,6 +137,7 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec send(&cm) } serverAPI.Tell = func(senderID int, msg string) { + log.Printf("Tell %s %d %s", sid, senderID, msg) cm := proto.ClientMessage{ Type: proto.ClientMessage_OVERHEARD, Text: msg, @@ -239,6 +240,10 @@ func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error return s.HandleError(send, err) } + for _, obj := range affected { + log.Printf("%s heard %s from %d", obj.Data["name"], cmd.Verb, avatar.ID) + } + for _, o = range affected { if err = s.verbHandler(cmd.Verb, cmd.Rest, *avatar, o); err != nil { log.Printf("error handling verb %s for object %d: %s", cmd.Verb, o.ID, err) diff --git a/server/db/db.go b/server/db/db.go index a2b4e65..717e1e9 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -113,9 +113,11 @@ func (db *pgDB) Ensure() error { } } + // TODO for some reason, when the seen() callback runs for foyer we're calling the stub Tell instead of the sid-closured Tell. figure out why. + roomScript := ` seen(function() - tellSender(my("description")) + tellMe(my("description")) end) ` @@ -426,7 +428,8 @@ func (db *pgDB) Earshot(obj Object) ([]Object, error) { WHERE id IN ( SELECT contained FROM contains WHERE container = ( - SELECT container FROM contains WHERE contained = $1 LIMIT 1))` + SELECT container FROM contains WHERE contained = $1 LIMIT 1)) + OR id = (SELECT container FROM contains WHERE contained = $1 LIMIT 1)` rows, err := db.pool.Query(context.Background(), stmt, obj.ID) if err != nil { return nil, err diff --git a/server/witch/header.go b/server/witch/header.go index 14e40ce..39eef16 100644 --- a/server/witch/header.go +++ b/server/witch/header.go @@ -12,18 +12,18 @@ func witchHas(l *lua.LState) int { // TODO provides func witchHears(l *lua.LState) int { - return addHandler(l, "say") + return addPatternHandler(l, "say") } func witchSees(l *lua.LState) int { - return addHandler(l, "emote") + return addPatternHandler(l, "emote") } func witchGo(l *lua.LState) int { // TODO get the handler map // - check if handler map has a Go handler already, exit early if so // TODO register this object as an exit in DB - return addHandler(l, "go") + return addPatternHandler(l, "go") } func witchSeen(l *lua.LState) int { @@ -45,8 +45,29 @@ func witchDoes(ls *lua.LState) int { } func addHandler(l *lua.LState, verb string) int { - pattern := l.ToString(1) - cb := l.ToFunction(2) + pattern := ".*" + cb := l.ToFunction(1) + + //log.Printf("adding handler: %s %s %#v", verb, pattern, cb) + + handlers := l.GetGlobal("_handlers").(*lua.LTable) + + verbHandlers, ok := handlers.RawGetString(verb).(*lua.LTable) + if !ok { + verbHandlers = l.NewTable() + handlers.RawSetString(verb, verbHandlers) + } + + verbHandlers.RawSetString(pattern, cb) + + return 0 +} + +func addPatternHandler(l *lua.LState, verb string) int { + pattern := l.ToString(1) + cb := l.ToFunction(2) + + //log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb) handlers := l.GetGlobal("_handlers").(*lua.LTable) diff --git a/server/witch/witch.go b/server/witch/witch.go index cf99b82..28f1534 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -73,6 +73,7 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int { sender := l.GetGlobal("sender").(*lua.LTable) senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) + log.Printf("tellMe: %d %s", senderID, l.ToString(1)) sc.serverAPI.Tell(senderID, l.ToString(1)) return 0 })) @@ -109,7 +110,7 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { })) // TODO check execute permission and bail out potentially - log.Printf("%#v", vc) + //log.Printf("%#v", vc) senderT := l.NewTable() senderT.RawSetString("name", lua.LString(vc.Sender.Data["name"]))