game filter wip

This commit is contained in:
Thibault Duplessis 2012-12-29 14:31:56 +01:00
parent cf800fe5f2
commit 7063449860
12 changed files with 81 additions and 18 deletions

View file

@ -37,10 +37,11 @@ object Lobby extends LilaController with Results {
myHook = myHook,
timeline = timelineRecent,
posts = forumRecent(ctx.me, teamCache.teamIds),
tours = openTours
tours = openTours,
filter = env.setup.filter
).map(_.fold(Redirect(_), {
case (preload, posts, tours, featured) status(html.lobby.home(
toJson(preload),
toJson(preload).pp,
myHook,
posts,
tours,

View file

@ -52,12 +52,13 @@ object Setup extends LilaController with TheftPrevention with RoundEventPerforme
}
}
val filterForm = TODO
// Open { implicit ctx
// IOk(forms.filterFilled map { html.setup.filter(_) })
// }
val filterForm = Open { implicit ctx
IOk(forms.filterFilled map { html.setup.filter(_) })
}
val filter = TODO
val filter = process(forms.filter) { config
implicit ctx processor filter config inject routes.Lobby.home()
}
def join(id: String) = Open { implicit ctx
IOptionIOResult(gameRepo game id) { game

View file

@ -7,6 +7,7 @@ import forum.PostLiteView
import controllers.routes
import socket.History
import tournament.Created
import setup.FilterConfig
import play.api.mvc.Call
import play.api.libs.concurrent.Akka
@ -35,7 +36,8 @@ final class Preload(
myHook: Option[Hook],
timeline: IO[List[Entry]],
posts: IO[List[PostLiteView]],
tours: IO[List[Created]]): Future[Response] =
tours: IO[List[Created]],
filter: IO[FilterConfig]): Future[Response] =
myHook.flatMap(_.gameId).fold(
gameId futureGame(gameId) map { gameOption
Left(gameOption.fold(
@ -48,12 +50,14 @@ final class Preload(
ioToFuture(timeline) zip
ioToFuture(posts) zip
ioToFuture(tours) zip
featured.one map {
case (((((hooks, messages), entries), posts), tours), feat) (Right((Map(
featured.one zip
ioToFuture(filter) map {
case ((((((hooks, messages), entries), posts), tours), feat), filter) (Right((Map(
"version" -> history.version,
"pool" -> renderHooks(hooks, myHook),
"chat" -> (messages.reverse map (_.render)),
"timeline" -> (entries.reverse map (_.render))
"timeline" -> (entries.reverse map (_.render)),
"filter" -> filter.toMap
), posts, tours, feat))): Response
}
)

View file

@ -11,7 +11,7 @@ import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.Imports._
import scalaz.effects._
final class AnonConfigRepo(collection: MongoCollection)
private[setup] final class AnonConfigRepo(collection: MongoCollection)
extends SalatDAO[RawUserConfig, String](collection) {
private val sessionKey = "setup"
@ -35,6 +35,15 @@ final class AnonConfigRepo(collection: MongoCollection)
private def configOption(req: RequestHeader): IO[Option[UserConfig]] =
~sessionId(req).map(s config(s) map (_.some))
def filter(req: RequestHeader): IO[FilterConfig] = io {
for {
sid sessionId(req)
obj collection.findOneByID(sid, DBObject("filter" -> true))
variant obj.getAs[Int]("v")
config RawFilterConfig(variant).decode
} yield config
} map (_ | FilterConfig.default)
private def save(config: UserConfig): IO[Unit] = io {
update(
DBObject("_id" -> config.id),

View file

@ -9,6 +9,10 @@ case class FilterConfig(variant: Option[Variant]) {
def encode = RawFilterConfig(
v = ~variant.map(_.id)
)
def >> = Some((variant map (_.id)))
def toMap = Map("variant" -> variant.map(_.id))
}
object FilterConfig {
@ -17,9 +21,10 @@ object FilterConfig {
variant = none)
val variants = 0 :: Config.variants
val variantChoices = variants map { id
(Config.variantChoices.toMap get id.toString map ("Only " +)) | "All"
}
def <<(v: Option[Int]) = new FilterConfig(
variant = v flatMap Variant.apply
)
}
private[setup] case class RawFilterConfig(v: Int) {

View file

@ -8,12 +8,23 @@ import play.api.data._
import play.api.data.Forms._
import scalaz.effects._
final class FormFactory(
private[setup] final class FormFactory(
userConfigRepo: UserConfigRepo,
anonConfigRepo: AnonConfigRepo) {
import Mappings._
def filterFilled(implicit ctx: Context): IO[Form[FilterConfig]] =
filterConfig map filter(ctx).fill
def filter(ctx: Context) = Form(
mapping(
"variant" -> optional(variant)
)(FilterConfig.<<)(_.>>)
)
def filterConfig(implicit ctx: Context): IO[FilterConfig] = savedConfig map (_.filter)
def aiFilled(implicit ctx: Context): IO[Form[AiConfig]] =
aiConfig map ai(ctx).fill

View file

@ -22,6 +22,9 @@ final class Processor(
timelinePush: DbGame IO[Unit],
ai: () Ai) extends core.Futuristic {
def filter(config: FilterConfig)(implicit ctx: Context): IO[Unit] =
saveConfig(_ withFilter config)
def ai(config: AiConfig)(implicit ctx: Context): IO[Pov] = for {
_ saveConfig(_ withAi config)
pov = config.pov

View file

@ -64,4 +64,9 @@ final class SetupEnv(
userRepo = userRepo,
timelinePush = timelinePush,
messenger = roundMessenger)
def filter(implicit ctx: http.Context): IO[FilterConfig] = ctx.me.fold(
userConfigRepo.filter,
anonConfigRepo filter ctx.req
)
}

View file

@ -14,6 +14,8 @@ case class UserConfig(
hook: HookConfig,
filter: FilterConfig) {
def withFilter(c: FilterConfig) = copy(filter = c)
def withAi(c: AiConfig) = copy(ai = c)
def withFriend(c: FriendConfig) = copy(friend = c)

View file

@ -9,7 +9,7 @@ import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.Imports._
import scalaz.effects._
final class UserConfigRepo(collection: MongoCollection)
private[setup] final class UserConfigRepo(collection: MongoCollection)
extends SalatDAO[RawUserConfig, String](collection) {
def update(user: User)(map: UserConfig UserConfig): IO[Unit] =
@ -21,6 +21,14 @@ final class UserConfigRepo(collection: MongoCollection)
putStrLn("Can't load config: " + e.getMessage) map (_ none[UserConfig])
} map (_ | UserConfig.default(user.id))
def filter(user: User): IO[FilterConfig] = io {
for {
obj collection.findOneByID(user.id, DBObject("filter" -> true))
variant obj.getAs[Int]("v")
config RawFilterConfig(variant).decode
} yield config
} map (_ | FilterConfig.default)
private def save(config: UserConfig): IO[Unit] = io {
update(
DBObject("_id" -> config.id),

View file

@ -0,0 +1,13 @@
@(form: Form[_])(implicit ctx: Context)
<div class="lichess_overboard auto_center game_config game_config_filter">
<h2>@trans.filterGames()</h2>
<div class="game_config_form">
@helper.form(action = routes.Setup.filter, 'novalidate -> "novalidate") {
<div class="variants">
@base.select(form("variant"), translatedVariantChoices, "Any variant".some)
</div>
<button type="submit" class="submit">Save filter</button>
}
</div>
</div>

View file

@ -15,7 +15,8 @@ div.game_config div.ui-buttonset {
#config_level .ui-button {
margin-right: -0.6em;
}
div.game_config .ui-button-text {
div.game_config .ui-button-text,
div.game_config select {
text-transform: capitalize;
}
div.game_config div.ui-slider {