forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ground.ts
97 lines (93 loc) · 3.13 KB
/
ground.ts
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
import * as round from './round';
import * as util from './util';
import resizeHandle from 'common/resize';
import RoundController from './ctrl';
import { Chessground } from 'chessground';
import { Config } from 'chessground/config';
import { h } from 'snabbdom';
import { plyStep } from './round';
import { RoundData } from './interfaces';
import { uciToMove } from 'chessground/util';
import * as Prefs from 'common/prefs';
export function makeConfig(ctrl: RoundController): Config {
const data = ctrl.data,
hooks = ctrl.makeCgHooks(),
step = plyStep(data, ctrl.ply),
playing = ctrl.isPlaying();
return {
fen: step.fen,
orientation: boardOrientation(data, ctrl.flip),
turnColor: step.ply % 2 === 0 ? 'white' : 'black',
lastMove: uciToMove(step.uci),
check: !!step.check,
coordinates: data.pref.coords !== Prefs.Coords.Hidden,
addPieceZIndex: ctrl.data.pref.is3d,
addDimensionsCssVarsTo: document.body,
highlight: {
lastMove: data.pref.highlight,
check: data.pref.highlight,
},
events: {
move: hooks.onMove,
dropNewPiece: hooks.onNewPiece,
insert(elements) {
const firstPly = round.firstPly(ctrl.data);
const isSecond = (firstPly % 2 === 0 ? 'white' : 'black') !== data.player.color;
const showUntil = firstPly + 2 + +isSecond;
resizeHandle(elements, ctrl.data.pref.resizeHandle, ctrl.ply, p => p <= showUntil);
},
},
movable: {
free: false,
color: playing ? data.player.color : undefined,
dests: playing ? util.parsePossibleMoves(data.possibleMoves) : new Map(),
showDests: data.pref.destination,
rookCastle: data.pref.rookCastle,
events: {
after: hooks.onUserMove,
afterNewPiece: hooks.onUserNewPiece,
},
},
animation: {
enabled: true,
duration: data.pref.animationDuration,
},
premovable: {
enabled: data.pref.enablePremove,
showDests: data.pref.destination,
castle: data.game.variant.key !== 'antichess',
events: {
set: hooks.onPremove,
unset: hooks.onCancelPremove,
},
},
predroppable: {
enabled: data.pref.enablePremove && data.game.variant.key === 'crazyhouse',
events: {
set: hooks.onPredrop,
unset() {
hooks.onPredrop(undefined);
},
},
},
draggable: {
enabled: data.pref.moveEvent !== Prefs.MoveEvent.Click,
showGhost: data.pref.highlight,
},
selectable: {
enabled: data.pref.moveEvent !== Prefs.MoveEvent.Drag,
},
drawable: {
enabled: true,
defaultSnapToValidMove: (lichess.storage.get('arrow.snap') || 1) != '0',
},
disableContextMenu: true,
};
}
export const reload = (ctrl: RoundController) => ctrl.chessground.set(makeConfig(ctrl));
export const boardOrientation = (data: RoundData, flip: boolean): Color =>
data.game.variant.key === 'racingKings' ? (flip ? 'black' : 'white') : flip ? data.opponent.color : data.player.color;
export const render = (ctrl: RoundController) =>
h('div.cg-wrap', {
hook: util.onInsert(el => ctrl.setChessground(Chessground(el, makeConfig(ctrl)))),
});