feat: 增强 Boss 脚本 HookAction 接入能力
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
引入 BossHookActionContext 封装战斗上下文,并支持脚本调用 useSkill 和 switchPet 函数控制战斗行为。
This commit is contained in:
@@ -2,21 +2,17 @@ package model
|
||||
|
||||
import "testing"
|
||||
|
||||
type testHookAction struct {
|
||||
Allow bool
|
||||
Round int
|
||||
}
|
||||
|
||||
func TestBossConfigRunHookActionScript(t *testing.T) {
|
||||
boss := &BossConfig{
|
||||
Script: `
|
||||
function hookAction(hookaction) {
|
||||
return hookaction.Allow && hookaction.Round >= 2;
|
||||
return hookaction.hookaction === true;
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
ok, err := boss.RunHookActionScript(testHookAction{Allow: true, Round: 2})
|
||||
ctx := &BossHookActionContext{HookAction: true}
|
||||
ok, err := boss.RunHookActionScript(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("RunHookActionScript returned error: %v", err)
|
||||
}
|
||||
@@ -25,20 +21,67 @@ func TestBossConfigRunHookActionScript(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBossConfigRunHookActionScriptEmptyReturnDefaultsTrue(t *testing.T) {
|
||||
func TestBossConfigRunHookActionScriptCallUseSkillFn(t *testing.T) {
|
||||
boss := &BossConfig{
|
||||
Script: `
|
||||
function hookAction(hookaction) {
|
||||
var _ = hookaction;
|
||||
if (hookaction.round >= 2) {
|
||||
useSkill(5001);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
ok, err := boss.RunHookActionScript(testHookAction{Allow: false, Round: 1})
|
||||
ctx := &BossHookActionContext{
|
||||
HookAction: true,
|
||||
Round: 2,
|
||||
Action: "auto",
|
||||
}
|
||||
ctx.UseSkillFn = func(skillID uint32) {
|
||||
ctx.Action = "skill"
|
||||
ctx.SkillID = skillID
|
||||
}
|
||||
|
||||
ok, err := boss.RunHookActionScript(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("RunHookActionScript returned error: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatalf("RunHookActionScript = false, want true")
|
||||
}
|
||||
if ctx.Action != "skill" || ctx.SkillID != 5001 {
|
||||
t.Fatalf("useSkill not applied, got action=%q skill_id=%d", ctx.Action, ctx.SkillID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBossConfigRunHookActionScriptCallSwitchPetFn(t *testing.T) {
|
||||
boss := &BossConfig{
|
||||
Script: `
|
||||
function hookAction(hookaction) {
|
||||
switchPet(3);
|
||||
return true;
|
||||
}
|
||||
`,
|
||||
}
|
||||
|
||||
ctx := &BossHookActionContext{
|
||||
HookAction: true,
|
||||
Action: "auto",
|
||||
}
|
||||
ctx.SwitchPetFn = func(catchTime uint32) {
|
||||
ctx.Action = "switch"
|
||||
ctx.CatchTime = catchTime
|
||||
}
|
||||
|
||||
ok, err := boss.RunHookActionScript(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("RunHookActionScript returned error: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatalf("RunHookActionScript = false, want true")
|
||||
}
|
||||
if ctx.Action != "switch" || ctx.CatchTime != 3 {
|
||||
t.Fatalf("switchPet not applied, got action=%q catch_time=%d", ctx.Action, ctx.CatchTime)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user