hermeticum/server/witch/witch.go

102 lines
2.0 KiB
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
"github.com/vilmibm/hermeticum/server/db"
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(".*eat.*", function(msg)
does("quivers nervously")
end)
2022-12-23 06:55:35 +00:00
hears(".*", function(msg)
tellMe(sender().name + " says " + msg)
end)
2022-12-20 08:38:47 +00:00
`
/*
allows({
read = "world",
write = "owner"
carry = "owner",
execute = "world",
})
hears(".*eat.*", function(msg)
does("quivers nervously")
end)
`
*/
// TODO figure out channel stuff
// TODO figure out how to inject WITCH header
// - do i inject from Go or prepend some Lua code?
// TODO figure out how the Lua code can affect Go and thus the database
2022-12-24 06:34:21 +00:00
type VerbContext struct {
Verb string
Rest string
Sender db.Object
Target db.Object
}
2022-12-20 08:38:47 +00:00
2022-12-24 06:34:21 +00:00
type ScriptContext struct {
script string
incoming chan VerbContext
}
2022-12-20 08:38:47 +00:00
2022-12-24 06:34:21 +00:00
func NewScriptContext() (*ScriptContext, error) {
sc := &ScriptContext{}
sc.incoming = make(chan VerbContext)
go func() {
var l *lua.LState
var err 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
l = lua.NewState()
l.SetGlobal("has", l.NewFunction(hasWrapper(vc.Target)))
l.SetGlobal("hears", l.NewFunction(hearsWrapper(vc.Target)))
l.SetGlobal("_handlers", l.NewTable())
// 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 actually trigger the Lua script
}
}()
return sc, nil
2022-12-20 08:38:47 +00:00
}
2022-12-24 06:34:21 +00:00
func (sc *ScriptContext) Handle(vc VerbContext) {
log.Printf("%#v %#v", sc, vc)
sc.incoming <- vc
2022-12-22 05:57:57 +00:00
}