hermeticum/server/witch/header.go

50 lines
1000 B
Go
Raw Normal View History

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)
// TODO
2022-12-28 05:19:42 +00:00
return 0
}
2022-12-20 08:38:47 +00:00
// TODO provides
2022-12-28 05:19:42 +00:00
func witchHears(l *lua.LState) int {
return addHandler(l, "say")
}
func witchSees(l *lua.LState) int {
log.Println("adding handler for emote")
return addHandler(l, "emote")
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
}
func addHandler(l *lua.LState, verb string) int {
pattern := l.ToString(1)
cb := l.ToFunction(2)
2022-12-22 06:38:15 +00:00
2022-12-28 05:19:42 +00:00
handlers := l.GetGlobal("_handlers").(*lua.LTable)
verbHandlers, ok := handlers.RawGetString(verb).(*lua.LTable)
if !ok {
verbHandlers = l.NewTable()
handlers.RawSetString(verb, verbHandlers)
}
verbHandlers.RawSetString(pattern, cb)
2022-12-22 06:38:15 +00:00
return 0
}