diff --git a/server/cmd/main.go b/server/cmd/main.go index 1f751a1..da6d3f8 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -73,7 +73,6 @@ func newServer() (*gameWorldServer, error) { return nil, err } - // TODO --reset flag reset := flag.Bool("reset", false, "fully reset the database to its initial state") flag.Parse() @@ -111,6 +110,9 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec serverAPI := witch.ServerAPI{ Show: func(_ int, _ string) {}, Tell: func(_ int, _ string) {}, + DB: func() db.DB { + return s.db + }, } if sid != "" { send := s.msgRouter[sid] diff --git a/server/db/db.go b/server/db/db.go index cfe40cb..a2b4e65 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -113,6 +113,12 @@ func (db *pgDB) Ensure() error { } } + roomScript := ` + seen(function() + tellSender(my("description")) + end) + ` + foyer, err := db.GetObject("system", "foyer") if err != nil { // TODO actually check error. for now assuming it means does not exist @@ -121,8 +127,7 @@ func (db *pgDB) Ensure() error { data["description"] = "a big room. the ceiling is painted with constellations." foyer = &Object{ Data: data, - Script: "", - // TODO default room script + Script: roomScript, } if err = db.CreateObject(sysAcc, foyer); err != nil { return err @@ -138,7 +143,6 @@ func (db *pgDB) Ensure() error { egg = &Object{ Data: data, Script: "", - // TODO default room script } if err = db.CreateObject(sysAcc, egg); err != nil { return err @@ -153,8 +157,7 @@ func (db *pgDB) Ensure() error { data["description"] = "a warm pub constructed of hard wood and brass" pub = &Object{ Data: data, - Script: "", - // TODO default room script + Script: roomScript, } if err = db.CreateObject(sysAcc, pub); err != nil { return err @@ -170,7 +173,7 @@ func (db *pgDB) Ensure() error { oakDoor = &Object{ Data: data, Script: ` - go("north", function + go("north", function() tellMe("the heavy door swings forward with ease. It creaks gently") moveSender("system", "pub") end) diff --git a/server/witch/header.go b/server/witch/header.go index 730d895..14e40ce 100644 --- a/server/witch/header.go +++ b/server/witch/header.go @@ -5,11 +5,7 @@ import ( ) func witchHas(l *lua.LState) int { - // TODO - /* - lv := l.ToTable(1) - log.Println(lv) - */ + l.SetGlobal("_has", l.ToTable(1)) return 0 } @@ -30,6 +26,17 @@ func witchGo(l *lua.LState) int { return addHandler(l, "go") } +func witchSeen(l *lua.LState) int { + return addHandler(l, "look") +} + +func witchMy(l *lua.LState) int { + hasT := l.GetGlobal("_has").(*lua.LTable) + val := hasT.RawGetString(l.ToString(1)) + l.Push(val) + return 1 +} + func witchDoes(ls *lua.LState) int { // TODO how to feed events back into the server? // it needs to behave like an event showing up in Commands stream diff --git a/server/witch/witch.go b/server/witch/witch.go index dceea0e..cf99b82 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -26,6 +26,7 @@ end) type ServerAPI struct { Tell func(int, string) Show func(int, string) + DB func() db.DB } type VerbContext struct { @@ -61,6 +62,8 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { l.SetGlobal("hears", l.NewFunction(witchHears)) l.SetGlobal("sees", l.NewFunction(witchSees)) l.SetGlobal("go", l.NewFunction(witchGo)) + l.SetGlobal("seen", l.NewFunction(witchSeen)) + l.SetGlobal("my", l.NewFunction(witchMy)) l.SetGlobal("_handlers", l.NewTable()) if err := l.DoString(vc.Target.Script); err != nil { log.Printf("error parsing script %s: %s", vc.Target.Script, err.Error()) @@ -74,10 +77,28 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) { return 0 })) - l.SetGlobal("moveSender", l.NewFunction(func(l *lua.LState) int { - // TODO get sender - // TODO add another func to serverAPI for moving an object - return 0 + l.SetGlobal("moveSender", l.NewFunction(func(l *lua.LState) (ret int) { + ret = 0 + sender := l.GetGlobal("sender").(*lua.LTable) + senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) + owner := l.ToString(1) + name := l.ToString(2) + db := sc.serverAPI.DB() + senderObj, err := db.GetObjectByID(senderID) + if err != nil { + log.Println(err.Error()) + return + } + container, err := db.GetObject(owner, name) + if err != nil { + log.Println(err.Error()) + return + } + if err = db.MoveInto(*senderObj, *container); err != nil { + log.Println(err.Error()) + } + + return })) l.SetGlobal("showMe", l.NewFunction(func(l *lua.LState) int {