From 0010336b51565414c9680696530400640459f2a6 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 4 May 2015 23:39:09 +0200 Subject: [PATCH] more serverside analysis WIP --- modules/round/src/main/SocketHandler.scala | 14 +++++++ ui/analyse/src/analyse.js | 31 +++++++-------- ui/analyse/src/ctrl.js | 44 ++++++++++++++-------- ui/analyse/src/socket.js | 15 ++++++++ 4 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 ui/analyse/src/socket.js diff --git a/modules/round/src/main/SocketHandler.scala b/modules/round/src/main/SocketHandler.scala index 09a3693200..a52903c65a 100644 --- a/modules/round/src/main/SocketHandler.scala +++ b/modules/round/src/main/SocketHandler.scala @@ -41,6 +41,12 @@ private[round] final class SocketHandler( messenger.watcher(gameId, member, text, socket) } case ("outoftime", _) => round(Outoftime) + case ("anaMove", o) => parseAnaMove(o) foreach { + case (orig, dest, prom, moves) => { + socket ! Ack(uid) + println(s"$orig $dest $moves") + } + } }) { playerId => { case ("p", o) => o int "v" foreach { v => socket ! PingVersion(uid, v) } @@ -138,4 +144,12 @@ private[round] final class SocketHandler( blur = (d int "b") == Some(1) lag = d int "lag" } yield (orig, dest, prom, blur, ~lag) + + private def parseAnaMove(o: JsObject) = for { + d ← o obj "d" + orig ← d str "orig" + dest ← d str "dest" + moves ← d str "moves" + prom = d str "promotion" + } yield (orig, dest, prom, moves) } diff --git a/ui/analyse/src/analyse.js b/ui/analyse/src/analyse.js index 89dcfd09f4..7631aae1e3 100644 --- a/ui/analyse/src/analyse.js +++ b/ui/analyse/src/analyse.js @@ -21,21 +21,22 @@ module.exports = function(steps, analysis) { } } - // this.moveList = function(path) { - // var tree = this.tree; - // var moves = []; - // path.forEach(function(step) { - // for (var i = 0, nb = tree.length; i < nb; i++) { - // var move = tree[i]; - // 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; - // } - // }); - // return moves; - // }.bind(this); + this.moveList = function(path) { + var tree = this.tree; + var moves = []; + for (var j in path) { + var p = path[j]; + for (var i = 0, nb = tree.length; i < nb; i++) { + var move = tree[i]; + if (p.ply == move.ply && p.variation) { + tree = move.variations[p.variation - 1]; + break; + } else if (p.ply >= move.ply) moves.push(move.san); + else break; + } + } + return moves; + }.bind(this); this.explore = function(path, san) { var nextPath = treePath.withPly(path, treePath.currentPly(path) + 1); diff --git a/ui/analyse/src/ctrl.js b/ui/analyse/src/ctrl.js index 214f749e69..61f7eaa591 100644 --- a/ui/analyse/src/ctrl.js +++ b/ui/analyse/src/ctrl.js @@ -96,25 +96,39 @@ module.exports = function(opts) { return role === 'knight' ? 'n' : role[0]; }; - // var addMove = function(orig, dest, promotionRole) { - // $.sound.move(); - // var chess = new Chess( - // this.vm.situation.fen, gameVariantChessId()); - // var promotionLetter = (dest[1] == 1 || dest[1] == 8) ? (promotionRole ? forsyth(promotionRole) : 'q') : null; - // var move = chess.move({ - // from: orig, - // to: dest, - // promotion: promotionLetter - // }); - // if (move) this.userJump(this.analyse.explore(this.vm.path, move.san)); - // else this.chessground.set(this.vm.situation); - // m.redraw(); - // }.bind(this); + var addMove = function(orig, dest, promotionRole) { + $.sound.move(); + var chess = new Chess( + this.vm.situation.fen, gameVariantChessId()); + var promotionLetter = (dest[1] == 1 || dest[1] == 8) ? (promotionRole ? forsyth(promotionRole) : 'q') : null; + var move = chess.move({ + from: orig, + to: dest, + promotion: promotionLetter + }); + if (move) this.userJump(this.analyse.explore(this.vm.path, move.san)); + else this.chessground.set(this.vm.situation); + m.redraw(); + }.bind(this); var userMove = function(orig, dest) { - if (!promotion.start(this, orig, dest, addMove)) addMove(orig, dest); + if (!promotion.start(this, orig, dest, sendMove)) sendMove(orig, dest); }.bind(this); + var sendMove = function(orig, dest, prom) { + var move = { + orig: orig, + dest: dest, + pgn: this.analyse.moveList(this.vm.path).join(' ') + }; + if (prom) move.promotion = prom; + this.socket.send('anaMove', move, { + ackable: true + }); + }.bind(this); + + this.socket = new socket(opts.socketSend, this); + this.router = opts.routes; this.trans = function(key) { diff --git a/ui/analyse/src/socket.js b/ui/analyse/src/socket.js new file mode 100644 index 0000000000..71b8261879 --- /dev/null +++ b/ui/analyse/src/socket.js @@ -0,0 +1,15 @@ +module.exports = function(send, ctrl) { + + this.send = send; + + var handlers = { + }; + + this.receive = function(type, data) { + if (handlers[type]) { + handlers[type](data); + return true; + } + return false; + }.bind(this); +}