2016-01-26 21:37:23 -07:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.data.Form
|
|
|
|
import play.api.i18n.Messages.Implicits._
|
2016-02-01 19:28:05 -07:00
|
|
|
import play.api.libs.json._
|
2016-02-04 09:10:23 -07:00
|
|
|
import play.api.mvc.{ Result, Cookie }
|
2016-01-26 21:37:23 -07:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
2016-01-28 05:04:55 -07:00
|
|
|
import lila.api.{ Context, BodyContext }
|
2016-01-26 21:37:23 -07:00
|
|
|
import lila.app._
|
2016-01-30 03:27:41 -07:00
|
|
|
import lila.challenge.{ Challenge => ChallengeModel }
|
2016-01-26 21:37:23 -07:00
|
|
|
import lila.common.{ HTTPRequest, LilaCookie }
|
2016-02-04 09:10:23 -07:00
|
|
|
import lila.game.{ GameRepo, AnonCookie }
|
2016-01-30 03:27:41 -07:00
|
|
|
import views.html
|
2016-01-26 21:37:23 -07:00
|
|
|
|
2016-01-26 22:56:30 -07:00
|
|
|
object Challenge extends LilaController {
|
2016-01-26 21:37:23 -07:00
|
|
|
|
2016-01-27 08:01:23 -07:00
|
|
|
private def env = Env.challenge
|
2016-01-26 21:37:23 -07:00
|
|
|
|
|
|
|
private val PostRateLimit = new lila.memo.RateLimit(5, 1 minute)
|
|
|
|
|
2016-01-27 08:01:23 -07:00
|
|
|
def all = Auth { implicit ctx =>
|
|
|
|
me =>
|
2016-02-03 01:26:22 -07:00
|
|
|
env.api allFor me.id map { all =>
|
|
|
|
Ok(env.jsonView(all)) as JSON
|
|
|
|
}
|
2016-01-28 05:04:55 -07:00
|
|
|
}
|
|
|
|
|
2016-02-03 01:26:22 -07:00
|
|
|
def show(id: String) = Open { implicit ctx =>
|
|
|
|
showId(id)
|
|
|
|
}
|
2016-01-30 19:04:27 -07:00
|
|
|
|
2016-02-03 01:26:22 -07:00
|
|
|
protected[controllers] def showId(id: String)(implicit ctx: Context): Fu[Result] =
|
|
|
|
OptionFuResult(env.api byId id)(showChallenge)
|
|
|
|
|
|
|
|
protected[controllers] def showChallenge(c: ChallengeModel)(implicit ctx: Context): Fu[Result] =
|
|
|
|
env version c.id flatMap { version =>
|
2016-02-07 02:07:54 -07:00
|
|
|
val mine = isMine(c)
|
|
|
|
import lila.challenge.Direction
|
|
|
|
val direction: Option[Direction] =
|
|
|
|
if (mine) Direction.Out.some
|
|
|
|
else if (isForMe(c)) Direction.In.some
|
|
|
|
else none
|
|
|
|
val json = env.jsonView.show(c, version, direction)
|
2016-02-07 03:10:49 -07:00
|
|
|
negotiate(
|
|
|
|
html = fuccess {
|
|
|
|
Ok(mine.fold(html.challenge.mine.apply _, html.challenge.theirs.apply _)(c, json))
|
|
|
|
},
|
|
|
|
api = _ => Ok(json).fuccess
|
|
|
|
) flatMap withChallengeAnonCookie(mine && c.challengerIsAnon, c, true)
|
2016-02-04 09:10:23 -07:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:27:41 -07:00
|
|
|
private def isMine(challenge: ChallengeModel)(implicit ctx: Context) = challenge.challenger match {
|
|
|
|
case Left(anon) => HTTPRequest sid ctx.req contains anon.secret
|
2016-01-28 05:04:55 -07:00
|
|
|
case Right(user) => ctx.userId contains user.id
|
2016-01-27 08:01:23 -07:00
|
|
|
}
|
2016-01-30 03:27:41 -07:00
|
|
|
|
2016-01-31 06:44:12 -07:00
|
|
|
private def isForMe(challenge: ChallengeModel)(implicit ctx: Context) =
|
|
|
|
challenge.destUserId.fold(true)(ctx.userId.contains)
|
|
|
|
|
|
|
|
def accept(id: String) = Open { implicit ctx =>
|
2016-02-03 01:26:22 -07:00
|
|
|
OptionFuResult(env.api byId id) { c =>
|
2016-02-03 17:29:30 -07:00
|
|
|
isForMe(c) ?? env.api.accept(c, ctx.me).flatMap {
|
|
|
|
case Some(pov) => negotiate(
|
2016-02-07 03:10:49 -07:00
|
|
|
html = Redirect(routes.Round.watcher(pov.game.id, "white")).fuccess,
|
|
|
|
api = apiVersion => Env.api.roundApi.player(pov, apiVersion) map { Ok(_) }
|
|
|
|
) flatMap withChallengeAnonCookie(ctx.isAnon, c, false)
|
2016-02-03 17:29:30 -07:00
|
|
|
case None => negotiate(
|
|
|
|
html = Redirect(routes.Round.watcher(c.id, "white")).fuccess,
|
|
|
|
api = _ => notFoundJson("Someone else accepted the challenge"))
|
2016-02-03 01:26:22 -07:00
|
|
|
}
|
2016-01-31 06:44:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 03:10:49 -07:00
|
|
|
private def withChallengeAnonCookie(cond: Boolean, c: ChallengeModel, owner: Boolean)(res: Result)(implicit ctx: Context): Fu[Result] =
|
|
|
|
cond ?? {
|
|
|
|
GameRepo.game(c.id).map {
|
|
|
|
_ map { game =>
|
|
|
|
implicit val req = ctx.req
|
|
|
|
LilaCookie.cookie(
|
|
|
|
AnonCookie.name,
|
|
|
|
game.player(owner.fold(c.finalColor, !c.finalColor)).id,
|
|
|
|
maxAge = AnonCookie.maxAge.some,
|
2016-02-19 22:17:25 -07:00
|
|
|
httpOnly = false.some)
|
2016-02-07 03:10:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} map { cookieOption =>
|
|
|
|
cookieOption.fold(res) { res.withCookies(_) }
|
|
|
|
}
|
|
|
|
|
2016-01-31 06:44:12 -07:00
|
|
|
def decline(id: String) = Auth { implicit ctx =>
|
|
|
|
me =>
|
2016-02-03 01:26:22 -07:00
|
|
|
OptionFuResult(env.api byId id) { c =>
|
|
|
|
if (isForMe(c)) env.api decline c
|
2016-01-31 06:44:12 -07:00
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:27:41 -07:00
|
|
|
def cancel(id: String) = Open { implicit ctx =>
|
2016-02-04 01:33:09 -07:00
|
|
|
OptionFuResult(env.api byId id) { c =>
|
|
|
|
if (isMine(c)) env.api cancel c
|
2016-01-30 03:27:41 -07:00
|
|
|
else notFound
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 19:28:05 -07:00
|
|
|
|
2016-02-05 07:52:13 -07:00
|
|
|
def rematchOf(gameId: String) = Auth { implicit ctx =>
|
|
|
|
me =>
|
2016-02-07 02:07:54 -07:00
|
|
|
OptionFuResult(GameRepo game gameId) { g =>
|
|
|
|
env.api.rematchOf(g, me) map {
|
|
|
|
_.fold(Ok, BadRequest)
|
|
|
|
}
|
2016-02-05 07:52:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 19:28:05 -07:00
|
|
|
def websocket(id: String, apiVersion: Int) = SocketOption[JsValue] { implicit ctx =>
|
|
|
|
env.api byId id flatMap {
|
|
|
|
_ ?? { c =>
|
|
|
|
get("sri") ?? { uid =>
|
|
|
|
env.socketHandler.join(id, uid, ctx.userId, isMine(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 21:37:23 -07:00
|
|
|
}
|