-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.gr
450 lines (404 loc) · 10.2 KB
/
discord.gr
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
/*
Library in grol for discord bot.
*/
// --- General library functions ---
// Note: some of these could be moved from recursive to iterative now that we have `for` loops.
// f argument takes the element and the index
func walk(f, a) {
(f,a,i) => {
if (len(a)==0) {
[]
} else {
[f(first(a),i)]+self(f,rest(a),i+1)
}
}(f,a,0)
}
// Apply a function to each element of an array.
func apply(f, a) {
if (len(a)==0) {
[]
} else {
[f(first(a))]+apply(f,rest(a))
}
}
// Reverse an array or map or string
func reverse(a) {
if len(a) == 0 {
return []
}
self(rest(a)) + first(a)
}
// Filter an array
func filter(predicate, arr) {
if len(arr) == 0 {
[]
} else if predicate(first(arr)) {
[first(arr)] + filter(predicate, rest(arr))
} else {
filter(predicate, rest(arr))
}
}
// ---- discord specific ----
discord = {
"actionRow": elems => {
{
"type": 1,
"components": elems
}
},
"button": (label, style, custom_id) => {
{
"type": 2,
"label": label,
"style": style,
"custom_id": custom_id
}
},
"buttonStyle" : {
"primary": 1,
"secondary": 2,
"success": 3,
"danger": 4,
"link": 5
},
"interaction" : {
"update_message" : 7
},
"doInteraction" : (msgId, userId, map) => { // tic-tac-toe example
state := discord_state[msgId]
if state == nil {
state = {
"board": tictactoe.empty_board,
"next": "X",
"end": nil,
"turn": 0
}
}
clicked = map.custom_id
if clicked == "reset" {
InteractionRespond({
"type": discord.interaction.update_message,
"data": {"content": "<@" + userId + "> has reset the board", "components": tictactoe.boardToComponents(tictactoe.empty_board,-1,-1)}})
discord_state[msgId] = nil
return
}
if state.end != nil || state.turn >= 9 {
InteractionRespond({"type": discord.interaction.update_message, "data": {"content": "Game is over <@" + userId + ">"}})
return
}
state.turn = state.turn + 1 // no ++ on map/indexes yet.
tag = "<@" + userId + ">"
if clicked == "play" {
move = tictactoe.play(state.board, state.next)
log("Move", move)
i = move[0]
j = move[1]
clicked = str(i+1) + "," + str(j+1)
tag = "Grol"
} else {
pair = split(clicked, ",")
i=int(pair[0])-1
j=int(pair[1])-1
}
if state.board[i][j] != "" {
InteractionRespond({"type": discord.interaction.update_message, "data": {"content": clicked + " is already occupied"}})
return
}
board = state.board
row = board[i]
row[j] = state.next
board[i] = row
state.board = board
state.next = tictactoe.next(state.next)
state.end = tictactoe.endGame(board)
if state.end != nil{
clicked = clicked + ": " + state.end + " wins!"
} else if state.turn >= 9 {
clicked = clicked + ": draw."
}
InteractionRespond({
"type": discord.interaction.update_message,
"data": {"content": tag + " played " + clicked, "components": tictactoe.boardToComponents(state.board,i,j)}
})
// println("msgId", msgId, "New state", state)
discord_state[msgId] = state // save state. annoyingly discord.state[msgId] = state doesn't work yet.
}
}
discord_state = {}
// ---- tic tac toe ----
tictactoe = {
"empty_board": [["","",""],["","",""],["","",""]],
"boardToComponents": (board, hi, hj) => {
walk((row,i) => {discord.actionRow(
walk((cell,j) => {
if cell == "" {
cell = "\u200B" // zero width space
}
style = discord.buttonStyle.secondary
if i == hi && j == hj {
style = discord.buttonStyle.primary
}
discord.button(cell, style, sprintf("%d,%d", i+1, j+1))
}, row))}, board) +
[discord.actionRow([discord.button("Grol Play",discord.buttonStyle.success,"play"), discord.button("Reset", discord.buttonStyle.danger, "reset")])]
},
"sendBoard": board => {
msg = {
"content": "Tic-Tac-Toe",
"components": tictactoe.boardToComponents(board, -1, -1)
}
ChannelMessageSendComplex(msg)
},
"endGame": board => {
// check for win
for i=0:3 {
if board[i][0] != "" && board[i][0] == board[i][1] && board[i][1] == board[i][2] {
return board[i][0]
} else if board[0][i] != "" && board[0][i] == board[1][i] && board[1][i] == board[2][i] {
return board[0][i]
}
}
if board[0][0] != "" && board[0][0] == board[1][1] && board[1][1] == board[2][2] {
board[0][0]
} else if board[0][2] != "" && board[0][2] == board[1][1] && board[1][1] == board[2][0] {
board[0][2]
} else {
nil
}
},
"next": cur => {
if cur == "X" {
"O"
} else {
"X"
}
},
"try": (board, i, j, next) => {
if board[i][j] != "" {
return nil
}
row = board[i]
row[j] = next
board[i] = row
return board
},
"play": (board, next) => {
freeList = []
for i = 0:3 {
for j = 0:3 {
check = tictactoe.try(board, i, j, next)
if check == nil {
continue
}
freeList = freeList + [[i,j]]
if tictactoe.endGame(check) != nil {
log("Win by", next, "at", i, j)
return [i,j]
}
}
}
log("no win yet", freeList)
n = tictactoe.next(next)
// No win, try to block
for c = freeList {
log("Checking", c)
i = c[0]
j = c[1]
check = tictactoe.try(board, i, j, n)
if tictactoe.endGame(check) != nil {
log("Loss by", next, "at", i, j)
return [i,j]
}
}
if len(freeList) == 1 { // only one move left
return freeList[0]
}
// try middle move
if board[1][1] == "" {
return [1,1]
}
// random move
freeList[rand(len(freeList))]
},
}
func TicTacToe() {
tictactoe.sendBoard(tictactoe.empty_board)
}
// --- Image demo ---
// Also from grol's example/image.gr
// with inspiration @shokhie + our colorful version etc...
msgId=""
// With angle as an int modulo 360 input, this gets memoized.
func ycbcr(angle) {
a = PI * angle / 180.
// Y Cb Cr
[190, 128 + 120*sin(a), 128 + 120*cos(a)]
}
func Butterfly() {
butterfly(550, 350) // dimensions that don't cause scaling (on desktop/web clients at least)
}
func butterfly(xsize, ysize) {
imgName := "canvas"
canvas := image.new(imgName, xsize, ysize)
div := 6
now := time.now()
t := 0
for t < 12*PI {
x := sin(t) * (pow(E, cos(t)) - 2*cos(4*t) - pow(sin(t/12), 5))
y := cos(t) * (pow(E, cos(t)) - 2*cos(4*t) - pow(sin(t/12), 5))
angle := int(t*180./PI) % 360 // so ycbr() get memoized with 360 values
color = ycbcr(angle)
image.set_ycbcr(canvas, int(xsize/2+(xsize/div)*x+0.5), int(ysize/2.5+(ysize/div)*y+0.5), color)
// could use image.set_hsl() too.
t = t + 0.0005
}
elapsed := time.now() - now
println("Time elapsed: ", elapsed, " seconds")
msgId = msgId // bind to top level/global
msgId = SendImage(imgName)
}
// -- bezier and circles --
func sign(x) {
if x < 0 {-1} else {1}
}
func circleQuadrant(img, x, y, rx, ry, color, thickness) {
quadrant(img, x, y, rx, ry, thickness)
image.draw(img, color)
}
func quadrant(img, x, y, rx, ry, thickness) {
sign = sign
xx := round(x)+.5 // semi magic adjustment that makes the bezier circle and trueCircle match
yy := round(y)+.5
a := 1.00005519
b := 0.55342686
c := 0.99873585
thickness2 = thickness / 2.
ythickness := thickness2
if sign(rx) != sign(ry) {
ythickness = -thickness2
}
p0x := xx + rx*a - thickness2
p0y := yy
image.move_to(img, p0x, p0y)
image.line_to(img, p0x+thickness, p0y)
rxx := rx + thickness2
ryy := ry + ythickness
p2x := xx + rxx*b
p2y := yy + ryy*c
p1x := xx + rxx*c
p1y := yy + ryy*b
p3x := xx
p3y := yy + ryy*a
image.cube_to(img, p1x, p1y, p2x, p2y, p3x, p3y)
rxx = rxx - thickness
ryy = ryy - 2*ythickness
p3y := yy + ryy*a
image.line_to(img, xx, p3y)
p2x := xx + rxx*b
p2y := yy + ryy*c
p1x := xx + rxx*c
p1y := yy + ryy*b
image.cube_to(img, p2x, p2y, p1x, p1y, p0x, p0y)
// image.line_to(img, x, y) // covered by close_path in draw
}
func elipse(img, x,y,rx,ry,color,thickness) {
quadrant(img, x,y,rx,ry,thickness)
quadrant(img, x,y,rx,-ry,thickness)
quadrant(img, x,y,-rx,ry,thickness)
quadrant(img, x,y,-rx,-ry,thickness)
image.draw(img, color)
}
func circle(img, x,y,r,color,thickness) {
elipse(img, x,y,r,r,color,thickness)
}
func CircleDemo() {
xsize:=256
ysize:=256
img := "canvas"
image.new(img, xsize, ysize)
cx := xsize/2.-0.5
cy := ysize/2.-0.5
thickness := ysize/5
r := ysize/2-thickness
circleQuadrant(img, cx, cy, r, r, [200,70,70,200], thickness)
circleQuadrant(img, cx, cy, -r, -r, [70,200,70,200], thickness)
circleQuadrant(img, cx, cy, -r, r, [70,70,200,200], thickness)
circleQuadrant(img, cx, cy, r, -r, [200,200,70,200], thickness)
r := ysize/2-0.5
circle(img, cx, cy, r, [200,200,200,180], 1)
msgId = SendImage(img)
}
// - plot random points
func line(img, x1, y1, x2, y2, color, thickness) {
angle := atan2(y2-y1, x2-x1) + PI/2
dx := thickness*cos(angle)/2.
dy := thickness*sin(angle)/2.
image.move_to(img, x1-dx, y1-dy)
image.line_to(img, x2-dx, y2-dy)
image.line_to(img, x2+dx, y2+dy)
image.line_to(img, x1+dx, y1+dy)
image.draw(img, color)
}
func PlotBezierDemo() {
i := "img"
x:=550
y:=350
image.new(i, x, y)
// generate a list of N point heights.
n:=8
points := []
for n {
points = points + (20 + rand(310))
}
func coordX(idx) {
8+idx*75
}
func coordY(height) {
y-8-height
}
// flat start of bezier curve - imaginary point at -1 same height as first point
ix := -1
prevX := coordX(-1)
curX := coordX(0)
curY := coordY(points[0])
ctrlX1 = 2*curX - (prevX + curX) / 2.
ctrlY1 = curY
ix := 0
image.move_to(i, curX, coordY(0))
image.line_to(i, curX, curY)
ix ++
prevX = curX
prevY = curY
for ix < n {
curX := coordX(ix)
curY := coordY(points[ix])
ctrlX2 := (prevX + 3* curX) / 4.
ctrlY2 := (prevY + 3* curY) / 4.
if ix == n-1 { // flatter end.
ctrlX2 = (prevX + curX) / 2
ctrlY2 = curY
}
image.cube_to(i, ctrlX1, ctrlY1, ctrlX2, ctrlY2, curX, curY)
prevX = curX
prevY = curY
ctrlX1 = 2* curX - ctrlX2
ctrlY1 = 2* curY - ctrlY2
ix++
}
image.line_to(i, coordX(n-1), coordY(0))
image.draw(i, [30,120,200,200])
color := [200,200,200,80]
for ix = n-1 {
yi := points[ix]
yp1 := points[ix+1]
log(ix, yi, yp1)
line(i, coordX(ix), coordY(yi), coordX(ix+1), coordY(yp1), color, 4)
}
msgId = SendImage(i)
}
// ------- Misc stuff ----
func layout(){
println("Simple project layout [github.com/go-standard/project-layout](<https://github.com/go-standard/project-layout#project-layout>) - simple is good!")
}
// Result of eval of this file is logged by bot.go Run(). confirm we reached the end without error.
print("Imported discord grol library ok")