-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision.html
271 lines (230 loc) · 7.35 KB
/
vision.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vision Trainer</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<link rel="stylesheet" type="text/css" href="https://unpkg.com/bulma-prefers-dark" />
<link rel="stylesheet" href="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.css"
integrity="sha384-q94+BZtLrkL1/ohfjR8c6L+A6qzNH9R2hBLwyoAfu3i/WCvQjzL2RQJ3uNHDISdU" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha384-ZvpUoO/+PpLXR1lu4jmpXWu80pZlYUAfxl5NsBMWOEPSjUn/6Z/hRTt8+pR6L4N2"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.js"
integrity="sha384-8Vi8VHwn3vjQ9eUHUxex3JSN/NFqUg3QbPyX8kWyb93+8AC/pPWTzj+nHtbC5bxD"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/chess.min.js"></script>
<style>
body {
margin: 0 auto;
max-width: 70vh;
}
#board {
margin: 2px;
touch-action: none;
}
img {
touch-action: none;
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
}
#status {
display: flex;
align-items: center;
min-height: 5rem;
padding: 1rem;
border: 2px solid gray;
}
#status.w {
background: white;
color: #333;
}
#status.b {
background: #333;
color: #ccc;
}
#controls {
margin: 1rem;
}
#controls button {
touch-action: manipulation;
}
.highlight1-32417,
.highlight2-9c5d2 {
box-shadow: none;
}
.highlight2-9c5d2 {
border: 2px solid yellow;
box-sizing: border-box;
}
.move-highlight.black-3c85d,
.highlight1-32417.black-3c85d {
background-color: #DAC247;
}
.move-highlight.white-1e1d7,
.highlight1-32417.white-1e1d7 {
background-color: #F3E469;
}
</style>
</head>
<body>
<div id="status" class="loading">Loading</div>
<div id="board"></div>
<div id="score" class="has-text-centered">
<div class="is-size-4 has-text-weight-bold" id="accuracy">-</div>
</div>
<div id="controls" class="level">
<div class="level-item buttons">
<button class="button is-info-outline is-large is-fullwidth js-done">Done</button>
<button class="button is-info-outline is-fullwidth js-next">Next</button>
</div>
</div>
</body>
<script>
let board
const game = new Chess()
const $done = $(".js-done")
const $next = $(".js-next")
const $status = $("#status")
const plays = JSON.parse(localStorage.getItem('attempts')) || []
// TODO:
// - Show graph over time
// - Decay old accuracy to encourage return?
// - trim accuracy to last n days or x attempts
renderAccuracy()
// Puzzle state
let puzzleAttempted = false
let startTime
let checks = []
let captures = []
let moves = []
let targets = []
const found = new Set()
const mistakes = new Set()
function onDragStart(source, piece, position, orientation) {
console.debug('onDragStart', { source, piece, position, orientation })
$(`.square-55d63`).removeClass('move-highlight')
// only pick up pieces for the side to move
if ((game.turn() === 'w' && piece.search(/^b/) !== -1) ||
(game.turn() === 'b' && piece.search(/^w/) !== -1)) {
return false
}
}
function onDrop(source, target) {
if (source === target) return
console.debug('onDrop', { source, target })
// see if the move is in targets
const move = targets.find(m => m.from === source && m.to === target)
if (move) {
if (!found.has(move)) $status.append(' ✅ ')
found.add(move)
} else {
mistakes.add(move)
$status.append(' ❌ ')
}
return 'snapback';
}
function onDone() {
// Only record accuracy on the first attempt for each puzzle
if (!puzzleAttempted) addAccuracy(found.size, targets.length)
const time = (performance.now() - startTime) / 1000
const rate = time / (targets.length + 1)
const missed = targets.filter(c => !found.has(c)).map(m => m.san)
// Update UI
renderAccuracy()
$status.removeClass()
$status.addClass(missed.length == 0 ? 'has-background-success has-text-black-bis' : 'has-background-warning has-text-black-bis')
if (missed.length > 0) {
$status.html(`
<span class="is-size-3 mr-3">${found.size}⁄${targets.length}</span>
<span class="mr-3">Missed: ${missed.join(', ')}</span>
`)
} else {
$status.html(`
<span class="is-size-3 mr-3">💯</span>
<span class="mr-3">${rate.toLocaleString(undefined, { style: 'decimal' })} sec/move</span>
`)
$done.hide()
$next.focus()
}
puzzleAttempted = true
}
function onNext() {
puzzleAttempted = false
startTime = null
found.clear()
mistakes.clear()
if (targets === checks) {
startTime = performance.now()
targets = captures
$status.removeClass()
$status.addClass(game.turn())
$status.html(`<span class="mr-3">Now find all the <b>captures</b></span>`)
} else {
checks = null
captures = null
moves = null
loadPosition()
}
$done.show()
$done.focus()
}
async function loadPosition() {
// Update UI
$status.removeClass()
$status.addClass('loading')
$status.html("<span>Getting puzzle…</span>")
// Fetch puzzle and update game state
const puzzle = await fetch("https://puzzle-api.f1cdn.workers.dev/").then(r => r.json())
const
firstMove = puzzle.Moves.split(" ")[0],
fromSq = firstMove.slice(0, 2),
toSq = firstMove.slice(2, 4)
board.position(puzzle.FEN, false)
game.load(puzzle.FEN)
game.move(firstMove, { sloppy: true })
board.position(game.fen())
moves = game.moves({ verbose: true })
checks = moves.filter(m => m.san.search(/[#\+]$/) > 0)
captures = moves.filter(m => m.san.indexOf('x') > 0)
startTime = performance.now()
targets = checks
// Update UI
$(`.square-55d63`).removeClass('move-highlight')
$(`.square-${fromSq}`).addClass('move-highlight')
$(`.square-${toSq}`).addClass('move-highlight')
$status.removeClass()
$status.addClass(game.turn())
$status.html(`<span class="mr-3">Find all the <b>checks</b> for ${game.turn() == 'w' ? 'White' : 'Black'}</span>`)
$done.focus()
}
function addAccuracy(found, total) {
if (total == 0) {
plays.push({ n: 1, t: 1 }) // Finding nothing counts as one
} else {
plays.push({ n: found, t: total })
}
if (plays.length > 100) plays.shift() // Cap our stats at the 100 last attempts
localStorage.setItem('attempts', JSON.stringify(plays))
}
function renderAccuracy() {
const totalAvailable = plays.reduce((acc, { t }) => acc + t, 0)
const totalFound = plays.reduce((acc, { n }) => acc + n, 0)
const totalAccuracy = totalAvailable > 0 ? totalFound / totalAvailable : 1
$('#accuracy').html(`
${totalAccuracy.toLocaleString(undefined, { style: 'percent' })}
`)
}
board = Chessboard('board', {
draggable: true,
onDragStart: onDragStart,
onDrop: onDrop,
});
$done.click(onDone)
$next.click(onNext)
loadPosition()
window.onresize = () => board.resize()
</script>
</html>