Explorar el Código

бутстрап луа, выполняется при создании экзекутора

kpmy hace 9 años
padre
commit
0f493693d1
Se han modificado 3 ficheros con 13 adiciones y 8 borrados
  1. 2 0
      luaexecutor/executor.go
  2. 8 8
      main.go
  3. 3 0
      static/bootstrap.lua

+ 2 - 0
luaexecutor/executor.go

@@ -5,6 +5,7 @@ import (
 	"github.com/kpmy/go-lua"
 	"github.com/kpmy/xippo/c2s/stream"
 	"github.com/kpmy/xippo/entity"
+	"path/filepath"
 	"sync"
 	"time"
 )
@@ -94,6 +95,7 @@ func NewExecutor(s stream.Stream) *Executor {
 	e.state.PushString(callbacksLocation)
 	e.state.NewTable()
 	e.state.SetTable(lua.RegistryIndex)
+	lua.DoFile(e.state, filepath.Join("static", "bootstrap.lua"))
 	return e
 }
 

+ 8 - 8
main.go

@@ -100,7 +100,7 @@ func doLua(script string) func(stream.Stream) error {
 }
 
 func doLuaAndPrint(script string) func(stream.Stream) error {
-	return doLua(fmt.Sprintf("chat.send(%s)", script))
+	return doLua(fmt.Sprintf(`chat.send("%s")`, script))
 }
 
 func loadTpl(name string) (ret *template.Template, err error) {
@@ -135,18 +135,18 @@ func bot(st stream.Stream) error {
 					if sender != ME {
 						executor.NewMessage(luaexecutor.IncomingMessage{sender, e.Body})
 						switch {
-						case strings.EqualFold(strings.TrimSpace(e.Body), "пщ"):
-							go func() {
-								actors.With(st).Do(doReply(sender, e.Type)).Run()
-							}()
+						/*case strings.EqualFold(strings.TrimSpace(e.Body), "пщ"):
+						go func() {
+							actors.With(st).Do(doReply(sender, e.Type)).Run()
+						}() */
 						case strings.HasPrefix(e.Body, "lua>"):
 							go func(script string) {
 								actors.With(st).Do(doLua(script)).Run()
 							}(strings.TrimPrefix(e.Body, "lua>"))
-						case strings.HasPrefix(e.Body, "luap>"):
+						case strings.HasPrefix(e.Body, "say"):
 							go func(script string) {
-								actors.With(st).Do(doLua(script)).Run()
-							}(strings.TrimPrefix(e.Body, "luap>"))
+								actors.With(st).Do(doLuaAndPrint(script)).Run()
+							}(strings.TrimSpace(strings.TrimPrefix(e.Body, "say")))
 						}
 					}
 				}

+ 3 - 0
static/bootstrap.lua

@@ -0,0 +1,3 @@
+chat.onmessage("пщ", function(s, m)
+   if m == "пщ" then chat.send("пщ") end
+end)