index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict'
  2. // ~~~ * Data * ~~~ \\
  3. const fs = require('fs'),
  4. fn = __dirname + '/blacklist.json',
  5. defaultBl = [112, 114, 116, 194, 130, 444, 6552, 6553, 6562, 6563, 9310, 9311, 60260, 70000, 80081, 80095, 81209, 150532, 150533, 150534, 150535, 150542, 151643, 155324, 160322, 167001, 177131, 177132, 177133, 200529, 200922, 200999, 201957, 201958, 202015, 206049, 206050, 206051]
  6. let blacklist = []
  7. function ReadFile() { try { blacklist = require(fn) } catch (err) { blacklist = defaultBl } }
  8. ReadFile()
  9. if (fs.existsSync(fn)) fs.watch(fn, event => { if (event === 'change') ReadFile() })
  10. module.exports = function AutoBank(mod) {
  11. // ~~~ * Constants * ~~~ \\
  12. const NotCP = typeof mod.compileProto !== 'undefined'
  13. const defs = { cPutWareItem: 3, sViewWareEx: 3 }
  14. if (NotCP) {
  15. defs.cPutWareItem = mod.compileProto('uint64 gameId\nint32 container\nint32 offset\nint64 money\nint32 fromPocket\nint32 fromSlot\nint32 id\nuint64 dbid\nint32 amount\nint32 toSlot')
  16. defs.sViewWareEx = mod.compileProto('array items\n- array<int32> crystals\n- int32 id\n- uint64 dbid\n- uint64 ownerId\n- int32 container\n- int32 pocket\n- uint32 slot\n- int32 amountTotal\n- int32 amount\n- int32 enchant\n- int32 durability\n- bool soulbound\n- uint32 unk\n- bool masterwork\n- int32 enigma\n- int32 enchantAdvantage\n- int32 enchantBonus\n- int32 enchantBonusMaxPlus\n- bool awakened\n- int32 liberationStatus\n- int64 availableUntil ^96\nuint64 gameId\nint32 container\nint32 action\nint32 offset\nint32 maxUsedSlot\nint32 numUsedSlots\nint64 money\nint16 numUnlockedSlots')
  17. }
  18. // ~~~ * Initialize dependency * ~~~ \\
  19. mod.game.initialize('inventory')
  20. // ~~~ * Variables * ~~~ \\
  21. let Enabled = false,
  22. GuildBanking = false,
  23. AddToBl = false,
  24. RmvFromBl = false,
  25. curBankContractId = -1,
  26. lastBankedPageOffset = -1,
  27. warningStack = -1,
  28. QueuedDups = new Map(),
  29. AllCurBankItems = new Map()
  30. // ~~~ * Hook Functions * ~~~ \\
  31. function sViewWareEx(event) {
  32. if (!Enabled) return
  33. if (lastBankedPageOffset >= event.offset) return false
  34. if (!GuildBanking && event.container === 3) return Msg('Banking to guild bank is <font color="#FE6F5E">disabled</font>.')
  35. let toBankItems = []
  36. if (event.items.length > 0 && mod.game.inventory.items.length > 0) {
  37. lastBankedPageOffset = event.offset
  38. for (const bankItem of event.items) {
  39. {
  40. if (AllCurBankItems.has(bankItem.id)) {
  41. const dupItem = AllCurBankItems.get(bankItem.id)
  42. if (dupItem.offset !== event.offset)
  43. QueuedDups.has(dupItem.id) ?
  44. QueuedDups.get(dupItem.id).count++ :
  45. QueuedDups.set(dupItem.id, { count: 1, link: convToCLA(dupItem.id), details1: getDetails(dupItem.offset, dupItem.slot), details2: getDetails(event.offset, bankItem.slot) })
  46. } else AllCurBankItems.set(bankItem.id, { offset: event.offset, id: bankItem.id, slot: bankItem.slot })
  47. }
  48. const found = mod.game.inventory.findAllInBagOrPockets(bankItem.id)
  49. if (found && found.length)
  50. toBankItems.push(...found)
  51. }
  52. toBankItems = toUniqueDbidArr(toBankItems)
  53. toBankItems = remvBlacklisted(toBankItems)
  54. for (const toBank of toBankItems)
  55. mod.toServer('C_PUT_WARE_ITEM', defs.cPutWareItem, { gameId: mod.game.me.gameId, container: event.container, offset: event.offset, fromPocket: toBank.pocket, fromSlot: toBank.slot, id: toBank.id, dbid: toBank.dbid, amount: toBank.amount, toSlot: event.offset })
  56. }
  57. mod.setTimeout(() => {
  58. if (!Enabled) return
  59. if (event.offset + 72 < event.numUnlockedSlots) mod.toServer('C_VIEW_WARE', 2, { gameId: mod.game.me.gameId, type: event.container, offset: event.offset + 72 })
  60. else mod.toServer('C_CANCEL_CONTRACT', 1, { type: 26, id: curBankContractId })
  61. }, toBankItems.length * 47 + 150)
  62. }
  63. function cPutWareItem({ id }) {
  64. if (AddToBl) {
  65. addToBl(id)
  66. return false
  67. }
  68. if (RmvFromBl) {
  69. rmvFromBl(id)
  70. return false
  71. }
  72. }
  73. function sRequestContract(event) { if (event.type === 26) curBankContractId = event.id }
  74. function sCancelContract(event) {
  75. if (event.type === 26) {
  76. curBankContractId = -1
  77. resetValues()
  78. }
  79. }
  80. //function sSytemMessage(event) { if (enabled && mod.parseSystemMessage(event.message).id === 'SMT_WAREHOUSE_FULL') return false }
  81. // ~~~ * Hooks * ~~~ \\
  82. mod.hook('S_VIEW_WARE_EX', defs.sViewWareEx, sViewWareEx)
  83. mod.hook('C_PUT_WARE_ITEM', defs.cPutWareItem, cPutWareItem)
  84. mod.hook('S_REQUEST_CONTRACT', 1, sRequestContract)
  85. mod.hook('S_CANCEL_CONTRACT', 1, sCancelContract)
  86. //mod.hook('S_SYSTEM_MESSAGE', 1, sSytemMessage)
  87. // ~~~ * Helper Functions * ~~~ \\
  88. function toUniqueDbidArr(a, b, c = {}) {
  89. b = a.length
  90. if (!b) return []
  91. while (b--)
  92. c[a[b].dbid] = a[b]
  93. return Object.values(c)
  94. }
  95. function remvBlacklisted(a, b, c = []) {
  96. b = a.length
  97. if (!b) return c
  98. while (b--) {
  99. const el = a[b]
  100. if (el.pocket !== 0) {
  101. if (blacklist.indexOf('pocket1') !== -1 && el.pocket === 1) continue
  102. if (blacklist.indexOf('pocket2') !== -1 && el.pocket === 2) continue
  103. if (blacklist.indexOf('pocket3') !== -1 && el.pocket === 3) continue
  104. }
  105. if (blacklist.indexOf(el.id) === -1) c.push(el)
  106. }
  107. return c
  108. }
  109. function getLine(n, m = 1) {
  110. while (8 * m < n)
  111. m++
  112. return m
  113. }
  114. function getDetails(a, b, s = 0) {
  115. s = b - a + 1
  116. return `${a / 72 + 1}:${getLine(s)}:${s % 8 || 8}`
  117. }
  118. function convToCLA(id) {
  119. return typeof id === 'string' ? id : `item <font color="#7289DA"><ChatLinkAction param="1#####${id}">&lt;Click Me&gt;</ChatLinkAction></font>`
  120. }
  121. function validIdOrCLA(str, res = []) {
  122. //Match if <ChatLinkAction> OR <start or space>Id<space or end> OR <start or space>pocket<1 or 2 or 3><space or end>.
  123. const [...match] = str.matchAll(/<ChatLinkAction.+?1#####([0-9]+).*?<\/ChatLinkAction>|(\b(?:(?<=\s)|^)[0-9]+(?:(?=\s)|$)\b)|(\b(?:(?<=\s)|^)pocket[1-3](?:(?=\s)|$)\b)/gi)
  124. for (const m of match) res.push(Number.parseInt(m[1] || m[2]) || m[3]?.toLowerCase())
  125. return res
  126. }
  127. function sortStrNum(arr) {
  128. arr.sort((a, b) => typeof a === 'string' ? -1 : a - b)
  129. arr.sort((a, b) => typeof a === 'string' && typeof b === 'string' ? a.slice(-1) - b.slice(-1) : 0)
  130. return arr
  131. }
  132. // ~~~ * Command Functions * ~~~ \\
  133. const Msg = msg => mod.command.message(`${NotCP ? '[Auto-Bank]' : ''} ${msg}`),
  134. resetValues = () => {
  135. if (AddToBl) toggleAddToBl()
  136. if (RmvFromBl) toggleRmvFromBl()
  137. if (QueuedDups.size && warningStack !== QueuedDups.size) {
  138. warningStack = QueuedDups.size
  139. Msg(`[Warning] <font color="#FE6F5E">Duplicate</font> items were found in your bank (page:line:slot).`)
  140. if (QueuedDups.size < 50)
  141. for (const d of QueuedDups.values())
  142. Msg(`- ${d.count}x${d.link} | (${d.details1}) & (${d.details2})`)
  143. else Msg(`Over 50 items; won't be listed to prevent spam.`)
  144. }
  145. if (Enabled) Msg('Auto Banking is <font color="#FE6F5E">disabled</font>.')
  146. Enabled = false
  147. QueuedDups.clear()
  148. AllCurBankItems.clear()
  149. lastBankedPageOffset = -1
  150. },
  151. saveBlacklistFile = () => fs.writeFileSync(fn, JSON.stringify(sortStrNum(blacklist)), err => { ReadFile(); if (err) console.log(err) }),
  152. toggleAddToBl = () => {
  153. AddToBl = !AddToBl
  154. if (RmvFromBl && AddToBl) toggleRmvFromBl()
  155. Msg(`Adding to Blacklist <font color="#${AddToBl ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  156. if (!AddToBl) saveBlacklistFile()
  157. },
  158. toggleRmvFromBl = () => {
  159. RmvFromBl = !RmvFromBl
  160. if (RmvFromBl && AddToBl) toggleAddToBl()
  161. Msg(`Removing from Blacklist <font color="#${RmvFromBl ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  162. if (!RmvFromBl) saveBlacklistFile()
  163. },
  164. addToBl = (...ids) => {
  165. const str = ids.join(' '), res = validIdOrCLA(str)
  166. if (!res.length) return Msg(`The argument(s) <font color="#FE6F5E">doesn't contain</font> valid id/itemLink to be blacklisted.\nGiven: '${str}'`)
  167. for (const id of res)
  168. if (blacklist.indexOf(id) === -1) {
  169. blacklist.push(id)
  170. Msg(`The ${convToCLA(id)} was <font color="#4DE19C">Added</font> to blacklist.`)
  171. }
  172. else Msg(`The ${convToCLA(id)} is <font color="#AB58F4">Already</font> blacklisted.`)
  173. saveBlacklistFile()
  174. },
  175. rmvFromBl = (...ids) => {
  176. const str = ids.join(' '), res = validIdOrCLA(str)
  177. if (!res.length) return Msg(`The argument(s) <font color="#FE6F5E">doesn't contain</font> valid id/itemLink to be unblacklisted.\nGiven: '${str}'`)
  178. for (const id of res) {
  179. const blItemIndex = blacklist.indexOf(id)
  180. if (blItemIndex !== -1) {
  181. blacklist.splice(blItemIndex, 1)
  182. Msg(`The ${convToCLA(id)} was <font color="#FE6F5E">Removed</font> from blacklist.`)
  183. }
  184. else Msg(`The ${convToCLA(id)} is <font color="#AB58F4">Not</font> blacklisted.`)
  185. }
  186. saveBlacklistFile()
  187. }
  188. // ~~~ * Command * ~~~ \\
  189. mod.command.add(['autobank', 'ab'], (key, arg, ...args) => {
  190. if (key) key = key.toLowerCase()
  191. if (arg) arg = arg.toLowerCase()
  192. switch (key) {
  193. case 'guild': case 'g':
  194. GuildBanking = !GuildBanking
  195. Msg(`Banking to Guild Bank is <font color="#${GuildBanking ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  196. break
  197. case 'blacklist': case 'bl':
  198. switch (arg) {
  199. case 'add': case 'ad': case '+':
  200. if (!args.length) toggleAddToBl()
  201. else addToBl(...args)
  202. break
  203. case 'remove': case 'rem': case 'rm': case 'rmv': case '-':
  204. if (!args.length) toggleRmvFromBl()
  205. else rmvFromBl(...args)
  206. break
  207. case 'reset': case 'default':
  208. blacklist = defaultBl
  209. saveBlacklistFile()
  210. Msg(`Blacklist has been reset to default.`)
  211. break
  212. case 'clear': case 'empty':
  213. blacklist = []
  214. saveBlacklistFile()
  215. Msg(`Blacklist has been cleared.`)
  216. break
  217. case 'list':
  218. let bll = blacklist.length
  219. Msg(`Blacklist has ${bll} items.`)
  220. while (bll--)
  221. Msg(`\t- ${convToCLA(blacklist[bll])}`)
  222. break
  223. default:
  224. Msg('Invalid blacklist command, please refer to the README.')
  225. break
  226. }
  227. break
  228. case 'rmblacklist': case 'rmvblacklist': case 'rmbl': case 'rmvbl':
  229. Msg('<font color="#FE6F5E">Deprecated command</font>, please use `<font color="#AB58F4">ab bl rem \<items?...\></font>` instead!')
  230. break
  231. default:
  232. if (!Enabled) {
  233. Enabled = true
  234. Msg('Auto Banking is <font color="#4DE19C">enabled</font>.')
  235. }
  236. else resetValues()
  237. break
  238. }
  239. })
  240. }