simplify and fix game import - or at least break it in a different way
parent
293b6b60fb
commit
10601838f1
|
@ -20,11 +20,10 @@ private[importer] final class DataForm {
|
|||
ImportData(pgn, none).preprocess(none).isSuccess
|
||||
}
|
||||
|
||||
private[importer] case class Result(status: Status, winner: Option[Color])
|
||||
private[importer] case class TagResult(status: Status, winner: Option[Color])
|
||||
case class Preprocessed(
|
||||
game: NewGame,
|
||||
replay: Replay,
|
||||
result: Result,
|
||||
initialFen: Option[FEN],
|
||||
parsed: ParsedPgn
|
||||
)
|
||||
|
@ -73,15 +72,6 @@ case class ImportData(pgn: String, analyse: Option[String]) {
|
|||
case Some(_) => Status.UnknownFinish
|
||||
}
|
||||
|
||||
val result =
|
||||
parsed.tags.resultColor
|
||||
.ifFalse(game.situation.end)
|
||||
.fold(Result(Status.Started, none)) {
|
||||
case Some(color) => Result(status, color.some)
|
||||
case None if status == Status.Outoftime => Result(status, none)
|
||||
case None => Result(Status.Draw, none)
|
||||
}
|
||||
|
||||
val date = parsed.tags.anyDate
|
||||
|
||||
def name(whichName: TagPicker, whichRating: TagPicker): String = parsed.tags(whichName).fold("?") { n =>
|
||||
|
@ -95,9 +85,21 @@ case class ImportData(pgn: String, analyse: Option[String]) {
|
|||
mode = Mode.Casual,
|
||||
source = Source.Import,
|
||||
pgnImport = PgnImport.make(user = user, date = date, pgn = pgn).some
|
||||
).start
|
||||
).sloppy.start |> { dbGame =>
|
||||
// apply the result from the board or the tags
|
||||
game.situation.status match {
|
||||
case Some(situationStatus) => dbGame.finish(situationStatus, game.situation.winner).game
|
||||
case None => parsed.tags.resultColor.map {
|
||||
case Some(color) => TagResult(status, color.some)
|
||||
case None if status == Status.Outoftime => TagResult(status, none)
|
||||
case None => TagResult(Status.Draw, none)
|
||||
}.filter(_.status > Status.Started).fold(dbGame) { res =>
|
||||
dbGame.finish(res.status, res.winner).game
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Preprocessed(dbGame, replay.copy(state = game), result, initialFen, parsed)
|
||||
Preprocessed(NewGame(dbGame), replay.copy(state = game), initialFen, parsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,18 +18,10 @@ final class Importer(
|
|||
def gameExists(processing: => Fu[Game]): Fu[Game] =
|
||||
GameRepo.findPgnImport(data.pgn) flatMap { _.fold(processing)(fuccess) }
|
||||
|
||||
def applyResult(game: Game, result: Result, situation: Situation): Game =
|
||||
if (game.finished) game
|
||||
else situation.status match {
|
||||
case Some(situationStatus) => game.finish(situationStatus, situation.winner).game
|
||||
case _ if result.status <= Status.Started => game
|
||||
case _ => game.finish(result.status, result.winner).game
|
||||
}
|
||||
|
||||
gameExists {
|
||||
(data preprocess user).future flatMap {
|
||||
case Preprocessed(g, replay, result, initialFen, _) =>
|
||||
val game = applyResult(forceId.fold(g.sloppy)(g.withId), result, replay.state.situation)
|
||||
case Preprocessed(g, replay, initialFen, _) =>
|
||||
val game = forceId.fold(g.sloppy)(g.withId)
|
||||
(GameRepo.insertDenormalized(game, initialFen = initialFen)) >> {
|
||||
game.pgnImport.flatMap(_.user).isDefined ?? GameRepo.setImportCreatedAt(game)
|
||||
} >> {
|
||||
|
@ -45,6 +37,6 @@ final class Importer(
|
|||
}
|
||||
|
||||
def inMemory(data: ImportData): Valid[(Game, Option[FEN])] = data.preprocess(user = none).map {
|
||||
case Preprocessed(game, replay, _, fen, _) => (game withId "synthetic", fen)
|
||||
case Preprocessed(game, replay, fen, _) => (game withId "synthetic", fen)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ object PgnImport {
|
|||
|
||||
def apply(pgn: String, contributors: List[LightUser]): Valid[Result] =
|
||||
ImportData(pgn, analyse = none).preprocess(user = none).map {
|
||||
case prep @ Preprocessed(game, replay, result, initialFen, parsedPgn) =>
|
||||
case prep @ Preprocessed(game, replay, initialFen, parsedPgn) =>
|
||||
val annotator = findAnnotator(parsedPgn, contributors)
|
||||
parseComments(parsedPgn.initialPosition.comments, annotator) match {
|
||||
case (shapes, _, comments) =>
|
||||
|
@ -51,17 +51,13 @@ object PgnImport {
|
|||
).fold(variations)(_ :: variations).toVector
|
||||
}
|
||||
)
|
||||
val end: Option[End] = {
|
||||
(if (game.finished) game.status else result.status).some
|
||||
.filter(chess.Status.Aborted <=).map { status =>
|
||||
val winner = game.winnerColor orElse result.winner
|
||||
End(
|
||||
status = status,
|
||||
winner = winner,
|
||||
resultText = chess.Color.showResult(winner),
|
||||
statusText = lila.game.StatusText(status, winner, game.variant)
|
||||
)
|
||||
}
|
||||
val end: Option[End] = (game.finished option game.status).map { status =>
|
||||
End(
|
||||
status = status,
|
||||
winner = game.winnerColor,
|
||||
resultText = chess.Color.showResult(game.winnerColor),
|
||||
statusText = lila.game.StatusText(status, game.winnerColor, game.variant)
|
||||
)
|
||||
}
|
||||
val commented =
|
||||
if (root.mainline.lastOption.??(_.isCommented)) root
|
||||
|
|
Loading…
Reference in New Issue