Skip to content

Commit

Permalink
Merge pull request #27 from pojol/feature/page_display_adjust
Browse files Browse the repository at this point in the history
Feature/page display adjust
  • Loading branch information
pojol authored May 21, 2024
2 parents ad127dc + 076ed2f commit 0fb5671
Show file tree
Hide file tree
Showing 19 changed files with 555 additions and 298 deletions.
5 changes: 3 additions & 2 deletions driver/bot/behavior/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Tree struct {

Wait int32 `xml:"wait"`

Loop int32 `xml:"loop"` // 用于记录循环节点的循环x次数
Code string `xml:"code"`
Loop int32 `xml:"loop"` // 用于记录循环节点的循环x次数
Code string `xml:"code"`
Alias string `xml:"alias"` // 用于记录别名

root INod

Expand Down
15 changes: 13 additions & 2 deletions driver/bot/behavior/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type INod interface {
}

type Node struct {
id string
ty string
id string
name string
ty string

child []INod
parent INod
Expand All @@ -53,6 +54,7 @@ type Node struct {
func (n *Node) Init(t *Tree, parent INod) {
n.id = t.ID
n.ty = t.Ty
n.name = t.Alias

n.parent = parent
}
Expand All @@ -61,6 +63,15 @@ func (a *Node) ID() string {
return a.id
}

func (a *Node) GetShortID() string {
return a.id[len(a.id)-12:]
}

// Name 返回节点的名称
func (a *Node) Name() string {
return a.name
}

func (a *Node) Type() string {
return a.ty
}
Expand Down
28 changes: 19 additions & 9 deletions driver/bot/behavior/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/pojol/gobot/driver/bot/pool"
"github.com/pojol/gobot/driver/utils"
Expand Down Expand Up @@ -91,36 +92,45 @@ func (t *Tick) Do(mod Mode) (state string, end bool) {
var msg string

for _, n := range nods {
// 要将一个节点的日志收集到一起,将alias写入到meta中
if n.getType() == SCRIPT && mod == Step {
log := time.Now().Format("2006-01-02 15:04:05") + " tick " + n.getBase().Name() + " " + n.getBase().GetShortID() + " =>"
t.bs.L.DoString(`
log.info("` + log + `")
`)
}
err = n.onTick(t)

state, msg, parseerr = t.stateCheck(mod, n.getType())

// thread 信息用于编辑器标记运行时节点信息(展示项
threadInfo := ThreadInfo{
Number: n.getBase().getThread(),
CurNod: n.getBase().ID(),
Change: msg,
}

if err != nil {
threadInfo.ErrMsg = fmt.Sprintf("tick err %v", err.Error())
fmt.Println("tick err", threadInfo.ErrMsg)
log := fmt.Sprintf("<b><u>check err</u></b> thread:%v name:%v id:%v\n%v",
n.getBase().getThread(),
n.getBase().ID(),
n.getBase().Name(),
err.Error())
t.bs.L.DoString(`
log.info("` + log + `")
`)
}
if parseerr != nil {
threadInfo.ErrMsg = fmt.Sprintf("%v parse err %v", threadInfo.ErrMsg, parseerr.Error())
fmt.Println("tick parse err", threadInfo.ErrMsg)
//threadInfo.ErrMsg = fmt.Sprintf("%v parse err %v", threadInfo.ErrMsg, parseerr.Error())
}

if state != Succ {
if state == Exit {
end = true
} else if state == Break {
end = true
threadInfo.ErrMsg = fmt.Sprintf("script break err %v", msg)
fmt.Println("tick break err", threadInfo.ErrMsg)
} else if state == Error {
// 节点脚本出错,脚本逻辑自行抛出的错误
threadInfo.ErrMsg = fmt.Sprintf("script err %v", msg)
fmt.Println("tick script err", threadInfo.ErrMsg)
//threadInfo.ErrMsg = fmt.Sprintf("script err %v", msg)
}
}

Expand Down
7 changes: 6 additions & 1 deletion driver/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,13 @@ func (b *Bot) close() {
} else {
pool.FreeState(b.bs)
}
}

fmt.Println("bot", b.Name(), "batch", b.batch, "idx", b.id, "close")
// PopLog - 弹出一条日志
func (b *Bot) PopLog() string {
line := b.bs.LogMod.Pop()

return line
}

type State int32
Expand All @@ -281,6 +285,7 @@ func (b *Bot) RunByStep() State {
stepmu.Lock()
defer stepmu.Unlock()

// 这边的错误日志需要记录下
state, end := b.tick.Do(b.mod)
if end {
return SEnd
Expand Down
14 changes: 14 additions & 0 deletions driver/bot/pool/lua_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BotState struct {
base64Mod *script.Base64Module
mgoMod *script.MgoModule
md5Mod *script.MD5Module
LogMod *script.LogModule
}

func (pl *lStatePool) Get() *BotState {
Expand Down Expand Up @@ -50,6 +51,7 @@ func _new_state() *BotState {
base64Mod: &script.Base64Module{},
mgoMod: &script.MgoModule{},
md5Mod: &script.MD5Module{},
LogMod: &script.LogModule{},
}

b.L.PreloadModule("proto", b.protoMod.Loader)
Expand All @@ -61,6 +63,9 @@ func _new_state() *BotState {
b.L.PreloadModule("mgo", b.mgoMod.Loader)
b.L.PreloadModule("md5", b.md5Mod.Loader)

// 全局 log
b.LogMod.Loader(b.L)

return b
}

Expand All @@ -80,7 +85,14 @@ func GetState() *BotState {
return luaPool.Get()
}

func (bs *BotState) Clean() {
// module clean
bs.LogMod.Clean()
}

func PutState(state *BotState) {
state.Clean()

luaPool.Put(state)
}

Expand All @@ -89,6 +101,8 @@ func NewState() *BotState {
}

func FreeState(state *BotState) {
state.Clean()

state.L.Close()
}

Expand Down
102 changes: 102 additions & 0 deletions driver/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package driver

import (
"context"
"flag"
"fmt"
"net/http"
"runtime"
"strconv"

"github.com/pojol/gobot/driver/factory"
"github.com/pojol/gobot/driver/mock"
"github.com/pojol/gobot/driver/openapi"
"github.com/pojol/gobot/driver/utils"
lua "github.com/yuin/gopher-lua"
)

const (
// Version of gobot driver
Version = "v0.4.4"

banner = `
__ __
/\ \ /\ \__
__ ___\ \ \____ ___\ \ ,_\
/'_ '\ / __'\ \ '__'\ / __'\ \ \
/\ \L\ \/\ \L\ \ \ \L\ \/\ \L\ \ \ \_
\ \____ \ \____/\ \_,__/\ \____/\ \__\
\/___L\ \/___/ \/___/ \/___/ \/__/
/\____/
\_/__/ %s
`
)

func Run() {
defer func() {
if err := recover(); err != nil {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
fmt.Println("panic:", string(buf[:n]))
}
}()

f := utils.InitFlag()
flag.Parse()
if utils.ShowUseage() {
return
}

fmt.Printf(banner, Version)

botFactory, err := factory.Create(
factory.WithDatabase(f.DBType),
factory.WithClusterMode(f.Cluster),
)
if err != nil {
panic(err)
}
defer botFactory.Close()

L := lua.NewState()
defer L.Close()
L.DoFile(f.ScriptPath + "/" + "message.lua")
byteOrder := L.GetGlobal("ByteOrder").String()

if f.OpenHttpMock != 0 {
ms := mock.NewHttpServer()
go ms.Start(":" + strconv.Itoa(f.OpenHttpMock))
defer ms.Close()
}

if f.OpenTcpMock != 0 {
tcpls := mock.StarTCPServer(byteOrder, ":"+strconv.Itoa(f.OpenTcpMock))
defer tcpls.Close()
}

if f.OpenWSMock != 0 {
ws := mock.StartWebsocketServe(byteOrder, ":"+strconv.Itoa(f.OpenWSMock))
defer ws.Close()
}

go func() {
http.ListenAndServe(":6060", nil)
}()

// 查看有没有未完成的队列
factory.Global.CheckTaskHistory()

openApiPort := 8888
if f.OpenAPIPort != 0 {
openApiPort = f.OpenAPIPort
}

e := openapi.Start(openApiPort)
defer e.Close()

// Stop the service gracefully.
if err := e.Shutdown(context.TODO()); err != nil {
panic(err)
}
}
9 changes: 9 additions & 0 deletions driver/openapi/api_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ type PrefabSetTagsReq struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}

// ------------------------ runtime ------------------------
type RuntimeInfoReq struct {
ID string
}

type RuntimeInfoRes struct {
Msg string
}
39 changes: 39 additions & 0 deletions driver/openapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,43 @@ EXT:
return nil
}

func BotRuntimeInfo(ctx echo.Context) error {
code := Succ
var b *bot.Bot
req := &RuntimeInfoReq{}
res := &response{}
body := &RuntimeInfoRes{}

bts, err := ioutil.ReadAll(ctx.Request().Body)
if err != nil {
code = ErrContentRead // tmp
fmt.Println(err.Error())
goto EXT
}
err = json.Unmarshal(bts, &req)
if err != nil {
code = ErrContentRead // tmp
fmt.Println(err.Error())
goto EXT
}

b = factory.Global.FindBot(req.ID)
if b == nil {
code = ErrCantFindBot
goto EXT
}

body.Msg = b.PopLog()

EXT:
res.Code = int(code)
res.Msg = errmap[code]
res.Body = body

ctx.JSON(http.StatusOK, res)
return nil
}

func ReqPrint() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
Expand All @@ -774,6 +811,8 @@ func Route(e *echo.Echo) {
return nil
})

e.POST("/runtime.info", BotRuntimeInfo)

e.POST("/file.uploadTxt", FileTextUpload)
e.POST("/file.uploadBlob", FileBlobUpload)
e.POST("/file.remove", FileRemove)
Expand Down
Loading

0 comments on commit 0fb5671

Please sign in to comment.