-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActions.js
More file actions
118 lines (113 loc) · 2.82 KB
/
Actions.js
File metadata and controls
118 lines (113 loc) · 2.82 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
import m from 'mithril'
let Stream = require('mithril/stream')
import { Chess } from 'chess.js'
import { COLORS } from './Color'
import { uci } from './ChessMaths'
import { playOtherSide } from './utils'
import { auth } from './User'
export const range = (start, end) =>
Array.apply(0, Array(end - 1)).map((element, index) => index + start)
export const State = () => ({
theme: 'dark',
input: null,
output: null,
inputName: null,
outputName: null,
inputs: Stream([]),
outputs: Stream([]),
deviceId: null,
deviceName: null,
deviceType: null,
connected: false,
top: range(91, 10),
header: null,
layout: null,
changeLayout: null,
game: null,
games: Stream([]),
opponent: null,
chess: new Chess(),
ground: null,
color: 'w',
selectedSquare: null,
selectedPiece: null,
invert: Stream(false),
grid: true,
pieces: true,
influence: false,
history: Stream(1),
animationDuration: 400,
animations: [],
colors: COLORS,
})
export const Actions = (state, actions) => ({
incrementHistory: () => {
state.history(state.history() + 1)
actions.lightBoard()
},
decrementHistory: () => {
state.history(Math.max(0, state.history() - 1))
actions.lightBoard()
},
send: (type, message) => {
try {
state.output.send(type, message)
} catch (error) {
console.debug('failed to send message', error)
}
},
sendSysex: (header, message) => {
try {
state.output.sendSysex(header, message)
} catch (error) {
console.debug('failed to send sysex', error)
}
},
lightMatrix: () => null,
lightGame: () => null,
clearAnimations: () => null,
clear: () => null,
highlightAvailableMoves: () => null,
setColor: (piece, color) => {
console.debug(`setting {piece} to {color}`)
state.colors[piece] = color
},
})
export const OnlineActions = (state, actions) => ({
onmove: (orig, dest) => {
let move = { from: orig, to: dest }
let piece = state.chess.get(move.from)
if (
piece.type == 'p' &&
((piece.color == 'w' && move.to.charAt(1) == 8) ||
(piece.color == 'b' && move.to.charAt(1) == 1))
) {
console.log('promotion! Auto promote to Queen')
move.promotion = 'q'
}
state.chess.move(move)
console.log('moved', move, piece, state.chess.ascii())
state.ground.set({ fen: state.chess.fen() })
actions.lightBoard(true)
playOtherSide(state.chess, state.ground)(orig, dest)
m.redraw()
// send to lichess api
let move_uci = uci(move)
auth(
'https://lichess.org/api/board/game/' +
m.route.param('id') +
'/move/' +
move_uci,
{
method: 'post',
}
).then(e => {
console.log('played move', move_uci, e)
})
},
afterInit: () => {
console.log('initing online actions')
actions.afterInit()
actions.streamGame()
},
})