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
}
// 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]

View File

@ -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)

View File

@ -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

View File

@ -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 {