2013-04-01 21:43:01 -06:00
|
|
|
package lila.app
|
2013-04-12 17:47:03 -06:00
|
|
|
package mashup
|
2013-04-01 21:43:01 -06:00
|
|
|
|
|
|
|
import lila.user.{ User, Context }
|
|
|
|
import lila.game.{ Game, Query }
|
|
|
|
|
|
|
|
import play.api.libs.json._
|
|
|
|
import scalaz.{ NonEmptyList, NonEmptyLists }
|
|
|
|
|
|
|
|
sealed abstract class GameFilter(val name: String)
|
|
|
|
|
|
|
|
object GameFilter {
|
|
|
|
|
|
|
|
case object All extends GameFilter("all")
|
|
|
|
case object Me extends GameFilter("me")
|
|
|
|
case object Rated extends GameFilter("rated")
|
|
|
|
case object Win extends GameFilter("win")
|
|
|
|
case object Loss extends GameFilter("loss")
|
|
|
|
case object Draw extends GameFilter("draw")
|
|
|
|
case object Playing extends GameFilter("playing")
|
|
|
|
case object Bookmark extends GameFilter("bookmark")
|
|
|
|
}
|
|
|
|
|
|
|
|
case class GameFilterMenu(
|
|
|
|
all: NonEmptyList[GameFilter],
|
|
|
|
current: GameFilter,
|
|
|
|
query: Option[JsObject],
|
|
|
|
cachedNb: Option[Int]) {
|
|
|
|
|
|
|
|
def list = all.list
|
|
|
|
}
|
|
|
|
|
|
|
|
object GameFilterMenu extends NonEmptyLists {
|
|
|
|
|
|
|
|
import GameFilter._
|
|
|
|
import lila.db.Implicits.docId
|
|
|
|
|
|
|
|
def apply(
|
|
|
|
info: UserInfo,
|
|
|
|
me: Option[User],
|
|
|
|
currentName: String): GameFilterMenu = {
|
|
|
|
|
|
|
|
val user = info.user
|
|
|
|
|
|
|
|
val all = nel(All, List(
|
2013-05-17 18:09:35 -06:00
|
|
|
info.nbWithMe.??(_ > 0) option Me,
|
2013-04-01 21:43:01 -06:00
|
|
|
(info.nbRated > 0) option Rated,
|
2013-06-02 05:04:18 -06:00
|
|
|
(info.user.count.win > 0) option Win,
|
|
|
|
(info.user.count.loss > 0) option Loss,
|
|
|
|
(info.user.count.draw > 0) option Draw,
|
2013-04-01 21:43:01 -06:00
|
|
|
(info.nbPlaying > 0) option Playing,
|
|
|
|
(info.nbBookmark > 0) option Bookmark
|
|
|
|
).flatten)
|
|
|
|
|
|
|
|
val current = (all.list find (_.name == currentName)) | all.head
|
|
|
|
|
|
|
|
val query: Option[JsObject] = current match {
|
|
|
|
case All ⇒ Some(Query user user)
|
|
|
|
case Me ⇒ Some(Query.opponents(user, me | user))
|
|
|
|
case Rated ⇒ Some(Query rated user)
|
|
|
|
case Win ⇒ Some(Query win user)
|
|
|
|
case Loss ⇒ Some(Query loss user)
|
|
|
|
case Draw ⇒ Some(Query draw user)
|
|
|
|
case Playing ⇒ Some(Query notFinished user)
|
|
|
|
case Bookmark ⇒ None
|
|
|
|
}
|
|
|
|
|
|
|
|
val cachedNb: Option[Int] = current match {
|
2013-06-02 05:04:18 -06:00
|
|
|
case All ⇒ info.user.count.game.some
|
|
|
|
case Rated ⇒ info.user.count.rated.some
|
|
|
|
case Win ⇒ info.user.count.win.some
|
|
|
|
case Loss ⇒ info.user.count.loss.some
|
|
|
|
case Draw ⇒ info.user.count.draw.some
|
2013-04-01 21:43:01 -06:00
|
|
|
case _ ⇒ None
|
|
|
|
}
|
|
|
|
|
|
|
|
new GameFilterMenu(all, current, query, cachedNb)
|
|
|
|
}
|
|
|
|
}
|