feat: 增强踢人逻辑与BOSS脚本支持
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
优化踢人超时处理和僵尸连接清理,支持BOSS动作脚本并增加测试,修复事件匹配与战斗循环中的并发问题。
This commit is contained in:
@@ -2,6 +2,10 @@ package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -46,3 +50,47 @@ func NewBossConfig() *BossConfig {
|
||||
func init() {
|
||||
cool.CreateTable(&BossConfig{})
|
||||
}
|
||||
|
||||
// RunHookActionScript 执行BOSS脚本中的 hookAction,并传入 fight 的 hookaction 参数。
|
||||
// 返回值遵循 HookAction 语义:true 允许继续出手,false 阻止继续出手。
|
||||
func (b *BossConfig) RunHookActionScript(hookAction any) (bool, error) {
|
||||
if b == nil || strings.TrimSpace(b.Script) == "" {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
program, err := goja.Compile("boss_hook_action.js", b.Script, false)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("compile boss script: %w", err)
|
||||
}
|
||||
|
||||
vm := goja.New()
|
||||
if _, err = vm.RunProgram(program); err != nil {
|
||||
return false, fmt.Errorf("run boss script: %w", err)
|
||||
}
|
||||
|
||||
var (
|
||||
callable goja.Callable
|
||||
ok bool
|
||||
)
|
||||
for _, fnName := range []string{"hookAction", "HookAction", "hookaction"} {
|
||||
callable, ok = goja.AssertFunction(vm.Get(fnName))
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return false, fmt.Errorf("boss script function not found: hookAction")
|
||||
}
|
||||
|
||||
result, err := callable(goja.Undefined(), vm.ToValue(hookAction))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("execute boss hookAction: %w", err)
|
||||
}
|
||||
|
||||
// 与既有HookAction默认行为保持一致:未显式返回时视为允许继续出手。
|
||||
if goja.IsUndefined(result) || goja.IsNull(result) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return result.ToBoolean(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user