make witch more working

trunk
vilmibm 2022-12-28 23:39:14 -05:00
parent ace32f9801
commit e085fa0fd1
4 changed files with 40 additions and 65 deletions

View File

@ -118,7 +118,10 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec
sender, err := s.db.GetObjectByID(senderID) sender, err := s.db.GetObjectByID(senderID)
if err == nil { if err == nil {
log.Printf("%#v", sender)
senderName = sender.Data["name"] senderName = sender.Data["name"]
} else {
log.Println(err.Error())
} }
cm := proto.ClientMessage{ cm := proto.ClientMessage{

View File

@ -180,6 +180,12 @@ func (db *pgDB) createAccount(account *Account) (err error) {
Script: "", Script: "",
} }
av.Script = fmt.Sprintf(`%s
hears(".*", function()
tellMe(msg)
end)
`, hasInvocation(av))
stmt = "INSERT INTO objects ( avatar, data, owner, script ) VALUES ( $1, $2, $3, $4 ) RETURNING id" stmt = "INSERT INTO objects ( avatar, data, owner, script ) VALUES ( $1, $2, $3, $4 ) RETURNING id"
err = tx.QueryRow(ctx, stmt, av.Avatar, av.Data, account.ID, av.Script).Scan(&av.ID) err = tx.QueryRow(ctx, stmt, av.Avatar, av.Data, account.ID, av.Script).Scan(&av.ID)
if err != nil { if err != nil {
@ -392,13 +398,14 @@ func (db *pgDB) GetObjectByID(ID int) (*Object, error) {
return obj, err return obj, err
} }
// TODO fix arg
func (db *pgDB) GetObject(owner, name string) (obj *Object, err error) { func (db *pgDB) GetObject(owner, name string) (obj *Object, err error) {
ctx := context.Background() ctx := context.Background()
obj = &Object{} obj = &Object{}
stmt := ` stmt := `
SELECT id, avatar, data, owner, script SELECT id, avatar, data, owner, script
FROM objects FROM objects
WHERE owner = $1 AND data['name'] = $2` WHERE owner = (SELECT id FROM accounts WHERE name=$1) AND data['name'] = $2`
err = db.pool.QueryRow(ctx, stmt, owner, fmt.Sprintf(`"%s"`, name)).Scan( err = db.pool.QueryRow(ctx, stmt, owner, fmt.Sprintf(`"%s"`, name)).Scan(
&obj.ID, &obj.Avatar, &obj.Data, &obj.OwnerID, &obj.Script) &obj.ID, &obj.Avatar, &obj.Data, &obj.OwnerID, &obj.Script)
@ -447,6 +454,16 @@ func (db *pgDB) ClearSessions() (err error) {
return return
} }
func hasInvocation(obj *Object) string {
hi := "has({\n"
for k, v := range obj.Data {
hi += fmt.Sprintf(`%s = "%s",`, k, v) + "\n"
}
hi += "})"
return hi
}
func (db *pgDB) CreateObject(owner *Account, obj *Object) error { func (db *pgDB) CreateObject(owner *Account, obj *Object) error {
ctx := context.Background() ctx := context.Background()
stmt := ` stmt := `
@ -455,6 +472,8 @@ func (db *pgDB) CreateObject(owner *Account, obj *Object) error {
RETURNING id RETURNING id
` `
obj.Script = hasInvocation(obj)
err := db.pool.QueryRow(ctx, stmt, err := db.pool.QueryRow(ctx, stmt,
obj.Avatar, obj.Bedroom, obj.Data, obj.Script, owner.ID).Scan( obj.Avatar, obj.Bedroom, obj.Data, obj.Script, owner.ID).Scan(
&obj.ID) &obj.ID)

View File

@ -1,34 +1,11 @@
package witch package witch
import ( import (
"fmt"
"log" "log"
"github.com/vilmibm/hermeticum/server/db"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
const DefaultAvatarScript = `
has({
name =
})
`
func SetDefaultAvatarScript(obj *db.Object) {
hasInvocation := "has({\n"
for k, v := range obj.Data {
hasInvocation += fmt.Sprintf(`%s = "%s"\n`, k, v)
}
hasInvocation += "})"
obj.Script = fmt.Sprintf(`%s
hears(".*", function()
tellMe(sender, msg)
end)
`, hasInvocation)
}
func witchHas(l *lua.LState) int { func witchHas(l *lua.LState) int {
lv := l.ToTable(1) lv := l.ToTable(1)
log.Println(lv) log.Println(lv)

View File

@ -9,29 +9,6 @@ import (
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
/*
the purpose of this package is to provide abstractions for sending verbs to game objects.
Game objects get pulled from the DB into memory and their scripts become Lua States.
*/
// TODO using a dummy script for now
const dummyScript = `
has({
name = "spaghetti",
description = "a plate of pasta covered in pomodoro sauce"
})
hears(".*", function()
print(sender.name)
print(msg)
tellMe(msg)
end)
`
/* /*
allows({ allows({
read = "world", read = "world",
@ -40,7 +17,7 @@ allows({
execute = "world", execute = "world",
}) })
hears(".*eat.*", function(msg) hears(".*eat.*", function()
does("quivers nervously") does("quivers nervously")
end) end)
` `
@ -71,34 +48,33 @@ func NewScriptContext(tell func(int, string)) (*ScriptContext, error) {
var vc VerbContext var vc VerbContext
for { for {
vc = <-sc.incoming vc = <-sc.incoming
//if vc.Target.Script != sc.script { if vc.Target.Script != sc.script {
if dummyScript != sc.script { sc.script = vc.Target.Script
//sc.script = vc.Target.Script
sc.script = dummyScript
l = lua.NewState() l = lua.NewState()
l.SetGlobal("has", l.NewFunction(witchHas)) l.SetGlobal("has", l.NewFunction(witchHas))
l.SetGlobal("hears", l.NewFunction(witchHears)) l.SetGlobal("hears", l.NewFunction(witchHears))
l.SetGlobal("_handlers", l.NewTable()) 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())
}
}
l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int { l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int {
// TODO not getting senderID properly here
sender := l.GetGlobal("sender").(*lua.LTable) sender := l.GetGlobal("sender").(*lua.LTable)
senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) senderID := int(lua.LVAsNumber(sender.RawGetString("ID")))
msg := l.ToString(1) msg := l.ToString(1)
log.Printf("%#v %s\n", sender, msg) log.Printf("%#v %s\n", sender, msg)
log.Println(senderID)
sc.tell(senderID, msg) sc.tell(senderID, msg)
return 0 return 0
})) }))
// TODO other setup
//if err := l.DoString(obj.Script); err != nil {
if err = l.DoString(dummyScript); err != nil {
log.Printf("error parsing script %s: %s", dummyScript, err.Error())
}
}
// TODO check execute permission and bail out potentially // TODO check execute permission and bail out potentially
senderT := l.NewTable() senderT := l.NewTable()
senderT.RawSetString("name", lua.LString(vc.Sender.Data["name"])) senderT.RawSetString("name", lua.LString(vc.Sender.Data["name"]))
senderT.RawSetString("ID", lua.LString(vc.Sender.Data["ID"])) senderT.RawSetString("ID", lua.LNumber(vc.Sender.ID))
l.SetGlobal("sender", senderT) l.SetGlobal("sender", senderT)
l.SetGlobal("msg", lua.LString(vc.Rest)) l.SetGlobal("msg", lua.LString(vc.Rest))