diff --git a/app/massivedecks/Config.scala b/app/massivedecks/Config.scala index 3d23f67..31b9cc9 100644 --- a/app/massivedecks/Config.scala +++ b/app/massivedecks/Config.scala @@ -1,17 +1,20 @@ package massivedecks -import play.Play +import javax.inject.Inject + +import play.api.Configuration import play.api.mvc.RequestHeader object Config { - val base = Play.application().configuration() - - def getString(key: String) = Option(base.getString(key)) - - def apply(request: RequestHeader) = new Config(request) + /** + * Factory for dependency injection. + */ + class Factory @Inject()(base: Configuration) { + def apply(request: RequestHeader) = new Config(base, request) + } } -class Config(request: RequestHeader) { - import Config._ +class Config(base: Configuration, request: RequestHeader) { + def getString(key: String) = base.getString(key) def protocol = getString("md_protocol").getOrElse { request.headers.get("X-Forwarded-Proto") match { diff --git a/app/massivedecks/controllers/API.scala b/app/massivedecks/controllers/API.scala index ad2cc19..e10ac74 100644 --- a/app/massivedecks/controllers/API.scala +++ b/app/massivedecks/controllers/API.scala @@ -2,8 +2,10 @@ package massivedecks.controllers import javax.inject.Inject +import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success, Try} +import akka.stream.scaladsl.{Flow, Sink, Source} import play.api.libs.json.{JsResult, JsValue, Json} import play.api.mvc._ import massivedecks.exceptions._ @@ -13,8 +15,9 @@ import massivedecks.models.Lobby.Formatters._ import massivedecks.models.Player import massivedecks.models.Player.Formatters._ import massivedecks.stores.LobbyStore +import play.api.libs.streams.Streams -class API @Inject()(store: LobbyStore) extends Controller { +class API @Inject()(store: LobbyStore) (implicit context: ExecutionContext) extends Controller { def createLobby() = Action { wrap { @@ -25,11 +28,26 @@ class API @Inject()(store: LobbyStore) extends Controller { } } - def notifications(gameCode: String) = WebSocket.using[String] { requestHeader => - store.performInLobby(gameCode) { lobby => - lobby.register() + def notifications(gameCode: String) = WebSocket.acceptOrResult[String, String] { requestHeader => Future { + Try { + store.performInLobby(gameCode) { lobby => + lobby.register() + } + } match { + case Success((iteratee, enumerator)) => + val (sub, _) = Streams.iterateeToSubscriber(iteratee) + val pub = Streams.enumeratorToPublisher(enumerator) + Right(Flow.fromSinkAndSource(Sink.fromSubscriber(sub), Source.fromPublisher(pub))) + + case Failure(error) => + error match { + case exception: RequestException => + Left(Status(exception.statusCode)(exception.details.toJson())) + case _ => + throw error + } } - } + } } def getLobby(gameCode: String) = Action { wrap(store.readFromLobby(gameCode) { lobby => diff --git a/app/massivedecks/controllers/ErrorHandler.scala b/app/massivedecks/controllers/ErrorHandler.scala index a5f1659..70687da 100644 --- a/app/massivedecks/controllers/ErrorHandler.scala +++ b/app/massivedecks/controllers/ErrorHandler.scala @@ -2,7 +2,7 @@ package massivedecks.controllers import java.net.URLEncoder import java.nio.charset.StandardCharsets -import javax.inject.Singleton +import javax.inject.{Inject, Singleton} import scala.concurrent.Future @@ -11,14 +11,13 @@ import play.api.http.HttpErrorHandler import play.api.mvc.Results._ import play.api.mvc._ import play.api.Logger - import massivedecks.Config @Singleton -class ErrorHandler extends HttpErrorHandler { +class ErrorHandler @Inject()(getConfig: Config.Factory) extends HttpErrorHandler { def onClientError(request: RequestHeader, statusCode: Int, rawMessage: String) = Future.successful { - val config = Config(request) + val config = getConfig(request) val (message, description) = statusCode match { case 400 => ("Bad Request", "there was a problem with the request you made") case 403 => ("Forbidden", ",you are not able to access the resource you requested") @@ -31,7 +30,7 @@ class ErrorHandler extends HttpErrorHandler { } def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = Future.successful { - val config = Config(request) + val config = getConfig(request) val description = "the server had a problem trying to handle your request" Logger.error(exception.getMessage) NotFound(views.html.error( diff --git a/app/massivedecks/controllers/Pages.scala b/app/massivedecks/controllers/Pages.scala index e24aa12..531588d 100644 --- a/app/massivedecks/controllers/Pages.scala +++ b/app/massivedecks/controllers/Pages.scala @@ -9,15 +9,15 @@ import play.api.Play.current import massivedecks.Config -class Pages @Inject() (cached: Cached) extends Controller { +class Pages @Inject() (cached: Cached, getConfig: Config.Factory) extends Controller { - def index() = Cached("index")(Action { request => - val config = Config(request) + def index() = cached("index")(Action { request => + val config = getConfig(request) Ok(views.html.index(config.url, config.version)).as(HTML) }) - def manifest() = Cached("manifest")(Action { request => - val config = Config(request) + def manifest() = cached("manifest")(Action { request => + val config = getConfig(request) val json = Json.obj( "name" -> "Massive Decks", "description" -> "An online party game inspired by Cards Against Humanity.", diff --git a/app/massivedecks/lobby/Game.scala b/app/massivedecks/lobby/Game.scala index b03fdbf..f560b57 100644 --- a/app/massivedecks/lobby/Game.scala +++ b/app/massivedecks/lobby/Game.scala @@ -87,6 +87,7 @@ class Game(players: Players, config: Config, notifiers: Notifiers) (implicit con /** * Begin the round. * + * @param firstRound If this is the first round in the game. * @throws BadRequestException with key "not-enough-players" if there are not enough players in the game to begin a * round. The value "required" gives the minimum number of players needed for the request * to succeed. diff --git a/project/build.properties b/project/build.properties index a6e117b..35c88ba 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.8 +sbt.version=0.13.12 diff --git a/project/plugins.sbt b/project/plugins.sbt index 7033808..b151e32 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ // The Play plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.4") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.4") // web plugins