async homepage
This commit is contained in:
parent
a1bdf721ba
commit
0e736074a1
|
@ -7,9 +7,11 @@ import views._
|
|||
|
||||
import play.api.mvc._
|
||||
import play.api.libs.json.JsValue
|
||||
import play.api.libs.concurrent._
|
||||
import akka.dispatch.Future
|
||||
import scalaz.effects._
|
||||
|
||||
object Lobby extends LilaController {
|
||||
object Lobby extends LilaController with Results {
|
||||
|
||||
def preloader = env.lobby.preloader
|
||||
def hookRepo = env.lobby.hookRepo
|
||||
|
@ -21,28 +23,33 @@ object Lobby extends LilaController {
|
|||
def featured = env.game.featured
|
||||
|
||||
val home = Open { implicit ctx ⇒
|
||||
renderHome(none).fold(identity, Ok(_))
|
||||
Async {
|
||||
renderHome(none, Ok)
|
||||
}
|
||||
}
|
||||
|
||||
def handleNotFound(req: RequestHeader): Result =
|
||||
handleNotFound(reqToCtx(req))
|
||||
|
||||
def handleNotFound(implicit ctx: Context): Result =
|
||||
renderHome(none).fold(identity, NotFound(_))
|
||||
Async {
|
||||
renderHome(none, NotFound)
|
||||
}
|
||||
|
||||
private def renderHome(myHook: Option[Hook])(implicit ctx: Context) = preloader(
|
||||
auth = ctx.isAuth,
|
||||
chat = ctx.canSeeChat,
|
||||
myHook = myHook,
|
||||
timeline = timelineRecent
|
||||
).unsafePerformIO.bimap(
|
||||
url ⇒ Redirect(url),
|
||||
preload ⇒ html.lobby.home(
|
||||
toJson(preload),
|
||||
myHook,
|
||||
forumRecent(ctx.me),
|
||||
featured.one)
|
||||
)
|
||||
private def renderHome[A](myHook: Option[Hook], status: Status)(implicit ctx: Context): Promise[Result] =
|
||||
preloader(
|
||||
auth = ctx.isAuth,
|
||||
chat = ctx.canSeeChat,
|
||||
myHook = myHook,
|
||||
timeline = timelineRecent
|
||||
).map(_.fold(
|
||||
url ⇒ Redirect(url),
|
||||
preload ⇒ status(html.lobby.home(
|
||||
toJson(preload),
|
||||
myHook,
|
||||
forumRecent(ctx.me),
|
||||
featured.one))
|
||||
)).asPromise
|
||||
|
||||
def socket = WebSocket.async[JsValue] { implicit req ⇒
|
||||
implicit val ctx = reqToCtx(req)
|
||||
|
@ -55,9 +62,14 @@ object Lobby extends LilaController {
|
|||
}
|
||||
|
||||
def hook(ownerId: String) = Open { implicit ctx ⇒
|
||||
hookRepo.ownedHook(ownerId).unsafePerformIO.fold(
|
||||
hook ⇒ renderHome(hook.some).fold(identity, Ok(_)),
|
||||
Redirect(routes.Lobby.home))
|
||||
Async {
|
||||
hookRepo.ownedHook(ownerId).unsafePerformIO.fold(
|
||||
hook ⇒ renderHome(hook.some, Ok),
|
||||
Promise.pure {
|
||||
Redirect(routes.Lobby.home)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def join(hookId: String) = Open { implicit ctx ⇒
|
||||
|
|
|
@ -6,6 +6,11 @@ import game.DbGame
|
|||
import controllers.routes
|
||||
|
||||
import play.api.mvc.Call
|
||||
import play.api.libs.concurrent.Akka
|
||||
import play.api.Play.current
|
||||
import akka.dispatch.Future
|
||||
import akka.util.duration._
|
||||
import akka.util.Timeout
|
||||
import scalaz.effects._
|
||||
|
||||
final class Preload(
|
||||
|
@ -15,31 +20,49 @@ final class Preload(
|
|||
getGame: String ⇒ IO[Option[DbGame]],
|
||||
messageRepo: MessageRepo) {
|
||||
|
||||
type Response = Either[Call, Map[String, Any]]
|
||||
private implicit val executor = Akka.system.dispatcher
|
||||
private implicit val timeout = Timeout(1 second)
|
||||
private type Response = Either[Call, Map[String, Any]]
|
||||
|
||||
def apply(
|
||||
auth: Boolean,
|
||||
chat: Boolean,
|
||||
myHook: Option[Hook],
|
||||
timeline: IO[List[Entry]]): IO[Response] = for {
|
||||
hooks ← auth.fold(hookRepo.allOpen, hookRepo.allOpenCasual)
|
||||
res ← myHook.flatMap(_.gameId).fold(
|
||||
gid ⇒ getGame(gid) map { game ⇒
|
||||
Left(game.fold(
|
||||
g ⇒ routes.Round.player(g fullIdOf g.creatorColor),
|
||||
routes.Lobby.home()
|
||||
))
|
||||
}, for {
|
||||
messages ← chat.fold(messageRepo.recent, io(Nil))
|
||||
entries ← timeline
|
||||
timeline: IO[List[Entry]]): Future[Response] =
|
||||
myHook.flatMap(_.gameId).fold(
|
||||
hookBitten,
|
||||
for {
|
||||
hooks ← futureHooks(auth)
|
||||
messages ← futureMessages(chat)
|
||||
entries ← ioToFuture(timeline)
|
||||
} yield Right(Map(
|
||||
"version" -> history.version,
|
||||
"pool" -> renderHooks(hooks, myHook),
|
||||
"chat" -> (messages.reverse map (_.render)),
|
||||
"timeline" -> (entries.reverse map (_.render))
|
||||
))
|
||||
)): Response
|
||||
)
|
||||
} yield res
|
||||
|
||||
private def futureHooks(auth: Boolean): Future[List[Hook]] = ioToFuture {
|
||||
auth.fold(hookRepo.allOpen, hookRepo.allOpenCasual)
|
||||
}
|
||||
|
||||
private def futureMessages(chat: Boolean) = ioToFuture {
|
||||
chat.fold(messageRepo.recent, io(Nil))
|
||||
}
|
||||
|
||||
private def hookBitten(gameId: String): Future[Response] = ioToFuture {
|
||||
getGame(gameId) map { game ⇒
|
||||
Left(game.fold(
|
||||
g ⇒ routes.Round.player(g fullIdOf g.creatorColor),
|
||||
routes.Lobby.home()
|
||||
)): Response
|
||||
}
|
||||
}
|
||||
|
||||
private def ioToFuture[A](ioa: IO[A]): Future[A] = Future {
|
||||
ioa.unsafePerformIO
|
||||
}
|
||||
|
||||
private def renderHooks(
|
||||
hooks: List[Hook],
|
||||
|
|
|
@ -48,7 +48,6 @@ final class Reporting(
|
|||
val memoryStats = ManagementFactory.getMemoryMXBean
|
||||
val cpuStats = new CPU()
|
||||
implicit val executor = Akka.system.dispatcher
|
||||
|
||||
implicit val timeout = Timeout(100 millis)
|
||||
|
||||
def receive = {
|
||||
|
|
Loading…
Reference in a new issue