hermeticum/server/witch/header.go

117 lines
2.8 KiB
Go
Raw Normal View History

2022-12-20 08:38:47 +00:00
package witch
2023-05-01 06:19:20 +00:00
/*
This file contains the definitions of functions that are injected into scope for WITCH scripts. See witch.go's ScriptContext to see how they are actually added to a LuaState.
TODO: consider making this (or witch.go) a different package entirely. the `witch` prefix for the function names in this file is a little annoying.
*/
2022-12-20 08:38:47 +00:00
import (
2023-05-03 03:23:12 +00:00
"log"
2023-05-01 06:19:20 +00:00
"strings"
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 {
l.SetGlobal("_has", l.ToTable(1))
2022-12-28 05:19:42 +00:00
return 0
}
2022-12-20 08:38:47 +00:00
2023-05-01 06:19:20 +00:00
func witchProvides(l *lua.LState) int {
// TODO test this manually
verbAndPattern := l.ToString(1)
2023-05-03 03:23:12 +00:00
cb := l.ToFunction(2)
2023-05-01 06:19:20 +00:00
split := strings.SplitN(verbAndPattern, " ", 2)
verb := split[0]
pattern := split[1]
2023-05-03 03:23:12 +00:00
return addPatternHandler(l, verb, pattern, cb)
2023-05-01 06:19:20 +00:00
}
2022-12-28 05:19:42 +00:00
func witchHears(l *lua.LState) int {
2023-05-03 03:23:12 +00:00
pattern := l.ToString(1)
cb := l.ToFunction(2)
return addPatternHandler(l, "say", pattern, cb)
}
func witchSees(l *lua.LState) int {
2023-05-03 03:23:12 +00:00
pattern := l.ToString(1)
cb := l.ToFunction(2)
return addPatternHandler(l, "emote", pattern, cb)
2022-12-20 08:38:47 +00:00
}
2023-04-29 06:52:58 +00:00
func witchGoes(l *lua.LState) int {
2023-05-03 03:23:12 +00:00
// TODO validate direction
// TODO convert direction constant to english
direction := l.ToString(1)
targetRoom := l.ToString(2)
cb := func(l *lua.LState) int {
log.Printf("please move sender to target room '%s'", targetRoom)
return 0
}
// TODO call addPatternHandler again for the reverse direction (make a reverse helper)
return addPatternHandler(l, "go", direction, l.NewFunction(cb))
2023-01-04 05:25:03 +00:00
}
func witchSeen(l *lua.LState) int {
2023-05-03 03:23:12 +00:00
cb := l.ToFunction(1)
return addHandler(l, "look", cb)
}
func witchMy(l *lua.LState) int {
hasT := l.GetGlobal("_has").(*lua.LTable)
val := hasT.RawGetString(l.ToString(1))
l.Push(val)
return 1
}
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
}
2023-05-03 03:23:12 +00:00
func addHandler(l *lua.LState, verb string, cb *lua.LFunction) int {
2023-01-11 03:05:43 +00:00
pattern := ".*"
2023-05-03 03:23:12 +00:00
log.Printf("adding handler: %s %s %#v", verb, pattern, cb)
2023-01-11 03:05:43 +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)
return 0
}
2023-05-03 03:23:12 +00:00
func addPatternHandler(l *lua.LState, verb, pattern string, cb *lua.LFunction) int {
log.Printf("adding handler: %s %s %#v", verb, string(pattern), cb)
2023-01-11 03:05:43 +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
}