2013-03-22 11:53:13 -06:00
|
|
|
package lila.game
|
2012-05-14 14:36:32 -06:00
|
|
|
|
2014-02-12 16:23:18 -07:00
|
|
|
import lila.common.PimpedJson._
|
2012-05-14 14:36:32 -06:00
|
|
|
import play.api.libs.json._
|
|
|
|
|
2015-06-16 04:09:36 -06:00
|
|
|
import chess.Pos
|
2016-01-15 07:39:35 -07:00
|
|
|
import chess.variant.Crazyhouse
|
2017-06-16 13:55:04 -06:00
|
|
|
import chess.{ Centis, PromotableRole, Pos, Color, Situation, Move => ChessMove, Drop => ChessDrop, Clock => ChessClock, Status }
|
2016-01-15 07:39:35 -07:00
|
|
|
import JsonView._
|
2017-01-15 05:26:08 -07:00
|
|
|
import lila.chat.{ UserLine, PlayerLine }
|
2015-12-24 20:17:26 -07:00
|
|
|
import lila.common.Maths.truncateAt
|
2012-05-14 14:36:32 -06:00
|
|
|
|
|
|
|
sealed trait Event {
|
|
|
|
def typ: String
|
|
|
|
def data: JsValue
|
|
|
|
def only: Option[Color] = None
|
|
|
|
def owner: Boolean = false
|
2012-06-10 08:48:06 -06:00
|
|
|
def watcher: Boolean = false
|
2013-06-05 12:03:30 -06:00
|
|
|
def troll: Boolean = false
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
object Event {
|
|
|
|
|
|
|
|
sealed trait Empty extends Event {
|
|
|
|
def data = JsNull
|
|
|
|
}
|
|
|
|
|
2013-03-30 15:30:47 -06:00
|
|
|
object Start extends Empty {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "start"
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:21:52 -07:00
|
|
|
private def withCrazyData(
|
|
|
|
data: Option[Crazyhouse.Data],
|
2017-02-14 08:34:07 -07:00
|
|
|
drops: Option[List[Pos]]
|
|
|
|
)(js: JsObject): JsObject =
|
2016-01-18 00:21:52 -07:00
|
|
|
data.fold(js) { d =>
|
|
|
|
val js1 = js + ("crazyhouse" -> crazyhouseDataWriter.writes(d))
|
|
|
|
drops.fold(js1) { squares =>
|
|
|
|
js1 + ("drops" -> JsString(squares.map(_.key).mkString))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object MoveOrDrop {
|
|
|
|
|
|
|
|
def data(
|
|
|
|
fen: String,
|
|
|
|
check: Boolean,
|
|
|
|
threefold: Boolean,
|
|
|
|
state: State,
|
|
|
|
clock: Option[Event],
|
|
|
|
possibleMoves: Map[Pos, List[Pos]],
|
|
|
|
possibleDrops: Option[List[Pos]],
|
2017-02-14 08:34:07 -07:00
|
|
|
crazyData: Option[Crazyhouse.Data]
|
|
|
|
)(extra: JsObject) = {
|
2016-01-18 00:21:52 -07:00
|
|
|
extra ++ Json.obj(
|
|
|
|
"fen" -> fen,
|
|
|
|
"check" -> check.option(true),
|
|
|
|
"threefold" -> threefold.option(true),
|
|
|
|
"ply" -> state.turns,
|
|
|
|
"status" -> state.status,
|
|
|
|
"winner" -> state.winner,
|
|
|
|
"wDraw" -> state.whiteOffersDraw.option(true),
|
|
|
|
"bDraw" -> state.blackOffersDraw.option(true),
|
|
|
|
"clock" -> clock.map(_.data),
|
2017-02-14 08:34:07 -07:00
|
|
|
"dests" -> PossibleMoves.json(possibleMoves)
|
|
|
|
)
|
2016-01-18 00:21:52 -07:00
|
|
|
}.noNull |> withCrazyData(crazyData, possibleDrops)
|
|
|
|
}
|
2016-01-15 07:39:35 -07:00
|
|
|
|
2015-04-27 14:36:43 -06:00
|
|
|
case class Move(
|
|
|
|
orig: Pos,
|
|
|
|
dest: Pos,
|
|
|
|
san: String,
|
|
|
|
fen: String,
|
2015-06-15 17:26:39 -06:00
|
|
|
check: Boolean,
|
|
|
|
threefold: Boolean,
|
|
|
|
promotion: Option[Promotion],
|
|
|
|
enpassant: Option[Enpassant],
|
2015-06-15 17:56:27 -06:00
|
|
|
castle: Option[Castling],
|
2015-06-15 18:09:38 -06:00
|
|
|
state: State,
|
2015-06-16 04:09:36 -06:00
|
|
|
clock: Option[Event],
|
2016-01-15 07:39:35 -07:00
|
|
|
possibleMoves: Map[Pos, List[Pos]],
|
2016-01-18 00:21:52 -07:00
|
|
|
possibleDrops: Option[List[Pos]],
|
2017-02-14 08:34:07 -07:00
|
|
|
crazyData: Option[Crazyhouse.Data]
|
|
|
|
) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "move"
|
2016-01-18 00:21:52 -07:00
|
|
|
def data = MoveOrDrop.data(fen, check, threefold, state, clock, possibleMoves, possibleDrops, crazyData) {
|
|
|
|
Json.obj(
|
|
|
|
"uci" -> s"${orig.key}${dest.key}",
|
|
|
|
"san" -> san,
|
|
|
|
"promotion" -> promotion.map(_.data),
|
|
|
|
"enpassant" -> enpassant.map(_.data),
|
2017-02-14 08:34:07 -07:00
|
|
|
"castle" -> castle.map(_.data)
|
|
|
|
)
|
2016-01-18 00:21:52 -07:00
|
|
|
}
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
object Move {
|
2016-01-15 07:39:35 -07:00
|
|
|
def apply(move: ChessMove, situation: Situation, state: State, clock: Option[Event], crazyData: Option[Crazyhouse.Data]): Move = Move(
|
2015-06-16 04:09:36 -06:00
|
|
|
orig = move.orig,
|
|
|
|
dest = move.dest,
|
|
|
|
san = chess.format.pgn.Dumper(move),
|
|
|
|
fen = chess.format.Forsyth.exportBoard(situation.board),
|
|
|
|
check = situation.check,
|
|
|
|
threefold = situation.threefoldRepetition,
|
|
|
|
promotion = move.promotion.map { Promotion(_, move.dest) },
|
|
|
|
enpassant = (move.capture ifTrue move.enpassant).map {
|
|
|
|
Event.Enpassant(_, !move.color)
|
|
|
|
},
|
|
|
|
castle = move.castle.map {
|
|
|
|
case (king, rook) => Castling(king, rook, move.color)
|
|
|
|
},
|
|
|
|
state = state,
|
|
|
|
clock = clock,
|
2016-01-15 07:39:35 -07:00
|
|
|
possibleMoves = situation.destinations,
|
2016-01-18 00:21:52 -07:00
|
|
|
possibleDrops = situation.drops,
|
2017-02-14 08:34:07 -07:00
|
|
|
crazyData = crazyData
|
|
|
|
)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2016-01-15 06:21:16 -07:00
|
|
|
case class Drop(
|
|
|
|
role: chess.Role,
|
|
|
|
pos: Pos,
|
|
|
|
san: String,
|
|
|
|
fen: String,
|
|
|
|
check: Boolean,
|
|
|
|
threefold: Boolean,
|
|
|
|
state: State,
|
|
|
|
clock: Option[Event],
|
2016-01-15 07:39:35 -07:00
|
|
|
possibleMoves: Map[Pos, List[Pos]],
|
2016-01-18 00:21:52 -07:00
|
|
|
crazyData: Option[Crazyhouse.Data],
|
2017-02-14 08:34:07 -07:00
|
|
|
possibleDrops: Option[List[Pos]]
|
|
|
|
) extends Event {
|
2016-01-15 06:21:16 -07:00
|
|
|
def typ = "drop"
|
2016-01-18 00:21:52 -07:00
|
|
|
def data = MoveOrDrop.data(fen, check, threefold, state, clock, possibleMoves, possibleDrops, crazyData) {
|
|
|
|
Json.obj(
|
|
|
|
"role" -> role.name,
|
|
|
|
"uci" -> s"${role.pgn}@${pos.key}",
|
2017-02-14 08:34:07 -07:00
|
|
|
"san" -> san
|
|
|
|
)
|
2016-01-18 00:21:52 -07:00
|
|
|
}
|
2016-01-15 06:21:16 -07:00
|
|
|
}
|
|
|
|
object Drop {
|
2016-01-15 07:39:35 -07:00
|
|
|
def apply(drop: ChessDrop, situation: Situation, state: State, clock: Option[Event], crazyData: Option[Crazyhouse.Data]): Drop = Drop(
|
2016-01-15 06:21:16 -07:00
|
|
|
role = drop.piece.role,
|
|
|
|
pos = drop.pos,
|
|
|
|
san = chess.format.pgn.Dumper(drop),
|
|
|
|
fen = chess.format.Forsyth.exportBoard(situation.board),
|
|
|
|
check = situation.check,
|
|
|
|
threefold = situation.threefoldRepetition,
|
|
|
|
state = state,
|
|
|
|
clock = clock,
|
2016-01-15 07:39:35 -07:00
|
|
|
possibleMoves = situation.destinations,
|
2016-01-18 00:21:52 -07:00
|
|
|
possibleDrops = situation.drops,
|
2017-02-14 08:34:07 -07:00
|
|
|
crazyData = crazyData
|
|
|
|
)
|
2016-01-15 06:21:16 -07:00
|
|
|
}
|
|
|
|
|
2015-06-16 04:09:36 -06:00
|
|
|
object PossibleMoves {
|
|
|
|
def json(moves: Map[Pos, List[Pos]]) =
|
|
|
|
if (moves.isEmpty) JsNull
|
|
|
|
else moves.foldLeft(JsObject(Nil)) {
|
|
|
|
case (res, (o, d)) => res + (o.key, JsString(d map (_.key) mkString))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 16:56:03 -07:00
|
|
|
case class Enpassant(pos: Pos, color: Color) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "enpassant"
|
2015-01-11 16:56:03 -07:00
|
|
|
def data = Json.obj(
|
|
|
|
"key" -> pos.key,
|
2017-02-14 08:34:07 -07:00
|
|
|
"color" -> color
|
|
|
|
)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case class Castling(king: (Pos, Pos), rook: (Pos, Pos), color: Color) extends Event {
|
|
|
|
def typ = "castling"
|
2013-04-01 21:43:01 -06:00
|
|
|
def data = Json.obj(
|
|
|
|
"king" -> Json.arr(king._1.key, king._2.key),
|
|
|
|
"rook" -> Json.arr(rook._1.key, rook._2.key),
|
2015-08-14 05:14:50 -06:00
|
|
|
"color" -> color
|
2013-04-01 21:43:01 -06:00
|
|
|
)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:23:18 -07:00
|
|
|
case class RedirectOwner(
|
|
|
|
color: Color,
|
2014-08-13 16:14:03 -06:00
|
|
|
id: String,
|
2017-02-14 08:34:07 -07:00
|
|
|
cookie: Option[JsObject]
|
|
|
|
) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "redirect"
|
2014-02-12 16:23:18 -07:00
|
|
|
def data = Json.obj(
|
2014-08-13 16:14:03 -06:00
|
|
|
"id" -> id,
|
|
|
|
"url" -> s"/$id",
|
2014-02-12 16:23:18 -07:00
|
|
|
"cookie" -> cookie
|
|
|
|
).noNull
|
2012-05-14 14:36:32 -06:00
|
|
|
override def only = Some(color)
|
|
|
|
override def owner = true
|
|
|
|
}
|
|
|
|
|
|
|
|
case class Promotion(role: PromotableRole, pos: Pos) extends Event {
|
|
|
|
def typ = "promotion"
|
2013-04-01 21:43:01 -06:00
|
|
|
def data = Json.obj(
|
|
|
|
"key" -> pos.key,
|
|
|
|
"pieceClass" -> role.toString.toLowerCase
|
|
|
|
)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-31 18:02:32 -07:00
|
|
|
case class PlayerMessage(line: PlayerLine) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "message"
|
2016-04-21 22:48:02 -06:00
|
|
|
def data = lila.chat.JsonView(line)
|
2012-05-14 14:36:32 -06:00
|
|
|
override def owner = true
|
2014-01-31 18:02:32 -07:00
|
|
|
override def troll = false
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-31 18:02:32 -07:00
|
|
|
case class UserMessage(line: UserLine, w: Boolean) extends Event {
|
2012-06-10 08:48:06 -06:00
|
|
|
def typ = "message"
|
2016-04-21 22:48:02 -06:00
|
|
|
def data = lila.chat.JsonView(line)
|
2014-01-31 18:02:32 -07:00
|
|
|
override def troll = line.troll
|
|
|
|
override def watcher = w
|
2014-02-19 11:52:22 -07:00
|
|
|
override def owner = !w
|
2012-06-10 08:48:06 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 06:59:54 -06:00
|
|
|
// for mobile app BC only
|
2015-06-16 03:33:49 -06:00
|
|
|
case class End(winner: Option[Color]) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "end"
|
2015-08-14 05:14:50 -06:00
|
|
|
def data = Json.toJson(winner)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 06:59:54 -06:00
|
|
|
case class EndData(game: Game, ratingDiff: Option[RatingDiffs]) extends Event {
|
|
|
|
def typ = "endData"
|
|
|
|
def data = Json.obj(
|
|
|
|
"winner" -> game.winnerColor,
|
|
|
|
"status" -> game.status
|
|
|
|
).add("ratingDiff" -> ratingDiff.map { rds =>
|
|
|
|
Json.obj(
|
|
|
|
Color.White.name -> rds.white,
|
|
|
|
Color.Black.name -> rds.black
|
|
|
|
)
|
|
|
|
}).add("boosted" -> game.boosted.option(true))
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:10:12 -06:00
|
|
|
case object Reload extends Empty {
|
|
|
|
def typ = "reload"
|
|
|
|
}
|
|
|
|
case object ReloadOwner extends Empty {
|
|
|
|
def typ = "reload"
|
2013-05-18 09:26:37 -06:00
|
|
|
override def owner = true
|
|
|
|
}
|
2013-05-18 07:51:26 -06:00
|
|
|
|
2017-07-07 04:53:47 -06:00
|
|
|
private def reloadOr[A: Writes](typ: String, data: A) = Json.obj("t" -> typ, "d" -> data)
|
|
|
|
|
|
|
|
// use t:reload for mobile app BC,
|
|
|
|
// but send extra data for the web to avoid reloading
|
2017-07-07 05:13:15 -06:00
|
|
|
case class RematchOffer(by: Option[Color]) extends Event {
|
2017-07-07 04:53:47 -06:00
|
|
|
def typ = "reload"
|
2017-07-07 05:13:15 -06:00
|
|
|
def data = reloadOr("rematchOffer", by)
|
2017-07-07 04:23:02 -06:00
|
|
|
override def owner = true
|
|
|
|
}
|
|
|
|
|
|
|
|
case class RematchTaken(nextId: Game.ID) extends Event {
|
2017-07-07 04:53:47 -06:00
|
|
|
def typ = "reload"
|
|
|
|
def data = reloadOr("rematchTaken", nextId)
|
2017-07-07 04:23:02 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 05:13:25 -06:00
|
|
|
case class DrawOffer(by: Option[Color]) extends Event {
|
|
|
|
def typ = "reload"
|
|
|
|
def data = reloadOr("drawOffer", by)
|
|
|
|
override def owner = true
|
|
|
|
}
|
|
|
|
|
2012-05-14 14:36:32 -06:00
|
|
|
case class Premove(color: Color) extends Empty {
|
|
|
|
def typ = "premove"
|
|
|
|
override def only = Some(color)
|
2013-05-30 04:18:26 -06:00
|
|
|
override def owner = true
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:55:04 -06:00
|
|
|
case class Clock(white: Float, black: Float, nextLagComp: Option[Centis] = None) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "clock"
|
2013-04-01 21:43:01 -06:00
|
|
|
def data = Json.obj(
|
2015-12-24 20:17:26 -07:00
|
|
|
"white" -> truncateAt(white, 2),
|
2017-06-16 13:55:04 -06:00
|
|
|
"black" -> truncateAt(black, 2),
|
2017-06-20 11:51:22 -06:00
|
|
|
"lag" -> nextLagComp.collect { case Centis(c) if c > 1 => c }
|
2017-06-16 13:55:04 -06:00
|
|
|
).noNull
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
object Clock {
|
|
|
|
def apply(clock: ChessClock): Clock = Clock(
|
2017-04-19 20:33:22 -06:00
|
|
|
clock remainingTime Color.White toSeconds,
|
2017-06-16 13:55:04 -06:00
|
|
|
clock remainingTime Color.Black toSeconds,
|
|
|
|
clock lagCompEstimate clock.color
|
2017-02-14 08:34:07 -07:00
|
|
|
)
|
2015-06-26 04:25:29 -06:00
|
|
|
def tenths(white: Int, black: Int): Clock = Clock(white.toFloat / 10, black.toFloat / 10)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2015-08-18 06:21:06 -06:00
|
|
|
case class Berserk(color: Color) extends Event {
|
|
|
|
def typ = "berserk"
|
|
|
|
def data = Json.toJson(color)
|
|
|
|
}
|
|
|
|
|
2014-11-30 03:22:23 -07:00
|
|
|
case class CorrespondenceClock(white: Float, black: Float) extends Event {
|
|
|
|
def typ = "cclock"
|
|
|
|
def data = Json.obj("white" -> white, "black" -> black)
|
|
|
|
}
|
|
|
|
object CorrespondenceClock {
|
|
|
|
def apply(clock: lila.game.CorrespondenceClock): CorrespondenceClock =
|
|
|
|
CorrespondenceClock(clock.whiteTime, clock.blackTime)
|
|
|
|
}
|
|
|
|
|
2014-07-31 13:06:22 -06:00
|
|
|
case class CheckCount(white: Int, black: Int) extends Event {
|
|
|
|
def typ = "checkCount"
|
|
|
|
def data = Json.obj(
|
|
|
|
"white" -> white,
|
|
|
|
"black" -> black
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-03-31 07:59:44 -06:00
|
|
|
case class State(
|
|
|
|
color: Color,
|
|
|
|
turns: Int,
|
2015-04-01 04:38:47 -06:00
|
|
|
status: Option[Status],
|
2015-08-14 05:14:50 -06:00
|
|
|
winner: Option[Color],
|
2015-03-31 07:59:44 -06:00
|
|
|
whiteOffersDraw: Boolean,
|
2017-02-14 08:34:07 -07:00
|
|
|
blackOffersDraw: Boolean
|
|
|
|
) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "state"
|
2013-04-01 21:43:01 -06:00
|
|
|
def data = Json.obj(
|
2015-08-14 05:14:50 -06:00
|
|
|
"color" -> color,
|
2015-03-31 07:59:44 -06:00
|
|
|
"turns" -> turns,
|
2015-08-14 05:14:50 -06:00
|
|
|
"status" -> status,
|
|
|
|
"winner" -> winner,
|
2015-03-31 07:59:44 -06:00
|
|
|
"wDraw" -> whiteOffersDraw.option(true),
|
|
|
|
"bDraw" -> blackOffersDraw.option(true)
|
|
|
|
).noNull
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
|
|
|
|
2015-04-21 13:21:26 -06:00
|
|
|
case class TakebackOffers(
|
|
|
|
white: Boolean,
|
2017-02-14 08:34:07 -07:00
|
|
|
black: Boolean
|
|
|
|
) extends Event {
|
2015-04-21 13:21:26 -06:00
|
|
|
def typ = "takebackOffers"
|
|
|
|
def data = Json.obj(
|
|
|
|
"white" -> white.option(true),
|
|
|
|
"black" -> black.option(true)
|
|
|
|
).noNull
|
2015-04-21 13:22:55 -06:00
|
|
|
override def owner = true
|
2015-04-21 13:21:26 -06:00
|
|
|
}
|
|
|
|
|
2012-05-14 14:36:32 -06:00
|
|
|
case class Crowd(
|
|
|
|
white: Boolean,
|
|
|
|
black: Boolean,
|
2017-02-14 08:34:07 -07:00
|
|
|
watchers: JsValue
|
|
|
|
) extends Event {
|
2012-05-14 14:36:32 -06:00
|
|
|
def typ = "crowd"
|
2013-04-01 21:43:01 -06:00
|
|
|
def data = Json.obj(
|
|
|
|
"white" -> white,
|
|
|
|
"black" -> black,
|
2017-02-14 08:34:07 -07:00
|
|
|
"watchers" -> watchers
|
|
|
|
)
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|
2015-08-14 05:14:50 -06:00
|
|
|
|
|
|
|
private implicit val colorWriter: Writes[Color] = Writes { c =>
|
|
|
|
JsString(c.name)
|
|
|
|
}
|
|
|
|
private implicit val statusWriter: OWrites[Status] = OWrites { s =>
|
|
|
|
Json.obj("id" -> s.id, "name" -> s.name)
|
|
|
|
}
|
2012-05-14 14:36:32 -06:00
|
|
|
}
|