1
1
module . exports = function TerableAngler ( mod ) {
2
+ mod . game . initialize ( [ "me" ] ) ;
2
3
const command = mod . command || mod . require . command ;
4
+
3
5
let enabled = false ,
4
6
selling = false ,
5
7
waitingInventory = false ,
@@ -8,13 +10,24 @@ module.exports = function TerableAngler(mod) {
8
10
numAnglerTokens = 0 ,
9
11
amountToBuy = 0 ,
10
12
amountBought = 0 ,
11
- gameId = 0n ,
12
13
itemsToProcess = [ ] ,
13
14
contactBuy = { } ,
14
15
contactSell = { } ,
15
16
dialogBuy = { } ,
16
- dialogSell = { } ;
17
+ dialogSell = { } ,
18
+ hooks = [ ] ;
17
19
20
+ function hook ( ) { hooks . push ( mod . hook ( ...arguments ) ) ; }
21
+
22
+ function unload ( ) {
23
+ enabled = false ;
24
+ if ( hooks . length ) {
25
+ for ( let h of hooks )
26
+ mod . unhook ( h ) ;
27
+ hooks = [ ] ;
28
+ }
29
+ }
30
+
18
31
if ( mod . proxyAuthor !== 'caali' ) {
19
32
const options = require ( './module' ) . options
20
33
if ( options ) {
@@ -35,6 +48,8 @@ module.exports = function TerableAngler(mod) {
35
48
getNumAnglerTokens = false ;
36
49
command . message ( `TerableAngler is now ${ enabled ? "enabled" : "disabled" } .` ) ;
37
50
if ( enabled ) command . message ( "Talk to Angler Token Vendor, then talk to summoned merchant" ) ;
51
+ if ( enabled ) load ( ) ;
52
+ else unload ( ) ;
38
53
} ,
39
54
id ( x ) {
40
55
x = parseInt ( x ) ;
@@ -74,7 +89,7 @@ module.exports = function TerableAngler(mod) {
74
89
//amountToBuy = parseInt(number/8); // buy 8 inventory slots at a time
75
90
amountToBuy = 1 ; // buy 8 inventory slots at a time
76
91
amountBought = 0 ;
77
- enabled = true ;
92
+ load ( ) ;
78
93
selling = false ;
79
94
waitingInventory = false ;
80
95
getNumAnglerTokens = false ;
@@ -94,53 +109,113 @@ module.exports = function TerableAngler(mod) {
94
109
dialogSell = { } ;
95
110
}
96
111
97
- mod . hook ( 'S_LOGIN' , mod . majorPatchVersion >= 81 ? 13 : 12 , event => {
98
- gameId = event . gameId ;
112
+ mod . game . on ( 'enter_game' , ( ) => {
99
113
enabled = false ;
100
114
selling = false ;
101
115
waitingInventory = false ;
102
116
getNumAnglerTokens = false ;
103
117
timeout = null ;
104
118
itemsToProcess = [ ] ;
105
119
clearNPC ( ) ;
120
+ unload ( ) ;
106
121
} ) ;
107
122
108
- mod . hook ( 'C_NPC_CONTACT' , 2 , event => {
109
- if ( ! enabled ) return ;
110
- if ( ! contactBuy . gameId ) { Object . assign ( contactBuy , event ) ; }
111
- else if ( ! contactSell . gameId ) { Object . assign ( contactSell , event ) ; }
112
- } ) ;
113
-
114
- mod . hook ( 'C_DIALOG' , 1 , event => {
115
- if ( ! enabled ) return ;
116
- if ( ! dialogBuy . id ) { Object . assign ( dialogBuy , event ) ; }
117
- else if ( ! dialogSell . id ) { Object . assign ( dialogSell , event ) ; }
118
- } ) ;
119
-
120
- mod . hook ( 'S_INVEN' , 18 , event => {
121
- if ( ! enabled ) return ;
122
- if ( waitingInventory ) {
123
- for ( const item of event . items ) { // add items
124
- if ( 204200 == item . id ) itemsToProcess . push ( { id : item . id , slot : item . slot } ) ;
125
- }
126
- if ( ! event . more ) {
127
- waitingInventory = false ;
128
- processItemsToSell ( ) ;
129
- }
130
- } else if ( getNumAnglerTokens ) {
131
- for ( const item of event . items ) {
132
- if ( 204051 == item . id && item . amount > numAnglerTokens ) numAnglerTokens = item . amount ;
133
- }
134
- if ( ! event . more ) {
135
- getNumAnglerTokens = false ;
136
- if ( amountToBuy * 100 > numAnglerTokens ) {
137
- enabled = false ;
138
- command . message ( "You're out of Angler Tokens. Stopping..." ) ;
139
- } else { processItemsToBuy ( ) ; }
140
- }
123
+ function load ( ) {
124
+ if ( ! hooks . length ) {
125
+ hook ( 'C_NPC_CONTACT' , 2 , event => {
126
+ if ( ! contactBuy . gameId ) { Object . assign ( contactBuy , event ) ; }
127
+ else if ( ! contactSell . gameId ) { Object . assign ( contactSell , event ) ; }
128
+ } ) ;
129
+
130
+ hook ( 'C_DIALOG' , 1 , event => {
131
+ if ( ! dialogBuy . id ) { Object . assign ( dialogBuy , event ) ; }
132
+ else if ( ! dialogSell . id ) { Object . assign ( dialogSell , event ) ; }
133
+ } ) ;
134
+
135
+ hook ( 'S_INVEN' , 18 , event => {
136
+ if ( waitingInventory ) {
137
+ for ( const item of event . items ) { // add items
138
+ if ( 204200 == item . id ) itemsToProcess . push ( { id : item . id , slot : item . slot } ) ;
139
+ }
140
+ if ( ! event . more ) {
141
+ waitingInventory = false ;
142
+ processItemsToSell ( ) ;
143
+ }
144
+ } else if ( getNumAnglerTokens ) {
145
+ for ( const item of event . items ) {
146
+ if ( 204051 == item . id && item . amount > numAnglerTokens ) numAnglerTokens = item . amount ;
147
+ }
148
+ if ( ! event . more ) {
149
+ getNumAnglerTokens = false ;
150
+ if ( amountToBuy * 800 > numAnglerTokens ) {
151
+ command . message ( "You're out of Angler Tokens. Stopping..." ) ;
152
+ unload ( ) ;
153
+ } else { processItemsToBuy ( ) ; }
154
+ }
155
+ }
156
+ } ) ;
157
+
158
+ hook ( 'S_REQUEST_CONTRACT' , 1 , event => {
159
+ if ( ! contactBuy . gameId || ! contactSell . gameId || ! dialogBuy . id || ! dialogSell . id ) return ;
160
+ if ( selling && event . type === 9 ) { // 9 = merchant, fishing or crystal
161
+ if ( itemsToProcess . length > 0 ) {
162
+ let delay = mod . settings . initialDelay ;
163
+ sortSlot ( ) ;
164
+ let item = itemsToProcess [ 0 ] ;
165
+ timeout = mod . setTimeout ( ( ) => {
166
+ mod . toServer ( 'C_STORE_SELL_ADD_BASKET' , 1 , {
167
+ cid : mod . game . me . gameId ,
168
+ npc : event . id ,
169
+ item : item . id ,
170
+ quantity : 80 ,
171
+ slot : item . slot
172
+ } ) ;
173
+ } , delay ) ;
174
+ delay += mod . settings . addItemDelay ;
175
+ itemsToProcess = itemsToProcess . slice ( 8 ) ;
176
+ timeout = mod . setTimeout ( ( ) => {
177
+ mod . toServer ( 'C_STORE_COMMIT' , 1 , { gameId : mod . game . me . gameId , contract : event . id } ) ;
178
+ } , delay ) ;
179
+ } else {
180
+ selling = false ;
181
+ mod . toServer ( 'C_CANCEL_CONTRACT' , 1 , {
182
+ type : 9 ,
183
+ id : event . id
184
+ } ) ;
185
+ clearTimeout ( timeout ) ;
186
+ timeout = setTimeout ( startBuying , mod . settings . timeBetweenNpcContacts ) ; // sell -> buy
187
+ }
188
+ } else if ( ! selling && event . type === 20 ) { // 20 = angler token
189
+ if ( amountToBuy > amountBought ) { // buy more
190
+ let delay = mod . settings . initialDelay ;
191
+ timeout = mod . setTimeout ( ( ) => {
192
+ mod . toServer ( 'C_MEDAL_STORE_BUY_ADD_BASKET' , 1 , {
193
+ gameId : mod . game . me . gameId ,
194
+ contract : event . id ,
195
+ item : 204200 ,
196
+ amount : 80
197
+ } ) ;
198
+ } , delay ) ;
199
+ amountBought ++ ;
200
+ delay += mod . settings . addItemDelay ;
201
+ timeout = mod . setTimeout ( ( ) => {
202
+ mod . toServer ( 'C_MEDAL_STORE_COMMIT' , 1 , { gameId : mod . game . me . gameId , contract : event . id } ) ;
203
+ } , delay ) ;
204
+ } else {
205
+ amountBought = 0 ;
206
+ selling = true ;
207
+ mod . toServer ( 'C_CANCEL_CONTRACT' , 1 , {
208
+ type : 20 ,
209
+ id : event . id
210
+ } ) ;
211
+ clearTimeout ( timeout ) ;
212
+ timeout = setTimeout ( startSelling , mod . settings . timeBetweenNpcContacts ) ; // buy -> sell
213
+ }
214
+ }
215
+ } ) ;
141
216
}
142
- } ) ;
143
-
217
+ }
218
+
144
219
145
220
function startSelling ( ) { // get item slots
146
221
if ( contactBuy . gameId && contactSell . gameId && dialogBuy . id && dialogSell . id ) {
@@ -173,6 +248,7 @@ module.exports = function TerableAngler(mod) {
173
248
if ( dialogHook ) {
174
249
mod . unhook ( dialogHook ) ;
175
250
command . message ( 'Failed to contact npc. Stopping...' ) ;
251
+ unload ( ) ;
176
252
}
177
253
} , 5000 ) ;
178
254
@@ -189,9 +265,9 @@ module.exports = function TerableAngler(mod) {
189
265
clearTimeout ( timeout ) ;
190
266
timeout = mod . setTimeout ( ( ) => {
191
267
if ( dialogHook ) {
192
- enabled = false ;
193
268
mod . unhook ( dialogHook ) ;
194
269
command . message ( 'Failed to contact npc. Stopping...' ) ;
270
+ unload ( ) ;
195
271
}
196
272
} , 5000 ) ;
197
273
@@ -200,65 +276,6 @@ module.exports = function TerableAngler(mod) {
200
276
mod . toServer ( 'C_DIALOG' , 1 , Object . assign ( dialogBuy , { id : event . id } ) ) ;
201
277
} ) ;
202
278
}
203
-
204
- mod . hook ( 'S_REQUEST_CONTRACT' , 1 , event => {
205
- if ( ! enabled || ! contactBuy . gameId || ! contactSell . gameId || ! dialogBuy . id || ! dialogSell . id ) return ;
206
- if ( selling && event . type === 9 ) { // 9 = merchant, fishing or crystal
207
- if ( itemsToProcess . length > 0 ) {
208
- let delay = mod . settings . initialDelay ;
209
- sortSlot ( ) ;
210
- let item = itemsToProcess [ 0 ] ;
211
- timeout = mod . setTimeout ( ( ) => {
212
- mod . toServer ( 'C_STORE_SELL_ADD_BASKET' , 1 , {
213
- cid : gameId ,
214
- npc : event . id ,
215
- item : item . id ,
216
- quantity : 80 ,
217
- slot : item . slot
218
- } ) ;
219
- } , delay ) ;
220
- delay += mod . settings . addItemDelay ;
221
- itemsToProcess = itemsToProcess . slice ( 8 ) ;
222
- timeout = mod . setTimeout ( ( ) => {
223
- mod . toServer ( 'C_STORE_COMMIT' , 1 , { gameId, contract : event . id } ) ;
224
- } , delay ) ;
225
- } else {
226
- selling = false ;
227
- mod . toServer ( 'C_CANCEL_CONTRACT' , 1 , {
228
- type : 9 ,
229
- id : event . id
230
- } ) ;
231
- clearTimeout ( timeout ) ;
232
- timeout = setTimeout ( startBuying , mod . settings . timeBetweenNpcContacts ) ; // sell -> buy
233
- }
234
- } else if ( ! selling && event . type === 20 ) { // 20 = angler token
235
- if ( amountToBuy > amountBought ) { // buy more
236
- let delay = mod . settings . initialDelay ;
237
- timeout = mod . setTimeout ( ( ) => {
238
- mod . toServer ( 'C_MEDAL_STORE_BUY_ADD_BASKET' , 1 , {
239
- gameId : gameId ,
240
- contract : event . id ,
241
- item : 204200 ,
242
- amount : 80
243
- } ) ;
244
- } , delay ) ;
245
- amountBought ++ ;
246
- delay += mod . settings . addItemDelay ;
247
- timeout = mod . setTimeout ( ( ) => {
248
- mod . toServer ( 'C_MEDAL_STORE_COMMIT' , 1 , { gameId, contract : event . id } ) ;
249
- } , delay ) ;
250
- } else {
251
- amountBought = 0 ;
252
- selling = true ;
253
- mod . toServer ( 'C_CANCEL_CONTRACT' , 1 , {
254
- type : 20 ,
255
- id : event . id
256
- } ) ;
257
- clearTimeout ( timeout ) ;
258
- timeout = setTimeout ( startSelling , mod . settings . timeBetweenNpcContacts ) ; // buy -> sell
259
- }
260
- }
261
- } ) ;
262
279
263
280
function sortSlot ( ) {
264
281
itemsToProcess . sort ( function ( a , b ) {
0 commit comments