This repository was archived by the owner on Jun 8, 2019. It is now read-only.
forked from xTCry/VCoin
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcore.js
More file actions
560 lines (556 loc) · 15.2 KB
/
core.js
File metadata and controls
560 lines (556 loc) · 15.2 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
const WebSocket = require('ws')
const safeEval = require('safe-eval')
const Window = require('window')
const window = new Window()
class VCoinWS {
constructor () {
this.ws = null
this.ttl = null
this.retryTime = 1e3
this.onOnlineCallback = null
this.clickCount = 0
this.clickTimer = null
this.clickPacks = []
this.sendedPacks = 0
this.allowReconnect = true
this.randomId = null
this.oldPlace = null
this.oldScore = null
this.confirmScore = null
this.tick = 0
this.tickTtl = null
this.callbackForPackId = {}
this.ccp = 10
this.connected = false
this.connecting = false
this.onConnectSend = []
this.tickCount = 0
this.wsServer = ''
this.group_id = null
this.groupInfo = []
this.groupData = []
}
run (wsServer, group_id, callback) {
this.wsServer = wsServer || this.wsServer
this.selfClose()
if (callback) { this.onOnlineCallback = callback }
try {
if (group_id) { this.group_id = group_id }
this.ws = new WebSocket(this.wsServer)
this.ws.onopen = _ => {
this.connected = true
this.connecting = false
this.onConnectSend.forEach(e => {
if (this.ws) { this.ws.send(e) }
})
this.onConnectSend = []
for (let pid in this.callbackForPackId) {
if (this.callbackForPackId.hasOwnProperty(pid) && this.ws) {
this.ws.send(this.callbackForPackId[pid].str)
clearTimeout(this.callbackForPackId[pid].ttl)
this.callbackForPackId[pid].ttl = setTimeout(function () {
this.callbackForPackId[pid].reject(new Error('TIMEOUT'))
this.dropCallback(pid)
}, 1e4)
}
};
this.onOpen()
if (this.group_id) { this.loadGroup(this.group_id) }
}
this.ws.onerror = e => {
console.error('На стороне сервера возникла ошибка: ' + e.message)
this.retryTime = 1e3
this.reconnect(wsServer, true)
}
this.ws.onclose = _ => {
this.connected = false
this.connecting = false
this.reconnect(wsServer)
clearInterval(this.tickTtl)
this.tickTtl = null
if (this.onOfflineCallback) { this.onOfflineCallback() }
this.ws = null
}
this.ws.onmessage = ({
data,
}) => {
let t = data
if (t[0] === '{') {
let data = JSON.parse(t)
if (data.type === 'INIT') {
let score = data.score,
place = data.place,
randomId = data.randomId,
items = data.items,
top = data.top,
tick = data.tick,
ccp = data.ccp,
firstTime = data.firstTime,
pow = data.pow
this.randomId = randomId
this.confirmScore = score
this.oldScore = score
this.oldPlace = place
this.groupInfo = data.top.groupInfo
if (pow) {
try {
// Фикс для новой защиты от ботов (POW_SIGN_NOT_RECEIVED)
let x = safeEval(pow, {
window: {
location: {
host: 'vk.com',
},
navigator: {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
},
WebSocket: true,
Math: Math,
parseInt: parseInt,
},
})
let str = 'C1 '.concat(this.randomId, ' ') + x
if (this.connected) this.ws.send(str)
else this.onConnectSend.push(str)
} catch (e) {
console.error(e)
}
}
this.onUserLoadedCallback && this.onUserLoadedCallback(place, score, items, top, firstTime, tick)
this.onMyDataCallback && this.onMyDataCallback(place, score)
this.tick = parseInt(tick, 10)
this.tickTtl = setInterval(_ => {
this.onTickEvent()
}, 1e3)
this.ccp = ccp || this.ccp
}
} else if (t.indexOf('SELF_DATA') === -1 &&
t.indexOf('WAIT_FOR_LOAD') === -1 &&
t.indexOf('MISS') === -1 &&
t.indexOf('TR') === -1 &&
t.indexOf('BROKEN') === -1 &&
t.indexOf('ALREADY_CONNECTED') === -1 &&
t[0] !== 'C' && t[0] !== 'R') { console.log('on Message:\n', t) }
if (t[0] === 'R') {
let x = t.replace('R', '').split(' ')
let y = x.shift()
this.rejectAndDropCallback(y, new Error(x.join(' ')))
}
if (t[0] === 'C') {
let x = t.replace('C', '').split(' ')
let y = x.shift()
this.resoveAndDropCallback(y, x.join(' '))
}
if (t === 'ALREADY_CONNECTED') {
this.retryTime = 18e5
if (this.onAlredyConnectedCallback) { this.onAlredyConnectedCallback() }
} else {
if (t.indexOf('WAIT_FOR_LOAD') === 0) {
if (this.onWaitLoadCallback) { this.onWaitLoadCallback(parseInt(t.replace('WAIT_FOR_LOAD ', ''), 10)) }
if (this.onChangeOnlineCallback) { this.onChangeOnlineCallback(parseInt(t.replace('WAIT_FOR_LOAD ', ''), 10)) }
if (t.indexOf('MSG') === 0) {
this.retryTime = 3e5
if (this.onMessageEventCallback) { this.onMessageEventCallback(t.replace('MSG ', '')) }
}
}
if (t.indexOf('SELF_DATA') === 0) {
let data = t.replace('SELF_DATA ', '').split(' ')
this.randomId = data[2]
let packId = parseInt(data[3], 10),
online = parseInt(data[4], 10),
_place = parseInt(data[0], 10),
_score = parseInt(data[1], 10)
this.oldPlace = _place
this.oldScore = _score
this.confirmScore = _score
this.onMyDataCallback && this.onMyDataCallback(_place, _score, true)
this.onChangeOnlineCallback && this.onChangeOnlineCallback(online)
this.resoveAndDropCallback(packId)
}
}
if (t === 'BROKEN' && this.onBrokenEventCallback) {
this.retryTime = 6e4
this.onBrokenEventCallback()
} else {
if (t.indexOf('MISS') === 0) {
this.randomId = parseInt(t.replace('MISS ', ''), 10)
if (this.onMissClickCallback) { this.onMissClickCallback(this.randomId) }
}
if (t.indexOf('TR') === 0) {
let data = t.replace('TR ', '').split(' '),
nscore = parseInt(data[0], 10),
from = parseInt(data[1])
this.oldScore += nscore
if (this.onTransferCallback) { this.onTransferCallback(from, nscore) }
if (this.onMyDataCallback) { this.onMyDataCallback(this.oldPlace, this.oldScore, true) }
}
}
}
this.connecting = true
} catch (e) {
console.error(e)
this.reconnect(wsServer)
}
}
onOpen () {
if (this.onOnlineCallback) { this.onOnlineCallback() }
this.retryTime = 1e3
}
close () {
this.allowReconnect = false
clearTimeout(this.ttl)
clearInterval(this.tickTtl)
this.selfClose()
}
selfClose () {
if (this.ws) {
try {
this.ws.close()
} catch (e) {
this.connected = false
}
}
}
reconnect (e, force) {
if (this.allowReconnect || force) {
clearTimeout(this.ttl)
this.ttl = setTimeout(_ => {
this.run(e || this.wsServer)
}, this.retryTime)
this.retryTime *= 1.3
}
}
onTransfer (e) {
this.onTransferCallback = e
}
onChangeOnline (e) {
this.onChangeOnlineCallback = e
}
onUserLoaded (e) {
this.onUserLoadedCallback = e
}
onGroupLoaded (e) {
this.onGroupLoadedCallback = e
}
onReceiveDataEvent (e) {
this.onMyDataCallback = e
}
onMissClickEvent (e) {
this.onMissClickCallback = e
}
onOffline (e) {
this.onOfflineCallback = e
}
onOnline (e) {
this.onOnlineCallback = e
}
onWaitEvent (e) {
this.onWaitLoadCallback = e
}
onMessageEvent (e) {
this.onMessageEventCallback = e
}
onAlreadyConnected (e) {
this.onAlredyConnectedCallback = e
}
onBrokenEvent (e) {
this.onBrokenEventCallback = e
}
resoveAndDropCallback (e, t) {
if (this.callbackForPackId[e]) {
this.callbackForPackId[e].resolve(t)
this.dropCallback(e)
}
}
rejectAndDropCallback (e, t) {
if (this.callbackForPackId[e]) {
this.callbackForPackId[e].reject(t)
this.dropCallback(e)
}
}
dropCallback (e) {
if (this.callbackForPackId[e]) {
clearTimeout(this.callbackForPackId[e].ttl)
delete this.callbackForPackId[e]
}
}
async onTickEvent () {
if (this.oldScore !== null && this.onMyDataCallback) {
this.tickCount++
if (this.tickCount % 30 === 0) {
try {
await this.getMyPlace()
} catch (e) {}
}
}
}
async sendClicks () {
this.clickPacks.push({
count: this.clickCount,
x: ++this.sendedPacks,
})
this.clickCount = 0
this.clickTimer = null
await this.queueTick()
}
sendPack (e, t) {
return new Promise((resolve, reject) => {
try {
let i = 'C'
.concat(e, ' ')
.concat(this.randomId, ' 1')
if (this.connected) this.ws.send(i)
else this.onConnectSend.push(i)
resolve(1)
} catch (e) {
this.dropCallback(t)
reject(e)
}
})
}
async queueTick () {
let t = this.clickPacks.shift()
try {
await this.sendPack(t.count, t.x)
} catch (e) {
console.error(e)
this.clickPacks.push(t)
setTimeout(async _ => {
return await this.queueTick()
}, 1e3 + 5e3 * Math.random())
}
}
click () {
if (this.clickCount >= this.ccp) {
return
}
this.clickCount++
if (this.clickTimer === null) {
this.clickTimer = setTimeout(async _ => {
await this.sendClicks()
}, 1200)
}
}
async buyItemById (id) {
let res
res = await this.sendPackMethod(['B', id])
res = JSON.parse(res)
let n = res.tick,
r = res.score,
o = res.place
this.tick = parseInt(n, 10)
this.oldScore = r
this.oldPlace = o
this.onMyDataCallback && setTimeout(_ => {
this.onMyDataCallback(this.oldPlace, this.oldScore)
}, 1)
return res
}
async transferToUser (id, sum = 3e4) {
sum = Math.round(parseInt(sum) * 1e3)
let res = await this.sendPackMethod(['T', id, sum])
res = JSON.parse(res)
let t = res.score,
a = res.place,
r = res.reload
this.oldScore = t
this.oldPlace = a
this.onMyDataCallback && setTimeout(_ => {
this.onMyDataCallback(this.oldPlace, this.oldScore)
}, 1)
return res
}
async getMyPlace () {
let res = await this.sendPackMethod(['X'])
res = parseInt(res, 10)
this.oldPlace = res
return res
}
async loadGroup (e) {
let res = await this.sendPackMethod(['G', e])
this.groupData = JSON.parse(res)
if (!this.groupData) { return }
this.onGroupLoadedCallback && this.onGroupLoadedCallback(this.groupInfo, this.groupData)
}
async getUserScores (e) {
let res = await this.sendPackMethod(['GU'].concat(e))
return JSON.parse(res)
}
sendPackMethod (e) {
let t = this
let n = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0
return new Promise(function (resolve, reject) {
let o = ++t.sendedPacks
try {
let i = 'P' + o + ' ' + e.join(' ')
if (t.connected) { t.ws.send(i) } else { t.onConnectSend.push(i) }
t.setCallback(o, resolve, reject)
} catch (e) {
t.dropCallback(o)
reject(e)
}
})
.catch(function (r) {
if (r && r.message === 'TIMEOUT' && n < 3) { return t.sendPackMethod(e, n + 1) }
throw r
})
}
setCallback (e, t, n) {
this.dropCallback(e)
this.callbackForPackId[e] = {
resolve: t,
reject: n,
ttl: setTimeout(_ => {
n(new Error('TIMEOUT'))
this.dropCallback(e)
}, 1e4 + Math.round(500 * Math.random())),
}
}
}
class Miner {
constructor () {
this.score = 0
this.total = 0
this.stack = []
this.active = []
}
setScore (q) {
this.score = q
}
setActive (q) {
this.active = q
}
hasMoney (e) {
return this.score >= this.getPriceForItem(e)
}
getPriceForItem (e) {
let price = Entit.items[e].price
let count = 0
this.stack.forEach(el => {
if (el.value === e) { count = el.count }
})
return Entit.calcPrice(price, count + 1)
}
getItemCount (e) {
let count = 0
this.stack.forEach(el => {
if (el.value === e) { count = el.count }
})
return count
}
updateStack (items) {
this.stack = Entit.generateStack(items.filter(e => (e !== 'bonus' && e !== 'vkp1' && e !== 'vkp2' && e !== 'music')))
let total = 0
this.stack.forEach(function (e) {
let n = e.value
let a = e.count
total += Entit.items[n].amount * a
})
this.total = total
}
}
class EntitiesClass {
constructor () {
this.titles = {
cursor: 'Курсор',
cpu: 'Видеокарта',
cpu_stack: 'Стойка видеокарт',
computer: 'Суперкомпьютер',
server_vk: 'Сервер ВКонтакте',
quantum_pc: 'Квантовый компьютер',
datacenter: 'Датацентр',
}
this.items = {
cursor: {
price: 30,
amount: 1,
},
cpu: {
price: 100,
amount: 3,
},
cpu_stack: {
price: 1e3,
amount: 10,
},
computer: {
price: 1e4,
amount: 30,
},
server_vk: {
price: 5e4,
amount: 100,
},
quantum_pc: {
price: 2e5,
amount: 500,
},
datacenter: {
price: 5e6,
amount: 1e3,
},
vkp1: {
price: 0,
amount: 2e3,
},
vkp2: {
price: 0,
amount: 1e4,
},
music: {
price: 0,
amount: 4e3,
},
}
this.names = [
'cursor',
'cpu',
'cpu_stack',
'computer',
'server_vk',
'quantum_pc',
'datacenter',
// "vkp1",
// "vkp2",
// "music",
]
}
generateStack (e) {
let t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : (e, t) => (e === t)
let n = []
e.forEach(function (e) {
if (n.length === 0) {
n.push({
count: 1,
value: e,
})
} else {
let a = false
n.map(function (n) {
if (t(n.value, e)) {
n.count++
a = true
}
return n
})
a || n.push({
count: 1,
value: e,
})
}
})
return n
}
calcPrice (price, count) {
return (count <= 1) ? price : Math.ceil(1.3 * this.calcPrice(price, count - 1))
}
hashPassCoin (e, t) {
return e + t - 1
}
}
const Entit = new EntitiesClass()
const miner = new Miner()
module.exports = {
Entit,
VCoinWS,
miner,
}