cooldowns.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. const subMod = require('./require')
  3. class SPCooldowns {
  4. constructor(mod) {
  5. this.loaded = false
  6. this.mod = mod
  7. this.timeouts = new Map()
  8. this.hooks = []
  9. this.load()
  10. }
  11. load() {
  12. if(this.loaded) return
  13. const ping = subMod(this.mod, './ping'),
  14. hook = (...args) => { this.hooks.push(this.mod.hook(...args)) },
  15. handle = event => {
  16. if(event.cooldown > 0) {
  17. event.cooldown = Math.max(0, event.cooldown - ping.min)
  18. this.set(event.skill.id, event.cooldown)
  19. return true
  20. }
  21. this.end(event.skill.id)
  22. }
  23. hook('S_START_COOLTIME_SKILL', 3, handle)
  24. hook('S_DECREASE_COOLTIME_SKILL', 3, handle)
  25. hook('S_LOAD_TOPO', 'raw', () => { this.reset() })
  26. this.loaded = true
  27. }
  28. check(id) { return this.timeouts.has(id) }
  29. set(id, time) {
  30. this.end(id)
  31. if(time > 0) this.timeouts.set(id, setTimeout(() => { this.end(id) }, time))
  32. }
  33. end(id) {
  34. clearTimeout(this.timeouts.get(id))
  35. this.timeouts.delete(id)
  36. }
  37. reset() {
  38. for(let id of this.timeouts.keys()) this.end(id)
  39. }
  40. unload() {
  41. if(!this.loaded) return
  42. reset()
  43. for(let hook of this.hooks) this.mod.unhook(hook)
  44. this.hooks = []
  45. this.loaded = false
  46. }
  47. }
  48. module.exports = SPCooldowns