-
Notifications
You must be signed in to change notification settings - Fork 8
/
pixelcanvas.io.go
408 lines (341 loc) · 13 KB
/
pixelcanvas.io.go
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
/* D3pixelbot - Custom client, recorder and bot for pixel drawing games
Copyright (C) 2019 David Vogel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
// TODO: Send pixels to game API
// TODO: Handle captchas, and forward them somewhere
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"image"
"image/color"
"io/ioutil"
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
var pixelcanvasioChunkSize = pixelSize{64, 64} // Not the chunk size that the canvas is initialized with
var pixelcanvasioChunkCollectionRadius = 7
var pixelcanvasioChunkCollectionSize = chunkSize{pixelcanvasioChunkCollectionRadius*2 + 1, pixelcanvasioChunkCollectionRadius*2 + 1} // Arraysize of chunks that's returned on the bigchunk request
var pixelcanvasioChunkOffset = image.Point{pixelcanvasioChunkCollectionRadius * pixelcanvasioChunkSize.X, pixelcanvasioChunkCollectionRadius * pixelcanvasioChunkSize.Y}
var pixelcanvasioChunkCollectionPixelSize = pixelSize{pixelcanvasioChunkCollectionSize.X * pixelcanvasioChunkSize.X, pixelcanvasioChunkCollectionSize.Y * pixelcanvasioChunkSize.Y}
var pixelcanvasioCanvasRect = image.Rectangle{image.Point{-999999, -999999}, image.Point{1000000, 1000000}}
var pixelcanvasioPalette = []color.Color{
color.RGBA{255, 255, 255, 255},
color.RGBA{228, 228, 228, 255},
color.RGBA{136, 136, 136, 255},
color.RGBA{34, 34, 34, 255},
color.RGBA{255, 167, 209, 255},
color.RGBA{229, 0, 0, 255},
color.RGBA{229, 149, 0, 255},
color.RGBA{160, 106, 66, 255},
color.RGBA{229, 217, 0, 255},
color.RGBA{148, 224, 68, 255},
color.RGBA{2, 190, 1, 255},
color.RGBA{0, 211, 221, 255},
color.RGBA{0, 131, 199, 255},
color.RGBA{0, 0, 234, 255},
color.RGBA{207, 110, 228, 255},
color.RGBA{130, 0, 128, 255},
}
type connectionPixelcanvasio struct {
Fingerprint string
OnlinePlayers uint32 // Must be read atomically
Center image.Point
AuthName, AuthID string
NextPixel time.Time
Canvas *canvas
GoroutineQuit chan struct{} // Closing this channel stops the goroutines
QuitWaitgroup sync.WaitGroup
ChunkDownloadChan <-chan *chunk // Receives download requests from the canvas
}
func init() {
// Register connection types (all init functions are called from a single thread, thus threadsafe)
connectionTypes["pixelcanvasio"] = connectionType{
Name: "PixelCanvas.io",
FunctionNew: newPixelcanvasio,
}
}
var pixelcanvasioSingleton = &refCountingSingleton{}
func newPixelcanvasio() (connection, *canvas) {
// Init function. It isn't called if there is already an instance of connectionPixelcanvasio
init := func() interface{} {
con := &connectionPixelcanvasio{
Fingerprint: "11111111111111111111111111111111",
GoroutineQuit: make(chan struct{}),
}
con.Canvas, con.ChunkDownloadChan = newCanvas(pixelcanvasioChunkCollectionPixelSize, pixelcanvasioChunkOffset, pixelcanvasioCanvasRect)
// Main goroutine that handles queries and timed things
con.QuitWaitgroup.Add(1)
go func() {
defer con.QuitWaitgroup.Done()
queryTicker := time.NewTicker(10 * time.Second)
defer queryTicker.Stop()
getOnlinePlayers := func() {
response := &struct {
Online int `json:"online"`
}{}
if err := getJSON("https://pixelcanvas.io/api/online", response); err == nil {
atomic.StoreUint32(&con.OnlinePlayers, uint32(response.Online))
log.Debugf("Player amount: %v", response.Online)
}
}
getOnlinePlayers()
for {
select {
case <-queryTicker.C:
getOnlinePlayers()
case <-con.GoroutineQuit:
return
}
}
}()
myClient := &http.Client{Timeout: 1 * time.Minute}
downloadWaitgroup := sync.WaitGroup{} // To wait until all downloads are finished
downloadLimit := make(chan struct{}, 3) // Limit maximum amount of simultaneous downloads to 3
handleDownload := func(chu *chunk) error {
// Round to nearest bigchunk // TODO: Simplify, especially as there is an origin parameter now
ccOffset := image.Point(pixelcanvasioChunkSize).Mul(pixelcanvasioChunkCollectionRadius)
cc := pixelcanvasioChunkCollectionSize.getPixelSize(pixelcanvasioChunkSize).getChunkCoord(chu.Rect.Min.Add(ccOffset), image.Point{})
cc.X, cc.Y = cc.X*pixelcanvasioChunkCollectionSize.X, cc.Y*pixelcanvasioChunkCollectionSize.Y
ca := chunkRectangle{image.Rectangle{
Min: image.Point(cc).Add(image.Point{-pixelcanvasioChunkCollectionRadius, -pixelcanvasioChunkCollectionRadius}),
Max: image.Point(cc).Add(image.Point{pixelcanvasioChunkCollectionRadius + 1, pixelcanvasioChunkCollectionRadius + 1}),
}}.getPixelRectangle(pixelcanvasioChunkSize, image.Point{})
// Signalling must not be in the goroutine, so that the download isn't started several times because of neighbors
chunks, err := con.Canvas.signalDownload(ca)
if err != nil {
return fmt.Errorf("Can't signal downloading of chunks at %v: %v", cc, err)
}
if len(chunks) == 0 {
return fmt.Errorf("Couldn't signal download for any chunk at %v", cc)
}
// TODO: Only setImage on chunks returned by signalDownload
log.Tracef("Download at %v signalled", cc)
downloadWaitgroup.Add(1)
go func() {
downloadLimit <- struct{}{} // Block inside the goroutine, so downloads will queue up without blocking anything else
defer downloadWaitgroup.Done()
defer func() { <-downloadLimit }()
startTime := time.Now()
log.Tracef("Download at %v started", cc)
r, err := myClient.Get(fmt.Sprintf("https://api.pixelcanvas.io/api/bigchunk/%v.%v.bmp", cc.X, cc.Y))
if err != nil {
log.Errorf("Can't get bigchunk at %v: %v", cc, err)
return
}
defer r.Body.Close()
raw, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("Error in bigchunk result: %v", err)
return
}
expectedLen := pixelcanvasioChunkSize.X * pixelcanvasioChunkSize.Y * ((pixelcanvasioChunkCollectionSize.X) * (pixelcanvasioChunkCollectionSize.Y)) / 2
if len(raw) != expectedLen {
log.Errorf("Returned image data has the wrong length (%v, expected %v)", len(raw), expectedLen)
log.Errorf("API returned %v", string(raw[:1000]))
return
}
downloadTime := time.Now().Sub(startTime).Seconds()
startTime = time.Now()
img := image.NewPaletted(ca, pixelcanvasioPalette)
i := 0
for iy := 0; iy < pixelcanvasioChunkCollectionSize.Y; iy++ {
for ix := 0; ix < pixelcanvasioChunkCollectionSize.X; ix++ {
c := chunkCoordinate{
X: cc.X + ix - pixelcanvasioChunkCollectionRadius,
Y: cc.Y + iy - pixelcanvasioChunkCollectionRadius,
}
for jy := 0; jy < pixelcanvasioChunkSize.Y; jy++ {
for jx := 0; jx < pixelcanvasioChunkSize.X; jx += 2 {
p := image.Point{
X: c.X*pixelcanvasioChunkSize.X + jx,
Y: c.Y*pixelcanvasioChunkSize.Y + jy,
}
img.SetColorIndex(p.X, p.Y, (raw[i]>>4)&0x0F) // TODO: Optimize image drawing for receiving
img.SetColorIndex(p.X+1, p.Y, raw[i]&0x0F)
i++
}
}
}
}
drawTime := time.Now().Sub(startTime).Seconds()
startTime = time.Now()
err = con.Canvas.setImage(img, false, true)
if err != nil {
log.Warningf("Can't set image at %v: %v", img.Rect, err)
return
}
setTime := time.Now().Sub(startTime).Seconds()
log.Tracef("Times for %v: Download %.3fs, Drawing %.3fs, setImage() %.5fs ", cc, downloadTime, drawTime, setTime)
}()
return nil
}
// Main goroutine that handles the websocket connection (It will always try to reconnect)
con.QuitWaitgroup.Add(1)
go func() {
defer con.QuitWaitgroup.Done()
waitTime := 0 * time.Second
for {
select {
case <-con.GoroutineQuit:
return
case <-time.After(waitTime):
}
// Any following connection attempt should be delayed a few seconds
waitTime = 5 * time.Second
u, err := url.Parse("wss://ws.pixelcanvas.io:8443")
if err != nil {
log.Errorf("Invalid websocket URL: %v", err)
continue
}
u.RawQuery = "fingerprint=" + con.Fingerprint
// Connect to websocket server
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) // TODO: Ping websocket connection and set timeouts
if err != nil {
log.Errorf("Failed to connect to websocket server %v: %v", u.String(), err)
continue
}
// Handle chunk downloading in a goroutine
chunkDownloaderQuit := make(chan struct{})
go func() {
for {
select {
case chu := <-con.ChunkDownloadChan:
// Check if the chunk still needs to be downloaded
if chu.getQueryState(false) == chunkDownload {
handleDownload(chu)
}
case <-chunkDownloaderQuit:
return
}
}
}()
// Wait for and handle external close events, or connection errors
quitChannel := make(chan struct{})
go func(c *websocket.Conn, quitChannel chan struct{}) {
select {
case <-con.GoroutineQuit:
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-quitChannel:
case <-time.After(time.Second):
}
case <-quitChannel:
}
c.Close()
}(c, quitChannel)
log.Debugf("Websocket connection opened")
// Handle events
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Warnf("Websocket connection error: %v", err)
break
}
if len(message) >= 1 {
opcode := uint8(message[0])
switch opcode {
case 0xC1:
if len(message) == 7 {
cx := int16(binary.BigEndian.Uint16(message[1:]))
cy := int16(binary.BigEndian.Uint16(message[3:]))
mixed := binary.BigEndian.Uint16(message[5:])
colorIndex := uint8(mixed & 0x0F)
color := pixelcanvasioPalette[colorIndex] // colorIndex technically can't be >= 16, so it should be save
ox := int((mixed >> 4) & 0x3F)
oy := int((mixed >> 10) & 0x3F)
log.Tracef("Pixelchange: color %v @ chunk %v, %v with offset %v, %v", colorIndex, cx, cy, ox, oy)
pos := image.Point{
X: int(cx)*pixelcanvasioChunkSize.X + ox,
Y: int(cy)*pixelcanvasioChunkSize.Y + oy,
}
if err := con.Canvas.setPixel(pos, color); err != nil {
log.Debugf("Couldn't draw pixel at %v with color %v: %v", pos, colorIndex, err)
}
}
default:
log.Errorf("Unknown websocket opcode: %v", opcode)
}
}
}
log.Debugf("Websocket connection closed")
close(chunkDownloaderQuit)
close(quitChannel)
log.Trace("Waiting for downloads to finish")
downloadWaitgroup.Wait() // Wait until all chunk downloads are finished
log.Tracef("All downloads finished")
con.Canvas.invalidateAll()
}
}()
// TODO: Authenticate before setting/sending a pixel
//fmt.Print(con.authenticateMe())
return con
}
// Create or reuse instance of connectionPixelcanvasio
con := pixelcanvasioSingleton.get(init).(*connectionPixelcanvasio)
return con, con.Canvas
}
func (con *connectionPixelcanvasio) getShortName() string {
return "pixelcanvasio"
}
func (con *connectionPixelcanvasio) getName() string {
return "PixelCanvas.io"
}
func (con *connectionPixelcanvasio) getOnlinePlayers() int {
return int(atomic.LoadUint32(&con.OnlinePlayers))
}
func (con *connectionPixelcanvasio) authenticateMe() error {
// TODO: Make threadsafe
request := struct {
Fingerprint string `json:"fingerprint"`
}{
Fingerprint: con.Fingerprint,
}
statusCode, _, body, err := postJSON("https://europe-west1-pixelcanvasv2.cloudfunctions.net/me", "https://pixelcanvas.io/", request)
if err != nil {
return err
}
response := &struct {
ID string `json:"id"`
Name string `json:"name"`
Center []int `json:"center"`
WaitSeconds float32 `json:"waitSeconds"`
}{}
if err := json.Unmarshal(body, response); err != nil {
return err
}
if statusCode != 200 {
return fmt.Errorf("Authentication failed with wrong status code: %v (body: %v)", statusCode, string(body))
}
if len(response.Center) < 2 {
return fmt.Errorf("Invalid center given in authentication response")
}
con.AuthID = response.ID
con.AuthName = response.Name
con.Center.X, con.Center.Y = response.Center[0], response.Center[1]
con.NextPixel = time.Now().Add(time.Duration(response.WaitSeconds*1000) * time.Millisecond)
return nil
}
// Closes connection and canvas
func (con *connectionPixelcanvasio) Close() {
if pixelcanvasioSingleton.release(con) {
// Stop goroutines gracefully
close(con.GoroutineQuit)
con.QuitWaitgroup.Wait()
con.Canvas.Close()
}
}