Skip to content

Commit

Permalink
optimisation: don't resend unchanged game status for each move
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 1, 2015
1 parent 575b28a commit 5819413
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 6 additions & 4 deletions modules/game/src/main/Event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,18 @@ object Event {
case class State(
color: Color,
turns: Int,
status: Status,
status: Option[Status],
whiteOffersDraw: Boolean,
blackOffersDraw: Boolean) extends Event {
def typ = "state"
def data = Json.obj(
"color" -> color.name,
"turns" -> turns,
"status" -> Json.obj(
"id" -> status.id,
"name" -> status.name),
"status" -> status.map { s =>
Json.obj(
"id" -> s.id,
"name" -> s.name)
},
"wDraw" -> whiteOffersDraw.option(true),
"bDraw" -> blackOffersDraw.option(true)
).noNull
Expand Down
24 changes: 12 additions & 12 deletions modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,6 @@ case class Game(
blur: Boolean = false): Progress = {
val (history, situation) = (game.board.history, game.situation)

val events = (players collect {
case p if p.isHuman => Event.possibleMoves(situation, p.color)
}) :::
Event.State(
situation.color,
game.turns,
status,
whiteOffersDraw = whitePlayer.isOfferingDraw,
blackOffersDraw = blackPlayer.isOfferingDraw) ::
(Event fromMove move) :::
(Event fromSituation situation)

def copyPlayer(player: Player) = player.copy(
blurs = math.min(
playerMoves(player.color),
Expand All @@ -176,6 +164,18 @@ 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) :::
(Event fromSituation situation)

val clockEvent = updated.clock map Event.Clock.apply orElse {
updated.correspondenceClock map Event.CorrespondenceClock.apply
}
Expand Down
2 changes: 1 addition & 1 deletion ui/round/src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function(send, ctrl) {
});
ctrl.data.game.player = o.color;
ctrl.data.game.turns = o.turns;
ctrl.data.game.status = o.status;
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();
Expand Down

0 comments on commit 5819413

Please sign in to comment.