Skip to content

Commit 0f508c5

Browse files
authored
New Hook format & removed S_LOGIN
1 parent c40bb1c commit 0f508c5

File tree

2 files changed

+118
-102
lines changed

2 files changed

+118
-102
lines changed

index.js

+117-100
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = function TerableAngler(mod) {
2+
mod.game.initialize(["me"]);
23
const command = mod.command || mod.require.command;
4+
35
let enabled = false,
46
selling = false,
57
waitingInventory = false,
@@ -8,13 +10,24 @@ module.exports = function TerableAngler(mod) {
810
numAnglerTokens = 0,
911
amountToBuy = 0,
1012
amountBought = 0,
11-
gameId = 0n,
1213
itemsToProcess = [],
1314
contactBuy = {},
1415
contactSell = {},
1516
dialogBuy = {},
16-
dialogSell = {};
17+
dialogSell = {},
18+
hooks = [];
1719

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+
1831
if(mod.proxyAuthor !== 'caali'){
1932
const options = require('./module').options
2033
if(options){
@@ -35,6 +48,8 @@ module.exports = function TerableAngler(mod) {
3548
getNumAnglerTokens = false;
3649
command.message(`TerableAngler is now ${enabled ? "enabled" : "disabled"}.`);
3750
if(enabled) command.message("Talk to Angler Token Vendor, then talk to summoned merchant");
51+
if(enabled) load();
52+
else unload();
3853
},
3954
id(x){
4055
x = parseInt(x);
@@ -74,7 +89,7 @@ module.exports = function TerableAngler(mod) {
7489
//amountToBuy = parseInt(number/8); // buy 8 inventory slots at a time
7590
amountToBuy = 1; // buy 8 inventory slots at a time
7691
amountBought = 0;
77-
enabled = true;
92+
load();
7893
selling = false;
7994
waitingInventory = false;
8095
getNumAnglerTokens = false;
@@ -94,53 +109,113 @@ module.exports = function TerableAngler(mod) {
94109
dialogSell = {};
95110
}
96111

97-
mod.hook('S_LOGIN', mod.majorPatchVersion >= 81 ? 13 : 12, event => {
98-
gameId = event.gameId;
112+
mod.game.on('enter_game', () => {
99113
enabled = false;
100114
selling = false;
101115
waitingInventory = false;
102116
getNumAnglerTokens = false;
103117
timeout = null;
104118
itemsToProcess = [];
105119
clearNPC();
120+
unload();
106121
});
107122

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+
});
141216
}
142-
});
143-
217+
}
218+
144219

145220
function startSelling(){ // get item slots
146221
if(contactBuy.gameId && contactSell.gameId && dialogBuy.id && dialogSell.id) {
@@ -173,6 +248,7 @@ module.exports = function TerableAngler(mod) {
173248
if (dialogHook) {
174249
mod.unhook(dialogHook);
175250
command.message('Failed to contact npc. Stopping...');
251+
unload();
176252
}
177253
}, 5000);
178254

@@ -189,9 +265,9 @@ module.exports = function TerableAngler(mod) {
189265
clearTimeout(timeout);
190266
timeout = mod.setTimeout(() => {
191267
if(dialogHook){
192-
enabled = false;
193268
mod.unhook(dialogHook);
194269
command.message('Failed to contact npc. Stopping...');
270+
unload();
195271
}
196272
}, 5000);
197273

@@ -200,65 +276,6 @@ module.exports = function TerableAngler(mod) {
200276
mod.toServer('C_DIALOG', 1, Object.assign(dialogBuy, { id: event.id }));
201277
});
202278
}
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-
});
262279

263280
function sortSlot(){
264281
itemsToProcess.sort(function (a, b){

manifest.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"hash": "b7ca95a0fb5dfe01dfc0045bfe7227417743aa097d9785db8e5b03e3d96e1919"
66
},
77
"example.gif": "ca8270707f0369928a43b6b6f1f386a04c50c044b2bf3c9d9c29fde23fbffc20",
8-
"index.js": "81700d7b7488b899f47623499ad7ee97d0132704268ec443d497bd4a97c3ac52",
8+
"index.js": "e42be058d9eac41543de3a65f94335fd125ed2ef8ed1cf41474d73b67cd85173",
99
"module.json": "7fae27fc2a0e78329d45ef7224939c6bfa031237edbd6f10108f42ea54d055dc",
1010
"README.md": "e9742271145599af430b08243a073ec205d75e6c7d9d7771492292d5b67fd93d",
1111
"settings_migrator.js": "15e6e0a3cf0139438aa2d3145a465774905ec658aab0a6c54406c3a73724f58a",
@@ -19,7 +19,6 @@
1919
"protocol/C_STORE_COMMIT.1.def": "59850a2c3c41f15fae068084fe305084b94313070b818cdeda4168510756ab89"
2020
},
2121
"defs": {
22-
"S_LOGIN": [12,13],
2322
"C_NPC_CONTACT": 2,
2423
"C_DIALOG": 1,
2524
"S_INVEN": 18,

0 commit comments

Comments
 (0)