2013-03-18 17:36:22 -06:00
|
|
|
package controllers
|
|
|
|
|
2013-05-08 15:34:37 -06:00
|
|
|
import lila.app._
|
2013-03-20 10:34:38 -06:00
|
|
|
import lila.user.Context
|
2013-05-08 15:34:37 -06:00
|
|
|
import lila.common.LilaCookie
|
2013-05-08 19:15:34 -06:00
|
|
|
import lila.lobby.{ Hook, HookRepo, MessageRepo }
|
2013-05-08 15:34:37 -06:00
|
|
|
import lila.tournament.Created
|
|
|
|
import views._
|
2013-03-18 17:36:22 -06:00
|
|
|
|
2013-05-08 15:34:37 -06:00
|
|
|
import play.api.mvc._
|
|
|
|
import play.api.libs.json.JsValue
|
|
|
|
import play.api.libs.json.Json
|
2013-03-18 17:36:22 -06:00
|
|
|
|
2013-05-08 15:34:37 -06:00
|
|
|
object Lobby extends LilaController with Results {
|
2013-03-18 17:36:22 -06:00
|
|
|
|
2013-05-08 15:34:37 -06:00
|
|
|
// private def openTours = Env.tournament.repo.created
|
|
|
|
private def openTours = fuccess(List[Created]())
|
|
|
|
|
|
|
|
def home = Open { implicit ctx ⇒
|
|
|
|
renderHome(none, Ok).map(_.withHeaders(
|
|
|
|
CACHE_CONTROL -> "no-cache", PRAGMA -> "no-cache"
|
|
|
|
))
|
|
|
|
}
|
2013-03-20 10:34:38 -06:00
|
|
|
|
|
|
|
def handleNotFound(req: RequestHeader): Fu[Result] =
|
2013-05-08 15:34:37 -06:00
|
|
|
reqToCtx(req) flatMap { ctx ⇒ handleNotFound(ctx) }
|
2013-03-20 10:34:38 -06:00
|
|
|
|
|
|
|
def handleNotFound(implicit ctx: Context): Fu[Result] =
|
2013-05-08 15:34:37 -06:00
|
|
|
renderHome(none, NotFound)
|
|
|
|
|
|
|
|
private def renderHome[A](myHook: Option[Hook], status: Status)(implicit ctx: Context): Fu[Result] =
|
|
|
|
Env.current.preloader(
|
|
|
|
auth = ctx.isAuth,
|
|
|
|
chat = ctx.canSeeChat,
|
|
|
|
myHook = myHook,
|
|
|
|
timeline = Env.timeline.recent,
|
|
|
|
posts = Env.forum.recent(ctx.me, Env.team.cached.teamIds.apply),
|
|
|
|
tours = openTours,
|
2013-05-08 19:15:34 -06:00
|
|
|
filter = Env.setup.filter
|
2013-05-08 15:34:37 -06:00
|
|
|
).map(_.fold(Redirect(_), {
|
|
|
|
case (preload, posts, tours, featured) ⇒ status(html.lobby.home(
|
|
|
|
Json stringify preload,
|
|
|
|
myHook,
|
|
|
|
posts,
|
|
|
|
tours,
|
|
|
|
featured)) |> { response ⇒
|
|
|
|
ctx.req.session.data.contains(LilaCookie.sessionId).fold(
|
|
|
|
response,
|
|
|
|
response withCookies LilaCookie.makeSessionId(ctx.req)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
def socket = WebSocket.async[JsValue] { implicit req ⇒
|
2013-05-08 19:15:34 -06:00
|
|
|
reqToCtx(req) flatMap { implicit ctx ⇒
|
|
|
|
(get("sri") |@| getInt("version")).tupled zmap {
|
|
|
|
case (uid, version) ⇒ Env.lobby.socketHandler(
|
|
|
|
uid = uid,
|
|
|
|
user = ctx.me,
|
|
|
|
version = version,
|
|
|
|
hook = get("hook"))
|
|
|
|
}
|
2013-05-08 15:34:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def hook(ownerId: String) = Open { implicit ctx ⇒
|
2013-05-08 19:15:34 -06:00
|
|
|
HookRepo.ownedHook(ownerId) flatMap {
|
2013-05-08 15:34:37 -06:00
|
|
|
_.fold(Redirect(routes.Lobby.home).fuccess) { hook ⇒
|
|
|
|
renderHome(hook.some, Ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def join(hookId: String) = Open { implicit ctx ⇒
|
|
|
|
val myHookId = get("cancel")
|
|
|
|
Env.setup.hookJoiner(hookId, myHookId)(ctx.me) map { result ⇒
|
|
|
|
Redirect {
|
|
|
|
result.fold(
|
|
|
|
_ ⇒ myHookId.fold(routes.Lobby.home)(routes.Lobby.hook(_)),
|
|
|
|
pov ⇒ routes.Round.player(pov.fullId))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def cancel(ownerId: String) = Open { implicit ctx ⇒
|
2013-05-08 19:15:34 -06:00
|
|
|
HookRepo ownedHook ownerId flatMap {
|
2013-05-08 15:34:37 -06:00
|
|
|
_ zmap Env.lobby.fisherman.delete inject Redirect(routes.Lobby.home)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def log = Open { implicit ctx ⇒
|
|
|
|
MessageRepo.all map { html.lobby.log(_) }
|
|
|
|
}
|
2013-03-18 17:36:22 -06:00
|
|
|
}
|