lila/app/views/setup/forms.scala

158 lines
5.0 KiB
Scala
Raw Normal View History

2018-12-15 23:12:36 -07:00
package views.html.setup
import play.api.data.Form
import play.api.mvc.Call
import lila.api.Context
import lila.app.templating.Environment._
import lila.app.ui.ScalatagsTemplate._
import lila.rating.RatingRange
import lila.setup.{ FriendConfig, HookConfig }
import lila.user.User
import controllers.routes
object forms {
import bits._
def hook(form: Form[_])(implicit ctx: Context) = layout(
form,
"hook",
trans.createAGame.frag(),
routes.Setup.hook("uid-placeholder")
) {
frag(
renderVariant(form, translatedVariantChoicesWithVariants),
renderTimeMode(form, lila.setup.HookConfig),
ctx.isAuth option frag(
div(cls := "mode_choice buttons")(
renderRadios(form("mode"), translatedModeChoices)
),
2019-01-19 02:08:45 -07:00
ctx.noBlind option div(cls := "optional_config")(
2019-02-27 18:55:28 -07:00
div(cls := "rating-range-config slider")(
2019-03-21 19:19:30 -06:00
trans.ratingRange.frag(),
2018-12-15 23:12:36 -07:00
": ",
span(cls := "range")("? - ?"),
2019-02-27 18:55:28 -07:00
div(cls := "rating-range")(
2018-12-15 23:12:36 -07:00
renderInput(form("ratingRange"))(
dataMin := RatingRange.min,
dataMax := RatingRange.max
)
)
)
)
)
)
}
def ai(form: Form[_], ratings: Map[Int, Int], validFen: Option[lila.setup.ValidFen])(implicit ctx: Context) =
2019-03-21 19:19:30 -06:00
layout(form, "ai", trans.playWithTheMachine.frag(), routes.Setup.ai) {
2018-12-15 23:12:36 -07:00
frag(
renderVariant(form, translatedAiVariantChoices),
fenInput(form("fen"), true, validFen),
renderTimeMode(form, lila.setup.AiConfig),
2019-01-19 02:08:45 -07:00
if (ctx.blind) frag(
2019-03-21 19:19:30 -06:00
renderLabel(form("level"), trans.level.frag()),
renderSelect(form("level"), lila.setup.AiConfig.levelChoices),
blindSideChoice(form)
)
else frag(
2019-02-27 21:14:18 -07:00
br,
2019-03-21 19:19:30 -06:00
trans.level.frag(),
div(cls := "level buttons")(
div(id := "config_level")(
renderRadios(form("level"), lila.setup.AiConfig.levelChoices)
),
div(cls := "ai_info")(
ratings.toList.map {
2019-03-21 19:19:30 -06:00
case (level, rating) => div(cls := s"${prefix}level_$level")(trans.aiNameLevelAiLevel.frag("A.I.", level))
}
)
2018-12-15 23:12:36 -07:00
)
)
)
}
def friend(
form: Form[_],
user: Option[User],
error: Option[String],
validFen: Option[lila.setup.ValidFen]
)(implicit ctx: Context) =
layout(
form,
"friend",
(if (user.isDefined) trans.challengeToPlay else trans.playWithAFriend)(),
routes.Setup.friend(user map (_.id)),
2019-04-11 04:12:16 -06:00
error.map(e => raw(e.replace("{{user}}", userIdLink(user.map(_.id)).toString)))
2018-12-15 23:12:36 -07:00
)(frag(
user.map { u =>
userLink(u, cssClass = "target".some)
},
renderVariant(form, translatedVariantChoicesWithVariantsAndFen),
fenInput(form("fen"), false, validFen),
renderTimeMode(form, lila.setup.FriendConfig),
ctx.isAuth option div(cls := "mode_choice buttons")(
renderRadios(form("mode"), translatedModeChoices)
2019-03-21 19:19:30 -06:00
),
blindSideChoice(form)
2018-12-15 23:12:36 -07:00
))
2019-03-21 19:19:30 -06:00
private def blindSideChoice(form: Form[_])(implicit ctx: Context) =
ctx.blind option frag(
renderLabel(form("color"), trans.side.frag()),
renderSelect(form("color").copy(value = "random".some), translatedSideChoices)
)
2018-12-15 23:12:36 -07:00
private def layout(
form: Form[_],
typ: String,
titleF: Frag,
2018-12-15 23:12:36 -07:00
route: Call,
error: Option[Frag] = None
)(fields: Frag)(implicit ctx: Context) =
2019-02-27 21:14:18 -07:00
div(cls := error.isDefined option "error")(
a(href := routes.Lobby.home, cls := "close icon", st.title := trans.cancel.txt(), dataIcon := "L"),
h2(titleF),
error.map { e =>
frag(
p(cls := "error")(e),
br,
a(href := routes.Lobby.home, cls := "button text", dataIcon := "L")(trans.cancel.txt())
)
}.getOrElse {
2019-04-18 02:47:59 -06:00
st.form(action := route, method := "post", novalidate,
2019-02-27 21:14:18 -07:00
dataRandomColorVariants,
dataType := typ,
dataAnon := ctx.isAnon.option("1"))(
2018-12-15 23:12:36 -07:00
fields,
2019-03-21 19:19:30 -06:00
if (ctx.blind) button(tpe := "submit")("Create the game")
2019-02-27 21:14:18 -07:00
else div(cls := "color-submits")(
2019-03-21 19:19:30 -06:00
translatedSideChoices.map {
case (key, name, _) => button(
(typ == "hook") option disabled,
2019-03-21 19:19:30 -06:00
tpe := "submit",
title := name,
cls := s"color-submits__button button button-green $key",
2019-03-21 19:19:30 -06:00
st.name := "color",
value := key
)(i)
}
2018-12-15 23:12:36 -07:00
)
)
2019-02-27 21:14:18 -07:00
},
ctx.me.ifFalse(ctx.blind).map { me =>
div(cls := "ratings")(
lila.rating.PerfType.nonPuzzle.map { perfType =>
div(cls := perfType.key)(
trans.perfRatingX.frag(
2019-04-11 04:12:16 -06:00
raw(s"""<strong data-icon="${perfType.iconChar}">${me.perfs(perfType.key).map(_.intRating).getOrElse("?")}</strong> ${perfType.name}""")
2018-12-15 23:12:36 -07:00
)
2019-02-27 21:14:18 -07:00
)
}
)
}
)
2018-12-15 23:12:36 -07:00
}