Skip to content

Commit

Permalink
fix tourney creation form for web/mobile/oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed May 10, 2018
1 parent d67aaa0 commit c4404dc
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app/controllers/RequestGetter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import lila.api._
import lila.socket.Socket.Uid
import lila.user.UserContext
import lila.common.Form.trueish

import play.api.mvc.RequestHeader

Expand Down Expand Up @@ -39,6 +40,4 @@ trait RequestGetter {

protected def getBoolOpt(name: String, req: RequestHeader) =
(getInt(name, req) map (trueish)) orElse (get(name, req) map trueish)

private def trueish(v: Any) = v == 1 || v == "true"
}
2 changes: 1 addition & 1 deletion app/views/base/form/checkbox.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<label>
@text
<input type="checkbox" id="@field.id" name="@field.name" value="@value" @((field.value == Some(value.toString)).fold("checked", "")) />
<input type="checkbox" id="@field.id" name="@field.name" value="@value" @if(field.value.has(value.toString)){"checked"} />
</label>
2 changes: 1 addition & 1 deletion app/views/tournament/form.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h1>@trans.createANewTournament()</h1>
<tbody>
<tr>
<th><label for="isprivate">@trans.isPrivate()</label></th>
<td><input type="checkbox" name="private" id="isprivate" @if(form("private").value.isDefined) { checked } /></td>
<td><input type="checkbox" name="private" id="isprivate" @if(form("private").value.exists(lila.common.Form.trueish)) { checked } /></td>
</tr>
<tr class="password">
<th><label for="@form("password").id">@trans.password()</label></th>
Expand Down
2 changes: 1 addition & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ GET /api/game/:id controllers.Api.game(id: String)
GET /api/games/team/:teamId controllers.Api.gamesVsTeam(teamId: String)
GET /api/tournament controllers.Api.currentTournaments
GET /api/tournament/:id controllers.Api.tournament(id: String)
POST /api/tournament/new controllers.Tournament.apiCreate
POST /api/tournament controllers.Tournament.apiCreate
GET /api/status controllers.Api.status
GET /api/socket controllers.Main.apiWebsocket
GET /api/users/status controllers.Api.usersStatus
Expand Down
12 changes: 12 additions & 0 deletions modules/common/src/main/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ object Form {
def stringIn(choices: Iterable[(String, String)]) =
text.verifying(hasKey(choices, _))

def tolerantBoolean = of[Boolean](formatter.tolerantBooleanFormatter)

def hasKey[A](choices: Iterable[(A, _)], key: A) =
choices.map(_._1).toList contains key

def trueish(v: Any) = v == 1 || v == "1" || v == "true" || v == "on" || v == "yes"

private def pluralize(pattern: String, nb: Int) =
pattern.replace("{s}", (nb != 1).fold("s", ""))

Expand All @@ -47,6 +51,14 @@ object Form {
def bind(key: String, data: Map[String, String]) = intFormat.bind(key, data).right map to
def unbind(key: String, value: A) = intFormat.unbind(key, from(value))
}
val tolerantBooleanFormatter: Formatter[Boolean] = new Formatter[Boolean] {
override val format = Some(("format.boolean", Nil))
def bind(key: String, data: Map[String, String]) =
Right(data.get(key).getOrElse("false")).right.flatMap { v =>
Right(trueish(v))
}
def unbind(key: String, value: Boolean) = Map(key -> value.toString)
}
}

object UTCDate {
Expand Down
10 changes: 4 additions & 6 deletions modules/tournament/src/main/DataForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DataForm {
waitMinutes = waitMinuteDefault,
variant = chess.variant.Standard.key,
position = StartingPosition.initial.fen,
`private` = None,
`private` = false,
password = None,
mode = Mode.Rated.id.some,
conditionsOption = Condition.DataForm.AllSetup.default.some,
Expand All @@ -46,7 +46,7 @@ final class DataForm {
"variant" -> nonEmptyText.verifying(v => guessVariant(v).isDefined),
"position" -> nonEmptyText,
"mode" -> optional(number.verifying(Mode.all map (_.id) contains _)),
"private" -> optional(text.verifying("on" == _)),
"private" -> tolerantBoolean,
"password" -> optional(nonEmptyText),
"conditions" -> optional(Condition.DataForm.all),
"berserkable" -> boolean
Expand Down Expand Up @@ -117,7 +117,7 @@ private[tournament] case class TournamentSetup(
variant: String,
position: String,
mode: Option[Int],
`private`: Option[String],
`private`: Boolean,
password: Option[String],
conditionsOption: Option[Condition.DataForm.AllSetup],
berserkable: Boolean
Expand All @@ -129,7 +129,7 @@ private[tournament] case class TournamentSetup(

def validTiming = (minutes * 60) >= (3 * estimatedGameDuration)

def validPublic = isPrivate || {
def validPublic = `private` || {
DataForm.clockTimes.contains(clockTime) &&
DataForm.clockIncrements.contains(clockIncrement) &&
DataForm.minutes.contains(minutes)
Expand All @@ -145,7 +145,5 @@ private[tournament] case class TournamentSetup(
realMode == Mode.Casual ||
lila.game.Game.allowRated(realVariant, clockConfig)

def isPrivate = `private`.isDefined

private def estimatedGameDuration = 60 * clockTime + 30 * clockIncrement
}
4 changes: 2 additions & 2 deletions modules/tournament/src/main/TournamentApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ final class TournamentApi(
minutes = setup.minutes,
waitMinutes = setup.waitMinutes,
mode = setup.realMode,
`private` = setup.isPrivate,
password = setup.password.ifTrue(setup.isPrivate),
`private` = setup.`private`,
password = setup.password ifTrue setup.`private`,
system = System.Arena,
variant = setup.realVariant,
position = DataForm.startingPosition(setup.position, setup.realVariant),
Expand Down

0 comments on commit c4404dc

Please sign in to comment.