2015-04-01 14:22:28 -06:00
|
|
|
package lila.simul
|
|
|
|
|
|
|
|
import play.api.libs.json._
|
|
|
|
|
|
|
|
import lila.common.LightUser
|
|
|
|
import lila.common.PimpedJson._
|
|
|
|
import lila.game.{ Game, GameRepo }
|
|
|
|
import lila.user.{ User, UserRepo }
|
|
|
|
|
|
|
|
final class JsonView(
|
|
|
|
getLightUser: String => Option[LightUser]) {
|
|
|
|
|
2015-04-03 07:33:31 -06:00
|
|
|
def apply(simul: Simul): Fu[JsObject] =
|
|
|
|
GameRepo.games(simul.gameIds) map { games =>
|
|
|
|
val lightHost = getLightUser(simul.hostId)
|
|
|
|
Json.obj(
|
|
|
|
"id" -> simul.id,
|
|
|
|
"host" -> lightHost.map { host =>
|
|
|
|
Json.obj(
|
|
|
|
"id" -> host.id,
|
2015-04-05 01:57:13 -06:00
|
|
|
"username" -> host.name,
|
2015-04-03 07:33:31 -06:00
|
|
|
"title" -> host.title,
|
2015-04-05 13:25:29 -06:00
|
|
|
"rating" -> simul.hostRating,
|
|
|
|
"gameId" -> simul.hostGameId)
|
2015-04-03 07:33:31 -06:00
|
|
|
},
|
|
|
|
"name" -> simul.name,
|
2015-04-04 08:44:50 -06:00
|
|
|
"fullName" -> simul.fullName,
|
2015-04-03 15:04:59 -06:00
|
|
|
"variants" -> simul.variants.map(variantJson(chess.Speed(simul.clock.chessClock.some))),
|
2015-04-03 07:33:31 -06:00
|
|
|
"applicants" -> simul.applicants.sortBy(-_.player.rating).map(applicantJson),
|
|
|
|
"pairings" -> simul.pairings.sortBy(-_.player.rating).map(pairingJson(games)),
|
2015-04-04 06:51:22 -06:00
|
|
|
"isCreated" -> simul.isCreated,
|
|
|
|
"isRunning" -> simul.isRunning,
|
2015-04-03 07:33:31 -06:00
|
|
|
"isFinished" -> simul.isFinished)
|
2015-04-01 14:22:28 -06:00
|
|
|
}
|
|
|
|
|
2015-04-03 15:04:59 -06:00
|
|
|
private def variantJson(speed: chess.Speed)(v: chess.variant.Variant) = Json.obj(
|
2015-04-01 14:22:28 -06:00
|
|
|
"key" -> v.key,
|
2015-04-03 15:04:59 -06:00
|
|
|
"icon" -> lila.game.PerfPicker.perfType(speed, v, none).map(_.iconChar.toString),
|
2015-04-01 14:22:28 -06:00
|
|
|
"name" -> v.name)
|
|
|
|
|
|
|
|
private def playerJson(player: SimulPlayer) = {
|
|
|
|
val light = getLightUser(player.user)
|
|
|
|
Json.obj(
|
|
|
|
"id" -> player.user,
|
|
|
|
"variant" -> player.variant.key,
|
2015-04-03 07:33:31 -06:00
|
|
|
"username" -> light.map(_.name),
|
2015-04-01 14:22:28 -06:00
|
|
|
"title" -> light.map(_.title),
|
2015-06-01 10:12:30 -06:00
|
|
|
"rating" -> player.rating,
|
|
|
|
"provisional" -> player.provisional.filter(identity)
|
2015-04-01 14:22:28 -06:00
|
|
|
).noNull
|
|
|
|
}
|
|
|
|
|
|
|
|
private def applicantJson(app: SimulApplicant) = Json.obj(
|
|
|
|
"player" -> playerJson(app.player),
|
|
|
|
"accepted" -> app.accepted)
|
|
|
|
|
|
|
|
private def gameJson(g: Game) = Json.obj(
|
|
|
|
"id" -> g.id,
|
2015-04-03 07:33:31 -06:00
|
|
|
"status" -> g.status.id,
|
2015-04-01 14:22:28 -06:00
|
|
|
"fen" -> (chess.format.Forsyth exportBoard g.toChess.board),
|
|
|
|
"lastMove" -> ~g.castleLastMoveTime.lastMoveString)
|
|
|
|
|
|
|
|
private def pairingJson(games: List[Game])(p: SimulPairing) = Json.obj(
|
|
|
|
"player" -> playerJson(p.player),
|
2015-04-03 15:04:59 -06:00
|
|
|
"wins" -> p.wins,
|
2015-04-01 14:22:28 -06:00
|
|
|
"game" -> games.find(_.id == p.gameId).map(gameJson)
|
|
|
|
)
|
|
|
|
}
|