save anon setups in cookies WIP

pull/83/head
Thibault Duplessis 2012-12-16 18:59:58 +01:00
parent ee0c13090f
commit 078fccb047
6 changed files with 47 additions and 31 deletions

View File

@ -2,7 +2,7 @@ package controllers
import lila._
import views._
import http.{ Context, BodyContext, Setting => HttpSetting }
import http.{ Context, BodyContext, Setting HttpSetting }
import play.api.data.Form
import play.api.mvc.Cookie
@ -15,38 +15,35 @@ object Setting extends LilaController {
def set(name: String) = OpenBody { implicit ctx
implicit val req = ctx.body
val setter = name match {
case "theme" setTheme.some
case "sound" setSound.some
case "chat" setChat.some
case "bg" setBg.some
case _ none
}
setter.fold({
(setters get name).fold({
case (form, process)
FormResult(form) { value
Ok("ok") withCookies {
process(HttpSetting(ctx), value).unsafePerformIO
}
Ok() withCookies process(HttpSetting(ctx), value).unsafePerformIO
}
}, NotFound)
}
private type Setter = (Form[String], (HttpSetting, String) IO[Cookie])
private lazy val setTheme: Setter = forms.theme -> {
private lazy val setters = Map(
"theme" -> setTheme,
"sound" -> setSound,
"chat" -> setChat,
"bg" -> setBg)
private val setTheme: Setter = forms.theme -> {
(setting, v) setting.theme(v)(userRepo)
}
private lazy val setSound: Setter = forms.sound -> {
private val setSound: Setter = forms.sound -> {
(setting, v) setting.sound(v)(userRepo)
}
private lazy val setChat: Setter = forms.chat -> {
private val setChat: Setter = forms.chat -> {
(setting, v) setting.chat(v)(userRepo)
}
private lazy val setBg: Setter = forms.bg -> {
private val setBg: Setter = forms.bg -> {
(setting, v) setting.bg(v)(userRepo)
}
}

View File

@ -27,11 +27,9 @@ final class Setting(ctx: Context) {
} getOrElse default
private def set(name: String, value: String)(userRepo: UserRepo): IO[Cookie] =
ctx.me.fold(
m userRepo.saveSetting(m, name, value.toString),
io()) map { _
LilaCookie.session(name, value.toString)(ctx.req)
}
~ctx.me.map(m userRepo.saveSetting(m, name, value.toString).void) map { _
LilaCookie.session(name, value.toString)(ctx.req)
}
}
object Setting {

View File

@ -0,0 +1,26 @@
package lila
package setup
import play.api.mvc.{ Cookie, RequestHeader }
final class AnonConfigRepo {
private val name = "setup"
def update(map: UserConfig UserConfig)(implicit req: RequestHeader): Cookie =
LilaCookie.session(name, value.toString)(ctx.req)
config(user) flatMap { c save(map(c)) }
def config(implicit req: RequestHeader): IO[UserConfig] = io {
findOneById(user.id) flatMap (_.decode)
} except { e
putStrLn("Can't load config: " + e.getMessage) inject none[UserConfig]
} map (_ | UserConfig.default(user))
def save(config: UserConfig)(implicit req: RequestHeader): IO[Unit] = io {
update(
DBObject("_id" -> config.id),
_grater asDBObject config.encode,
upsert = true)
}
}

View File

@ -22,10 +22,7 @@ final class Processor(
ai: () Ai) extends core.Futuristic {
def ai(config: AiConfig)(implicit ctx: Context): IO[Pov] = for {
_ ctx.me.fold(
user configRepo.update(user)(_ withAi config),
io()
)
_ ~ctx.me.map(user configRepo.update(user)(_ withAi config))
pov = config.pov
game = ctx.me.fold(
user pov.game.updatePlayer(pov.color, _ withUser user),

View File

@ -9,14 +9,11 @@ import com.mongodb.casbah.MongoCollection
import com.mongodb.casbah.Imports._
import scalaz.effects._
class UserConfigRepo(collection: MongoCollection)
final class UserConfigRepo(collection: MongoCollection)
extends SalatDAO[RawUserConfig, String](collection) {
def update(user: User)(map: UserConfig UserConfig): IO[Unit] = for {
c1 config(user)
c2 = map(c1)
_ save(c2)
} yield ()
def update(user: User)(map: UserConfig UserConfig): IO[Unit] =
config(user) flatMap { c save(map(c)) }
def config(user: User): IO[UserConfig] = io {
findOneById(user.id) flatMap (_.decode)

1
todo
View File

@ -46,3 +46,4 @@ make chess captcha more usable
tournament 404 page
log off = tournament resign
[!!!!!!!!!!!!!!] save game prefs in cookie (for anons)
show team forums in forum list http://en.lichess.org/forum/lichess-feedback/new-feature-teams?page=1http://en.lichess.org/forum/lichess-feedback/new