Skip to content

Commit

Permalink
more crazyhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 16, 2016
1 parent 982fa72 commit 96e1d08
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion modules/chess
Submodule chess updated from 70bf8a to 4491a8
File renamed without changes.
3 changes: 2 additions & 1 deletion modules/game/src/main/Event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import play.api.libs.json._

import chess.Pos
import chess.Pos.{ piotr, allPiotrs }
import chess.{ PromotableRole, Pos, Color, Situation, Move => ChessMove, Drop => ChessDrop, Clock => ChessClock, Status }
import chess.variant.Crazyhouse
import chess.{ PromotableRole, Pos, Color, Situation, Move => ChessMove, Drop => ChessDrop, Clock => ChessClock, Status }
import JsonView._
import lila.chat.{ Line, UserLine, PlayerLine }
import lila.common.Maths.truncateAt
Expand Down Expand Up @@ -100,6 +100,7 @@ object Event {
crazyData: Option[Crazyhouse.Data]) extends Event {
def typ = "drop"
def data = Json.obj(
"role" -> role.name,
"uci" -> s"${role.pgn}@${pos.key}",
"san" -> san,
"fen" -> fen,
Expand Down
2 changes: 1 addition & 1 deletion modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chess.Color.{ White, Black }
import chess.format.Uci
import chess.Pos.piotr, chess.Role.forsyth
import chess.variant.{ Variant, Crazyhouse }
import chess.{ History => ChessHistory, CheckCount, Castles, Role, Board, Move, Drop, MoveOrDrop, Pos, Game => ChessGame, Clock, Status, Color, Piece, Mode, PositionHash }
import chess.{ History => ChessHistory, CheckCount, Castles, Role, Board, MoveOrDrop, Pos, Game => ChessGame, Clock, Status, Color, Piece, Mode, PositionHash }
import org.joda.time.DateTime
import scala.concurrent.duration.FiniteDuration

Expand Down
6 changes: 4 additions & 2 deletions modules/round/src/main/StepBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ object StepBuilder {
} yield Step.Move(uci, san),
fen = Forsyth >> g,
check = g.situation.check,
dests = None)
dests = None,
crazyData = g.situation.board.crazyData)
}
JsArray(a.fold[Seq[Step]](steps) {
case (pgn, analysis) => applyAnalysisAdvices(
Expand Down Expand Up @@ -86,7 +87,8 @@ object StepBuilder {
} yield Step.Move(uci, san),
fen = Forsyth >> g,
check = g.situation.check,
dests = None)
dests = None,
crazyData = g.situation.board.crazyData)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion modules/socket/src/main/AnaMove.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ case class AnaMove(
},
fen = chess.format.Forsyth >> game,
check = game.situation.check,
dests = Some(!game.situation.end ?? game.situation.destinations))
dests = Some(!game.situation.end ?? game.situation.destinations),
crazyData = game.situation.board.crazyData)
}
}

Expand Down
21 changes: 19 additions & 2 deletions modules/socket/src/main/Step.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lila.socket

import chess.format.Uci
import chess.Pos
import chess.variant.Crazyhouse

import play.api.libs.functional.syntax._
import play.api.libs.json._
Expand All @@ -17,7 +18,8 @@ case class Step(
eval: Option[Step.Eval] = None,
nag: Option[String] = None,
comments: List[String] = Nil,
variations: List[List[Step]] = Nil) {
variations: List[List[Step]] = Nil,
crazyData: Option[Crazyhouse.Data]) {

// who's color plays next
def color = chess.Color(ply % 2 == 0)
Expand All @@ -41,6 +43,20 @@ object Step {
}
private implicit val evalJsonWriter = Json.writes[Eval]

// TODO copied from lila.game
// put all that shit somewhere else
implicit val crazyhousePocketWriter: OWrites[Crazyhouse.Pocket] = OWrites { v =>
JsObject(
Crazyhouse.storableRoles.flatMap { role =>
Some(v.roles.count(role ==)).filter(0 <).map { count =>
role.name -> JsNumber(count)
}
})
}
implicit val crazyhouseDataWriter: OWrites[chess.variant.Crazyhouse.Data] = OWrites { v =>
Json.obj("pockets" -> List(v.pockets.white, v.pockets.black))
}

implicit val stepJsonWriter: Writes[Step] = Writes { step =>
import step._
(
Expand All @@ -53,7 +69,8 @@ object Step {
_.map {
case (orig, dests) => s"${orig.piotr}${dests.map(_.piotr).mkString}"
}.mkString(" ")
})
}) _ compose
add("crazy", crazyData)
)(Json.obj(
"ply" -> ply,
"uci" -> move.map(_.uciString),
Expand Down
2 changes: 2 additions & 0 deletions public/stylesheets/board.css
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ div.table {
.lichess_ground .pocket {
border-radius: 3px;
box-shadow: 0 2px 3px rgba(0,0,0,0.3) inset;
width: 100%;
height: 60px;
}
.lichess_ground .pocket {
background: #888;
Expand Down
10 changes: 6 additions & 4 deletions ui/round/src/ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ module.exports = function(opts) {
this.sendMove(orig, dest, false, meta.premove);
}.bind(this);

var onUserNewPiece = function(role, pos) {
this.sendNewPiece(role, pos);
var onUserNewPiece = function(piece, pos) {
this.sendNewPiece(piece.role, pos);
}.bind(this);

var onMove = function(orig, dest, captured) {
Expand Down Expand Up @@ -178,7 +178,8 @@ module.exports = function(opts) {
this.setTitle();
if (!this.replaying()) {
this.vm.ply++;
this.chessground.apiMove(o.uci.substr(0, 2), o.uci.substr(2, 2));
if (o.isMove) this.chessground.apiMove(o.uci.substr(0, 2), o.uci.substr(2, 2));
else this.chessground.apiNewPiece({role: o.role, color: playedColor}, o.uci.substr(2, 2));
if (o.enpassant) {
var p = o.enpassant,
pieces = {};
Expand Down Expand Up @@ -225,7 +226,8 @@ module.exports = function(opts) {
fen: o.fen,
san: o.san,
uci: o.uci,
check: o.check
check: o.check,
crazyhouse: o.crazyhouse
});
game.setOnGame(d, playedColor, true);
delete this.data.forecastCount;
Expand Down
5 changes: 5 additions & 0 deletions ui/round/src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ module.exports = function(send, ctrl) {
m.redraw();
},
move: function(o) {
o.isMove = true;
ctrl.apiMove(o);
},
drop: function(o) {
o.isDrop = true;
ctrl.apiMove(o);
},
reload: function(o) {
Expand Down
6 changes: 4 additions & 2 deletions ui/round/src/view/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var game = require('game').game;
var perf = require('game').perf;
var round = require('../round');
var chessground = require('chessground');
var renderTable = require('./table');
var renderPromotion = require('../promotion').view;
Expand Down Expand Up @@ -36,8 +37,9 @@ function crazyPocketTag(role, color) {
}

function renderCrazyPocket(ctrl, color, position) {
if (!ctrl.data.crazyhouse) return;
var pocket = ctrl.data.crazyhouse.pockets[color === 'white' ? 0 : 1];
var step = round.plyStep(ctrl.data, ctrl.vm.ply);
if (!step.crazy) return;
var pocket = step.crazy.pockets[color === 'white' ? 0 : 1];
var oKeys = Object.keys(pocket)
var crowded = oKeys.length > 4;
return m('div', {
Expand Down

0 comments on commit 96e1d08

Please sign in to comment.