lila/app/views/setup/forms.scala

193 lines
6.0 KiB
Scala
Raw Normal View History

2018-12-15 23:12:36 -07:00
package views.html.setup
2020-09-10 03:04:19 -06:00
import controllers.routes
2018-12-15 23:12:36 -07:00
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.user.User
object forms {
import bits._
2021-05-22 10:53:19 -06:00
def hook(form: Form[_], forceTimeMode: Boolean = false)(implicit ctx: Context) =
2019-12-13 07:30:20 -07:00
layout(
"hook",
trans.createAGame(),
2021-05-22 10:53:19 -06:00
routes.Setup.hook("sri-placeholder"),
forceTimeMode = forceTimeMode
2019-12-13 07:30:20 -07:00
) {
2018-12-15 23:12:36 -07:00
frag(
renderVariant(form, translatedVariantChoicesWithVariants),
renderTimeMode(form, allowAnon = false),
2018-12-15 23:12:36 -07:00
ctx.isAuth option frag(
div(cls := "mode_choice buttons")(
renderRadios(form("mode"), translatedModeChoices)
),
ctx.noBlind option div(cls := "optional_config")(
2020-09-10 03:04:19 -06:00
div(cls := "rating-range-config")(
2019-04-22 03:42:25 -06:00
trans.ratingRange(),
2020-09-10 03:04:19 -06:00
div(cls := "rating-range") {
val field = form("ratingRange")
frag(
renderInput(field),
input(
name := s"${field.name}_range_min",
tpe := "range",
2020-09-10 04:18:03 -06:00
cls := "range rating-range__min"
2020-09-10 03:04:19 -06:00
),
span(cls := "rating-min"),
"/",
span(cls := "rating-max"),
input(
name := s"${field.name}_range_max",
tpe := "range",
2020-09-10 04:18:03 -06:00
cls := "range rating-range__max"
2020-09-10 03:04:19 -06:00
)
2018-12-15 23:12:36 -07:00
)
2020-09-10 03:04:19 -06:00
}
2018-12-15 23:12:36 -07:00
)
)
)
)
}
2020-05-05 22:11:15 -06:00
def ai(form: Form[_], ratings: Map[Int, Int], validFen: Option[lila.setup.ValidFen])(implicit
ctx: Context
2019-12-13 07:30:20 -07:00
) =
layout("ai", trans.playWithTheMachine(), routes.Setup.ai) {
2018-12-15 23:12:36 -07:00
frag(
renderVariant(form, translatedAiVariantChoices),
2020-08-16 06:42:29 -06:00
fenInput(form("fen"), strict = true, validFen),
renderTimeMode(form, allowAnon = true),
2019-12-13 07:30:20 -07:00
if (ctx.blind)
frag(
renderLabel(form("level"), trans.strength()),
2019-12-13 07:30:20 -07:00
renderSelect(form("level"), lila.setup.AiConfig.levelChoices),
blindSideChoice(form)
)
else
frag(
br,
trans.strength(),
2019-12-13 07:30:20 -07:00
div(cls := "level buttons")(
div(id := "config_level")(
renderRadios(form("level"), lila.setup.AiConfig.levelChoices)
),
div(cls := "ai_info")(
2020-09-21 01:28:28 -06:00
ratings.toList.map { case (level, _) =>
2021-07-05 03:27:54 -06:00
div(cls := s"${prefix}level_$level")(
2021-09-12 15:31:59 -06:00
trans.aiNameLevelAiLevel("Fairy-Stockfish 14", level)
2021-07-05 03:27:54 -06:00
)
2019-12-13 07:30:20 -07:00
}
)
)
2018-12-15 23:12:36 -07:00
)
)
}
def friend(
2019-12-13 07:30:20 -07:00
form: Form[_],
user: Option[User],
error: Option[String],
validFen: Option[lila.setup.ValidFen]
2018-12-15 23:12:36 -07:00
)(implicit ctx: Context) =
layout(
"friend",
(if (user.isDefined) trans.challenge.challengeToPlay else trans.playWithAFriend)(),
2018-12-15 23:12:36 -07:00
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)))
2019-12-13 07:30:20 -07:00
)(
frag(
2018-12-15 23:12:36 -07:00
user.map { u =>
userLink(u, cssClass = "target".some)
},
renderVariant(form, translatedVariantChoicesWithVariantsAndFen),
2020-08-16 06:42:29 -06:00
fenInput(form("fen"), strict = false, validFen),
renderTimeMode(form, allowAnon = true),
2018-12-15 23:12:36 -07:00
ctx.isAuth option div(cls := "mode_choice buttons")(
renderRadios(form("mode"), translatedModeChoices)
2019-03-21 19:19:30 -06:00
),
blindSideChoice(form)
2019-12-13 07:30:20 -07:00
)
)
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(
2019-04-22 03:42:25 -06:00
renderLabel(form("color"), trans.side()),
2019-03-21 19:19:30 -06:00
renderSelect(form("color").copy(value = "random".some), translatedSideChoices)
)
2018-12-15 23:12:36 -07:00
private def layout(
2019-12-13 07:30:20 -07:00
typ: String,
titleF: Frag,
route: Call,
2021-05-22 10:53:19 -06:00
error: Option[Frag] = None,
2021-06-05 16:17:43 -06:00
forceTimeMode: Boolean = false
2018-12-15 23:12:36 -07:00
)(fields: Frag)(implicit ctx: Context) =
2019-02-27 21:14:18 -07:00
div(cls := error.isDefined option "error")(
h2(titleF),
2019-12-13 07:30:20 -07:00
error
.map { e =>
frag(
p(cls := "error")(e),
br,
a(href := routes.Lobby.home, cls := "button text", dataIcon := "")(trans.cancel.txt())
2019-12-13 07:30:20 -07:00
)
}
.getOrElse {
postForm(
action := route,
novalidate,
dataRandomColorVariants,
dataType := typ,
2021-05-22 10:53:19 -06:00
dataAnon := ctx.isAnon.option("1"),
dataForceTimeMode := forceTimeMode.option("1")
2019-12-13 07:30:20 -07:00
)(
2018-12-15 23:12:36 -07:00
fields,
2019-08-02 01:54:15 -06:00
if (ctx.blind) submitButton("Create the game")
2019-12-13 07:30:20 -07:00
else
div(cls := "color-submits")(
2020-09-21 01:28:28 -06:00
translatedSideChoices.map { case (key, name, _) =>
submitButton(
(typ == "hook") option disabled,
title := name,
cls := s"color-submits__button button button-metal $key",
st.name := "color",
value := key
)(i)
2019-12-13 07:30:20 -07:00
}
)
2018-12-15 23:12:36 -07:00
)
2019-12-13 07:30:20 -07:00
},
2019-02-27 21:14:18 -07:00
ctx.me.ifFalse(ctx.blind).map { me =>
div(cls := "ratings")(
2020-09-10 03:04:19 -06:00
form3.hidden("rating", "?"),
2019-02-27 21:14:18 -07:00
lila.rating.PerfType.nonPuzzle.map { perfType =>
{
val rating = me
.perfs(perfType.key)
.map(_.intRating.toString)
.getOrElse("?")
div(cls := perfType.key)(
if (ctx.pref.showRatings)
trans.perfRatingX(
raw(s"""<strong data-icon="${perfType.iconChar}">${rating}</strong> ${perfType.trans}""")
)
else
frag(
i(dataIcon := perfType.iconChar),
strong(cls := "none")(rating), // To calculate rating range in JS
perfType.trans
)
2018-12-15 23:12:36 -07:00
)
}
2019-02-27 21:14:18 -07:00
}
)
}
)
2018-12-15 23:12:36 -07:00
}