Skip to content

Commit 1a7c315

Browse files
committed
loading opening from Game, once, simplifies it all
1 parent f9d2714 commit 1a7c315

File tree

9 files changed

+15
-23
lines changed

9 files changed

+15
-23
lines changed

app/controllers/Analyse.scala

+1-7
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ object Analyse extends LilaController {
5959
Env.game.crosstableApi(pov.game) flatMap {
6060
case ((((pgn, analysis), tour), division), crosstable) =>
6161
Env.api.roundApi.watcher(pov, Env.api.version, tv = none, analysis.map(pgn -> _)) map { data =>
62-
val opening = gameOpening(pov.game)
6362
Ok(html.analyse.replay(
6463
pov,
6564
data,
66-
Env.analyse.annotator(pgn, analysis, opening, pov.game.winnerColor, pov.game.status, pov.game.clock).toString,
67-
opening,
65+
Env.analyse.annotator(pgn, analysis, pov.game.opening, pov.game.winnerColor, pov.game.status, pov.game.clock).toString,
6866
analysis,
6967
analysis filter (_.done) map { a => AdvantageChart(a.infoAdvices, pov.game.pgnMoves) },
7068
tour,
@@ -73,8 +71,4 @@ object Analyse extends LilaController {
7371
division))
7472
}
7573
}
76-
77-
private def gameOpening(game: GameModel) =
78-
if (game.fromPosition || game.variant.exotic) none
79-
else chess.OpeningExplorer openingOf game.pgnMoves
8074
}

app/controllers/Export.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Export extends LilaController {
1919
case None => for {
2020
pgn Env.game.pgnDump(game)
2121
analysis (~get("as") != "raw") ?? (Env.analyse.analyser getDone game.id)
22-
} yield Env.analyse.annotator(pgn, analysis, gameOpening(game), game.winnerColor, game.status, game.clock).toString
22+
} yield Env.analyse.annotator(pgn, analysis, game.opening, game.winnerColor, game.status, game.clock).toString
2323
}) map { content =>
2424
Ok(content).withHeaders(
2525
CONTENT_TYPE -> ContentTypes.TEXT,

app/views/analyse/replay.scala.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@(pov: Pov, data: play.api.libs.json.JsObject, pgn: String, opening: Option[chess.OpeningExplorer.Opening], analysis: Option[lila.analyse.Analysis], advantageChart: Option[String], tour: Option[lila.tournament.Tournament], timeChart: lila.analyse.TimeChart, cross: Option[lila.game.Crosstable], division: lila.game.Divider.Division)(implicit ctx: Context)
1+
@(pov: Pov, data: play.api.libs.json.JsObject, pgn: String, analysis: Option[lila.analyse.Analysis], advantageChart: Option[String], tour: Option[lila.tournament.Tournament], timeChart: lila.analyse.TimeChart, cross: Option[lila.game.Crosstable], division: lila.game.Divider.Division)(implicit ctx: Context)
22

33
@import pov._
44

5-
@title = @{ s"${playerText(pov.player)} vs ${playerText(pov.opponent)} in $gameId : ${opening.fold(trans.replayAndAnalyse.str())(_.fullName)}" }
5+
@title = @{ s"${playerText(pov.player)} vs ${playerText(pov.opponent)} in $gameId : ${game.opening.fold(trans.replayAndAnalyse.str())(_.fullName)}" }
66

77
@moreCss = {
88
@cssTag("analyse.css")

app/views/game/widgets.scala.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
@if(g.turns > 0) {
6161
@defining(g openingPgnMoves 20) { pgnMoves =>
6262
@if(!g.fromPosition) {
63-
@chess.OpeningExplorer.openingOf(pgnMoves).map { opening =>
63+
@g.opening.map { opening =>
6464
<div class="opening">@opening.name</div>
6565
}
6666
}

modules/api/src/main/GameApi.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private[api] final class GameApi(
138138
"analysis" -> analysisOption.ifTrue(withAnalysis).|@|(pgnOption).apply(analysisApi.game),
139139
"moves" -> withMoves.option(g.pgnMoves mkString " "),
140140
"opening" -> withOpening.?? {
141-
chess.OpeningExplorer.openingOf(g.pgnMoves) map { opening =>
141+
g.opening map { opening =>
142142
Json.obj("code" -> opening.code, "name" -> opening.name)
143143
}
144144
},

modules/game/src/main/Game.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,15 @@ case class Game(
372372

373373
def resetTurns = copy(turns = 0)
374374

375+
lazy val opening =
376+
if (playable || fromPosition || variant.exotic) none
377+
else chess.OpeningExplorer openingOf pgnMoves
378+
375379
private def playerMaps[A](f: Player => Option[A]): List[A] = players.map(f).flatten
376380
}
377381

378382
object Game {
379383

380-
381384
val analysableVariants: Set[Variant] = Set(Variant.Standard, Variant.Chess960, Variant.KingOfTheHill)
382385
val unanalysableVariants: Set[Variant] = Variant.all.toSet -- analysableVariants
383386

@@ -511,7 +514,7 @@ object Game {
511514
pgnImport = r.getO[PgnImport](pgnImport)(PgnImport.pgnImportBSONHandler),
512515
tournamentId = r strO tournamentId,
513516
tvAt = r dateO tvAt,
514-
analysed = r boolD analysed)
517+
analysed = r boolD analysed)
515518
)
516519
}
517520

modules/game/src/main/PgnDump.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ final class PgnDump(
4949

5050
private def tags(game: Game): Fu[List[Tag]] = gameLightUsers(game) match {
5151
case (wu, bu) =>
52-
val opening: Option[OpeningExplorer.Opening] =
53-
if (customStartPosition(game.variant)) none
54-
else OpeningExplorer openingOf game.pgnMoves
5552
(game.variant.standard.fold(fuccess(none), GameRepo initialFen game.id)) map { initialFen =>
5653
List(
5754
Tag(_.Event, game.rated.fold("Rated game", "Casual game")),
@@ -65,8 +62,8 @@ final class PgnDump(
6562
Tag("PlyCount", game.turns),
6663
Tag(_.Variant, game.variant.name.capitalize),
6764
Tag(_.TimeControl, game.clock.fold("-") { c => s"${c.limit}+${c.increment}" }),
68-
Tag(_.ECO, opening.fold("?")(_.code)),
69-
Tag(_.Opening, opening.fold("?")(_.name))
65+
Tag(_.ECO, game.opening.fold("?")(_.code)),
66+
Tag(_.Opening, game.opening.fold("?")(_.name))
7067
) ::: customStartPosition(game.variant).??(List(
7168
Tag(_.FEN, initialFen | "?"),
7269
Tag("SetUp", "1")

modules/gameSearch/src/main/Indexer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private[gameSearch] final class Indexer(
111111
ai -> game.aiLevel,
112112
date -> (ElasticSearch.Date.formatter print game.createdAt).some,
113113
duration -> game.estimateTotalTime.some,
114-
opening -> (chess.OpeningExplorer openingOf game.pgnMoves map (_.code.toLowerCase)),
114+
opening -> (game.opening map (_.code.toLowerCase)),
115115
analysed -> hasAnalyse.some
116116
).collect {
117117
case (key, Some(value)) => key -> value

modules/round/src/main/JsonView.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ final class JsonView(
136136
UserRepo.pair(pov.player.userId, pov.opponent.userId) map {
137137
case (((initialFen, socket), chat), (playerUser, opponentUser)) =>
138138
import pov._
139-
val opening = if (game.playable || initialFen.isDefined || game.fromPosition || game.variant.exotic) none
140-
else chess.OpeningExplorer openingOf game.pgnMoves
141139
Json.obj(
142140
"game" -> Json.obj(
143141
"id" -> gameId,
@@ -156,7 +154,7 @@ final class JsonView(
156154
"rematch" -> game.next,
157155
"source" -> game.source.map(sourceJson),
158156
"moves" -> game.pgnMoves.mkString(" "),
159-
"opening" -> opening.map { o =>
157+
"opening" -> game.opening.map { o =>
160158
Json.obj(
161159
"code" -> o.code,
162160
"name" -> o.name,

0 commit comments

Comments
 (0)