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

104 lines
3.2 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 }
2019-04-05 19:12:01 -06:00
import lila.user.User
2015-04-01 14:22:28 -06:00
2019-08-20 02:30:09 -06:00
final class JsonView(
getLightUser: LightUser.Getter,
proxyGame: Game.ID => Fu[Option[Game]]
) {
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
2019-08-20 02:30:09 -06:00
else simul.gameIds.map(proxyGame).sequenceFu.map(_.flatten)
2016-08-01 05:50:53 -06:00
2019-07-08 11:59:39 -06:00
def apply(simul: Simul, team: Option[SimulTeam]): Fu[JsObject] = for {
2017-01-26 06:59:04 -07:00
games <- fetchGames(simul)
lightHost <- getLightUser(simul.hostId)
applicants <- simul.applicants.sortBy(-_.player.rating).map(applicantJson).sequenceFu
2019-04-05 19:12:01 -06:00
pairingOptions <- simul.pairings
.sortBy(-_.player.rating)
.map(pairingJson(games, simul.hostId))
.sequenceFu
pairings = pairingOptions.flatten
2017-01-26 06:59:04 -07:00
} 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,
"patron" -> host.isPatron,
2017-01-26 06:59:04 -07:00
"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,
2019-04-28 02:04:42 -06:00
"quote" -> lila.quote.Quote.one(simul.id),
"text" -> simul.text
2019-07-08 11:59:39 -06:00
).add("team", team)
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
2019-04-05 19:12:01 -06:00
private def gameJson(hostId: User.ID, 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,
"fen" -> (chess.format.Forsyth exportBoard g.board),
2018-01-22 15:30:23 -07:00
"lastMove" -> ~g.lastMoveKeys,
"orient" -> g.playerByUserId(hostId).map(_.color)
)
2015-04-01 14:22:28 -06:00
2019-04-05 19:12:01 -06:00
private def pairingJson(games: List[Game], hostId: String)(p: SimulPairing): Fu[Option[JsObject]] =
games.find(_.id == p.gameId) ?? { game =>
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" -> gameJson(hostId, game)
).some
}
2017-01-26 06:59:04 -07:00
}
private implicit val colorWriter: Writes[chess.Color] = Writes { c =>
JsString(c.name)
}
2019-07-08 11:59:39 -06:00
private implicit val simulTeamWriter = Json.writes[SimulTeam]
2015-04-01 14:22:28 -06:00
}