Skip to content

Commit

Permalink
search: -opening, +clock
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Mar 14, 2016
1 parent 0f2e63b commit 48d2fbb
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 59 deletions.
26 changes: 18 additions & 8 deletions app/views/search/index.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ <h1 class="title">@trans.advancedSearch()</h1>
@base.select(form("mode"), Query.modes, "".some)
</td>
</tr>
<tr>
<th>
<label for="@form("opening").id">@trans.opening()</label>
</th>
<td class="single">
@base.select(form("opening"), Query.openings, "".some)
</td>
</tr>
<tr>
<th>
<label>Number of turns</label>
Expand All @@ -129,6 +121,24 @@ <h1 class="title">@trans.advancedSearch()</h1>
<div class="half">To @base.select(form("durationMax"), Query.durations, "".some)</div>
</td>
</tr>
<tr>
<th>
<label>Clock initial time</label>
</th>
<td>
<div class="half">From @base.select(form("clock")("initMin"), Query.clockInits, "".some)</div>
<div class="half">To @base.select(form("clock")("initMax"), Query.clockInits, "".some)</div>
</td>
</tr>
<tr>
<th>
<label>Clock increment</label>
</th>
<td>
<div class="half">From @base.select(form("clock")("incMin"), Query.clockIncs, "".some)</div>
<div class="half">To @base.select(form("clock")("incMax"), Query.clockIncs, "".some)</div>
</td>
</tr>
<tr>
<th>
<label for="@form("status").id">Result</label>
Expand Down
26 changes: 18 additions & 8 deletions app/views/search/user.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@
<div class="half">To @base.select(form("durationMax"), Query.durations, "".some)</div>
</td>
</tr>
<tr>
<th>
<label>Clock initial time</label>
</th>
<td>
<div class="half">From @base.select(form("clock")("initMin"), Query.clockInits, "".some)</div>
<div class="half">To @base.select(form("clock")("initMax"), Query.clockInits, "".some)</div>
</td>
</tr>
<tr>
<th>
<label>Clock increment</label>
</th>
<td>
<div class="half">From @base.select(form("clock")("incMin"), Query.clockIncs, "".some)</div>
<div class="half">To @base.select(form("clock")("incMax"), Query.clockIncs, "".some)</div>
</td>
</tr>
<tr>
<th>
<label for="@form("source").id">Source</label>
Expand All @@ -67,14 +85,6 @@
@base.select(form("mode"), Query.modes, "".some)
</td>
</tr>
<tr>
<th>
<label for="@form("opening").id">@trans.opening()</label>
</th>
<td class="single">
@base.select(form("opening"), Query.openings, "".some)
</td>
</tr>
</table>
<table>
<tr>
Expand Down
4 changes: 4 additions & 0 deletions modules/common/src/main/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ object Form {
d -> (pluralize(pattern, d) format d)
}

def options(it: Iterable[Int], transformer: Int => Int, pattern: String) = it map { d =>
d -> (pluralize(pattern, transformer(d)) format transformer(d))
}

def options(it: Iterable[Int], code: String, pattern: String) = it map { d =>
(d + code) -> (pluralize(pattern, d) format d)
}
Expand Down
17 changes: 14 additions & 3 deletions modules/gameSearch/src/main/DataForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ private[gameSearch] final class DataForm {
"perf" -> optional(numberIn(Query.perfs)),
"source" -> optional(numberIn(Query.sources)),
"mode" -> optional(numberIn(Query.modes)),
"opening" -> optional(stringIn(Query.openings)),
"turnsMin" -> optional(numberIn(Query.turns)),
"turnsMax" -> optional(numberIn(Query.turns)),
"ratingMin" -> optional(numberIn(Query.averageRatings)),
Expand All @@ -33,6 +32,12 @@ private[gameSearch] final class DataForm {
"aiLevelMax" -> optional(numberIn(Query.aiLevels)),
"durationMin" -> optional(numberIn(Query.durations)),
"durationMax" -> optional(numberIn(Query.durations)),
"clock" -> mapping(
"clockInitMin" -> optional(numberIn(Query.clockInits)),
"clockInitMax" -> optional(numberIn(Query.clockInits)),
"clockIncMin" -> optional(numberIn(Query.clockIncs)),
"clockIncMax" -> optional(numberIn(Query.clockIncs))
)(SearchClock.apply)(SearchClock.unapply),
"dateMin" -> DataForm.dateField,
"dateMax" -> DataForm.dateField,
"status" -> optional(numberIn(Query.statuses)),
Expand All @@ -59,7 +64,6 @@ private[gameSearch] case class SearchData(
perf: Option[Int] = None,
source: Option[Int] = None,
mode: Option[Int] = None,
opening: Option[String] = None,
turnsMin: Option[Int] = None,
turnsMax: Option[Int] = None,
ratingMin: Option[Int] = None,
Expand All @@ -69,6 +73,7 @@ private[gameSearch] case class SearchData(
aiLevelMax: Option[Int] = None,
durationMin: Option[Int] = None,
durationMax: Option[Int] = None,
clock: SearchClock = SearchClock(),
dateMin: Option[String] = None,
dateMax: Option[String] = None,
status: Option[Int] = None,
Expand All @@ -85,12 +90,12 @@ private[gameSearch] case class SearchData(
perf = perf,
source = source,
rated = mode flatMap Mode.apply map (_.rated),
opening = opening map (_.trim.toLowerCase),
turns = Range(turnsMin, turnsMax),
averageRating = Range(ratingMin, ratingMax),
hasAi = hasAi map (_ == 1),
aiLevel = Range(aiLevelMin, aiLevelMax),
duration = Range(durationMin, durationMax),
clock = Clocking(clock.initMin, clock.initMax, clock.incMin, clock.incMax),
date = Range(dateMin flatMap toDate, dateMax flatMap toDate),
status = status,
analysed = analysed map (_ == 1),
Expand Down Expand Up @@ -135,3 +140,9 @@ private[gameSearch] case class SearchPlayer(
private[gameSearch] case class SearchSort(
field: String = Sorting.default.f,
order: String = Sorting.default.order)

private[gameSearch] case class SearchClock(
initMin: Option[Int] = None,
initMax: Option[Int] = None,
incMin: Option[Int] = None,
incMax: Option[Int] = None)
33 changes: 17 additions & 16 deletions modules/gameSearch/src/main/Fields.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package lila.gameSearch

private[gameSearch] object Fields {

val status = "s"
val turns = "t"
val rated = "r"
val perf = "p"
val uids = "u"
val winner = "w"
val winnerColor = "c"
val averageRating = "a"
val ai = "i"
val opening = "o"
val date = "d"
val duration = "l"
val analysed = "n"
val whiteUser = "wu"
val blackUser = "bu"
val source = "so"
val status = "s"
val turns = "t"
val rated = "r"
val perf = "p"
val uids = "u"
val winner = "w"
val winnerColor = "c"
val averageRating = "a"
val ai = "i"
val date = "d"
val duration = "l"
val clockInit = "ct"
val clockInc = "ci"
val analysed = "n"
val whiteUser = "wu"
val blackUser = "bu"
val source = "so"
}
5 changes: 3 additions & 2 deletions modules/gameSearch/src/main/GameSearchApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ final class GameSearchApi(client: ESClient) extends SearchReadApi[Game, Query] {
Fields.averageRating -> game.averageUsersRating,
Fields.ai -> game.aiLevel,
Fields.date -> (lila.search.Date.formatter print game.updatedAtOrCreatedAt),
Fields.duration -> game.estimateTotalTime,
Fields.opening -> (game.opening map (_.opening.eco.toLowerCase)),
Fields.duration -> game.durationSeconds,
Fields.clockInit -> game.clock.map(_.limit),
Fields.clockInc -> game.clock.map(_.increment),
Fields.analysed -> analysed,
Fields.whiteUser -> game.whitePlayer.userId,
Fields.blackUser -> game.blackPlayer.userId,
Expand Down
51 changes: 29 additions & 22 deletions modules/gameSearch/src/main/Query.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lila.gameSearch

import chess.{ Mode, Status }
import chess.opening._
import org.joda.time.DateTime

import lila.rating.RatingRange
Expand All @@ -20,9 +19,9 @@ case class Query(
hasAi: Option[Boolean] = None,
aiLevel: Range[Int] = Range.none,
rated: Option[Boolean] = None,
opening: Option[String] = None,
date: Range[DateTime] = Range.none,
duration: Range[Int] = Range.none,
clock: Clocking = Clocking(),
sorting: Sorting = Sorting.default,
analysed: Option[Boolean] = None,
whiteUser: Option[String] = None,
Expand All @@ -41,9 +40,9 @@ case class Query(
hasAi.nonEmpty ||
aiLevel.nonEmpty ||
rated.nonEmpty ||
opening.nonEmpty ||
date.nonEmpty ||
duration.nonEmpty ||
clock.nonEmpty ||
analysed.nonEmpty
}

Expand All @@ -53,21 +52,33 @@ object Query {
import play.api.libs.json._

import Range.rangeJsonWriter
private implicit val sortingJsonWriter = play.api.libs.json.Json.writes[Sorting]
implicit val jsonWriter = play.api.libs.json.Json.writes[Query]

val durations =
options(List(1, 2, 3, 5, 10, 15, 20, 30), "%d minute{s}").toList :+
(60 * 1, "One hour") :+
(60 * 3, "Three hours") :+
(60 * 24, "One day") :+
(60 * 24 * 3, "Three days") :+
(60 * 24 * 7, "One week") :+
(60 * 24 * 7 * 2, "Two weeks") :+
(60 * 24 * 30, "One month") :+
(60 * 24 * 30 * 3, "Three months") :+
(60 * 24 * 30 * 6, "6 months") :+
(60 * 24 * 365, "One year")
private implicit val sortingJsonWriter = Json.writes[Sorting]
private implicit val clockingJsonWriter = Json.writes[Clocking]
implicit val jsonWriter = Json.writes[Query]

val durations = {
((30, "30 seconds") ::
options(List(60, 60 * 2, 60 * 3, 60 * 5, 60 * 10, 60 * 15, 60 * 20, 60 * 30), _ / 60, "%d minute{s}").toList) :+
(60 * 60 * 1, "One hour") :+
(60 * 60 * 3, "Three hours") :+
(60 * 60 * 24, "One day") :+
(60 * 60 * 24 * 3, "Three days") :+
(60 * 60 * 24 * 7, "One week") :+
(60 * 60 * 24 * 7 * 2, "Two weeks") :+
(60 * 60 * 24 * 30, "One month") :+
(60 * 60 * 24 * 30 * 3, "Three months") :+
(60 * 60 * 24 * 30 * 6, "6 months") :+
(60 * 60 * 24 * 365, "One year")
}

val clockInits = List(
(0, "0 seconds"),
(30, "30 seconds"),
(45, "45 seconds")
) ::: options(List(60 * 1, 60 * 2, 60 * 3, 60 * 5, 60 * 10, 60 * 15, 60 * 20, 60 * 30, 60 * 60, 60 * 90, 60 * 120, 60 * 150, 60 * 180), _ / 60, "%d minute{s}").toList

val clockIncs =
options(List(0, 1, 2, 3, 5, 10, 15, 20, 30, 60, 90, 120, 150, 180), "%d seconds{s}").toList

val winnerColors = List(1 -> "White", 2 -> "Black", 3 -> "None")

Expand All @@ -77,10 +88,6 @@ object Query {

val modes = Mode.all map { mode => mode.id -> mode.name.capitalize }

val openings = EcopeningDB.all map { o =>
o.eco -> s"${o.eco} ${o.name.take(50)}"
}

val turns = options(
(1 to 5) ++ (10 to 45 by 5) ++ (50 to 90 by 10) ++ (100 to 300 by 25),
"%d move{s}")
Expand Down
9 changes: 9 additions & 0 deletions modules/gameSearch/src/main/Sorting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ object Sorting {

val default = Sorting(Fields.date, "desc")
}

case class Clocking(
initMin: Option[Int] = None,
initMax: Option[Int] = None,
incMin: Option[Int] = None,
incMax: Option[Int] = None) {

def nonEmpty = initMin.isDefined || initMax.isDefined || incMin.isDefined || incMax.isDefined
}

0 comments on commit 48d2fbb

Please sign in to comment.