A LICENSE => LICENSE +10 -0
@@ 0,0 1,10 @@
+MIT License
+
+Copyright (c) 2022 Danny van Kooten
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
M puzzles.py => puzzles.py +10 -3
@@ 4,6 4,7 @@ import click
import io
import chess
import chess.engine
+from sqlalchemy import exc
from chess import pgn, Color, COLOR_NAMES
from datetime import datetime, timezone, timedelta
@@ 99,8 100,13 @@ def for_user(username):
if game is None:
break
- db.session.add(game)
- db.session.commit()
+ try:
+ db.session.add(game)
+ db.session.commit()
+ except exc.IntegrityError:
+ db.session.rollback()
+ continue
+
user.last_sync = datetime.utcnow()
db.session.commit()
@@ 108,7 114,8 @@ def for_user(username):
return game
def solve(p, engine):
- limit = chess.engine.Limit(depth=22)
+ print(f"Solving {p}")
+ limit = chess.engine.Limit(depth=21)
board = chess.Board(p.fen)
results = engine.analyse(board, limit, multipv=8)
top = results[0]
M web/src/components/Puzzle.js => web/src/components/Puzzle.js +37 -10
@@ 11,7 11,24 @@ const sounds = {
const game = new Chess();
const [PLAYING, GOOD, BAD] = ['p', 'g', 'b'];
+function sanToSquares(san) {
+ return [san.substring(0, 2), san.substring(2, 4)]
+}
+function sanToUci(san) {
+ const [from, to] = sanToSquares(san);
+ const piece = game.get(from);
+ const target = game.get(to);
+ const captured = target != null ? 'x' : '';
+ switch (piece.type) {
+ case 'p':
+ return from + captured + to;
+ break;
+
+ default:
+ return piece.type.toUpperCase() + captured + to;
+ }
+}
// TODO: Animate last move by opponent when opening puzzle
// TODO: Highlight last from and to square
@@ 30,14 47,18 @@ function Puzzle() {
}
api.request("/api/puzzles/" + id).then(puzzle => {
- setPuzzle(puzzle);
game.load(puzzle.fen);
+ puzzle.best_move_arrows = puzzle.move_engine.split(',').map(sanToSquares)
+ puzzle.best_moves_uci = puzzle.move_engine.split(',').map(sanToUci)
+
+ setPuzzle(puzzle);
+
setBoardConfig({
'orientation': game.turn() === 'w' ? 'white' : 'black',
'arrows': [],
})
setResult(PLAYING);
- })
+ })
}, [id]);
if (puzzle === null) {
@@ 70,22 91,24 @@ function Puzzle() {
// play move sound
sounds.move.play();
- // TODO: Determine bucket based on time to (correct) answer
-
// check move against answer
let bestMoves = puzzle.move_engine.split(',');
let movePlayed = sourceSquare + targetSquare;
- // clear arrows, not sure why this is needed but otherwise the arrow would not render
- setBoardConfig({ ...boardConfig, arrows: [] })
-
// if answer was in list of top moves, update due date
let result = bestMoves.indexOf(movePlayed) >= 0;
let timeout = result ? 1000 : 6000;
+ setResult(result ? GOOD : BAD);
if (!result) {
- setBoardConfig({ ...boardConfig, arrows: [[bestMoves[0].substring(0, 2), bestMoves[0].substring(2, 4)]] })
+ window.setTimeout(() => {
+ // undo last move
+ game.undo();
+ setBoardConfig({ ...boardConfig, arrows: [] })
+ setBoardConfig({ ...boardConfig, arrows: puzzle.best_move_arrows })
+ }, 1000);
+
}
- setResult(result ? GOOD : BAD);
+
api.post(`/api/puzzles/${puzzle.id}?correct=${result ? 1 : 0}`).then(() => {
loadNextPuzzle(timeout)
})
@@ 106,7 129,11 @@ function Puzzle() {
return (<p>✅ Correct!</p>);
case BAD:
- return (<p>❌ Sorry, that's not the best move.</p>);
+ return (
+ <div>
+ <p>❌ Sorry, that's not the best move.</p>
+ <p>Better was {puzzle.best_moves_uci.join(' or ')}.</p>
+ </div>);
}
}