From e085fa0fd1080af99a62513ffb5d5c12b89f4cf7 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 28 Dec 2022 23:39:14 -0500 Subject: [PATCH] make witch more working --- server/cmd/main.go | 3 +++ server/db/db.go | 21 ++++++++++++++- server/witch/header.go | 23 ----------------- server/witch/witch.go | 58 +++++++++++++----------------------------- 4 files changed, 40 insertions(+), 65 deletions(-) diff --git a/server/cmd/main.go b/server/cmd/main.go index 83ffaea..b62c02f 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -118,7 +118,10 @@ func (s *gameWorldServer) verbHandler(verb, rest string, sender, target db.Objec sender, err := s.db.GetObjectByID(senderID) if err == nil { + log.Printf("%#v", sender) senderName = sender.Data["name"] + } else { + log.Println(err.Error()) } cm := proto.ClientMessage{ diff --git a/server/db/db.go b/server/db/db.go index 9de1742..0b5a604 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -180,6 +180,12 @@ func (db *pgDB) createAccount(account *Account) (err error) { 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" err = tx.QueryRow(ctx, stmt, av.Avatar, av.Data, account.ID, av.Script).Scan(&av.ID) if err != nil { @@ -392,13 +398,14 @@ func (db *pgDB) GetObjectByID(ID int) (*Object, error) { return obj, err } +// TODO fix arg func (db *pgDB) GetObject(owner, name string) (obj *Object, err error) { ctx := context.Background() obj = &Object{} stmt := ` SELECT id, avatar, data, owner, script 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( &obj.ID, &obj.Avatar, &obj.Data, &obj.OwnerID, &obj.Script) @@ -447,6 +454,16 @@ func (db *pgDB) ClearSessions() (err error) { 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 { ctx := context.Background() stmt := ` @@ -455,6 +472,8 @@ func (db *pgDB) CreateObject(owner *Account, obj *Object) error { RETURNING id ` + obj.Script = hasInvocation(obj) + err := db.pool.QueryRow(ctx, stmt, obj.Avatar, obj.Bedroom, obj.Data, obj.Script, owner.ID).Scan( &obj.ID) diff --git a/server/witch/header.go b/server/witch/header.go index b6c627e..ab1ba13 100644 --- a/server/witch/header.go +++ b/server/witch/header.go @@ -1,34 +1,11 @@ package witch import ( - "fmt" "log" - "github.com/vilmibm/hermeticum/server/db" 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 { lv := l.ToTable(1) log.Println(lv) diff --git a/server/witch/witch.go b/server/witch/witch.go index d7495ee..ee6b82f 100644 --- a/server/witch/witch.go +++ b/server/witch/witch.go @@ -9,29 +9,6 @@ import ( 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({ read = "world", @@ -40,7 +17,7 @@ allows({ execute = "world", }) -hears(".*eat.*", function(msg) +hears(".*eat.*", function() does("quivers nervously") end) ` @@ -71,34 +48,33 @@ func NewScriptContext(tell func(int, string)) (*ScriptContext, error) { var vc VerbContext for { vc = <-sc.incoming - //if vc.Target.Script != sc.script { - if dummyScript != sc.script { - //sc.script = vc.Target.Script - sc.script = dummyScript + if vc.Target.Script != sc.script { + sc.script = vc.Target.Script l = lua.NewState() l.SetGlobal("has", l.NewFunction(witchHas)) l.SetGlobal("hears", l.NewFunction(witchHears)) l.SetGlobal("_handlers", l.NewTable()) - l.SetGlobal("tellMe", l.NewFunction(func(l *lua.LState) int { - sender := l.GetGlobal("sender").(*lua.LTable) - senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) - msg := l.ToString(1) - log.Printf("%#v %s\n", sender, msg) - sc.tell(senderID, msg) - 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()) + 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 { + // TODO not getting senderID properly here + sender := l.GetGlobal("sender").(*lua.LTable) + senderID := int(lua.LVAsNumber(sender.RawGetString("ID"))) + msg := l.ToString(1) + log.Printf("%#v %s\n", sender, msg) + log.Println(senderID) + sc.tell(senderID, msg) + return 0 + })) + // TODO check execute permission and bail out potentially senderT := l.NewTable() 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("msg", lua.LString(vc.Rest))