2022-12-20 08:38:47 +00:00
|
|
|
package witch
|
|
|
|
|
|
|
|
import (
|
2022-12-24 06:34:21 +00:00
|
|
|
"log"
|
|
|
|
|
2022-12-20 08:38:47 +00:00
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|