Skip to content

Commit

Permalink
expose puzzle dashboard through oAuth API
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jan 21, 2021
1 parent 458ef12 commit 1313b91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app/controllers/Puzzle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ final class Puzzle(
.fuccess
}

def dashboard(days: Int) =
Scoped(_.Puzzle.Read) { _ => me =>
JsonOk {
env.puzzle.dashboard(me, days) map env.puzzle.json.dashboardJson map
def apiDashboard(days: Int) =
Scoped(_.Puzzle.Read) { implicit req => me =>
implicit val lang = reqLang
JsonOptionOk {
env.puzzle.dashboard(me, days) map2 { env.puzzle.jsonView.dashboardJson(_, days) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ GET /api controllers.Api.index
POST /api/users controllers.Api.usersByIds
GET /api/user/puzzle-activity controllers.Puzzle.activity
GET /api/puzzle/activity controllers.Puzzle.activity
GET /api/puzzle/dashboard controllers.Puzzle.dashboard
GET /api/puzzle/dashboard/:days controllers.Puzzle.apiDashboard(days: Int)
GET /api/user/:name/tournament/created controllers.Api.tournamentsByOwner(name: String)
GET /api/user/:name controllers.Api.user(name: String)
GET /api/user/:name/activity controllers.Api.activity(name: String)
Expand Down
19 changes: 19 additions & 0 deletions modules/puzzle/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ final class JsonView(
"is3d" -> p.is3d
)

def dashboardJson(dash: PuzzleDashboard, days: Int)(implicit lang: Lang) = Json.obj(
"days" -> days,
"global" -> dashboardResults(dash.global),
"themes" -> JsObject(dash.byTheme.toList.sortBy(-_._2.nb).map { case (key, res) =>
key.value -> Json.obj(
"theme" -> PuzzleTheme(key).name.txt(),
"results" -> dashboardResults(res)
)
})
)

private def dashboardResults(res: PuzzleDashboard.Results) = Json.obj(
"nb" -> res.nb,
"firstWins" -> res.wins,
"replayWins" -> res.fixed,
"puzzleRatingAvg" -> res.puzzleRatingAvg,
"performance" -> res.performance
)

private def puzzleJson(puzzle: Puzzle): JsObject = Json.obj(
"id" -> puzzle.id,
"rating" -> puzzle.glicko.intRating,
Expand Down

0 comments on commit 1313b91

Please sign in to comment.