Skip to content

Commit

Permalink
Add ability to filter seeks by increment, closes lichess-org#6150
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-finley committed Mar 13, 2020
1 parent d781cbb commit 88fed84
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
7 changes: 6 additions & 1 deletion app/templating/SetupHelper.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lila.app
package templating

import chess.{ Mode, Speed }
import chess.{ Increment, Mode, Speed }
import play.api.i18n.Lang

import lila.i18n.{ I18nKeys => trans }
Expand Down Expand Up @@ -87,6 +87,11 @@ trait SetupHelper { self: I18nHelper =>
(Mode.Rated.id.toString, trans.rated.txt(), none)
)

def translatedIncrementChoices(implicit lang: Lang) = List(
(Increment.Yes.id.toString, trans.yes.txt(), none),
(Increment.No.id.toString, trans.no.txt(), none)
)

def translatedModeChoicesTournament(implicit lang: Lang) = List(
(Mode.Casual.id.toString, trans.casualTournament.txt(), none),
(Mode.Rated.id.toString, trans.ratedTournament.txt(), none)
Expand Down
4 changes: 4 additions & 0 deletions app/views/setup/filter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ object filter {
td(trans.timeControl()),
td(renderCheckboxes(form, "speed", filter.speed.map(_.id.toString), translatedSpeedChoices))
),
tr(
td(trans.increment()),
td(renderCheckboxes(form, "increment", filter.increment.map(_.id.toString), translatedIncrementChoices))
),
ctx.isAuth option tr(
td(trans.mode()),
td(renderCheckboxes(form, "mode", filter.mode.map(_.id.toString), translatedModeChoices))
Expand Down
5 changes: 4 additions & 1 deletion modules/lobby/src/main/Hook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ case class Hook(

val isAuth = user.nonEmpty

val hasIncrement = if (clock.incrementSeconds > 0) 0 else 1

def compatibleWith(h: Hook) =
isAuth == h.isAuth &&
mode == h.mode &&
Expand Down Expand Up @@ -67,7 +69,8 @@ case class Hook(
"sri" -> sri,
"clock" -> clock.show,
"t" -> clock.estimateTotalSeconds,
"s" -> speed.id
"s" -> speed.id,
"i" -> hasIncrement
)
.add("prov" -> perf.map(_.provisional).filter(identity))
.add("u" -> user.map(_.username))
Expand Down
22 changes: 17 additions & 5 deletions modules/setup/src/main/FilterConfig.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package lila.setup

import chess.{ Mode, Speed }
import chess.{ Increment, Mode, Speed }
import lila.rating.RatingRange

case class FilterConfig(
variant: List[chess.variant.Variant],
mode: List[Mode],
speed: List[Speed],
increment: List[Increment],
ratingRange: RatingRange
) {

Expand All @@ -15,19 +16,22 @@ case class FilterConfig(
variant map (_.id),
mode map (_.id),
speed map (_.id),
increment map (_.id),
ratingRange.toString
).some

def render = play.api.libs.json.Json.obj(
"variant" -> variant.map(_.key),
"mode" -> mode.map(_.id),
"speed" -> speed.map(_.id),
"increment" -> increment.map(_.id),
"rating" -> ratingRange.notBroad.map(rr => List(rr.min, rr.max))
)

def nonEmpty = copy(
variant = if (variant.isEmpty) FilterConfig.default.variant else variant,
mode = if (mode.isEmpty) FilterConfig.default.mode else mode,
increment = if (increment.isEmpty) FilterConfig.default.increment else increment,
speed = if (speed.isEmpty) FilterConfig.default.speed else speed
)
}
Expand All @@ -48,19 +52,22 @@ object FilterConfig {

val modes = Mode.all
val speeds = Speed.all
val increments = Increment.all

val default = FilterConfig(
variant = variants,
mode = modes,
speed = speeds,
increment = increments,
ratingRange = RatingRange.default
)

def <<(v: List[Int], m: List[Int], s: List[Int], e: String) =
def <<(v: List[Int], m: List[Int], s: List[Int], i: List[Int], e: String) =
new FilterConfig(
variant = v map chess.variant.Variant.apply flatten,
mode = m map Mode.apply flatten,
speed = s map Speed.apply flatten,
variant = v flatMap chess.variant.Variant.apply,
mode = m flatMap Mode.apply,
speed = s flatMap Speed.apply,
increment = i flatMap Increment.apply,
ratingRange = RatingRange orDefault e
).nonEmpty

Expand All @@ -73,13 +80,18 @@ object FilterConfig {
variant = r intsD "v" flatMap { chess.variant.Variant(_) },
mode = r intsD "m" flatMap { Mode(_) },
speed = r intsD "s" flatMap { Speed(_) },
increment = {
val maybeIncrement = r intsD "i" flatMap { Increment(_)}
if (maybeIncrement.isEmpty) FilterConfig.default.increment else maybeIncrement
} ,
ratingRange = r strO "e" flatMap RatingRange.apply getOrElse RatingRange.default
)

def writes(w: BSON.Writer, o: FilterConfig) = BSONDocument(
"v" -> o.variant.map(_.id),
"m" -> o.mode.map(_.id),
"s" -> o.speed.map(_.id),
"i" -> o.increment.map(_.id),
"e" -> o.ratingRange.toString
)
}
Expand Down
1 change: 1 addition & 0 deletions modules/setup/src/main/FormFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class FormFactory(
"variant" -> list(variantWithVariants),
"mode" -> list(rawMode(withRated = true)),
"speed" -> list(speed),
"increment" -> list(increment),
"ratingRange" -> ratingRange
)(FilterConfig.<<)(_.>>)
)
Expand Down
1 change: 1 addition & 0 deletions ui/lobby/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function(ctrl: LobbyController, hooks: Hook[]): Filtered {
if (!f.variant.includes(variant) ||
!f.mode.includes(hook.ra || 0) ||
!f.speed.includes(hook.s || 1 /* ultrabullet = bullet */) ||
!f.increment.includes(hook.i || 0) ||
(f.rating && (!hook.rating || (hook.rating < f.rating[0] || hook.rating > f.rating[1])))) {
hidden++;
} else {
Expand Down

0 comments on commit 88fed84

Please sign in to comment.