Skip to content

Commit

Permalink
buggy implementation of study explorer game insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 19, 2017
1 parent 95aa07b commit 750d9e8
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 35 deletions.
1 change: 0 additions & 1 deletion modules/explorer/src/main/ExplorerIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ private final class ExplorerIndexer(

private val maxGames = Int.MaxValue
private val batchSize = 50
private val maxPlies = 50
private val separator = "\n\n\n"
private val datePattern = "yyyy-MM-dd"
private val dateFormatter = DateTimeFormat forPattern datePattern
Expand Down
5 changes: 4 additions & 1 deletion modules/explorer/src/main/package.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package lila

package object explorer extends PackageObject with WithPlay
package object explorer extends PackageObject with WithPlay {

val maxPlies = 50
}
29 changes: 4 additions & 25 deletions modules/study/src/main/ChapterMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package lila.study
import chess.format.pgn.Tag
import chess.format.{ Forsyth, FEN }
import chess.variant.{ Variant, Crazyhouse }
import lila.chat.Chat
import lila.game.{ Game, Pov, GameRepo, Namer }
import lila.importer.Importer
import lila.round.JsonView.WithFlags
import lila.user.User
import lila.chat.Chat

private final class ChapterMaker(
domain: String,
Expand Down Expand Up @@ -140,29 +139,9 @@ private final class ChapterMaker(
}

def game2root(game: Game, initialFen: Option[FEN] = None): Fu[Node.Root] =
initialFen.fold(GameRepo initialFen game) { fen =>
fuccess(fen.value.some)
} map { initialFen =>
val root = Node.Root.fromRoot {
lila.round.TreeBuilder(
game = game,
analysis = none,
initialFen = initialFen | game.variant.initialFen,
withFlags = WithFlags(clocks = true)
)
}
endComment(game).fold(root) { comment =>
root updateMainlineLast { _.setComment(comment) }
}
}

private def endComment(game: Game) = game.finished option {
import lila.tree.Node.Comment
val result = chess.Color.showResult(game.winnerColor)
val status = lila.game.StatusText(game)
val text = s"$result $status"
Comment(Comment.Id.make, Comment.Text(text), Comment.Author.Lichess)
}
initialFen.fold(GameRepo initialFen game map2 FEN.apply) { fen =>
fuccess(fen.some)
} map { GameToRoot(game, _, withClocks = true) }

private val UrlRegex = {
val escapedDomain = domain.replace(".", "\\.")
Expand Down
42 changes: 35 additions & 7 deletions modules/study/src/main/ExplorerGame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.util.Try
import chess.format.pgn.{ Parser, Reader, ParsedPgn, Tag, TagType }
import lila.common.LightUser
import lila.game.{ Game, Namer }
import lila.round.JsonView.WithFlags
import lila.tree.Node.Comment
import lila.user.User

Expand All @@ -17,14 +18,43 @@ private final class ExplorerGame(
def quote(gameId: Game.ID): Fu[Option[Comment]] =
importer(gameId) map {
_ ?? { game =>
Comment(
id = Comment.Id.make,
text = Comment.Text(gameTitle(game)),
by = Comment.Author.Lichess
).some
gameComment(game).some
}
}

def insert(userId: User.ID, study: Study, position: Position, gameId: Game.ID): Fu[Option[(Chapter, Path)]] =
importer(gameId) map {
_ ?? { game =>
position.node ?? { fromNode =>
val gameRoot = GameToRoot(game, none, false)
merge(fromNode, position.path, gameRoot, gameComment(game)) flatMap {
case (newNode, path) => position.chapter.addNode(newNode, path) map (_ -> path)
}
}
}
}

private def merge(fromNode: RootOrNode, fromPath: Path, game: Node.Root, comment: Comment): Option[(Node, Path)] = {
val gameNodes = game.mainline.take(lila.explorer.maxPlies).reverse
val fromNodes = fromNode.mainline.take(lila.explorer.maxPlies - fromNode.ply)
val fromEndPath = fromNodes.drop(1).foldLeft(fromPath)(_ + _)
val (path, foundGameNode) = fromNodes.reverse.foldLeft((fromEndPath, none[Node])) {
case ((path, None), fromNode) =>
path.init -> gameNodes.find(_.fen == fromNode.fen)
case (found, _) => found
}
foundGameNode.flatMap(_.children.first).map { nextGameNode =>
val commentedNode = nextGameNode setComment comment
commentedNode -> path
}
}

private def gameComment(game: Game) = Comment(
id = Comment.Id.make,
text = Comment.Text(gameTitle(game)),
by = Comment.Author.Lichess
)

private def gameYear(pgn: Option[ParsedPgn], g: Game): Int = pgn.flatMap { p =>
p.tag(_.UTCDate) orElse p.tag(_.Date)
}.flatMap { pgnDate =>
Expand All @@ -44,6 +74,4 @@ private final class ExplorerGame(
}
s"$white - $black, $result, $event"
}

def insert(userId: User.ID, study: Study, chapter: Chapter, gameId: Game.ID) = ???
}
30 changes: 30 additions & 0 deletions modules/study/src/main/GameToRoot.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lila.study

import chess.format.FEN
import lila.game.Game
import lila.round.JsonView.WithFlags

private object GameToRoot {

def apply(game: Game, initialFen: Option[FEN], withClocks: Boolean): Node.Root = {
val root = Node.Root.fromRoot {
lila.round.TreeBuilder(
game = game,
analysis = none,
initialFen = initialFen.fold(game.variant.initialFen)(_.value),
withFlags = WithFlags(clocks = withClocks)
)
}
endComment(game).fold(root) { comment =>
root updateMainlineLast { _.setComment(comment) }
}
}

private def endComment(game: Game) = game.finished option {
import lila.tree.Node.Comment
val result = chess.Color.showResult(game.winnerColor)
val status = lila.game.StatusText(game)
val text = s"$result $status"
Comment(Comment.Id.make, Comment.Text(text), Comment.Author.Lichess)
}
}
3 changes: 3 additions & 0 deletions modules/study/src/main/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sealed trait RootOrNode {
val gamebook: Option[Gamebook]
val glyphs: Glyphs
def fullMoveNumber = 1 + ply / 2
def mainline: List[Node]
}

case class Node(
Expand Down Expand Up @@ -62,6 +63,8 @@ case class Node(
children.first.fold(f(this)) { main =>
copy(children = children.update(main updateMainlineLast f))
}

override def toString = s"$id:${move.san}"
}

object Node {
Expand Down
2 changes: 2 additions & 0 deletions modules/study/src/main/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ case class Path(ids: List[UciCharPair]) extends AnyVal {

def tail: Path = Path(ids drop 1)

def init: Path = Path(ids take (ids.length - 1))

def split: Option[(UciCharPair, Path)] = head.map(_ -> tail)

def isEmpty = ids.isEmpty
Expand Down
2 changes: 2 additions & 0 deletions modules/study/src/main/Position.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lila.study
case class Position(chapter: Chapter, path: Path) {

def ref = Position.Ref(chapter.id, path)

def node: Option[RootOrNode] = chapter.root nodeAt path
}

case object Position {
Expand Down
14 changes: 13 additions & 1 deletion modules/study/src/main/StudyApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,19 @@ final class StudyApi(

def explorerGame(userId: User.ID, studyId: Study.Id, data: actorApi.ExplorerGame, uid: Uid) = sequenceStudyWithChapter(studyId, data.position.chapterId) {
case Study.WithChapter(study, chapter) => Contribute(userId, study) {
if (data.insert) ???
if (data.insert) explorerGameHandler.insert(userId, study, Position(chapter, data.position.path), data.gameId) flatMap {
case None =>
fufail(s"Invalid explorerGame insert $studyId $data") >>-
reloadUidBecauseOf(study, uid, chapter.id)
case Some((chapter, path)) =>
studyRepo.updateNow(study)
chapter.root.nodeAt(path) ?? { parent =>
chapterRepo.setChildren(chapter, path, parent.children) >>- {
sendStudyEnters(study, userId)
sendTo(study, Socket.ReloadAll)
}
}
}
else explorerGameHandler.quote(data.gameId) flatMap {
_ ?? {
doSetComment(userId, study, Position(chapter, data.position.path), _, uid)
Expand Down

0 comments on commit 750d9e8

Please sign in to comment.