diff --git a/server/cmd/main.go b/server/cmd/main.go index 5aa9c84..fd77fc0 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -108,77 +108,12 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec s.scriptsMutex.RUnlock() var err error - // TODO this is no longer closing over anything truly interesting and can - // likely be simplified - 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 - }, + getSend := func(sid string) func(*proto.ClientMessage) error { + return s.msgRouter[sid] } if !ok || sc == nil { - if sc, err = witch.NewScriptContext(serverAPI); err != nil { + if sc, err = witch.NewScriptContext(s.db, getSend); err != nil { return err } diff --git a/server/witch/witch.go b/server/witch/witch.go index e553bd4..2ecd42b 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -5,6 +5,7 @@ import ( "log" "regexp" + "github.com/vilmibm/hermeticum/proto" "github.com/vilmibm/hermeticum/server/db" lua "github.com/yuin/gopher-lua" ) @@ -23,10 +24,76 @@ end) ` */ -type ServerAPI struct { - Tell func(int, int, string) - Show func(int, int, string) - DB func() db.DB +type serverAPI struct { + db db.DB + getSend func(string) func(*proto.ClientMessage) error +} + +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 { @@ -37,14 +104,16 @@ type VerbContext struct { } type ScriptContext struct { + db db.DB + getSend func(*proto.ClientMessage) error script string 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{ - serverAPI: sAPI, + serverAPI: serverAPI{db: db, getSend: getSend}, } sc.incoming = make(chan VerbContext)