unify clock event with move

pull/595/head
Thibault Duplessis 2015-06-16 02:09:38 +02:00
parent 758c19fdc9
commit f843296d21
4 changed files with 28 additions and 19 deletions

View File

@ -18,8 +18,8 @@ sealed trait Event {
object Event {
def fromMove(move: ChessMove, situation: Situation, state: State): List[Event] =
Move(move, situation, state) :: List(
def fromMove(move: ChessMove, situation: Situation, state: State, clock: Option[Event]): List[Event] =
Move(move, situation, state, clock) :: List(
(move.capture ifTrue move.enpassant) map { Event.Enpassant(_, !move.color) }, // BC
move.promotion map { Promotion(_, move.dest) }, // BC
move.castle map { case (king, rook) => Castling(king, rook, move.color) } // BC
@ -53,7 +53,8 @@ object Event {
promotion: Option[Promotion],
enpassant: Option[Enpassant],
castle: Option[Castling],
state: State) extends Event {
state: State,
clock: Option[Event]) extends Event {
def typ = "move"
def data = Json.obj(
// legacy data
@ -74,11 +75,12 @@ object Event {
Json.obj("id" -> s.id, "name" -> s.name)
},
"wDraw" -> state.whiteOffersDraw.option(true),
"bDraw" -> state.blackOffersDraw.option(true)
"bDraw" -> state.blackOffersDraw.option(true),
"clock" -> clock.map(_.data)
).noNull
}
object Move {
def apply(move: ChessMove, situation: Situation, state: State): Move =
def apply(move: ChessMove, situation: Situation, state: State, clock: Option[Event]): Move =
Move(
orig = move.orig,
dest = move.dest,
@ -94,7 +96,8 @@ object Event {
castle = move.castle.map {
case (king, rook) => Castling(king, rook, move.color)
},
state = state)
state = state,
clock = clock)
}
case class PossibleMoves(

View File

@ -177,20 +177,24 @@ case class Game(
updated.correspondenceClock map Event.CorrespondenceClock.apply
}
val events = (players collect {
val possibleMovesEvent = (players collect {
case p if p.isHuman => Event.possibleMoves(situation, p.color)
}) :::
})
val events = possibleMovesEvent ::: // make that BC
state :: // BC
Event.fromMove(move, situation, state, clockEvent) :::
(Event fromSituation situation)
(Event fromSituation situation) // BC
val finalEvents = events ::: clockEvent.toList ::: {
// abstraction leak, I know.
(updated.variant.threeCheck && situation.check) ?? List(Event.CheckCount(
white = updated.checkCount.white,
black = updated.checkCount.black
))
}
val finalEvents = events :::
clockEvent.toList ::: // BC
{
// abstraction leak, I know.
(updated.variant.threeCheck && situation.check) ?? List(Event.CheckCount(
white = updated.checkCount.white,
black = updated.checkCount.black
))
}
Progress(this, updated, finalEvents)
}

View File

@ -170,6 +170,11 @@ module.exports = function(opts) {
if (this.data.game.variant.key === 'atomic') setTimeout(this.chessground.playPremove, 100);
else this.chessground.playPremove();
}
if (o.clock) {
var c = o.clock
if (this.clock) this.clock.update(c.white, c.black);
else if (this.correspondenceClock) this.correspondenceClock.update(c.white, c.black);
}
this.data.game.threefold = !!o.threefold;
this.data.steps.push({
ply: this.lastPly() + 1,

View File

@ -36,9 +36,6 @@ module.exports = function(send, ctrl) {
clock: function(o) {
if (ctrl.clock) ctrl.clock.update(o.white, o.black);
},
cclock: function(o) {
if (ctrl.correspondenceClock) ctrl.correspondenceClock.update(o.white, o.black);
},
crowd: function(o) {
['white', 'black'].forEach(function(c) {
game.setOnGame(ctrl.data, c, o[c]);