fix some stuff. go works now but only in one direction

trunk
vilmibm 2023-01-11 20:41:39 -08:00
parent f4934e28f3
commit 7289d38459
3 changed files with 87 additions and 32 deletions

View File

@ -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
from, err := s.db.GetObjectByID(fromObjID)
if err != nil {
log.Println(err)
return
}
serverAPI.Show = func(senderID int, msg string) {
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)
},
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
}
serverAPI.Tell = func(senderID int, msg string) {
log.Printf("Tell %s %d %s", sid, senderID, msg)
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 {

View File

@ -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

View File

@ -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)