Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: choose 960 start position on the analysis board #16978

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/controllers/UserAnalysis.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import chess.format.Fen
import chess.variant.{ FromPosition, Standard, Variant }
import chess.variant.{ FromPosition, Standard, Variant, Chess960 }
import chess.{ ByColor, FullMoveNumber, Situation }
import play.api.libs.json.Json
import play.api.mvc.*
Expand Down Expand Up @@ -31,11 +31,16 @@ final class UserAnalysis(
case _ => load("", Standard)

def load(urlFen: String, variant: Variant) = Open:
val decodedFen: Option[Fen.Full] = lila.common.String
val input: Option[String] = lila.common.String
.decodeUriPath(urlFen)
.filter(_.trim.nonEmpty)
.orElse(get("fen"))
.map(Fen.Full.clean)
val parsed960: Option[Fen.Full] = input
.ifTrue(variant.chess960)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this takes the FEN input, if the variant is chess960

.orElse(get("position"))
.flatMap(_.toIntOption.flatMap(Chess960.positionToFen))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then, if position isn't defined, it attempts to convert the FEN into an Int?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping .map(Fen.Full.clean) on line 38 would help prevent that, as scala will then refuse to convert a Fen.Full to an Int

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to account for /chess960?poisiton={num} as well as /chess960/positionNum like we allow for fen. But I realise that was probably unnecessary

val decodedFen: Option[Fen.Full] = parsed960
.orElse(input.map(Fen.Full.clean))
val pov = makePov(decodedFen, variant)
val orientation = get("color").flatMap(Color.fromName) | pov.color
for
Expand All @@ -46,7 +51,7 @@ final class UserAnalysis(
orientation,
owner = false
)
page <- renderPage(views.board.userAnalysis(data, pov))
page <- renderPage(views.board.userAnalysis(data, pov, decodedFen))
yield Ok(page)
.withCanonical(routes.UserAnalysis.index)
.enforceCrossSiteIsolation
Expand Down
3 changes: 2 additions & 1 deletion app/views/board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import lila.ui.Context
def userAnalysis(
data: JsObject,
pov: Pov,
initialFen: Option[Fen.Full] = None,
withForecast: Boolean = false,
inlinePgn: Option[String] = None
)(using Context) =
views.analyse.ui
.userAnalysis(data, pov, withForecast)
.userAnalysis(data, pov, initialFen, withForecast)
.js(analyseNvuiTag)
.js(
views.analyse.bits.analyseModule(
Expand Down
40 changes: 33 additions & 7 deletions modules/analyse/src/main/ui/AnalyseUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class AnalyseUi(helpers: Helpers)(externalEngineEndpoint: String):
def userAnalysis(
data: JsObject,
pov: Pov,
initalFen: Option[chess.format.Fen.Full],
withForecast: Boolean = false
)(using ctx: Context) =
Page(trans.site.analysis.txt())
Expand All @@ -31,6 +32,8 @@ final class AnalyseUi(helpers: Helpers)(externalEngineEndpoint: String):
description = "Analyse chess positions and variations on an interactive chess board"
)
.flag(_.zoom):
val positionNum =
pov.game.variant.chess960.so(Chess960.positionNumber(initalFen | chess.format.Fen.initial))
main(
cls := List(
"analyse" -> true,
Expand All @@ -42,13 +45,36 @@ final class AnalyseUi(helpers: Helpers)(externalEngineEndpoint: String):
lila.ui.bits.mselect(
"analyse-variant",
span(cls := "text", dataIcon := iconByVariant(pov.game.variant))(pov.game.variant.name),
Variant.list.all.filter(FromPosition != _).map { v =>
a(
dataIcon := iconByVariant(v),
cls := (pov.game.variant == v).option("current"),
href := routes.UserAnalysis.parseArg(v.key.value)
)(v.name)
}
Variant.list.all
.filter(FromPosition != _)
.map: v =>
a(
dataIcon := iconByVariant(v),
cls := (pov.game.variant == v).option("current"),
href := routes.UserAnalysis.parseArg(v.key.value)
)(v.name)
),
pov.game.variant.chess960.option(
div(cls := "jump-960")(
positionNum
.map(pos => label(`for` := "position")(trans.site.chess960StartPosition.txt(pos))),
br,
form(
cls := "control-960",
method := "GET",
action := routes.UserAnalysis.parseArg("chess960")
)(
input(
id := "position",
`type` := "number",
name := "position",
min := 0,
max := 959,
value := positionNum
),
form3.submit(trans.site.apply())
)
)
),
pov.game.variant.standard.option(
fieldset(cls := "analyse__wiki empty toggle-box toggle-box--toggle", id := "wikibook-field")(
Expand Down
11 changes: 11 additions & 0 deletions ui/analyse/css/_analyse.free.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ $label-width: 5ch;
}
}
}

.jump-960 {
margin-top: 1em;
margin-bottom: 1em; //col1
.control-960 {
margin-top: 0.7em;
button {
margin-left: 1em;
}
}
}