lila/modules/simul/src/main/JsonView.scala

90 lines
2.9 KiB
Scala
Raw Normal View History

2015-04-01 14:22:28 -06:00
package lila.simul
import play.api.libs.json._
import lila.common.LightUser
import lila.game.{ Game, GameRepo }
2017-01-26 06:59:04 -07:00
final class JsonView(getLightUser: LightUser.Getter) {
2015-04-01 14:22:28 -06:00
2016-08-01 05:50:53 -06:00
private def fetchGames(simul: Simul) =
if (simul.isFinished) GameRepo gamesFromSecondary simul.gameIds
else GameRepo gamesFromPrimary simul.gameIds
2017-01-26 06:59:04 -07:00
def apply(simul: Simul): Fu[JsObject] = for {
games <- fetchGames(simul)
lightHost <- getLightUser(simul.hostId)
applicants <- simul.applicants.sortBy(-_.player.rating).map(applicantJson).sequenceFu
pairings <- simul.pairings.sortBy(-_.player.rating).map(pairingJson(games, simul.hostId)).sequenceFu
} yield Json.obj(
"id" -> simul.id,
"host" -> lightHost.map { host =>
2015-04-03 07:33:31 -06:00
Json.obj(
2017-01-26 06:59:04 -07:00
"id" -> host.id,
"username" -> host.name,
"title" -> host.title,
"rating" -> simul.hostRating,
"gameId" -> simul.hostGameId
)
2017-01-26 06:59:04 -07:00
},
"name" -> simul.name,
"fullName" -> simul.fullName,
"variants" -> simul.variants.map(variantJson(chess.Speed(simul.clock.config.some))),
"applicants" -> applicants,
"pairings" -> pairings,
"isCreated" -> simul.isCreated,
"isRunning" -> simul.isRunning,
"isFinished" -> simul.isFinished,
"quote" -> lila.quote.Quote.one(simul.id)
)
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),
"name" -> v.name
)
2015-04-01 14:22:28 -06:00
2017-01-26 06:59:04 -07:00
private def playerJson(player: SimulPlayer): Fu[JsObject] =
getLightUser(player.user) map { light =>
Json.obj(
"id" -> player.user,
"variant" -> player.variant.key,
2017-07-08 05:58:31 -06:00
"rating" -> player.rating
).add("username" -> light.map(_.name))
.add("title" -> light.map(_.title))
.add("provisional" -> player.provisional.filter(identity))
.add("patron" -> light.??(_.isPatron))
2017-01-26 06:59:04 -07:00
}
2015-04-01 14:22:28 -06:00
2017-01-26 06:59:04 -07:00
private def applicantJson(app: SimulApplicant): Fu[JsObject] =
playerJson(app.player) map { player =>
Json.obj(
"player" -> player,
"accepted" -> app.accepted
)
2017-01-26 06:59:04 -07:00
}
2015-04-01 14:22:28 -06:00
2015-06-04 07:13:54 -06:00
private def gameJson(hostId: String)(g: Game) = Json.obj(
2015-04-01 14:22:28 -06:00
"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),
2015-06-04 07:13:54 -06:00
"lastMove" -> ~g.castleLastMoveTime.lastMoveString,
"orient" -> g.playerByUserId(hostId).map(_.color)
)
2015-04-01 14:22:28 -06:00
2017-01-26 06:59:04 -07:00
private def pairingJson(games: List[Game], hostId: String)(p: SimulPairing): Fu[JsObject] =
playerJson(p.player) map { player =>
Json.obj(
"player" -> player,
"hostColor" -> p.hostColor,
"winnerColor" -> p.winnerColor,
"wins" -> p.wins, // can't be normalized because BC
"game" -> games.find(_.id == p.gameId).map(gameJson(hostId))
)
2017-01-26 06:59:04 -07:00
}
private implicit val colorWriter: Writes[chess.Color] = Writes { c =>
JsString(c.name)
}
2015-04-01 14:22:28 -06:00
}