Skip to content

Commit

Permalink
improve APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Aug 13, 2014
1 parent 29252f7 commit 345222c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,16 @@ name | type | default | description
{
"list": [
{
"id": "x2kpaixn",
"rated": false,
"id": "39b12Ikl",
"variant": "chess960", // standard/chess960/fromPosition/kingOfTheHill/threeCheck
"speed": "blitz", // bullet|blitz|classical|unlimited
"perf": "chess960", // bullet|blitz|classical|chess960|kingOfTheHill|threeCheck
"rated": true,
"status": "mate", // (1)
"clock":{ // all clock values are expressed in seconds
"limit": 300,
"initial": 300,
"increment": 8,
"totalTime": 540 // evaluation of the game duration = limit + 30 * increment
"totalTime": 540 // evaluation of the game duration = initial + 30 * increment
},
"timestamp": 1389100907239,
"turns": 44,
Expand Down Expand Up @@ -248,13 +251,16 @@ name | type | default | description

```javascript
{
"id": "x2kpaixn",
"rated": false,
"id": "39b12Ikl",
"variant": "chess960", // standard/chess960/fromPosition/kingofthehill/threeCheck
"speed": "blitz", // bullet|blitz|classical|unlimited
"perf": "chess960", // bullet|blitz|classical|chess960|kingOfTheHill|threeCheck
"rated": true,
"status": "mate", // (1)
"clock":{ // all clock values are expressed in seconds
"limit": 300,
"initial": 300,
"increment": 8,
"totalTime": 540 // evaluation of the game duration = limit + 30 * increment
"totalTime": 540 // evaluation of the game duration = initial + 30 * increment
},
"timestamp": 1389100907239,
"turns": 44,
Expand Down
35 changes: 24 additions & 11 deletions doc/mobile-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ http --form POST en.l.org/setup/ai variant=1 clock=false time=60 increment=60 le
```
- level: 1 to 8
- color: white | black | random
- variant: 1 (standard) | 2 (chess960) | 3 (from position) | 4 (KotH)
- variant: 1 (standard) | 2 (chess960) | 3 (from position) | 4 (KotH) | 5 (three-check)
- fen: if variant is 3, any valid FEN string

Response: `201 CREATED`
```javascript
{
"game": {
"id": "39b12Ikl",
"variant": "chess960", // standard/chess960/fromPosition/kingOfTheHill/threeCheck
"speed": "blitz", // bullet|blitz|classical|unlimited
"perf": "chess960", // bullet|blitz|classical|chess960|kingOfTheHill|threeCheck
"rated": true,
"clock": false,
"clockRunning": false,
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"finished": false,
"id": "39b12Ikl",
"lastMove": null,
"moves": "",
"player": "white",
Expand All @@ -32,9 +36,11 @@ Response: `201 CREATED`
},
"clock": {
// all durations are expressed in seconds
"black": 3600.0,
"emerg": 30, // critical threshold
"white": 3600.0
"initial": 300, // initial time of the clock, here 5 minutes
"increment": 8, // fisher increment
"black": 36.0, // current time left for black
"white": 78.0, // current time left for white
"emerg": 30 // critical threshold
},
"player": {
"color": "white",
Expand All @@ -43,8 +49,9 @@ Response: `201 CREATED`
"version": 0
},
"opponent": {
"ai": true,
"color": "black"
"color": "black",
"ai": false,
"user_id": "ozzie" // request more info at /api/user/ozzie
},
"possibleMoves": { // list of moves you can play. Empty if not your turn to play.
"a2": "a3a4", // from a2, you can go on a3 or a4.
Expand Down Expand Up @@ -123,11 +130,15 @@ Response: `200 OK`
```javascript
{
"game": {
"id": "39b12Ikl",
"variant": "chess960", // standard/chess960/fromPosition/kingOfTheHill/threeCheck
"speed": "blitz", // bullet|blitz|classical|unlimited
"perf": "chess960", // bullet|blitz|classical|chess960|kingOfTheHill|threeCheck
"rated": true,
"clock": false,
"clockRunning": false,
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"finished": false,
"id": "39b12Ikl",
"lastMove": null,
"moves": "e4 d5 exd5 Qxd5 Nc3",
"player": "white",
Expand All @@ -137,9 +148,11 @@ Response: `200 OK`
},
"clock": {
// all durations are expressed in seconds
"black": 3600.0,
"emerg": 30, // critical threshold
"white": 3600.0
"initial": 300, // initial time of the clock, here 5 minutes
"increment": 8, // fisher increment
"black": 36.0, // current time left for black
"white": 78.0, // current time left for white
"emerg": 30 // critical threshold
},
"player": {
"color": "white",
Expand Down
4 changes: 2 additions & 2 deletions modules/api/src/main/GameApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private[api] final class GameApi(
G.playerUids -> username,
G.rated -> rated.map(_.fold(JsBoolean(true), $exists(false))),
G.analysed -> analysed.map(_.fold(JsBoolean(true), $exists(false))),
G.variant -> check(token).option($in(Game.analysableVariants.map(_.id)))
G.variant -> check(token).option($nin(Game.unanalysableVariants.map(_.id)))
).noNull) sort lila.game.Query.sortCreated, makeNb(token, nb)) flatMap
gamesJson(withAnalysis, token) map { games =>
Json.obj("list" -> games)
Expand Down Expand Up @@ -80,7 +80,7 @@ private[api] final class GameApi(
"status" -> g.status.name.toLowerCase,
"clock" -> g.clock.map { clock =>
Json.obj(
"limit" -> clock.limit,
"initial" -> clock.limit,
"increment" -> clock.increment,
"totalTime" -> clock.estimateTotalTime
)
Expand Down
1 change: 1 addition & 0 deletions modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ object Game {


val analysableVariants: Set[Variant] = Set(Variant.Standard, Variant.Chess960, Variant.KingOfTheHill)
val unanalysableVariants: Set[Variant] = Variant.all.toSet -- analysableVariants

val gameIdSize = 8
val playerIdSize = 4
Expand Down
15 changes: 13 additions & 2 deletions modules/round/src/main/JsonView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.math.{ min, max, round }

import play.api.libs.json.Json

import lila.game.{ Pov, Game }
import lila.game.{ Pov, Game, PerfPicker }
import lila.pref.Pref

import chess.format.Forsyth
Expand All @@ -18,6 +18,10 @@ final class JsonView(baseAnimationDelay: Duration) {
Json.obj(
"game" -> Json.obj(
"id" -> gameId,
"variant" -> game.variant.key,
"speed" -> game.speed.key,
"perf" -> PerfPicker.key(game),
"rated" -> game.rated,
"fen" -> (Forsyth >> game.toChess),
"moves" -> game.pgnMoves.mkString(" "),
"started" -> game.started,
Expand All @@ -37,7 +41,8 @@ final class JsonView(baseAnimationDelay: Duration) {
),
"opponent" -> Json.obj(
"color" -> opponent.color.name,
"ai" -> opponent.isAi
"ai" -> opponent.isAi,
"userId" -> opponent.userId
),
"url" -> Json.obj(
"pov" -> s"/$fullId",
Expand All @@ -64,6 +69,10 @@ final class JsonView(baseAnimationDelay: Duration) {
Json.obj(
"game" -> Json.obj(
"id" -> gameId,
"variant" -> game.variant.key,
"speed" -> game.speed.key,
"perf" -> PerfPicker.key(game),
"rated" -> game.rated,
"started" -> game.started,
"finished" -> game.finishedOrAborted,
"clock" -> game.hasClock,
Expand Down Expand Up @@ -95,6 +104,8 @@ final class JsonView(baseAnimationDelay: Duration) {
}

private def clockJson(clock: Clock) = Json.obj(
"initial" -> clock.limit,
"increment" -> clock.increment,
"white" -> clock.remainingTime(Color.White),
"black" -> clock.remainingTime(Color.Black),
"emerg" -> clock.emergTime
Expand Down
2 changes: 1 addition & 1 deletion modules/user/src/main/Cached.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Cached(

val topPerf = AsyncCache[Perf.Key, List[User]](
f = (perf: Perf.Key) => UserRepo.topPerfSince(perf, twoWeeksAgo, leaderboardSize),
timeToLive = 30 minutes)
timeToLive = 10 minutes)

val topToday = AsyncCache.single[List[(User, PerfType)]](
f = perfs.map { perf =>
Expand Down

0 comments on commit 345222c

Please sign in to comment.