hide serverAPI in witch

trunk
nate smith 2023-04-03 23:41:05 -07:00
parent df68631aa3
commit 31af07f7e7
2 changed files with 79 additions and 75 deletions

View File

@ -108,77 +108,12 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec
s.scriptsMutex.RUnlock() s.scriptsMutex.RUnlock()
var err error var err error
// TODO this is no longer closing over anything truly interesting and can getSend := func(sid string) func(*proto.ClientMessage) error {
// likely be simplified return s.msgRouter[sid]
serverAPI := witch.ServerAPI{
Show: func(fromObjID, toObjID int, action string) {
sid, err := s.db.SessionIDForObjID(toObjID)
if err != nil {
log.Println(err.Error())
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_EMOTE,
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
}
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: &speakerName,
}
send(&cm)
},
DB: func() db.DB {
return s.db
},
} }
if !ok || sc == nil { if !ok || sc == nil {
if sc, err = witch.NewScriptContext(serverAPI); err != nil { if sc, err = witch.NewScriptContext(s.db, getSend); err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"regexp" "regexp"
"github.com/vilmibm/hermeticum/proto"
"github.com/vilmibm/hermeticum/server/db" "github.com/vilmibm/hermeticum/server/db"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
@ -23,10 +24,76 @@ end)
` `
*/ */
type ServerAPI struct { type serverAPI struct {
Tell func(int, int, string) db db.DB
Show func(int, int, string) getSend func(string) func(*proto.ClientMessage) error
DB func() db.DB }
func (s *serverAPI) Tell(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.getSend(sid)
cm := proto.ClientMessage{
Type: proto.ClientMessage_OVERHEARD,
Text: msg,
Speaker: &speakerName,
}
send(&cm)
}
func (s *serverAPI) Show(fromObjID, toObjID int, action string) {
sid, err := s.db.SessionIDForObjID(toObjID)
if err != nil {
log.Println(err.Error())
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.getSend(sid)
cm := proto.ClientMessage{
Type: proto.ClientMessage_EMOTE,
Text: action,
Speaker: &speakerName,
}
send(&cm)
}
func (s *serverAPI) DB() db.DB {
return s.db
} }
type VerbContext struct { type VerbContext struct {
@ -37,14 +104,16 @@ type VerbContext struct {
} }
type ScriptContext struct { type ScriptContext struct {
db db.DB
getSend func(*proto.ClientMessage) error
script string script string
incoming chan VerbContext incoming chan VerbContext
serverAPI ServerAPI serverAPI serverAPI
} }
func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { func NewScriptContext(db db.DB, getSend func(string) func(*proto.ClientMessage) error) (*ScriptContext, error) {
sc := &ScriptContext{ sc := &ScriptContext{
serverAPI: sAPI, serverAPI: serverAPI{db: db, getSend: getSend},
} }
sc.incoming = make(chan VerbContext) sc.incoming = make(chan VerbContext)