From 7289d3845902f9b28d06dea113db315c02fc8f53 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 11 Jan 2023 20:41:39 -0800 Subject: [PATCH] fix some stuff. go works now but only in one direction --- server/cmd/main.go | 82 +++++++++++++++++++++++++++++-------------- server/db/db.go | 15 ++++++-- server/witch/witch.go | 22 +++++++++--- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/server/cmd/main.go b/server/cmd/main.go index a60ce7f..5aa9c84 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -101,50 +101,80 @@ func newServer() (*gameWorldServer, error) { } func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Object) error { + log.Printf("VH %s %s %d %d", verb, rest, sender.ID, target.ID) + s.scriptsMutex.RLock() sc, ok := s.scripts[target.ID] s.scriptsMutex.RUnlock() var err error - sid, _ := s.db.SessionIDForAvatar(target) + // TODO this is no longer closing over anything truly interesting and can + // likely be simplified serverAPI := witch.ServerAPI{ - Show: func(_ int, _ string) {}, - Tell: func(_ int, _ string) { log.Println("stub tell called") }, - DB: func() db.DB { - return s.db - }, - } - if sid != "" { - send := s.msgRouter[sid] - getSenderName := func(senderID int) *string { - senderName := "a mysterious stranger" - - sender, err := s.db.GetObjectByID(senderID) - if err == nil { - senderName = sender.Data["name"] - } else { + Show: func(fromObjID, toObjID int, action string) { + sid, err := s.db.SessionIDForObjID(toObjID) + if err != nil { log.Println(err.Error()) + return } - return &senderName - } - serverAPI.Show = func(senderID int, msg string) { + from, err := s.db.GetObjectByID(fromObjID) + if err != nil { + log.Println(err) + return + } + + speakerName := "an ethereal presence" + if from.Data["name"] != "" { + speakerName = from.Data["name"] + } + + log.Println(sid) + + send := s.msgRouter[sid] cm := proto.ClientMessage{ Type: proto.ClientMessage_EMOTE, - Text: msg, - Speaker: getSenderName(senderID), + Text: action, + Speaker: &speakerName, } send(&cm) - } - serverAPI.Tell = func(senderID int, msg string) { - log.Printf("Tell %s %d %s", sid, senderID, msg) + }, + Tell: func(fromObjID, toObjID int, msg string) { + log.Printf("Tell: %d %d %s", fromObjID, toObjID, msg) + sid, err := s.db.SessionIDForObjID(toObjID) + if err != nil { + log.Println(err) + return + } + + if sid == "" { + return + } + + from, err := s.db.GetObjectByID(fromObjID) + if err != nil { + log.Println(err) + return + } + + speakerName := "an ethereal presence" + if from.Data["name"] != "" { + speakerName = from.Data["name"] + } + + log.Println(sid) + + send := s.msgRouter[sid] cm := proto.ClientMessage{ Type: proto.ClientMessage_OVERHEARD, Text: msg, - Speaker: getSenderName(senderID), + Speaker: &speakerName, } send(&cm) - } + }, + DB: func() db.DB { + return s.db + }, } if !ok || sc == nil { diff --git a/server/db/db.go b/server/db/db.go index 717e1e9..5891214 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -37,6 +37,7 @@ type DB interface { // Presence SessionIDForAvatar(Object) (string, error) + SessionIDForObjID(int) (string, error) AvatarBySessionID(string) (*Object, error) BedroomBySessionID(string) (*Object, error) MoveInto(toMove Object, container Object) error @@ -117,7 +118,7 @@ func (db *pgDB) Ensure() error { roomScript := ` seen(function() - tellMe(my("description")) + tellSender(my("description")) end) ` @@ -176,7 +177,7 @@ func (db *pgDB) Ensure() error { Data: data, Script: ` go("north", function() - tellMe("the heavy door swings forward with ease. It creaks gently") + tellSender("the heavy door swings forward with ease. It creaks gently") moveSender("system", "pub") end) `, @@ -374,6 +375,16 @@ func (db *pgDB) SessionIDForAvatar(obj Object) (string, error) { return *sid, nil } +func (db *pgDB) SessionIDForObjID(id int) (string, error) { + obj, err := db.GetObjectByID(id) + if err != nil { + return "", err + } + + return db.SessionIDForAvatar(*obj) + +} + func (db *pgDB) AvatarBySessionID(sid string) (avatar *Object, err error) { avatar = &Object{} // TODO subquery diff --git a/server/witch/witch.go b/server/witch/witch.go index 28f1534..e553bd4 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -24,8 +24,8 @@ end) */ type ServerAPI struct { - Tell func(int, string) - Show func(int, string) + Tell func(int, int, string) + Show func(int, int, string) DB func() db.DB } @@ -73,8 +73,18 @@ 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)) + sc.serverAPI.Tell(senderID, vc.Target.ID, l.ToString(1)) + return 0 + })) + + l.SetGlobal("tellSender", 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(vc.Target.ID, senderID, l.ToString(1)) return 0 })) @@ -105,10 +115,14 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { l.SetGlobal("showMe", l.NewFunction(func(l *lua.LState) int { sender := l.GetGlobal("sender").(*lua.LTable) senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) - sc.serverAPI.Show(senderID, l.ToString(1)) + + log.Printf("showMe: %d %s", senderID, l.ToString(1)) + sc.serverAPI.Show(senderID, vc.Target.ID, l.ToString(1)) return 0 })) + // TODO showSender? + // TODO check execute permission and bail out potentially //log.Printf("%#v", vc)