improve puzzle API

pull/83/head
Thibault Duplessis 2014-04-20 22:40:49 +02:00
parent ebce8d0765
commit 81f4747c4a
5 changed files with 33 additions and 19 deletions

View File

@ -222,10 +222,13 @@ name | type | default | description
```javascript
{
"id": 23045,
"position": "r2q1rk1/1pn2p1p/p1pp1bpQ/8/2PNR3/1P6/PB3PPP/4R1K1 b - - 1 21", // FEN initial position
"solution": ["h6g7", "d4f5", "f5h6"], // solution moves
"rating": 1639 // puzzle glicko2 rating
"id": 16177,
"url": "http://lichess.org/training/16177", // URL of the puzzle
"color": "black", // color of the player
"position": "6NK/5k2/2r5/2n3PP/8/8/8/8 w - - 7 39", // FEN initial position
"solution": ["c6h6", "g5h6", "c5e6", "h8h7", "e6g5",
"h7h8", "f7f8", "h6h7", "g5f7"], // solution moves
"rating": 1799 // puzzle glicko2 rating
}
```
@ -237,10 +240,13 @@ name | type | default | description
```javascript
{
"id": 23045,
"position": "r2q1rk1/1pn2p1p/p1pp1bpQ/8/2PNR3/1P6/PB3PPP/4R1K1 b - - 1 21", // FEN initial position
"solution": ["h6g7", "d4f5", "f5h6"], // solution moves
"rating": 1639 // puzzle glicko2 rating
"id": 16177,
"url": "http://lichess.org/training/16177", // URL of the puzzle
"color": "black", // color of the player
"position": "6NK/5k2/2r5/2n3PP/8/8/8/8 w - - 7 39", // FEN initial position
"solution": ["c6h6", "g5h6", "c5e6", "h8h7", "e6g5",
"h7h8", "f7f8", "h6h7", "g5f7"], // solution moves
"rating": 1799 // puzzle glicko2 rating
}
```

View File

@ -4,9 +4,9 @@ package actor
import lila.hub.actorApi.router._
import lila.i18n.I18nDomain
import controllers.{ routes => R }
import akka.actor._
import akka.pattern.{ ask, pipe }
import controllers.{ routes => R }
// returns String urls, not Call objects
private[app] final class Router(
@ -33,6 +33,9 @@ private[app] final class Router(
case Watcher(gameId, color) => sender ! R.Round.watcher(gameId, color).url
case Pgn(gameId) => sender ! R.Analyse.pgn(gameId).url
case Tourney(tourId) => sender ! R.Tournament.show(tourId).url
case Puzzle(id) => sender ! R.Puzzle.show(id).url
case msg => sender ! Status.Failure(new Exception(s"No route for $msg"))
}
private lazy val noLangBaseUrl = protocol + I18nDomain(domain).commonDomain

View File

@ -12,12 +12,18 @@ private[api] final class PuzzleApi(env: lila.puzzle.Env, makeUrl: Any => Fu[Stri
case None => fuccess(none)
}
def one(id: Int): Fu[Option[JsObject]] = env.api.puzzle find id map2 toJson
def one(id: Int): Fu[Option[JsObject]] = env.api.puzzle find id flatMap {
case None => fuccess(none)
case Some(p) => toJson(p) map (_.some)
}
private def toJson(p: Puzzle) = Json.obj(
"id" -> p.id,
"position" -> p.fen,
"solution" -> Line.solution(p.lines),
"rating" -> p.perf.intRating)
private def toJson(p: Puzzle) = makeUrl(R Puzzle p.id) map { url =>
Json.obj(
"id" -> p.id,
"url" -> url,
"color" -> p.color.name,
"position" -> p.fen,
"solution" -> Line.solution(p.lines),
"rating" -> p.perf.intRating)
}
}

View File

@ -115,6 +115,7 @@ case class Player(fullId: String)
case class Watcher(gameId: String, color: String)
case class Pgn(gameId: String)
case class Tourney(tourId: String)
case class Puzzle(id: Int)
}
package forum {

View File

@ -48,9 +48,7 @@ object Line {
loop(lines collect {
case Node(move, _) => List(move)
}).zipWithIndex.collect {
case (move, i) if i % 2 == 0 => move
}
})
}
def toString(lines: Lines, level: Int = 0): String = {