more serverside analysis WIP

This commit is contained in:
Thibault Duplessis 2015-05-04 23:39:09 +02:00
parent a6270ea7e0
commit 0010336b51
4 changed files with 74 additions and 30 deletions

View file

@ -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)
}

View file

@ -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);

View file

@ -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) {

15
ui/analyse/src/socket.js Normal file
View file

@ -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);
}