From b29426acc0bf049246f206ce80491b8fc09a7043 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sat, 19 May 2012 15:37:10 +0200 Subject: [PATCH] Progress on friend games --- app/controllers/LilaController.scala | 8 ++++++- app/controllers/Round.scala | 4 ++-- app/controllers/Setup.scala | 14 +++++++++++- app/game/GameHelper.scala | 9 ++++++-- app/i18n/I18hHelper.scala | 5 +++- app/setup/HumanConfig.scala | 4 +++- app/views/setup/await.scala.html | 34 ++++++++++++++++++++++++++++ conf/routes | 2 ++ 8 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 app/views/setup/await.scala.html diff --git a/app/controllers/LilaController.scala b/app/controllers/LilaController.scala index 092128a550..5cc3aa2bdb 100644 --- a/app/controllers/LilaController.scala +++ b/app/controllers/LilaController.scala @@ -85,9 +85,15 @@ trait LilaController def IORedirect(op: IO[Call]) = Redirect(op.unsafePerformIO) - def IOption[A, B](ioa: IO[Option[A]])(op: A ⇒ B)(implicit writer: Writeable[B], ctype: ContentTypeOf[B], ctx: Context) = + def IOption[A, B](ioa: IO[Option[A]])(op: A ⇒ B)( + implicit writer: Writeable[B], + ctype: ContentTypeOf[B], + ctx: Context) = ioa.unsafePerformIO.fold(a ⇒ Ok(op(a)), notFound(ctx)) + def IOptionResult[A](ioa: IO[Option[A]])(op: A ⇒ Result)(implicit ctx: Context) = + ioa.unsafePerformIO.fold(a ⇒ op(a), notFound(ctx)) + def notFound(ctx: Context) = Lobby handleNotFound ctx // I like Unit requests. diff --git a/app/controllers/Round.scala b/app/controllers/Round.scala index e31fffaeec..721267f198 100644 --- a/app/controllers/Round.scala +++ b/app/controllers/Round.scala @@ -45,8 +45,6 @@ object Round extends LilaController { } } - private def version(gameId: String): Int = socket blockingVersion gameId - def abort(fullId: String) = performAndRedirect(fullId, hand.abort) def resign(fullId: String) = performAndRedirect(fullId, hand.resign) def resignForce(fullId: String) = performAndRedirect(fullId, hand.resignForce) @@ -114,4 +112,6 @@ object Round extends LilaController { private def performEvents(fullId: String)(events: List[Event]): IO[Unit] = env.round.socket.send(DbGame takeGameId fullId, events) + + private def version(gameId: String): Int = socket blockingVersion gameId } diff --git a/app/controllers/Setup.scala b/app/controllers/Setup.scala index 846b129abd..96179b372e 100644 --- a/app/controllers/Setup.scala +++ b/app/controllers/Setup.scala @@ -14,6 +14,7 @@ object Setup extends LilaController { def forms = env.setup.formFactory def processor = env.setup.processor + def gameRepo = env.game.gameRepo val aiForm = Open { implicit ctx ⇒ IOk(forms.aiFilled map { html.setup.ai(_) }) @@ -33,10 +34,21 @@ object Setup extends LilaController { val friend = process(forms.friend) { config ⇒ implicit ctx ⇒ processor friend config map { pov ⇒ - routes.Round.player(pov.fullId) + routes.Setup.await(pov.fullId) } } + def await(fullId: String) = Open { implicit ctx ⇒ + IOptionResult(gameRepo pov fullId) { pov ⇒ + pov.game.started.fold( + Redirect(routes.Round.player(pov.fullId)), + Ok(html.setup.await(pov, None)) + ) + } + } + + def cancel(fullId: String) = TODO + val hookForm = TODO val hook = TODO diff --git a/app/game/GameHelper.scala b/app/game/GameHelper.scala index fb8c5083e1..ca653577e9 100644 --- a/app/game/GameHelper.scala +++ b/app/game/GameHelper.scala @@ -2,7 +2,7 @@ package lila package game import chess.format.Forsyth -import chess.{ Status, Variant, Color, Clock } +import chess.{ Status, Variant, Color, Clock, Mode } import user.{ User, UserHelper } import http.Context import i18n.I18nHelper @@ -21,11 +21,16 @@ trait GameHelper { self: I18nHelper with UserHelper ⇒ case Variant.Chess960 ⇒ "chess960" } - def clockName(clock: Option[Clock])(implicit ctx: Context): String = + def clockName(clock: Option[Clock])(implicit ctx: Context): String = clock.fold(clockName, trans.unlimited.str()) def clockName(clock: Clock): String = Namer clock clock + def modeName(mode: Mode)(implicit ctx: Context): String = mode match { + case Mode.Casual ⇒ trans.casual.str() + case Mode.Rated ⇒ trans.rated.str() + } + def usernameWithElo(player: DbPlayer) = Namer.player(player)(userIdToUsername) def playerLink(player: DbPlayer, cssClass: Option[String] = None) = Html { diff --git a/app/i18n/I18hHelper.scala b/app/i18n/I18hHelper.scala index 6635de4243..ec00dda5f9 100644 --- a/app/i18n/I18hHelper.scala +++ b/app/i18n/I18hHelper.scala @@ -7,7 +7,7 @@ import http.Context import play.api.i18n.Lang import play.api.templates.Html -import play.api.mvc.RequestHeader +import play.api.mvc.{ RequestHeader, Call } trait I18nHelper { @@ -34,6 +34,9 @@ trait I18nHelper { }) } + def commonDomain(implicit ctx: Context): String = + I18nDomain(ctx.req.domain).commonDomain + val protocol = "http://" private def langUrl(lang: Lang)(req: RequestHeader) = diff --git a/app/setup/HumanConfig.scala b/app/setup/HumanConfig.scala index f07a1d67b8..d58670f9ae 100644 --- a/app/setup/HumanConfig.scala +++ b/app/setup/HumanConfig.scala @@ -1,7 +1,7 @@ package lila package setup -import chess.{ Mode } +import chess.{ Mode, PausedClock } trait HumanConfig extends Config { @@ -18,6 +18,8 @@ trait HumanConfig extends Config { val mode: Mode def validClock = clock.fold(time + increment > 0, true) + + def makeClock = clock option PausedClock(time, increment) } trait BaseHumanConfig extends BaseConfig { diff --git a/app/views/setup/await.scala.html b/app/views/setup/await.scala.html new file mode 100644 index 0000000000..43bba99a1d --- /dev/null +++ b/app/views/setup/await.scala.html @@ -0,0 +1,34 @@ +@(pov: Pov, config: Option[lila.setup.FriendConfig])(implicit ctx: Context) + +@import pov._ + +@round.layout( +title = trans.playWithAFriend.str(), +goodies = Html("")) { +
+
+ @color.fold(board.white(), board.black()) +
+ @trans.toInviteSomeoneToPlayGiveThisUrl(): + +

+ @trans.theFirstPersonToComeOnThisUrlWillPlayWithYou() + @trans.cancel() +

+ @config.map { c => +

+ @trans.variant(): @variantName(c.variant)
+ @trans.timeControl(): @clockName(c.makeClock)
+ @trans.mode(): @modeName(c.mode) +

+ } +
+
+
+ +} diff --git a/conf/routes b/conf/routes index 06f2a02db2..8859e70f1f 100644 --- a/conf/routes +++ b/conf/routes @@ -43,6 +43,8 @@ GET /setup/ai controllers.Setup.aiForm POST /setup/ai controllers.Setup.ai GET /setup/friend controllers.Setup.friendForm POST /setup/friend controllers.Setup.friend +GET /$fullId<[\w\-]{12}>/await controllers.Setup.await(fullId: String) +GET /$fullId<[\w\-]{12}>/cancel controllers.Setup.cancel(fullId: String) GET /setup/hook controllers.Setup.hookForm POST /setup/hook controllers.Setup.hook