big WIP: start on look support and exit handling

trunk
vilmibm 2023-01-08 00:25:57 -08:00
parent 0607fe2545
commit 9b7e92c118
4 changed files with 49 additions and 16 deletions

View File

@ -73,7 +73,6 @@ func newServer() (*gameWorldServer, error) {
return nil, err return nil, err
} }
// TODO --reset flag
reset := flag.Bool("reset", false, "fully reset the database to its initial state") reset := flag.Bool("reset", false, "fully reset the database to its initial state")
flag.Parse() flag.Parse()
@ -111,6 +110,9 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec
serverAPI := witch.ServerAPI{ serverAPI := witch.ServerAPI{
Show: func(_ int, _ string) {}, Show: func(_ int, _ string) {},
Tell: func(_ int, _ string) {}, Tell: func(_ int, _ string) {},
DB: func() db.DB {
return s.db
},
} }
if sid != "" { if sid != "" {
send := s.msgRouter[sid] send := s.msgRouter[sid]

View File

@ -113,6 +113,12 @@ func (db *pgDB) Ensure() error {
} }
} }
roomScript := `
seen(function()
tellSender(my("description"))
end)
`
foyer, err := db.GetObject("system", "foyer") foyer, err := db.GetObject("system", "foyer")
if err != nil { if err != nil {
// TODO actually check error. for now assuming it means does not exist // 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." data["description"] = "a big room. the ceiling is painted with constellations."
foyer = &Object{ foyer = &Object{
Data: data, Data: data,
Script: "", Script: roomScript,
// TODO default room script
} }
if err = db.CreateObject(sysAcc, foyer); err != nil { if err = db.CreateObject(sysAcc, foyer); err != nil {
return err return err
@ -138,7 +143,6 @@ func (db *pgDB) Ensure() error {
egg = &Object{ egg = &Object{
Data: data, Data: data,
Script: "", Script: "",
// TODO default room script
} }
if err = db.CreateObject(sysAcc, egg); err != nil { if err = db.CreateObject(sysAcc, egg); err != nil {
return err return err
@ -153,8 +157,7 @@ func (db *pgDB) Ensure() error {
data["description"] = "a warm pub constructed of hard wood and brass" data["description"] = "a warm pub constructed of hard wood and brass"
pub = &Object{ pub = &Object{
Data: data, Data: data,
Script: "", Script: roomScript,
// TODO default room script
} }
if err = db.CreateObject(sysAcc, pub); err != nil { if err = db.CreateObject(sysAcc, pub); err != nil {
return err return err
@ -170,7 +173,7 @@ func (db *pgDB) Ensure() error {
oakDoor = &Object{ oakDoor = &Object{
Data: data, Data: data,
Script: ` Script: `
go("north", function go("north", function()
tellMe("the heavy door swings forward with ease. It creaks gently") tellMe("the heavy door swings forward with ease. It creaks gently")
moveSender("system", "pub") moveSender("system", "pub")
end) end)

View File

@ -5,11 +5,7 @@ import (
) )
func witchHas(l *lua.LState) int { func witchHas(l *lua.LState) int {
// TODO l.SetGlobal("_has", l.ToTable(1))
/*
lv := l.ToTable(1)
log.Println(lv)
*/
return 0 return 0
} }
@ -30,6 +26,17 @@ func witchGo(l *lua.LState) int {
return addHandler(l, "go") 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 { func witchDoes(ls *lua.LState) int {
// TODO how to feed events back into the server? // TODO how to feed events back into the server?
// it needs to behave like an event showing up in Commands stream // it needs to behave like an event showing up in Commands stream

View File

@ -26,6 +26,7 @@ end)
type ServerAPI struct { type ServerAPI struct {
Tell func(int, string) Tell func(int, string)
Show func(int, string) Show func(int, string)
DB func() db.DB
} }
type VerbContext struct { type VerbContext struct {
@ -61,6 +62,8 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) {
l.SetGlobal("hears", l.NewFunction(witchHears)) l.SetGlobal("hears", l.NewFunction(witchHears))
l.SetGlobal("sees", l.NewFunction(witchSees)) l.SetGlobal("sees", l.NewFunction(witchSees))
l.SetGlobal("go", l.NewFunction(witchGo)) l.SetGlobal("go", l.NewFunction(witchGo))
l.SetGlobal("seen", l.NewFunction(witchSeen))
l.SetGlobal("my", l.NewFunction(witchMy))
l.SetGlobal("_handlers", l.NewTable()) l.SetGlobal("_handlers", l.NewTable())
if err := l.DoString(vc.Target.Script); err != nil { if err := l.DoString(vc.Target.Script); err != nil {
log.Printf("error parsing script %s: %s", vc.Target.Script, err.Error()) log.Printf("error parsing script %s: %s", vc.Target.Script, err.Error())
@ -74,10 +77,28 @@ func NewScriptContext(sAPI ServerAPI) (*ScriptContext, error) {
return 0 return 0
})) }))
l.SetGlobal("moveSender", l.NewFunction(func(l *lua.LState) int { l.SetGlobal("moveSender", l.NewFunction(func(l *lua.LState) (ret int) {
// TODO get sender ret = 0
// TODO add another func to serverAPI for moving an object sender := l.GetGlobal("sender").(*lua.LTable)
return 0 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 { l.SetGlobal("showMe", l.NewFunction(func(l *lua.LState) int {