user perf rating percentile

pull/1352/head
Thibault Duplessis 2015-12-26 13:52:06 +07:00
parent fb59bdb486
commit 55871941fc
3 changed files with 15 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import views._
object Stat extends LilaController {
def ratingDistribution(perfKey: lila.rating.Perf.Key) = Open { implicit ctx =>
lila.rating.PerfType(perfKey).filter(lila.rating.PerfType.nonPuzzle.contains) match {
lila.rating.PerfType(perfKey).filter(lila.rating.PerfType.isGame) match {
case Some(perfType) => Env.user.cached.ratingDistribution(perfType.key) map { data =>
Ok(html.stat.ratingDistribution(perfType, data))
}

View File

@ -226,12 +226,12 @@ object User extends LilaController {
OptionFuResult(UserRepo named username) { u =>
if ((u.disabled || (u.lame && !ctx.is(u))) && !isGranted(_.UserSpy)) notFound
else lila.rating.PerfType(perfKey).fold(notFound) { perfType =>
Env.perfStat.get(u, perfType).flatMap { perfStat =>
Env.user.cached.ranking.getAll(u.id).map { ranks =>
val data = Env.perfStat.jsonView(u, perfStat, ranks get perfType.key)
Ok(html.user.perfStat(u, ranks, perfType, data))
}
}
for {
perfStat <- Env.perfStat.get(u, perfType)
ranks <- Env.user.cached.ranking.getAll(u.id)
distribution <- Env.user.cached.ratingDistribution(perfType.key)
data = Env.perfStat.jsonView(u, perfStat, ranks get perfType.key, distribution)
} yield Ok(html.user.perfStat(u, ranks, perfType, data))
}
}
}

View File

@ -10,10 +10,17 @@ import play.api.libs.json._
final class JsonView(getLightUser: String => Option[LightUser]) {
def apply(user: User, stat: PerfStat, rank: Option[Int]) = Json.obj(
def apply(
user: User,
stat: PerfStat,
rank: Option[Int],
ratingDistribution: List[Int]) = Json.obj(
"user" -> user,
"perf" -> user.perfs(stat.perfType),
"rank" -> rank,
"percentile" -> (lila.user.Stat.percentile(ratingDistribution, user.perfs(stat.perfType).intRating) match {
case (under, sum) => Math.round(under * 100.0 / sum)
}),
"stat" -> stat)
private def truncate(v: Double) = lila.common.Maths.truncateAt(v, 2)