147 lines
3.9 KiB
Scala
147 lines
3.9 KiB
Scala
package lila.game
|
|
|
|
import play.api.libs.json._
|
|
|
|
import chess.format.{ FEN, Forsyth }
|
|
import chess.variant.Crazyhouse
|
|
import chess.{ Clock, Color }
|
|
import lila.common.Json._
|
|
|
|
final class JsonView(rematches: Rematches) {
|
|
|
|
import JsonView._
|
|
|
|
def apply(game: Game, initialFen: Option[FEN]) =
|
|
Json
|
|
.obj(
|
|
"id" -> game.id,
|
|
"variant" -> game.variant,
|
|
"speed" -> game.speed.key,
|
|
"perf" -> PerfPicker.key(game),
|
|
"rated" -> game.rated,
|
|
"fen" -> (Forsyth >> game.chess),
|
|
"player" -> game.turnColor,
|
|
"turns" -> game.turns,
|
|
"startedAtTurn" -> game.chess.startedAtTurn,
|
|
"source" -> game.source,
|
|
"status" -> game.status,
|
|
"createdAt" -> game.createdAt
|
|
)
|
|
.add("initialFen" -> initialFen)
|
|
.add("threefold" -> game.history.threefoldRepetition)
|
|
.add("boosted" -> game.boosted)
|
|
.add("tournamentId" -> game.tournamentId)
|
|
.add("swissId" -> game.swissId)
|
|
.add("winner" -> game.winnerColor)
|
|
.add("lastMove" -> game.lastMoveKeys)
|
|
.add("check" -> game.situation.checkSquare.map(_.key))
|
|
.add("rematch" -> rematches.of(game.id))
|
|
.add("drawOffers" -> (!game.drawOffers.isEmpty).option(game.drawOffers.normalizedPlies))
|
|
}
|
|
|
|
object JsonView {
|
|
|
|
implicit val statusWrites: OWrites[chess.Status] = OWrites { s =>
|
|
Json.obj(
|
|
"id" -> s.id,
|
|
"name" -> s.name
|
|
)
|
|
}
|
|
|
|
implicit val crosstableResultWrites = Json.writes[Crosstable.Result]
|
|
|
|
implicit val crosstableUsersWrites = OWrites[Crosstable.Users] { users =>
|
|
JsObject(users.toList.map { u =>
|
|
u.id -> JsNumber(u.score / 10d)
|
|
})
|
|
}
|
|
|
|
implicit val crosstableWrites = OWrites[Crosstable] { c =>
|
|
Json.obj(
|
|
"users" -> c.users,
|
|
"nbGames" -> c.nbGames
|
|
// "results" -> c.results
|
|
)
|
|
}
|
|
|
|
implicit val matchupWrites = OWrites[Crosstable.Matchup] { m =>
|
|
Json.obj(
|
|
"users" -> m.users,
|
|
"nbGames" -> m.users.nbGames
|
|
)
|
|
}
|
|
|
|
def crosstable(ct: Crosstable, matchup: Option[Crosstable.Matchup]) =
|
|
crosstableWrites
|
|
.writes(ct)
|
|
.add("matchup" -> matchup)
|
|
|
|
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 blursWriter: OWrites[Blurs] = OWrites { blurs =>
|
|
Json.obj(
|
|
"nb" -> blurs.nb,
|
|
"bits" -> blurs.binaryString
|
|
)
|
|
}
|
|
|
|
implicit val variantWriter: OWrites[chess.variant.Variant] = OWrites { v =>
|
|
Json.obj(
|
|
"key" -> v.key,
|
|
"name" -> v.name,
|
|
"short" -> v.shortName
|
|
)
|
|
}
|
|
|
|
implicit val clockWriter: OWrites[Clock] = OWrites { c =>
|
|
Json.obj(
|
|
"running" -> c.isRunning,
|
|
"initial" -> c.limitSeconds,
|
|
"increment" -> c.incrementSeconds,
|
|
"white" -> c.remainingTime(Color.White).toSeconds,
|
|
"black" -> c.remainingTime(Color.Black).toSeconds,
|
|
"emerg" -> c.config.emergSeconds
|
|
)
|
|
}
|
|
|
|
implicit val correspondenceWriter: OWrites[CorrespondenceClock] = OWrites { c =>
|
|
Json.obj(
|
|
"daysPerTurn" -> c.daysPerTurn,
|
|
"increment" -> c.increment,
|
|
"white" -> c.whiteTime,
|
|
"black" -> c.blackTime
|
|
)
|
|
}
|
|
|
|
implicit val openingWriter: OWrites[chess.opening.FullOpening.AtPly] = OWrites { o =>
|
|
Json.obj(
|
|
"eco" -> o.opening.eco,
|
|
"name" -> o.opening.name,
|
|
"ply" -> o.ply
|
|
)
|
|
}
|
|
|
|
implicit val divisionWriter: OWrites[chess.Division] = OWrites { o =>
|
|
Json.obj(
|
|
"middle" -> o.middle,
|
|
"end" -> o.end
|
|
)
|
|
}
|
|
|
|
implicit val sourceWriter: Writes[Source] = Writes { s =>
|
|
JsString(s.name)
|
|
}
|
|
}
|