-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
404 lines (360 loc) · 14.7 KB
/
main.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
NOTE TO AUSTIN:
Welcome to my messy excuse of a WIP TradeBot. Sorry if the code is a bit annoying to follow, I'm not the best at organization. The accept code begins at Line 195.
NOTE-TO-SELF
~~~~~~~~~~~
TO-DO:
-Create if-statements for "selling" and "buying" (needed?)
-Create a way to prevent non-duplicate cards from being traded.
-Find better way to only show pending trades in the bot's "analysis" of the trades.
**/
//Here are all of the variables. Some are unused, of course, cleaning up the entire code of unused stuff is on my agenda once this thing works.
var fetch = require("node-fetch");
var request = require("request");
var http = require('http');
var express = require('express');
var path = require("path");
var server = http.createServer(function(req, res) {
fs.readFile('./index.html', 'utf-8', function(error, content) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
var io = require('socket.io').listen(server);
var fs = require("fs");
var qs = require('qs');
var exports = module.exports;
var events = require("events").EventEmitter;
var CLAY_API_URL = 'https://clay.io/api/mittens/v1/';
var ACCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0NjQxMTg4MjEsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.bIt9IUVm8VjXbKw7ore3Hrc77Pj0hcvaiyyulKMA5jbJXovTR3bRL-P_MF-ce-VKQxZ9QWrynjDD4BW2UA4KZw';
var WEB = 'https://clay.io/api/mittens/v1/trades?accessToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0NjgyODcxNDIsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.UtBBYE958CB2ro9fTX2zJnzFhARAxRdmuufUSSDYf87rVTFy7nF5KQ9-_T3kiAEb73iVB6bLKEQ5cqGTVW63Yw&clientVersion=1';
var tradeChat = 'https://clay.io/api/mittens/v1/chatMessages?accessToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0NjQxMTg4MjEsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.bIt9IUVm8VjXbKw7ore3Hrc77Pj0hcvaiyyulKMA5jbJXovTR3bRL-P_MF-ce-VKQxZ9QWrynjDD4BW2UA4KZw&clientVersion=1&channel=trade&crewId=41d67b48-6d79-4037-adf4-e9308dbb054d';
var TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0NjQxMTg4MjEsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.bIt9IUVm8VjXbKw7ore3Hrc77Pj0hcvaiyyulKMA5jbJXovTR3bRL-P_MF-ce-VKQxZ9QWrynjDD4BW2UA4KZw';
var LINK = 'https://clay.io/api/mittens/v1/chatMessages/';
var evenTrade = '0';
var sendPrice;
var receivePrice;
var sendNumber;
var receiveNumber;
var cardSType;
var cardRType;
var receiveGold = 0;
var sendGold = 0;
var currentGold;
var select;
var MAX_SELL_AMOUNT = 20;
var MAX_BUY_AMOUNT = 20;
var sellAmount = 0;
var buyAmount = 0;
var gold;
var goldContents;
var tradeAmount = 0;
var totalTradesAccepted = 0;
var totalTradesDenied = 0;
var tradeStatus = 'both';
var ping = "https://clay.io/api/mittens/v1/ping";
var ip = "http://whatismyip.org/";
var isSingle = 'false';
var isSet = false;
function setData() {
var tradecontents = fs.readFileSync("./tradetotal.json");
var newdata = JSON.parse(tradecontents);
totalTradesAccepted = newdata.accepted;
totalTradesDenied = newdata.denied;
isSet = true;
}
setData();
if(isSet == true) {
checkTrade();
}
function checkTrade() {
// PING CODE //
/*request(ping, function(error, response, body) {
console.log(body);
});*/
//Obtains your current gold amount.
var goldCheck = 'https://clay.io/api/mittens/v1/users/me?accessToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0Nzg0ODExODEsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.ALZgHdL8TMncP0Z356gW2oRlTUjtb3vm0ZOkyIhg7I-qjweB3_DXWOYwTmjC-1R_HFRpyNzH54I6Y58BJMMrRg&clientVersion=1'
request(goldCheck, function(error, response, body) {
fs.writeFile('./goldcheck.json', body, (err) => {
if (err) throw err;
console.log('Gold Data saved to File...');
goldContents = fs.readFileSync("./goldcheck.json");
info = JSON.parse(goldContents);
currentGold = info.gold;
console.log('Gold Data saved to Bot...');
});
});
// ==Trade Checker Script starts here==
//Load JSON to File.
request(WEB, function(error, response, body) {
fs.writeFile('./output.json', body, (err) => {
if (err) throw err;
console.log('Trade Data saved to File.');
var contents = fs.readFileSync("./output.json");
var trades = JSON.parse(contents);
//The "4" in this case is how many trades the bot will look at. Change as neccesary.
for(select = 0; select < 5; ++select) {
var trade = trades[select];
// Set variables for status and deny checks.
if (trade == undefined) {
return;
}
var status = trade.status;
// Check if trade has status 'pending', and isn't already denied.
// AUSTIN: you had comma here instead of &&. Not sure why you're checked the declinedUserIds against some id, removed
if (status === 'pending' && trade.declinedByUserIds[0] !== 'd7618969-cdc4-4a39-863f-17119f9ec66d') {
var receiveCount = 0;
for(i = 0; i < trade.sendItems.length; i++) {
sendCount += 1;
cardRType = trade.sendItems[i]['item'].subTypes['0'];
cardRVol = trade.sendItems[i]['item'].volume;
receiveCount = receiveCount + parseFloat(trade.sendItems[i].count, 10);
}
// Gold is displayed after the loop, as it doesn't have duplicates
receiveGold = parseFloat(trade.sendGold, 10);
// Price Cards depending on what type they are and how many of them there are in the trade.
if (cardRType === 'blue' && cardRVol === 2) {
receivePrice = receiveCount *= 100
} else if (cardRType === 'green' && cardRVol === 2) {
receivePrice = receiveCount *= 200
} else if (cardRType === 'red' && cardRVol === 2) {
receivePrice = receiveCount *= 300
} else if (cardRType === 'silver' && cardRVol === 2) {
receivePrice = receiveCount *= 500
}
if (cardRType === 'blue' && cardRVol === 1) {
receivePrice = receiveCount *= 200
} else if (cardRType === 'green' && cardRVol === 1) {
receivePrice = receiveCount *= 400
} else if (cardRType === 'red' && cardRVol === 1) {
receivePrice = receiveCount *= 500
} else if (cardRType === 'silver' && cardRVol === 1) {
receivePrice = receiveCount *= 800
}
if (receiveGold !== '') {
receivePrice = receiveCount += receiveGold
}
var sendCount = 0;
for(i = 0; i < trade.receiveItems.length; i++) {
cardSType = trade.receiveItems[i]['item'].subTypes['0'];
cardSVol = trade.receiveItems[i]['item'].volume;
sendCount = sendCount + parseFloat(trade.receiveItems[i].count,10);
}
sendGold = parseFloat(trade.receiveGold, 10);
if (cardSType === 'blue' && cardSVol === 2) {
sendPrice = sendCount *= 100
} else if (cardSType === 'green' && cardSVol === 2) {
sendPrice = sendCount *= 200
} else if (cardSType === 'red' && cardSVol === 2) {
sendPrice = sendCount *= 300
} else if (cardSType === 'silver' && cardSVol === 2) {
sendPrice = sendCount *= 500
}
if (cardSType === 'blue' && cardSVol === 1) {
sendPrice = sendCount *= 200
} else if (cardSType === 'green' && cardSVol === 1) {
sendPrice = sendCount *= 400
} else if (cardSType === 'red' && cardSVol === 1) {
sendPrice = sendCount *= 500
} else if (cardSType === 'silver' && cardSVol === 1) {
sendPrice = sendCount *= 800
}
if (sendGold !== ' ') {
sendPrice = sendCount += sendGold
}
/*if (trade.receiveItems === undefined) {
var name = trade.receiveItems[i]['item'].name;
var vol = trade.receiveItems[i]['item'].volume;
}*/
// checkSingle();
//Bunch of variables for testing random crap, mostly irrelevant to the actual code.
var id = trade.id;
// AUSTIN: what you had here this definitely is not the accessToken
var url = CLAY_API_URL + 'trades/' + trade.id + '?accessToken=' + ACCESS_TOKEN + '&clientVersion=1';
var url2 = LINK + '?accessToken=' + TOKEN + '&clientVersion=1';
var playerId = trade['from'].id;
var playername = trade['from'].username;
if (receivePrice === sendPrice && status === 'pending' && sellAmount <= MAX_SELL_AMOUNT && sendGold <= currentGold && playerId != 'ae8fbd3f-2349-4dcf-9f3e-eef1253a7643') {
console.log('Fair Trade! Accepting...');
//io.sockets.emit('console', {fromUser: playername, receive: receivePrice, send: sendPrice, fair: 'YES'});
totalTradesAccepted += 1;
//Trade accept code starts here
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: url,
multipart: {
data: [
{
'content-type': 'application/json',
body: JSON.stringify({status: 'approved'})
}
]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
tradeAmount += 1;
})
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: url2,
json: {
body: "~FAIR TRADE~\nACCEPTED\n-------\nYou sent a total of " + receivePrice + "g\nYou received a total of " + sendPrice + "g\n-------\nEnjoy! ฅ^•ﻌ•^ฅ",
toId: playerId
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
} else if (receivePrice !== sendPrice && status === 'pending' || sendGold > currentGold || playerId == 'ae8fbd3f-2349-4dcf-9f3e-eef1253a7643') {
console.log('Trade is unfair! Rejecting Trade...');
io.sockets.emit('console', {fromUser: playername, receive: receivePrice, send: sendPrice, fair: 'NO'});
totalTradesDenied += 1;
var urld = CLAY_API_URL + 'trades/' + trade.id + '/declinedByUserIds' + '?accessToken=' + ACCESS_TOKEN + '&clientVersion=1';
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: urld,
json: {
declinedByUserIds: ["d7618969-cdc4-4a39-863f-17119f9ec66d"]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: url2,
json: {
body: "NOT ACCEPTED\n-------\nPossible reasons:\n1. You sent an unfair trade.\n2. You're blacklisted.\n3. I don't have what you're asking for.\n-------\n>Sent: " + receivePrice + "g\nReceived " + sendPrice + "g\n-------\nTry resending a fair trade. ฅ^•ﻌ•^ฅ",
toId: playerId
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
}
sendPrice = sendPrice -= sendPrice;
receivePrice = receivePrice -= receivePrice;
sellAmount = 0;
}
};
console.log("TOTAL TRADES ACCEPTED: " + totalTradesAccepted);
console.log("TOTAL TRADE DENIED: " + totalTradesDenied);
io.sockets.emit('trades', {accept: totalTradesAccepted, deny: totalTradesDenied});
var tradedata = {accepted: totalTradesAccepted, denied: totalTradesDenied};
fs.writeFileSync("tradetotal.json", JSON.stringify(tradedata));
});
});
}
setInterval(checkTrade, 30000);
server.listen(3000);
function sendAd() {
if (tradeStatus === 'buy') {
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: tradeChat,
json: {
body: "BUYING:\n----------\nBase Cards ONLY\n----------\nDo NOT take singles!\n----------\n20 Blue: 2000g\n20 Green: 4000g\n20 Red: 6000g\n20 Silver 10000g\n----------\nTradeBot Status: ONLINE\nTotal Trades Accepted since last startup: " + totalTradesAccepted,
channel: "trade"
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
}
if (tradeStatus === 'sell') {
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: tradeChat,
json: {
body: "SELLING:\n----------\nBase Cards ONLY\n----------\nDo NOT take singles!\n----------\n20 Blue: 2000g\n20 Green: 4000g\n20 Red: 6000g\n20 Silver 10000g\n----------\nTradeBot Status: ONLINE\nTotal Trades Accepted since last startup: " + totalTradesAccepted,
channel: "trade"
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
}
if (tradeStatus === 'both') {
request({
method: 'POST',
preambleCRLF: true,
postambleCRLF: true,
uri: tradeChat,
json: {
body: "SELLING/BUYING:\n(Base only)\n---\nVol 1-\nBlue: 200g\nGreen: 400g\nRed: 500g\nSilver 800g\n---\nVol 2-\nBlue: 100g\nGreen: 200g\nRed: 300g\nSilver 500g\nTradeBot Status: ONLINE",
channel: "trade"
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
})
}
}
setInterval(sendAd, 7200000)
var cardurl = 'https://clay.io/api/mittens/v1/items/?accessToken=eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1c2VySWQiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUiLCJzY29wZXMiOlsiKiJdLCJpYXQiOjE0NjQxMTg4MjEsImlzcyI6ImNsYXkiLCJzdWIiOiI5Y2MzZmU4YS02MDgyLTRkZGEtYTkzYi05NjMzODI5M2M0ZTUifQ.bIt9IUVm8VjXbKw7ore3Hrc77Pj0hcvaiyyulKMA5jbJXovTR3bRL-P_MF-ce-VKQxZ9QWrynjDD4BW2UA4KZw&clientVersion=1';
function checkSingle() {
request(cardurl, function(error, response, body) {
fs.writeFile('KittenBot/goldcheck.json', body, (err) => {
if (err) throw err;
cardContents = fs.readFileSync("KittenBot/cards.json");
card = JSON.parse(cardContents);
});
});
if (vol = 2) {
} else if (vol = 1) {
}
}
// Default Bot Accept Code. DO NOT DELETE
/**
var url = CLAY_API_URL + '/v1/trades/' + trade.id + accessToken;
if (evenTrade === 'true') {
console.log('Fair Trade! Accepting...');
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: CLAY_API_URL + '/v1/trades?accessToken=' + ACCESS_TOKEN + '&clientVersion=1',
multipart: {
data: [
{
'content-type': 'application/json',
body: JSON.stringify({status: 'approved'})
}
]
}
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Trade Accepted Successfully! :D');
})
}
**/