Skip to content

Instantly share code, notes, and snippets.

@LukasGasior1
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukasGasior1/699439c80d69e3a8d961 to your computer and use it in GitHub Desktop.
Save LukasGasior1/699439c80d69e3a8d961 to your computer and use it in GitHub Desktop.
def createGame() = Action.async { request =>
WS.url(s"$apiUrl/game")
.post("")
.map {
case res if res.status == CREATED => Created(res.body)
case _ => InternalServerError
}
.recover { case _ => InternalServerError }
}
def startGame(gameId: String, playersCount: Int) = Action.async { request =>
val players = 1 to playersCount map { n => s"Player$n"}
postCommand(
url = s"/game/$gameId/start",
data = Json.obj("players" -> players))
}
def roll(gameId: String, playerId: String) = Action.async { request =>
postCommand(
url = s"/game/$gameId/roll/$playerId",
data = Json.obj())
}
private def postCommand(url: String, data: JsValue) = {
WS.url(s"$apiUrl$url")
.post(data)
.map {
case res if res.status == ACCEPTED => Accepted
case res if res.status == BAD_REQUEST => BadRequest(res.body)
case res => InternalServerError
}
.recover { case _ => InternalServerError }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment