From 37b9781e79d6cc9fc3242f5891af88b8540f722a Mon Sep 17 00:00:00 2001 From: cel Date: Tue, 25 Jul 2017 10:31:22 -1000 Subject: Improve chess rendering - Try to align grid better. %24ydxCxrlEp+fQ+nOQars80VWKIXaT/LwKTCf0MvHKk=.sha256 - Render unprefixed message types - Render game end --- lib/render-msg.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 13 deletions(-) (limited to 'lib/render-msg.js') diff --git a/lib/render-msg.js b/lib/render-msg.js index 7a00580..e9fc413 100644 --- a/lib/render-msg.js +++ b/lib/render-msg.js @@ -238,13 +238,22 @@ RenderMsg.prototype.message = function (cb) { case 'ferment/update': case 'robeson/update': return this.update(cb) + case 'chess_invite': + case 'ssb_chess_invite': + return this.chessInvite(cb) + case 'chess_invite_accept': + case 'ssb_chess_invite_accept': + return this.chessInviteAccept(cb) + case 'chess_move': + case 'ssb_chess_move': + return this.chessMove(cb) + case 'chess_game_end': + case 'ssb_chess_game_end': + return this.chessGameEnd(cb) case 'wifi-network': return this.wifiNetwork(cb) case 'mutual/credit': return this.mutualCredit(cb) case 'mutual/account': return this.mutualAccount(cb) case 'npm-publish': return this.npmPublish(cb) - case 'ssb_chess_invite': return this.chessInvite(cb) - case 'ssb_chess_invite_accept': return this.chessInviteAccept(cb) - case 'ssb_chess_move': return this.chessMove(cb) default: return this.object(cb) } } @@ -1027,6 +1036,19 @@ function chessIdxsToLoc(i, j) { return 'abcdefgh'[j] + (8-i) } +RenderMsg.prototype.chessBoard = function (board) { + return h('table.chess-board', + board.map(function (rank, i) { + return h('tr', rank.map(function (piece, j) { + var dark = (i ^ j) & 1 + return h('td', { + class: 'chess-square chess-square-' + (dark ? 'dark' : 'light'), + }, renderChessSymbol(piece, chessIdxsToLoc(i, j))) + })) + }) + ) +} + RenderMsg.prototype.chessMove = function (cb) { var self = this var c = self.c @@ -1043,16 +1065,7 @@ RenderMsg.prototype.chessMove = function (cb) { 'from ', c.orig, ' ', 'to ', c.dest ), - h('table.chess-board', - game.board.map(function (rank, i) { - return h('tr', rank.map(function (piece, j) { - var dark = (i ^ j) & 1 - return h('td', { - class: 'chess-square-' + (dark ? 'dark' : 'light'), - }, renderChessSymbol(piece, chessIdxsToLoc(i, j))) - })) - }) - ) + self.chessBoard(game.board) ], cb) }) } @@ -1079,3 +1092,49 @@ RenderMsg.prototype.chessInviteAccept = function (cb) { ], cb) }) } + +RenderMsg.prototype.chessGameEnd = function (cb) { + var self = this + var c = self.c + var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen + var game = parseChess(fen) + var piece = game && lookupPiece(game.board, c.dest) + var done = multicb({pluck: 1, spread: true}) + self.link(self.c.root, done()) + self.link(self.c.winner, done()) + done(function (err, rootLink, winnerLink) { + if (err) return cb(err) + self.wrap([ + h('div', h('small', '> ', rootLink)), + h('p', + 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', + 'from ', c.orig, ' ', + 'to ', c.dest + ), + h('p', + h('strong', self.c.status), '. winner: ', h('strong', winnerLink)), + self.chessBoard(game.board) + ], cb) + }) +} + +RenderMsg.prototype.chessMove = function (cb) { + var self = this + var c = self.c + var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen + var game = parseChess(fen) + var piece = game && lookupPiece(game.board, c.dest) + self.link(self.c.root, function (err, rootLink) { + if (err) return cb(err) + self.wrap([ + h('div', h('small', '> ', rootLink)), + h('p', + // 'player ', (c.ply || ''), ' ', + 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', + 'from ', c.orig, ' ', + 'to ', c.dest + ), + self.chessBoard(game.board) + ], cb) + }) +} -- cgit v1.2.3