-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
238 lines (195 loc) · 7.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const AUTO_ACCEPT_THRESHOLD = 1, // Automatically accepts offers for *equal or more than* the specified amount (0 to disable).
AUTO_REJECT_THRESHOLD = 0.75, /* Automatically declines offers for *less* than the specified amount (or AUTO_ACCEPT_THRESHOLD).
Example: 0.75 will decline offers for less than 75% of the asking price (0 to disable).
*/
UNATTENDED_MANUAL_NEGOTIATE = false, /* Allows the user to click the Accept button once, and the negotiation will be handled automatically.
Warning: Use this at your own risk. Recommended to set Bargain to a seperate chat tab to prevent
clicking accidentally.
*/
DELAY_ACTIONS = true, // Simulate human-like response times.
ACTION_DELAY_LONG_MS = [1200, 2600], // [Min, Max]
ACTION_DELAY_SHORT_MS = [400, 800] // [Min, Max]
const TYPE_NEGOTIATION_PENDING = 35,
TYPE_NEGOTIATION = 36
const sysmsg = require('tera-data-parser').sysmsg,
Command = require('command')
module.exports = function AutoNegotiate(dispatch) {
const command = Command(dispatch)
let recentDeals = UNATTENDED_MANUAL_NEGOTIATE ? {} : null,
pendingDeals = [],
currentDeal = null,
currentContract = null,
actionTimeout = null,
cancelTimeout = null
dispatch.hook('S_TRADE_BROKER_DEAL_SUGGESTED', 1, event => {
// Remove old deals that haven't been processed yet
for(let i = 0; i < pendingDeals.length; i++) {
let deal = pendingDeals[i]
if(deal.playerId == event.playerId && deal.listing == event.listing) pendingDeals.splice(i--, 1)
}
if(comparePrice(event.offeredPrice, event.sellerPrice) != 0) {
pendingDeals.push(event)
queueNextDeal(true)
return false
}
else if(UNATTENDED_MANUAL_NEGOTIATE) {
let dealId = event.playerId + '-' + event.listing
if(recentDeals[dealId]) clearTimeout(recentDeals[dealId].timeout)
recentDeals[dealId] = event
recentDeals[dealId].timeout = setTimeout(() => { delete recentDeals[dealId] }, 30000)
}
})
dispatch.hook('S_TRADE_BROKER_REQUEST_DEAL_RESULT', 1, event => {
if(currentDeal) {
if(!event.ok) endDeal()
return false
}
})
dispatch.hook('S_TRADE_BROKER_DEAL_INFO_UPDATE', 1, event => {
if(currentDeal) {
if(event.buyerStage == 2 && event.sellerStage < 2) {
let deal = currentDeal
// This abandoned timeout is not a good design, but it's unlikely that it will cause any issues
setTimeout(() => {
if(currentDeal && deal.playerId == currentDeal.playerId && deal.listing == currentDeal.listing && event.price.toNumber() >= currentDeal.offeredPrice.toNumber()) {
dispatch.toServer('C_TRADE_BROKER_DEAL_CONFIRM', 1, {
listing: currentDeal.listing,
stage: event.sellerStage + 1
})
}
else endDeal() // We negotiated the wrong one, whoops! - TODO: Inspect S_REQUEST_CONTRACT.data for price and other info
}, event.sellerStage == 0 ? rng(ACTION_DELAY_SHORT_MS) : 0)
}
return false
}
})
dispatch.hook('S_REQUEST_CONTRACT', 1, event => {
if(currentDeal && (event.type == TYPE_NEGOTIATION_PENDING || event.type == TYPE_NEGOTIATION)) {
currentContract = event
setEndTimeout()
return false
}
})
dispatch.hook('S_REPLY_REQUEST_CONTRACT', 1, replyOrAccept)
dispatch.hook('S_ACCEPT_CONTRACT', 1, replyOrAccept)
dispatch.hook('S_REJECT_CONTRACT', 1, event => {
if(currentDeal && (event.type == TYPE_NEGOTIATION_PENDING || event.type == TYPE_NEGOTIATION)) {
command.message(currentDeal.name + ' aborted negotiation.')
// Fix listing becoming un-negotiable (server-side) if the other user aborts the initial dialog
if(event.type == TYPE_NEGOTIATION_PENDING)
dispatch.toServer('C_TRADE_BROKER_REJECT_SUGGEST', 1, {
playerId: currentDeal.playerId,
listing: currentDeal.listing
})
currentContract = null
endDeal()
return false
}
})
dispatch.hook('S_CANCEL_CONTRACT', 1, event => {
if(currentDeal && (event.type == TYPE_NEGOTIATION_PENDING || event.type == TYPE_NEGOTIATION)) {
currentContract = null
endDeal()
return false
}
})
dispatch.hook('S_SYSTEM_MESSAGE', 1, event => {
if(currentDeal) {
let msg = event.message.split('\x0b'),
type = msg[0].startsWith('@') ? sysmsg.maps.get(dispatch.base.protocolVersion).code.get(msg[0].slice(1)) : ''
//if(type == 'SMT_MEDIATE_DISCONNECT_CANCEL_OFFER_BY_ME' || type == 'SMT_MEDIATE_TRADE_CANCEL_ME') return false
if(type == 'SMT_MEDIATE_TRADE_CANCEL_OPPONENT') {
command.message(currentDeal.name + ' cancelled negotiation.')
return false
}
}
})
if(UNATTENDED_MANUAL_NEGOTIATE)
dispatch.hook('C_REQUEST_CONTRACT', 1, event => {
if(event.type == 35) {
let deal = recentDeals[event.data.readUInt32LE(0) + '-' + event.data.readUInt32LE(4)]
if(deal) {
currentDeal = deal
command.message('Handling negotiation with ' + currentDeal.name + '...')
process.nextTick(() => {
dispatch.toClient('S_REPLY_REQUEST_CONTRACT', 1, { type: event.type })
})
}
}
})
function replyOrAccept(event) {
if(currentDeal && event.type == TYPE_NEGOTIATION_PENDING) {
setEndTimeout()
return false
}
}
// 1 = Auto Accept, 0 = No Action, -1 = Auto-decline
function comparePrice(offer, seller) {
if(AUTO_ACCEPT_THRESHOLD && offer.toNumber() >= seller.toNumber() * AUTO_ACCEPT_THRESHOLD) return 1
if(AUTO_REJECT_THRESHOLD && offer.toNumber() < seller.toNumber() * AUTO_REJECT_THRESHOLD) return -1
return 0
}
function queueNextDeal(slow) {
if(!actionTimeout && !currentDeal)
actionTimeout = setTimeout(tryNextDeal, DELAY_ACTIONS ? rng(slow ? ACTION_DELAY_LONG_MS : ACTION_DELAY_SHORT_MS) : 0)
}
function tryNextDeal() {
actionTimeout = null
if(!(currentDeal = pendingDeals.shift())) return
if(comparePrice(currentDeal.offeredPrice, currentDeal.sellerPrice) == 1) {
command.message('Attempting to negotiate with ' + currentDeal.name + '...')
command.message('Price: ' + formatGold(currentDeal.sellerPrice) + ' - Offered: ' + formatGold(currentDeal.offeredPrice))
const data = Buffer.alloc(30)
data.writeUInt32LE(currentDeal.playerId, 0)
data.writeUInt32LE(currentDeal.listing, 4)
dispatch.toServer('C_REQUEST_CONTRACT', 1, {
type: 35,
unk2: 0,
unk3: 0,
unk4: 0,
name: '',
data
})
}
else {
dispatch.toServer('C_TRADE_BROKER_REJECT_SUGGEST', 1, {
playerId: currentDeal.playerId,
listing: currentDeal.listing
})
command.message('Declined negotiation from ' + currentDeal.name + '.')
command.message('Price: ' + formatGold(currentDeal.sellerPrice) + ' - Offered: ' + formatGold(currentDeal.offeredPrice))
currentDeal = null
queueNextDeal()
}
}
function setEndTimeout() {
clearTimeout(cancelTimeout)
cancelTimeout = setTimeout(endDeal, pendingDeals.length ? 15000 : 30000)
}
function endDeal() {
clearTimeout(cancelTimeout)
if(currentContract) {
command.message('Negotiation timed out.')
dispatch.toServer('C_CANCEL_CONTRACT', 1, {
type: currentContract.type,
id: currentContract.id
})
currentContract = null
setEndTimeout()
return
}
currentDeal = null
queueNextDeal()
}
function formatGold(gold) {
gold = gold.toString()
let str = ''
if(gold.length > 4) str += '<font color="#ffb033">' + Number(gold.slice(0, -4)).toLocaleString() + '</font><img src="Gold_smalltoken" vspace="-4"/>'
if(gold.length > 2) str += '<font color="#d7d7d7">' + gold.slice(-4, -2) + '</font><img src="Silver_smalltoken" vspace="-4"/>'
str += '<font color="#c87551">' + gold.slice(-2) + '</font><img src="Copper_smalltoken" vspace="-4"/>'
return str
}
function rng([min, max]) {
return min + Math.floor(Math.random() * (max - min + 1))
}
}