Skip to content

技能与冷却

技能页通常同时包含两条链路:客户端抑制重复点击,以及服务端回写权威冷却。

两条链路

  • 节流:减少连点产生的无意义重复请求
  • 冷却:服务端权威状态,应该回写到 sync:*

客户端动作层

javascript
import { command } from "bui:action"

const castThrottle = Throttle.create(300, true, () => {
  command("skill.cast", { id: "fireball" })
})

function castFireball() {
  castThrottle.trigger()
}

模板层

xml
<template>
  <Panel id="skillRoot">
    <Button
      text="Fireball"
      @click="castFireball()"
      b-disabled="(internalSyncStore.get('sync:cooldown') ?? 0) > 0"
    />

    <Label :text="'CD: ' + (internalSyncStore.get('sync:cooldown') ?? 0)" />
  </Panel>
</template>

服务端回写

kotlin
BUIServerAPI.action().command().register("skills/main", "skill.cast") { player, _, storeId, payload ->
    val result = skillService.cast(player, payload)
    BUIServerAPI.state().setSyncValue(player, storeId, "cooldown", result.cooldown)
    result
}

落地方式

  • 冷却数字必须和真实权威状态绑定。
  • 节流是优化,不是冷却判定。
  • 禁用态、透明度、按钮提示、特效高亮都应该依赖同一份状态源。