This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcore.js
More file actions
185 lines (151 loc) · 4.56 KB
/
core.js
File metadata and controls
185 lines (151 loc) · 4.56 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
const WebSocket = require('ws')
module.exports = class PixelBot {
constructor (wsslink, store, index, totalBots) {
this.wsslink = wsslink
this.MAX_WIDTH = 1590
this.MAX_HEIGHT = 400
this.MAX_COLOR_ID = 25
this.MIN_COLOR_ID = 0
this.index = index
this.totalBots = totalBots
this.SIZE = this.MAX_WIDTH * this.MAX_HEIGHT
this.SEND_PIXEL = 0
this.ws = null
this.busy = false
this.isStartedWork = false
this.noPixelsBefore = false
this.store = store
this.load().catch(console.error)
}
async load () {
this.startWork()
}
async initWs () {
this.ws = new WebSocket(this.wsslink)
this.ws.on('open', async () => {
this.log('Подключение к WebSocket было успешным.')
})
this.ws.on('error', () => {})
this.ws.on('message', async (event) => {
while (this.busy) {
await this.sleep(500)
}
try {
if (typeof event === 'string') return
this.busy = true
const c = this.toArrayBuffer(event)
for (let d = c.byteLength / 4, e = new Int32Array(c, 0, d), f = Math.floor(d / 3), g = 0; g < f; g++) {
const h = e[3 * g], k = this.unpack(h), l = k.x, m = k.y, n = k.color
this.store.data[[l, m]] = n
}
if (!this.isStartedWork) {
this.startWork()
}
this.busy = false
} catch (e) {
this.busy = false
}
})
this.ws.on('error', (err) => console.error(err))
this.ws.on('close', () => {
this.log('Подключение к WebSocket было закрыто.')
this.ws = null
})
}
async sendPixel () {
const keys = Object.keys(this.store.pixelDataToDraw)
const pixelsToDraw = []
for (let i = 0; i < keys.length; i++) {
const index = keys[i]
const colorWeNeed = this.store.pixelDataToDraw[index]
if (this.store.data[index] !== colorWeNeed) {
pixelsToDraw.push([index, colorWeNeed])
}
}
if (pixelsToDraw.length > 0) {
if (this.index >= pixelsToDraw.length - 1 && !this.noPixelsBefore) {
this.noPixelsBefore = true
await this.sleep(1000)
return this.sendPixel()
}
const [coords, color] = pixelsToDraw[this.noPixelsBefore ? Math.max(pixelsToDraw.length - 1, 0) : this.index]
this.noPixelsBefore = false
const [x, y] = coords.split(',')
if (this.store.data) {
this.store.data[coords] = color
}
await this.send(color, this.SEND_PIXEL, x, y)
setTimeout(() => {
this.sendPixel()
}, 60000)
} else {
await this.store.load()
await this.sleep(1000)
return this.sendPixel()
}
}
// Thanks to @nitreojs (nitrojs)
log (text) {
console.log(`\x1b[33m[#${this.index}]\x1b[0m ${text}`)
}
async startWork () {
this.log('Производится запуск скрипта.')
this.isStartedWork = true
if (!this.ws) {
await this.initWs()
}
await this.sendPixel()
}
async send (colorId, flag, x, y) {
if (!this.ws) {
await this.initWs()
}
if (this.ws.readyState !== 1) {
await this.sleep(100)
return this.send(colorId, flag, x, y)
}
const c = new ArrayBuffer(4)
new Int32Array(c, 0, 1)[0] = this.pack(colorId, flag, x, y)
this.ws.send(c)
this.log(`Был закрашен пиксель [${x}, ${y}] -> \x1b[35m${colorId}\x1b[0m`)
}
pack (colorId, flag, x, y) {
const b = parseInt(colorId, 10) + parseInt(flag, 10) * this.MAX_COLOR_ID
return parseInt(x, 10) + parseInt(y, 10) * this.MAX_WIDTH + this.SIZE * b
}
unpack (b) {
const c = Math.floor(b / this.SIZE)
const d = (b -= c * this.SIZE) % this.MAX_WIDTH
return {
x: d,
y: (b - d) / this.MAX_WIDTH,
color: c % this.MAX_COLOR_ID,
flag: Math.floor(c / this.MAX_COLOR_ID),
}
}
sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time))
}
toArrayBuffer (buf) {
const ab = new ArrayBuffer(buf.length)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i]
}
return ab
}
chunkString (str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'))
}
shuffle (array) {
let currentIndex = array.length, temporaryValue, randomIndex
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
}