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 { 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() s.scriptsMutex.RLock()
sc, ok := s.scripts[target.ID] sc, ok := s.scripts[target.ID]
s.scriptsMutex.RUnlock() s.scriptsMutex.RUnlock()
var err error 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{ serverAPI := witch.ServerAPI{
Show: func(_ int, _ string) {}, Show: func(fromObjID, toObjID int, action string) {
Tell: func(_ int, _ string) { log.Println("stub tell called") }, sid, err := s.db.SessionIDForObjID(toObjID)
DB: func() db.DB { if err != nil {
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 {
log.Println(err.Error()) log.Println(err.Error())
return
} }
return &senderName from, err := s.db.GetObjectByID(fromObjID)
} if err != nil {
serverAPI.Show = func(senderID int, msg string) { 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{ cm := proto.ClientMessage{
Type: proto.ClientMessage_EMOTE, Type: proto.ClientMessage_EMOTE,
Text: msg, Text: action,
Speaker: getSenderName(senderID), Speaker: &speakerName,
} }
send(&cm) send(&cm)
} },
serverAPI.Tell = func(senderID int, msg string) { Tell: func(fromObjID, toObjID int, msg string) {
log.Printf("Tell %s %d %s", sid, senderID, msg) 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{ cm := proto.ClientMessage{
Type: proto.ClientMessage_OVERHEARD, Type: proto.ClientMessage_OVERHEARD,
Text: msg, Text: msg,
Speaker: getSenderName(senderID), Speaker: &speakerName,
} }
send(&cm) send(&cm)
} },
DB: func() db.DB {
return s.db
},
} }
if !ok || sc == nil { if !ok || sc == nil {

View File

@ -37,6 +37,7 @@ type DB interface {
// Presence // Presence
SessionIDForAvatar(Object) (string, error) SessionIDForAvatar(Object) (string, error)
SessionIDForObjID(int) (string, error)
AvatarBySessionID(string) (*Object, error) AvatarBySessionID(string) (*Object, error)
BedroomBySessionID(string) (*Object, error) BedroomBySessionID(string) (*Object, error)
MoveInto(toMove Object, container Object) error MoveInto(toMove Object, container Object) error
@ -117,7 +118,7 @@ func (db *pgDB) Ensure() error {
roomScript := ` roomScript := `
seen(function() seen(function()
tellMe(my("description")) tellSender(my("description"))
end) end)
` `
@ -176,7 +177,7 @@ func (db *pgDB) Ensure() error {
Data: data, Data: data,
Script: ` Script: `
go("north", function() 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") moveSender("system", "pub")
end) end)
`, `,
@ -374,6 +375,16 @@ func (db *pgDB) SessionIDForAvatar(obj Object) (string, error) {
return *sid, nil 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) { func (db *pgDB) AvatarBySessionID(sid string) (avatar *Object, err error) {
avatar = &Object{} avatar = &Object{}
// TODO subquery // TODO subquery

View File

@ -24,8 +24,8 @@ end)
*/ */
type ServerAPI struct { type ServerAPI struct {
Tell func(int, string) Tell func(int, int, string)
Show func(int, string) Show func(int, int, string)
DB func() db.DB DB func() db.DB
} }
@ -73,8 +73,18 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) {
l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int { l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int {
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")))
log.Printf("tellMe: %d %s", senderID, l.ToString(1)) 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 return 0
})) }))
@ -105,10 +115,14 @@ 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 {
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))
log.Printf("showMe: %d %s", senderID, l.ToString(1))
sc.serverAPI.Show(senderID, vc.Target.ID, l.ToString(1))
return 0 return 0
})) }))
// TODO showSender?
// TODO check execute permission and bail out potentially // TODO check execute permission and bail out potentially
//log.Printf("%#v", vc) //log.Printf("%#v", vc)