deprecate state socket event

pull/595/head
Thibault Duplessis 2015-06-16 01:56:27 +02:00
parent dca9db8368
commit 43cfce5785
4 changed files with 38 additions and 32 deletions

View File

@ -18,8 +18,8 @@ sealed trait Event {
object Event {
def fromMove(move: ChessMove, situation: Situation): List[Event] =
Move(move, situation) :: List(
def fromMove(move: ChessMove, situation: Situation, state: State): List[Event] =
Move(move, situation, state) :: 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
@ -52,7 +52,8 @@ object Event {
threefold: Boolean,
promotion: Option[Promotion],
enpassant: Option[Enpassant],
castle: Option[Castling]) extends Event {
castle: Option[Castling],
state: State) extends Event {
def typ = "move"
def data = Json.obj(
// legacy data
@ -67,11 +68,17 @@ object Event {
"threefold" -> threefold.option(true),
"promotion" -> promotion.map(_.data),
"enpassant" -> enpassant.map(_.data),
"castle" -> castle.map(_.data)
"castle" -> castle.map(_.data),
"ply" -> state.turns,
"status" -> state.status.map { s =>
Json.obj("id" -> s.id, "name" -> s.name)
},
"wDraw" -> state.whiteOffersDraw.option(true),
"bDraw" -> state.blackOffersDraw.option(true)
).noNull
}
object Move {
def apply(move: ChessMove, situation: Situation): Move =
def apply(move: ChessMove, situation: Situation, state: State): Move =
Move(
orig = move.orig,
dest = move.dest,
@ -86,7 +93,8 @@ object Event {
},
castle = move.castle.map {
case (king, rook) => Castling(king, rook, move.color)
})
},
state = state)
}
case class PossibleMoves(

View File

@ -166,22 +166,24 @@ case class Game(
status = situation.status | status,
clock = game.clock)
val events = (players collect {
case p if p.isHuman => Event.possibleMoves(situation, p.color)
}) :::
Event.State(
situation.color,
game.turns,
(status != updated.status) option status,
whiteOffersDraw = whitePlayer.isOfferingDraw,
blackOffersDraw = blackPlayer.isOfferingDraw) ::
Event.fromMove(move, situation) :::
(Event fromSituation situation)
val state = Event.State(
situation.color,
game.turns,
(status != updated.status) option updated.status,
whiteOffersDraw = whitePlayer.isOfferingDraw,
blackOffersDraw = blackPlayer.isOfferingDraw)
val clockEvent = updated.clock map Event.Clock.apply orElse {
updated.correspondenceClock map Event.CorrespondenceClock.apply
}
val events = (players collect {
case p if p.isHuman => Event.possibleMoves(situation, p.color)
}) :::
state :: // BC
Event.fromMove(move, situation, state, clockEvent) :::
(Event fromSituation situation)
val finalEvents = events ::: clockEvent.toList ::: {
// abstraction leak, I know.
(updated.variant.threeCheck && situation.check) ?? List(Event.CheckCount(

View File

@ -127,6 +127,12 @@ module.exports = function(opts) {
this.apiMove = function(o) {
m.startComputation();
this.data.game.turns = o.ply;
this.data.game.player = o.ply % 2 === 0 ? 'white' : 'black';
if (o.status) this.data.game.status = o.status;
this.data[this.data.player.color === 'white' ? 'player' : 'opponent'].offeringDraw = o.wDraw;
this.data[this.data.player.color === 'black' ? 'player' : 'opponent'].offeringDraw = o.bDraw;
this.setTitle();
if (!this.replaying()) {
this.vm.ply++;
this.chessground.apiMove(o.from, o.to);
@ -140,7 +146,8 @@ module.exports = function(opts) {
}
if (o.promotion) ground.promote(this.chessground, o.promotion.key, o.promotion.pieceClass);
if (o.castling && !this.chessground.data.autoCastle) {
var c = o.castling, pieces = {};
var c = o.castling,
pieces = {};
pieces[c.king[0]] = null;
pieces[c.rook[0]] = null;
pieces[c.king[1]] = {
@ -153,8 +160,9 @@ module.exports = function(opts) {
};
this.chessground.setPieces(pieces);
}
if (o.check) this.chessground.set({
check: true
this.chessground.set({
turnColor: this.data.game.player,
check: o.check
});
// atrocious hack to prevent race condition
// with explosions and premoves

View File

@ -18,18 +18,6 @@ module.exports = function(send, ctrl) {
}
});
},
state: function(o) {
if (!ctrl.replaying()) ctrl.chessground.set({
turnColor: o.color
});
ctrl.data.game.player = o.color;
ctrl.data.game.turns = o.turns;
if (o.status) ctrl.data.game.status = o.status;
ctrl.data[ctrl.data.player.color === 'white' ? 'player' : 'opponent'].offeringDraw = o.wDraw;
ctrl.data[ctrl.data.player.color === 'black' ? 'player' : 'opponent'].offeringDraw = o.bDraw;
m.redraw();
ctrl.setTitle();
},
takebackOffers: function(o) {
ctrl.data.player.proposingTakeback = o[ctrl.data.player.color];
ctrl.data.opponent.proposingTakeback = o[ctrl.data.opponent.color];