improve seek limit

This commit is contained in:
Thibault Duplessis 2016-07-17 14:36:15 +02:00
parent 324897c71f
commit fa9dfe62cc
2 changed files with 28 additions and 17 deletions

View file

@ -11,6 +11,7 @@ import lila.api.{ Context, BodyContext }
import lila.app._
import lila.common.{ HTTPRequest, LilaCookie }
import lila.game.{ GameRepo, Pov, AnonCookie }
import lila.setup.Processor.HookResult
import lila.setup.{ HookConfig, ValidFen }
import lila.user.UserRepo
import views._
@ -108,10 +109,14 @@ object Setup extends LilaController with TheftPrevention {
}
}
private def hookResponse(hookId: String) =
Ok(Json.obj(
private def hookResponse(res: HookResult) = res match {
case HookResult.Created(id) => Ok(Json.obj(
"ok" -> true,
"hook" -> Json.obj("id" -> hookId))) as JSON
"hook" -> Json.obj("id" -> id))) as JSON
case HookResult.Refused => BadRequest(jsonError("Game was not created"))
}
private val hookRefused = BadRequest(jsonError("Game was not created"))
def hook(uid: String) = OpenBody { implicit ctx =>
implicit val req = ctx.body
@ -123,9 +128,7 @@ object Setup extends LilaController with TheftPrevention {
api = _ => BadRequest(errorsAsJson(err)).fuccess),
config => (ctx.userId ?? Env.relation.api.fetchBlocking) flatMap {
blocking =>
env.processor.hook(config, uid, HTTPRequest sid req, blocking) map hookResponse recover {
case e: IllegalArgumentException => BadRequest(jsonError(e.getMessage)) as JSON
}
env.processor.hook(config, uid, HTTPRequest sid req, blocking) map hookResponse
}
)
}
@ -140,9 +143,7 @@ object Setup extends LilaController with TheftPrevention {
_.fold(config)(config.updateFrom)
} flatMap { config =>
(ctx.userId ?? Env.relation.api.fetchBlocking) flatMap { blocking =>
env.processor.hook(config, uid, HTTPRequest sid ctx.req, blocking) map hookResponse recover {
case e: IllegalArgumentException => BadRequest(jsonError(e.getMessage)) as JSON
}
env.processor.hook(config, uid, HTTPRequest sid ctx.req, blocking) map hookResponse
}
}
}

View file

@ -42,23 +42,24 @@ private[setup] final class Processor(
configBase: HookConfig,
uid: String,
sid: Option[String],
blocking: Set[String])(implicit ctx: UserContext): Fu[String] = {
blocking: Set[String])(implicit ctx: UserContext): Fu[Processor.HookResult] = {
import Processor.HookResult._
val config = configBase.fixColor
saveConfig(_ withHook config) >> {
config.hook(uid, ctx.me, sid, blocking) match {
case Left(hook) => fuccess {
lobby ! AddHook(hook)
hook.id
Created(hook.id)
}
case Right(Some(seek)) => ctx.userId.??(gameCache.nbPlaying) flatMap { nbPlaying =>
if (nbPlaying >= maxPlaying) fufail(s"${ctx.userId} already playing $maxPlaying games")
else fuccess {
case Right(Some(seek)) => ctx.userId.??(gameCache.nbPlaying) map { nbPlaying =>
if (nbPlaying >= maxPlaying) Refused
else {
lobby ! AddSeek(seek)
seek.id
Created(seek.id)
}
}
case Right(None) if ctx.me.isEmpty => fufail(new IllegalArgumentException("Anon can't create seek"))
case _ => fufail("Can't create seek for some unknown reason")
case Right(None) if ctx.me.isEmpty => fuccess(Refused)
case _ => fuccess(Refused)
}
}
}
@ -69,3 +70,12 @@ private[setup] final class Processor(
private def saveConfig(map: UserConfig => UserConfig)(implicit ctx: UserContext): Funit =
ctx.me.fold(AnonConfigRepo.update(ctx.req) _)(user => UserConfigRepo.update(user) _)(map)
}
object Processor {
sealed trait HookResult
object HookResult {
case class Created(id: String) extends HookResult
case object Refused extends HookResult
}
}