abnormalities.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict'
  2. const subMod = require('./require'),
  3. abnormals = require('../config/abnormalities')
  4. class SPAbnormals {
  5. constructor(mod) {
  6. this.mod = mod
  7. this.player = subMod(mod, './player')
  8. this.ping = subMod(mod, './ping')
  9. this.myAbnormals = {}
  10. this.listWithShit = {
  11. "10152030": true,
  12. "10152031": true,
  13. "30171005": true,
  14. "30180202": true,
  15. "30180240": true,
  16. "30180302": true,
  17. "30190116": true,
  18. "30190211": true,
  19. "30190239": true,
  20. "30190309": true,
  21. "32010110": true,
  22. "457010": true,
  23. "460002": true,
  24. "460025": true,
  25. "620000": true,
  26. "620001": true,
  27. "623005": true,
  28. "626004": true,
  29. "70408": true,
  30. "7201005": true,
  31. "757010": true,
  32. "760002": true,
  33. "760025": true,
  34. "781076": true,
  35. "78200116": true,
  36. "78200211": true,
  37. "78200239": true,
  38. "78200309": true,
  39. "78300202": true,
  40. "78300240": true,
  41. "78300302": true,
  42. "7941013": true,
  43. "801900": true,
  44. "801920": true,
  45. "860002": true,
  46. "860025": true,
  47. "88605": true,
  48. "90340704": true,
  49. "905620": true,
  50. "905628": true,
  51. "905922": true,
  52. "9201005": true,
  53. "93910011": true,
  54. "93920014": true,
  55. "950317": true,
  56. "980207": true,
  57. "981076": true,
  58. "98200116": true,
  59. "98200211": true,
  60. "98200239": true,
  61. "98200309": true,
  62. "98300202": true,
  63. "98300240": true,
  64. "98300302": true,
  65. "9941013": true
  66. }
  67. mod.hook('S_RETURN_TO_LOBBY', 1, () => { this.removeAll() })
  68. mod.hook('S_CREATURE_LIFE', 3, event => {
  69. if (event.gameId === this.player.gameId && !event.alive) this.removeAll()
  70. })
  71. let abnormalityUpdate = (type, event) => {
  72. if (event.target === this.player.gameId) {
  73. if (mod.settings.debug.abnormals) debug(abnormals[event.id] == true ? '<X' : '<-', type, event.id, event.duration, event.stacks)
  74. let info = abnormals[event.id]
  75. if (info) {
  76. if (info == true) return false
  77. if (info.overrides && this.exists(info.overrides)) this.remove(info.overrides)
  78. }
  79. if (event.duration != 0x7fffffff && !this.listWithShit[event.id]) event.duration = Math.max(event.duration - this.ping.min, 0)
  80. if (type === 'S_ABNORMALITY_BEGIN' === this.exists(event.id)) { // Transform packet type so it will always be valid
  81. this.add(event.id, event.duration, event.stacks)
  82. return false
  83. }
  84. this._add(event.id, event.duration)
  85. return true
  86. }
  87. }
  88. mod.hook('S_ABNORMALITY_BEGIN', 3, abnormalityUpdate.bind(null, 'S_ABNORMALITY_BEGIN'))
  89. mod.hook('S_ABNORMALITY_REFRESH', 1, abnormalityUpdate.bind(null, 'S_ABNORMALITY_REFRESH'))
  90. mod.hook('S_ABNORMALITY_END', 1, event => {
  91. if (event.target === this.player.gameId) {
  92. if (mod.settings.debug.abnormals) debug(abnormals[event.id] == true ? '<X' : '<-', 'S_ABNORMALITY_END', event.id)
  93. if (abnormals[event.id] == true) return false
  94. if (!this.myAbnormals[event.id]) return false
  95. this._remove(event.id)
  96. }
  97. })
  98. }
  99. exists(id) {
  100. return !!this.myAbnormals[id]
  101. }
  102. inMap(map) {
  103. for (let id in this.myAbnormals)
  104. if (map[id]) return true
  105. return false
  106. }
  107. add(id, duration, stacks) {
  108. let type = this.myAbnormals[id] ? 'S_ABNORMALITY_REFRESH' : 'S_ABNORMALITY_BEGIN',
  109. version = this.myAbnormals[id] ? 1 : 3
  110. if (this.mod.settings.debug.abnormals) debug('<*', type, id, duration, stacks)
  111. this.mod.toClient(type, version, {
  112. target: this.player.gameId,
  113. source: this.player.gameId,
  114. id,
  115. duration,
  116. unk: 0,
  117. stacks,
  118. unk2: 0
  119. })
  120. this._add(id, duration)
  121. }
  122. remove(id) {
  123. if (!this.exists(id)) return
  124. if (this.mod.settings.debug.abnormals) debug('<* S_ABNORMALITY_END', id)
  125. this.mod.toClient('S_ABNORMALITY_END', 1, {
  126. target: this.player.gameId,
  127. id
  128. })
  129. this._remove(id)
  130. }
  131. removeAll() {
  132. for (let id in this.myAbnormals) this.remove(id)
  133. }
  134. _add(id, duration) {
  135. this.mod.clearTimeout(this.myAbnormals[id])
  136. this.myAbnormals[id] = duration >= 0x7fffffff ? true : this.mod.setTimeout(() => { this.remove(id) }, duration)
  137. }
  138. _remove(id) {
  139. this.mod.clearTimeout(this.myAbnormals[id])
  140. delete this.myAbnormals[id]
  141. }
  142. }
  143. function debug() {
  144. console.log(`[${(Date.now() % 10000).toString().padStart(4, '0')}]`, ...arguments)
  145. }
  146. module.exports = SPAbnormals