2022-12-20 08:38:47 +00:00
|
|
|
package witch
|
|
|
|
|
|
|
|
import (
|
2022-12-28 05:19:42 +00:00
|
|
|
"fmt"
|
2022-12-24 06:34:21 +00:00
|
|
|
"log"
|
|
|
|
|
2022-12-20 08:38:47 +00:00
|
|
|
"github.com/vilmibm/hermeticum/server/db"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
2022-12-28 05:19:42 +00:00
|
|
|
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)
|
2022-12-20 08:38:47 +00:00
|
|
|
}
|
2022-12-28 05:19:42 +00:00
|
|
|
hasInvocation += "})"
|
|
|
|
|
|
|
|
obj.Script = fmt.Sprintf(`%s
|
|
|
|
hears(".*", function()
|
|
|
|
tellMe(sender, msg)
|
|
|
|
end)
|
|
|
|
`, hasInvocation)
|
|
|
|
|
2022-12-20 08:38:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 05:19:42 +00:00
|
|
|
func witchHas(l *lua.LState) int {
|
|
|
|
lv := l.ToTable(1)
|
|
|
|
log.Println(lv)
|
|
|
|
return 0
|
|
|
|
}
|
2022-12-20 08:38:47 +00:00
|
|
|
|
2022-12-28 05:19:42 +00:00
|
|
|
func witchHears(l *lua.LState) int {
|
|
|
|
// TODO register handler
|
|
|
|
handlers := l.GetGlobal("_handlers").(*lua.LTable)
|
|
|
|
log.Println(handlers)
|
|
|
|
pattern := l.ToString(1)
|
|
|
|
cb := l.ToFunction(2)
|
|
|
|
addHandler(l, "say", pattern, cb)
|
|
|
|
return 0
|
2022-12-20 08:38:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 05:19:42 +00:00
|
|
|
func witchDoes(ls *lua.LState) int {
|
2022-12-22 06:38:15 +00:00
|
|
|
// TODO how to feed events back into the server?
|
|
|
|
// it needs to behave like an event showing up in Commands stream
|
|
|
|
// this handler needs a reference to the gateway which has a channel for sending events that the server will see?
|
2022-12-20 08:38:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-12-22 06:38:15 +00:00
|
|
|
/*
|
|
|
|
string -> fn does not work because there might be multiple handlers for a given verb.
|
|
|
|
|
|
|
|
i can:
|
|
|
|
- have a list of handlers. call each one. it is the handler's
|
|
|
|
responsibility to decide if it's a match or not.
|
|
|
|
- store string -> map[string]fn. do the matching in Go.
|
|
|
|
|
|
|
|
handlers = {
|
|
|
|
"hear" = {
|
|
|
|
"*eat*" = cbfn0
|
|
|
|
"*slurp*" = cbfn1
|
|
|
|
}
|
|
|
|
|
|
|
|
"see" = {
|
|
|
|
"*fork*" = cbfn2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2022-12-28 05:19:42 +00:00
|
|
|
func addHandler(l *lua.LState, verb, pattern string, cb *lua.LFunction) int {
|
|
|
|
handlers := l.GetGlobal("_handlers").(*lua.LTable)
|
|
|
|
|
|
|
|
verbHandlers, ok := handlers.RawGetString(verb).(*lua.LTable)
|
|
|
|
if !ok {
|
|
|
|
verbHandlers = l.NewTable()
|
|
|
|
handlers.RawSetString(verb, verbHandlers)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("addHandler")
|
|
|
|
log.Printf("%#v", cb)
|
|
|
|
|
|
|
|
verbHandlers.RawSetString(pattern, cb)
|
2022-12-22 06:38:15 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|