lila/app/controllers/Setup.scala

152 lines
4.6 KiB
Scala
Raw Normal View History

2013-05-08 20:00:13 -06:00
package controllers
import lila.app._
import lila.game.GameRepo
2013-06-05 02:08:22 -06:00
import lila.user.UserRepo
import lila.user.{ Context, BodyContext }
2013-07-26 06:53:04 -06:00
import play.api.data.Form
import play.api.mvc.{ SimpleResult, Call }
2013-05-08 20:00:13 -06:00
import views._
object Setup extends LilaController with TheftPrevention {
2013-05-08 20:00:13 -06:00
private def env = Env.setup
def aiForm = Open { implicit ctx
env.forms aiFilled get("fen") map { html.setup.ai(_) }
}
def ai = process(env.forms.ai) { config
implicit ctx
env.processor ai config map { pov
routes.Round.player(pov.fullId)
}
}
2013-06-05 05:55:16 -06:00
def friendForm(username: Option[String]) = Open { implicit ctx
2013-06-05 06:10:01 -06:00
username ?? UserRepo.named flatMap { userOption
(userOption |@| ctx.me).tupled ?? {
case (user, me) Env.relation.api.blocks(user.id, me.id) map { blocks
!blocks option user
}
}
} flatMap { user
env.forms friendFilled get("fen") map {
html.setup.friend(_, user map (_.username))
}
2013-06-05 05:55:16 -06:00
}
2013-05-08 20:00:13 -06:00
}
2013-06-05 05:55:16 -06:00
def friend(username: Option[String]) = process(env.forms.friend) { config
2013-05-08 20:00:13 -06:00
implicit ctx
env.processor friend config map { pov
2013-06-05 05:55:16 -06:00
routes.Setup.await(pov.fullId, username)
2013-05-08 20:00:13 -06:00
}
}
2013-06-05 06:54:33 -06:00
def decline(gameId: String) = Auth { implicit ctx
me
OptionFuResult(GameRepo game gameId) { game
game.started.fold(
BadRequest("Cannot decline started challenge").fuccess,
(GameRepo remove game.id) >>
(Env.bookmark.api removeByGame game) >>-
(Env.hub.actor.challenger ! lila.hub.actorApi.setup.DeclineChallenge(gameId)) map { Ok(_) }
)
}
}
2013-05-08 20:00:13 -06:00
def hookForm = Open { implicit ctx
env.forms.hookFilled map { html.setup.hook(_) }
}
2013-05-18 21:31:06 -06:00
def hook(uid: String) = OpenBody { implicit ctx
implicit val req = ctx.body
env.forms.hook(ctx).bindFromRequest.value ?? { config
2013-09-16 10:48:15 -06:00
env.processor.hook(config, uid, lila.common.HTTPRequest sid req, ctx.me)
2013-05-18 21:31:06 -06:00
}
2013-05-08 20:00:13 -06:00
}
def filterForm = Open { implicit ctx
2013-07-26 06:53:04 -06:00
env.forms.filterFilled map {
case (form, filter) html.setup.filter(form, filter)
}
2013-05-08 20:00:13 -06:00
}
def filter = OpenBody { implicit ctx
implicit val req = ctx.body
env.forms.filter(ctx).bindFromRequest.fold[Fu[SimpleResult]](
2013-05-08 20:00:13 -06:00
f fulogwarn(f.errors.toString) inject BadRequest(),
config JsonOk(env.processor filter config inject config.render)
2013-05-08 20:00:13 -06:00
)
}
def join(id: String) = Open { implicit ctx
OptionFuResult(GameRepo game id) { game
env.friendJoiner(game, ctx.me).fold(
err fulogwarn("setup join " + err) inject
Redirect(routes.Round.watcher(id, game.creatorColor.name)),
_ map {
case (p, events) {
2013-06-02 07:00:27 -06:00
Env.round.roundMap ! lila.hub.actorApi.map.Tell(p.gameId, lila.round.actorApi.round.Send(events))
2013-05-08 20:00:13 -06:00
Redirect(routes.Round.player(p.fullId))
}
})
}
}
2013-06-05 05:55:16 -06:00
def await(fullId: String, username: Option[String]) = Open { implicit ctx
2013-05-08 20:00:13 -06:00
OptionFuResult(GameRepo pov fullId) { pov
pov.game.started.fold(
Redirect(routes.Round.player(pov.fullId)).fuccess,
2013-06-05 05:55:16 -06:00
Env.round.version(pov.gameId) zip
(username ?? UserRepo.named) flatMap {
case (version, user) PreventTheft(pov) {
Ok(html.setup.await(
pov,
version,
env.friendConfigMemo get pov.game.id,
user)).fuccess
}
2013-05-08 20:00:13 -06:00
}
)
}
}
def cancel(fullId: String) = Open { implicit ctx
OptionFuResult(GameRepo pov fullId) { pov
pov.game.started.fold(
Redirect(routes.Round.player(pov.fullId)).fuccess,
(GameRepo remove pov.game.id) >>
(Env.bookmark.api removeByGame pov.game) inject
Redirect(routes.Lobby.home)
)
}
}
def api = Open { implicit ctx
JsonOk(env.processor.api)
}
def validateFen = Open { implicit ctx
{
for {
fen get("fen")
parsed chess.format.Forsyth <<< fen
strict = get("strict").isDefined
if (parsed.situation playable strict)
validated = chess.format.Forsyth >> parsed
} yield html.game.miniBoard(validated, parsed.situation.color.name)
}.fold[SimpleResult](BadRequest)(Ok(_)).fuccess
2013-05-08 20:00:13 -06:00
}
private def process[A](form: Context Form[A])(op: A BodyContext Fu[Call]) =
OpenBody { ctx
implicit val req = ctx.body
FuRedirect(form(ctx).bindFromRequest.fold(
f fulogwarn(f.errors.toString) inject routes.Lobby.home,
config op(config)(ctx)
))
}
}