index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict'
  2. const fs = require('fs'),
  3. fn = __dirname + '/blacklist.json',
  4. 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]
  5. let blacklist = []
  6. function ReadFile() { try { blacklist = require(fn) } catch (err) { blacklist = defaultBl } }
  7. ReadFile()
  8. if (fs.existsSync(fn)) fs.watch(fn, event => { if (event === 'change') ReadFile() })
  9. module.exports = function AutoBank(mod) {
  10. const NotCP = typeof mod.compileProto !== 'undefined'
  11. const defs = { cPutWareItem: 3, sViewWareEx: 2 }
  12. if (NotCP) {
  13. defs.cPutWareItem = mod.compileProto('uint64 gameId\nint32 container\nint32 offset\nint64 money\nint32 fromPocket\nint32 fromSlot\nint32 id\nuint64 dbid\nint32 amount\nint32 toSlot')
  14. 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\nuint64 gameId\nint32 container\nint32 action\nint32 offset\nint32 maxUsedSlot\nint32 numUsedSlots\nint64 money\nint16 numUnlockedSlots')
  15. }
  16. // ** Initialize dependency ** \\
  17. mod.game.initialize('inventory')
  18. // ** Variables ** \\
  19. let enabled = false,
  20. GuildBanking = false,
  21. AddToBl = false,
  22. RmvFromBl = false,
  23. curBankContractId = -1,
  24. lastBankedPageOffset = -1,
  25. QueuedMsgs = [],
  26. AllCurrentBankItems = [],
  27. AllCurrentBankItemsIds = []
  28. // ** Hook Functions ** \\
  29. function sViewWareEx(event) {
  30. if (!enabled) return
  31. if (lastBankedPageOffset >= event.offset) return false
  32. if (!GuildBanking && event.container === 3) return Msg('Banking to guild bank is <font color="#FE6F5E">disabled</font>.')
  33. let toBankItems = []
  34. if (event.items.length > 0 && mod.game.inventory.items.length > 0) {
  35. lastBankedPageOffset = event.offset
  36. for (const bankItem of event.items) {
  37. {
  38. const bankItemDub = { offset: event.offset, id: bankItem.id, slot: bankItem.slot },
  39. dubIndex = AllCurrentBankItemsIds.indexOf(bankItemDub.id)
  40. if (dubIndex !== -1) {
  41. const dubItem = AllCurrentBankItems[dubIndex],
  42. dub1Slot = dubItem.slot - dubItem.offset + 1,
  43. dub2Slot = bankItemDub.slot - bankItemDub.offset + 1
  44. if (dubItem.offset !== event.offset) QueuedMsgs.push(`${convToCLA(dubItem.id)} | First (${dubItem.offset / 72 + 1}:${getLine(dub1Slot)}:${dub1Slot !== 8 ? dub1Slot % 8 : 8}) ^ Second (${bankItemDub.offset / 72 + 1}:${getLine(dub2Slot)}:${dub2Slot !== 8 ? dub2Slot % 8 : 8})`)
  45. }
  46. AllCurrentBankItems.push(bankItemDub)
  47. AllCurrentBankItemsIds.push(bankItemDub.id)
  48. }
  49. const found = mod.game.inventory.findAllInBagOrPockets(bankItem.id)
  50. if (found && found.length)
  51. toBankItems.push(...found)
  52. }
  53. toBankItems = toUniqueDbidArr(toBankItems)
  54. toBankItems = remvBlacklisted(toBankItems)
  55. for (const toBank of toBankItems)
  56. 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 })
  57. }
  58. mod.setTimeout(() => {
  59. if (!enabled) return
  60. if (event.offset + 72 < event.numUnlockedSlots) mod.toServer('C_VIEW_WARE', 2, { gameId: mod.game.me.gameId, type: event.container, offset: event.offset + 72 })
  61. else mod.toServer('C_CANCEL_CONTRACT', 1, { type: 26, id: curBankContractId })
  62. }, toBankItems.length * 47 + 150)
  63. }
  64. function cPutWareItem({ id }) {
  65. if (AddToBl) {
  66. addToBl(id)
  67. return false
  68. }
  69. if (RmvFromBl) {
  70. rmvFromBl(id)
  71. return false
  72. }
  73. }
  74. function sRequestContract(event) { if (event.type === 26) curBankContractId = event.id }
  75. function sCancelContract(event) {
  76. if (event.type === 26) {
  77. curBankContractId = -1
  78. resetValues()
  79. }
  80. }
  81. //function sSytemMessage(event) { if (enabled && mod.parseSystemMessage(event.message).id === 'SMT_WAREHOUSE_FULL') return false }
  82. // ** Hooks ** \\
  83. mod.hook('S_VIEW_WARE_EX', defs.sViewWareEx, sViewWareEx)
  84. mod.hook('C_PUT_WARE_ITEM', defs.cPutWareItem, cPutWareItem)
  85. mod.hook('S_REQUEST_CONTRACT', 1, sRequestContract)
  86. mod.hook('S_CANCEL_CONTRACT', 1, sCancelContract)
  87. //mod.hook('S_SYSTEM_MESSAGE', 1, sSytemMessage)
  88. // ** Helper Functions ** \\
  89. function toUniqueDbidArr(a, b, c = {}) {
  90. b = a.length
  91. if (!b) return []
  92. while (b--)
  93. c[a[b].dbid] = a[b]
  94. return Object.values(c)
  95. }
  96. function remvBlacklisted(a, b, c = []) {
  97. b = a.length
  98. if (!b) return c
  99. while (b--)
  100. if (blacklist.indexOf(a[b].id) === -1) c.push(a[b])
  101. return c
  102. }
  103. function getLine(n, m = 1) {
  104. while (8 * m < n)
  105. m++
  106. return m
  107. }
  108. function convToCLA(id) {
  109. return `<font color="#7289DA"><ChatLinkAction param="1#####${id}">&lt;ID=${id}&gt;</ChatLinkAction></font>`
  110. }
  111. function validIdOrCLA(str) {
  112. const [...match] = str.matchAll(/<ChatLinkAction.+?1#####([0-9]+).*?<\/ChatLinkAction>|(^[0-9]+|\s[0-9]+|[0-9]+\s)/gm)
  113. let res = []
  114. for (const m of match) res.push(Number.parseInt(m[1] || m[2]))
  115. return res
  116. }
  117. const Msg = msg => mod.command.message(`${NotCP ? '[Auto-Bank]' : ''} ${msg}`),
  118. resetValues = () => {
  119. if (AddToBl) toggleAddToBl()
  120. if (RmvFromBl) toggleRmvFromBl()
  121. if (QueuedMsgs.length) {
  122. Msg(`[Warning] <font color="#FE6F5E">Duplicate</font> items were found in your bank (page:line:slot).`)
  123. for (const msg of QueuedMsgs) Msg(`\t- ${msg}`)
  124. }
  125. if (enabled) Msg('Auto Banking is <font color="#FE6F5E">disabled</font>.')
  126. enabled = false
  127. QueuedMsgs.length = 0
  128. AllCurrentBankItems.length = 0
  129. AllCurrentBankItemsIds.length = 0
  130. lastBankedPageOffset = -1
  131. },
  132. saveBlacklistFile = () => fs.writeFileSync(fn, JSON.stringify(blacklist.sort((a, b) => a - b)), err => { ReadFile(); if (err) console.log(err) }),
  133. toggleAddToBl = () => {
  134. AddToBl = !AddToBl
  135. if (RmvFromBl && AddToBl) toggleRmvFromBl()
  136. Msg(`Adding to Blacklist <font color="#${AddToBl ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  137. if (!AddToBl) saveBlacklistFile()
  138. },
  139. toggleRmvFromBl = () => {
  140. RmvFromBl = !RmvFromBl
  141. if (RmvFromBl && AddToBl) toggleAddToBl()
  142. Msg(`Removing from Blacklist <font color="#${RmvFromBl ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  143. if (!RmvFromBl) saveBlacklistFile()
  144. },
  145. addToBl = (...ids) => {
  146. const str = ids.join(' '), res = validIdOrCLA(str)
  147. if (!res.length) return Msg(`The argument(s) <font color="#FE6F5E">doesn't contain</font> valid id/itemLink to be blacklisted.\nGiven: '${str}'`)
  148. for (const id of res)
  149. if (blacklist.indexOf(id) === -1) {
  150. blacklist.push(id)
  151. Msg(`The item ${convToCLA(id)} was <font color="#4DE19C">Added</font> to blacklist.`)
  152. }
  153. else Msg(`The item ${convToCLA(id)} is <font color="#AB58F4">Already</font> blacklisted.`)
  154. saveBlacklistFile()
  155. },
  156. rmvFromBl = (...ids) => {
  157. const str = ids.join(' '), res = validIdOrCLA(str)
  158. if (!res.length) return Msg(`The argument(s) <font color="#FE6F5E">doesn't contain</font> valid id/itemLink to be unblacklisted.\nGiven: '${str}'`)
  159. for (const id of res) {
  160. const blItemIndex = blacklist.indexOf(id)
  161. if (blItemIndex !== -1) {
  162. blacklist.splice(blItemIndex, 1)
  163. Msg(`The item ${convToCLA(id)} was <font color="#FE6F5E">Removed</font> from blacklist.`)
  164. }
  165. else Msg(`The item ${convToCLA(id)} is <font color="#AB58F4">Not</font> blacklisted.`)
  166. }
  167. saveBlacklistFile()
  168. }
  169. // ** Command ** \\
  170. mod.command.add(['autobank', 'ab'], (key, arg, ...args) => {
  171. if (key) key = key.toLowerCase()
  172. switch (key) {
  173. case 'guild': case 'g':
  174. GuildBanking = !GuildBanking
  175. Msg(`Banking to Guild Bank is <font color="#${GuildBanking ? `4DE19C">En` : `FE6F5E">Dis`}abled</font>.`)
  176. break
  177. case 'blacklist': case 'bl':
  178. switch (arg) {
  179. case 'add': case '+':
  180. if (!args.length) toggleAddToBl()
  181. else addToBl(...args)
  182. break
  183. case 'remove': case 'rem': case 'rm': case '-':
  184. if (!args.length) toggleRmvFromBl()
  185. else rmvFromBl(...args)
  186. break
  187. case 'reset': case 'default':
  188. blacklist = defaultBl
  189. saveBlacklistFile()
  190. Msg(`Blacklist has been reset to default.`)
  191. break
  192. case 'clear': case 'empty':
  193. blacklist = []
  194. saveBlacklistFile()
  195. Msg(`Blacklist has been cleared.`)
  196. break
  197. case 'list':
  198. let bll = blacklist.length
  199. Msg(`Blacklist has ${bll} items.`)
  200. while (bll--)
  201. Msg(`\t- ${convToCLA(blacklist[bll])}`)
  202. break
  203. default:
  204. Msg('Invalid blacklist command, please refer to the readme.')
  205. break
  206. }
  207. break
  208. case 'rmblacklist': case 'rmvblacklist': case 'rmbl': case 'rmvbl':
  209. Msg('<font color="#FE6F5E">Deprecated command</font>, please use `<font color="#AB58F4">ab bl rem \<items?...\></font>` instead!')
  210. break
  211. default:
  212. if (!enabled) {
  213. enabled = true
  214. Msg('Auto Banking is <font color="#4DE19C">enabled</font>.')
  215. }
  216. else resetValues()
  217. break
  218. }
  219. })
  220. }