From 45c766acb71e463eb143c2811895a8a5aa6a2793 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 3 Nov 2014 21:32:58 +0100 Subject: [PATCH] progress on pgn4web replacement --- modules/tv/src/main/Featured.scala | 4 +- public/stylesheets/analyse.css | 3 ++ submodules/pdfexporter | 2 +- ui/analyse/src/analyse.js | 8 +--- ui/analyse/src/ctrl.js | 17 ++++---- ui/analyse/src/path.js | 41 +++++++++++++++++++ ui/analyse/src/view.js | 63 ++++++++++++++++++++---------- 7 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 ui/analyse/src/path.js diff --git a/modules/tv/src/main/Featured.scala b/modules/tv/src/main/Featured.scala index c36c0430f1..a6a7b45737 100644 --- a/modules/tv/src/main/Featured.scala +++ b/modules/tv/src/main/Featured.scala @@ -102,7 +102,7 @@ object Featured { private type Heuristic = Game => Float private val heuristicBox = box(0 to 1) _ - private val ratingBox = box(1000 to 2600) _ + private val ratingBox = box(1000 to 2700) _ private val turnBox = box(1 to 25) _ private val heuristics: List[(Heuristic, Float)] = List( @@ -111,7 +111,7 @@ object Featured { progressHeuristic -> 0.7f) private[tv] def ratingHeuristic(color: Color): Heuristic = game => - ratingBox(game.player(color).rating | 1100) + ratingBox(game.player(color).rating | 1400) private[tv] def progressHeuristic: Heuristic = game => 1 - turnBox(game.turns) diff --git a/public/stylesheets/analyse.css b/public/stylesheets/analyse.css index 0ef00238fa..266577d15a 100644 --- a/public/stylesheets/analyse.css +++ b/public/stylesheets/analyse.css @@ -171,6 +171,9 @@ div.game_control .jumps { display: inline-block; padding: 2px 5px; } +.lichess_ground .replay .turn .move.empty { + opacity: 0.5; +} .lichess_ground .replay a.move { cursor: pointer; transition: background-color 0.13s; diff --git a/submodules/pdfexporter b/submodules/pdfexporter index 15a3d5ea63..f9f5a9e9d9 160000 --- a/submodules/pdfexporter +++ b/submodules/pdfexporter @@ -1 +1 @@ -Subproject commit 15a3d5ea63dbc83515c57184fdef3681d2856502 +Subproject commit f9f5a9e9d90b1ff72beb45d9ef60e68054d4b3fb diff --git a/ui/analyse/src/analyse.js b/ui/analyse/src/analyse.js index 0936066ab9..0bce049a18 100644 --- a/ui/analyse/src/analyse.js +++ b/ui/analyse/src/analyse.js @@ -1,9 +1,5 @@ module.exports = function(game, analysis) { - var plyToTurn = function(ply) { - return Math.floor((ply - 1) / 2) + 1; - } - var makeTree = function(sans, fromPly) { return sans.map(function(san, i) { return { @@ -34,8 +30,8 @@ module.exports = function(game, analysis) { path.forEach(function(step) { for (i = 0, nb = tree.length; i < nb; i++) { var move = tree[i]; - if (step.ply === move.ply && (step.variation || step.variation === 0)) { - tree = move.variations[step.variation]; + if (step.ply === move.ply && step.variation) { + tree = move.variations[step.variation - 1]; break; } else if (step.ply >= move.ply) moves.push(move.san); else break; diff --git a/ui/analyse/src/ctrl.js b/ui/analyse/src/ctrl.js index f5bf1dd214..55462e7a45 100644 --- a/ui/analyse/src/ctrl.js +++ b/ui/analyse/src/ctrl.js @@ -4,6 +4,7 @@ var data = require('./data'); var analyse = require('./analyse'); var ground = require('./ground'); var keyboard = require('./keyboard'); +var treePath = require('./path'); module.exports = function(cfg, router, i18n, onChange) { @@ -12,10 +13,8 @@ module.exports = function(cfg, router, i18n, onChange) { this.vm = { flip: false, - path: [{ - ply: 1, - variation: null - }], + path: treePath.default, + pathStr: treePath.write(treePath.default), situation: null, continue: false }; @@ -24,7 +23,7 @@ module.exports = function(cfg, router, i18n, onChange) { var showGround = function() { var moves = this.analyse.moveList(this.vm.path); var nbMoves = moves.length; - var ply, move, cached, fen, hash, h, lm; + var ply, move, cached, fen, hash = '', h, lm; for (ply = 1; ply <= nbMoves; ply++) { move = moves[ply - 1]; h += move; @@ -55,9 +54,11 @@ module.exports = function(cfg, router, i18n, onChange) { onChange(this.vm.situation.fen, this.vm.ply); }.bind(this); - this.jump = function(ply) { - if (this.vm.path.ply == ply || ply < 1 || ply > this.data.game.moves.length) return; - this.vm.path.ply = ply; + this.jump = function(path) { + // if (this.vm.path.ply == ply || ply < 1 || ply > this.data.game.moves.length) return; + this.vm.path = path; + this.vm.pathStr = treePath.write(path); + console.log(this.vm.path, this.vm.pathStr); showGround(); }.bind(this); diff --git a/ui/analyse/src/path.js b/ui/analyse/src/path.js new file mode 100644 index 0000000000..2844fff6f1 --- /dev/null +++ b/ui/analyse/src/path.js @@ -0,0 +1,41 @@ +module.exports = { + + default: [{ + ply: 1, + variation: null + }], + + read: function(str) { + return str.split(',').map(function(step) { + var s = step.split(':'); + return { + ply: parseInt(s[0]), + variation: s[1] ? parseInt(s[1]) : null + }; + }) + }, + + write: function(path) { + return path.map(function(step) { + return step.variation ? step.ply + ':' + step.variation : step.ply; + }).join(','); + }, + + setPly: function(path, ply) { + path[path.length -1].ply = ply; + }, + + withVariation: function(path, index) { + var p2 = path.slice(0); + var last = p2.length - 1; + p2[last] = { + ply: p2[last].ply - 1, + variation: index + }; + p2.push({ + ply: null, + variation: null + }); + return p2; + } +}; diff --git a/ui/analyse/src/view.js b/ui/analyse/src/view.js index 0f9cf5a939..da3770cb1a 100644 --- a/ui/analyse/src/view.js +++ b/ui/analyse/src/view.js @@ -5,6 +5,7 @@ var game = require('game').game; var partial = require('chessground').util.partial; var renderStatus = require('game').view.status; var mod = require('game').view.mod; +var treePath = require('./path'); function renderEval(e) { e = Math.round(e / 10) / 10; @@ -18,13 +19,15 @@ function autoScroll(movelist) { var emptyMove = m('em.move.empty', '...'); -function renderMove(ctrl, move) { +function renderMove(ctrl, move, path) { if (!move) return emptyMove; + treePath.setPly(path, move.ply); + var pathStr = treePath.write(path); return { tag: 'a', attrs: { - class: 'move' + (move.ply === ctrl.vm.ply ? ' active' : ''), - 'data-ply': move.ply + class: 'move' + (pathStr === ctrl.vm.pathStr ? ' active' : ''), + 'data-path': pathStr }, children: [ move.san, @@ -34,37 +37,54 @@ function renderMove(ctrl, move) { }; } -function renderVariation(ctrl, variation) { - return m('div.variation', variation.map(function(turn) { - return renderVariationTurn(ctrl, turn); +function plyToTurn(ply) { + return Math.floor((ply - 1) / 2) + 1; +} + +function renderVariation(ctrl, variation, path) { + var turns = []; + if (variation[0].ply % 2 === 0) { + var move = variation.shift(); + turns.push({ + turn: plyToTurn(move.ply), + black: move + }); + } + for (i = 0, nb = variation.length; i < nb; i += 2) turns.push({ + turn: plyToTurn(variation[i].ply), + white: variation[i], + black: variation[i + 1] + }); + return m('div.variation', turns.map(function(turn) { + return renderVariationTurn(ctrl, turn, path); })); } -function renderVariationTurn(ctrl, turn) { - var wMove = turn.white ? renderMove(ctrl, turn.white) : null; - var bMove = turn.black ? renderMove(ctrl, turn.black) : null; +function renderVariationTurn(ctrl, turn, path) { + var wMove = turn.white ? renderMove(ctrl, turn.white, path) : null; + var bMove = turn.black ? renderMove(ctrl, turn.black, path) : null; if (turn.white) return [turn.turn + '.', wMove, (turn.black ? [m.trust(' '), bMove] : ''), ' ']; return [turn.turn + '...', bMove, ' ']; } -function renderMeta(ctrl, move) { +function renderMeta(ctrl, move, path) { if (!move || !move.comments.length || !move.variations.length) return; return [ move.comments.length ? move.comments.map(function(comment) { return m('div.comment', comment); }) : null, - move.variations.length ? move.variations.map(function(variation) { - return renderVariation(ctrl, variation); + move.variations.length ? move.variations.map(function(variation, i) { + return renderVariation(ctrl, variation, treePath.withVariation(path, i + 1)); }) : null, ]; } -function renderTurn(ctrl, turn) { +function renderTurn(ctrl, turn, path) { var index = m('div.index', turn.turn); - var wMove = turn.white ? renderMove(ctrl, turn.white) : null; - var bMove = turn.black ? renderMove(ctrl, turn.black) : null; - var wMeta = renderMeta(ctrl, turn.white); - var bMeta = renderMeta(ctrl, turn.black); + var wMove = turn.white ? renderMove(ctrl, turn.white, path) : null; + var bMove = turn.black ? renderMove(ctrl, turn.black, path) : null; + var wMeta = renderMeta(ctrl, turn.white, path); + var bMeta = renderMeta(ctrl, turn.black, path); if (turn.white) { if (wMeta) return [ m('div.turn', [index, wMove, emptyMove]), @@ -92,8 +112,9 @@ function renderTree(ctrl, tree) { white: tree[i], black: tree[i + 1] }); + var path = treePath.default; return turns.map(function(turn) { - return renderTurn(ctrl, turn); + return renderTurn(ctrl, turn, path); }); } @@ -120,9 +141,9 @@ function renderAnalyse(ctrl) { } return m('div.analyse', { onclick: function(e) { - var ply = e.target.getAttribute('data-ply') || e.target.parentNode.getAttribute('data-ply'); - if (ply) { - ctrl.jump(parseInt(ply)); + var path = e.target.getAttribute('data-path') || e.target.parentNode.getAttribute('data-path'); + if (path) { + ctrl.jump(treePath.read(path)); m.redraw(); } }