make witch more working
parent
ace32f9801
commit
e085fa0fd1
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
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 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
|
||||
|
||||
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))
|
||||
|
||||
|
|
Loading…
Reference in New Issue