allow creating correspondence seeks from the board API - for #8868

pull/9158/head
Thibault Duplessis 2021-06-17 11:52:35 +02:00
parent 7903826228
commit baaad5268f
4 changed files with 50 additions and 8 deletions

View File

@ -235,7 +235,13 @@ final class Setup(
env.lobby.boardApiHookStream(hook.copy(boardApi = true))
)(apiC.sourceToNdJsonOption).fuccess
}(rateLimitedFu)
case _ => BadRequest(jsonError("Invalid board API seek")).fuccess
case Right(Some(seek)) =>
env.setup.processor.createSeekIfAllowed(seek, me.id) map {
case HookResult.Refused =>
BadRequest(Json.obj("error" -> "Already playing too many games"))
case HookResult.Created(id) => Ok(Json.obj("id" -> id))
}
case Right(None) => notFoundJson()
}
}
)

View File

@ -30,7 +30,7 @@ private object Mappings {
val boardApiVariantKeys = text.verifying(boardApiVariants contains _)
val time = of[Double].verifying(HookConfig validateTime _)
val increment = number.verifying(HookConfig validateIncrement _)
val days = number(min = 1, max = 14)
val days = lila.common.Form.numberIn(List(1, 3, 5, 7, 10, 14))
def timeMode = number.verifying(TimeMode.ids contains _)
def mode(withRated: Boolean) = optional(rawMode(withRated))
def rawMode(withRated: Boolean) =

View File

@ -4,6 +4,7 @@ import lila.common.Bus
import lila.common.config.Max
import lila.game.Pov
import lila.lobby.actorApi.{ AddHook, AddSeek }
import lila.lobby.Seek
import lila.user.{ User, UserContext }
final private[setup] class Processor(
@ -45,16 +46,23 @@ final private[setup] class Processor(
Created(hook.id)
}
case Right(Some(seek)) =>
ctx.userId.??(gameCache.nbPlaying) dmap { nbPlaying =>
if (maxPlaying <= nbPlaying) Refused
else {
Bus.publish(AddSeek(seek), "lobbyTrouper")
Created(seek.id)
}
ctx.userId match {
case None => fuccess(Refused)
case Some(userId) => createSeekIfAllowed(seek, userId)
}
case _ => fuccess(Refused)
}
}
def createSeekIfAllowed(seek: Seek, userId: User.ID): Fu[Processor.HookResult] =
gameCache.nbPlaying(userId) map { nbPlaying =>
import Processor.HookResult._
if (maxPlaying <= nbPlaying) Refused
else {
Bus.publish(AddSeek(seek), "lobbyTrouper")
Created(seek.id)
}
}
}
object Processor {

View File

@ -76,6 +76,34 @@ object SetupForm {
)
lazy val boardApiHook = Form(
mapping(
"time" -> optional(time),
"increment" -> optional(increment),
"days" -> optional(days),
"variant" -> optional(boardApiVariantKeys),
"rated" -> optional(boolean),
"color" -> optional(color),
"ratingRange" -> optional(ratingRange)
)((t, i, d, v, r, c, g) =>
HookConfig(
variant = v.flatMap(Variant.apply) | Variant.default,
timeMode = if (d.isDefined) TimeMode.Correspondence else TimeMode.RealTime,
time = t | 10,
increment = i | 5,
days = d | 7,
mode = chess.Mode(~r),
color = lila.lobby.Color.orDefault(c),
ratingRange = g.fold(RatingRange.default)(RatingRange.orDefault)
)
)(_ => none)
.verifying("Invalid clock", _.validClock)
.verifying(
"Invalid time control",
hook => hook.makeClock.exists(lila.game.Game.isBoardCompatible) || hook.makeDaysPerTurn.isDefined
)
)
lazy val boardApiSeek = Form(
mapping(
"time" -> time,
"increment" -> increment,