importer app
This commit is contained in:
parent
46d3370a67
commit
2a83d25eae
|
@ -1,17 +1,17 @@
|
|||
package controllers
|
||||
|
||||
import lila.app._
|
||||
import views._
|
||||
import lila.user.Context
|
||||
import lila.game.ListMenu
|
||||
import views._
|
||||
|
||||
import play.api.mvc.Result
|
||||
|
||||
private[controllers] trait BaseGame { self: LilaController ⇒
|
||||
|
||||
protected def nbAnalysis = Env.analyse.cached.nbAnalysis _
|
||||
protected def bookmarkApi = Env.bookmark.api
|
||||
protected def listMenu = Env.game.listMenu
|
||||
|
||||
protected def makeListMenu(implicit ctx: Context) =
|
||||
listMenu(bookmarkApi countByUser _, nbAnalysis, ctx.me)
|
||||
protected def makeListMenu(implicit ctx: Context): Fu[ListMenu] =
|
||||
Env.game.listMenu(
|
||||
Env.bookmark.api countByUser _,
|
||||
Env.analyse.cached.nbAnalysis _,
|
||||
ctx.me)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ object Game extends LilaController with BaseGame {
|
|||
def bookmark(page: Int) = Auth { implicit ctx ⇒
|
||||
me ⇒
|
||||
Reasonable(page) {
|
||||
bookmarkApi.gamePaginatorByUser(me, page) zip makeListMenu map {
|
||||
Env.bookmark.api.gamePaginatorByUser(me, page) zip makeListMenu map {
|
||||
case (pag, menu) ⇒ html.game.bookmarked(pag, menu)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,28 +5,28 @@ import views._
|
|||
|
||||
object Importer extends LilaController with BaseGame {
|
||||
|
||||
// private def forms = env.importer.forms
|
||||
// private def importer = env.importer.importer
|
||||
private def env = Env.importer
|
||||
|
||||
def importGame = TODO
|
||||
// Open { implicit ctx ⇒
|
||||
// Ok(html.game.importGame(makeListMenu, forms.importForm))
|
||||
// }
|
||||
|
||||
def sendGame = TODO
|
||||
// OpenBody { implicit ctx ⇒
|
||||
// Async {
|
||||
// implicit def req = ctx.body
|
||||
// forms.importForm.bindFromRequest.fold(
|
||||
// failure ⇒ Akka.future {
|
||||
// Ok(html.game.importGame(makeListMenu, failure))
|
||||
// },
|
||||
// data ⇒ (importer(data, ctx.userId) map {
|
||||
// _.fold(Redirect(routes.Importer.importGame)) { game =>
|
||||
// Redirect(routes.Analyse.replay(game.id, "white"))
|
||||
// }
|
||||
// })
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
def importGame = Open { implicit ctx ⇒
|
||||
makeListMenu map { listMenu ⇒
|
||||
Ok(html.game.importGame(listMenu, env.forms.importForm))
|
||||
}
|
||||
}
|
||||
|
||||
def sendGame = OpenBody { implicit ctx ⇒
|
||||
implicit def req = ctx.body
|
||||
env.forms.importForm.bindFromRequest.fold(
|
||||
failure ⇒ makeListMenu map { listMenu ⇒
|
||||
Ok(html.game.importGame(listMenu, failure))
|
||||
},
|
||||
data ⇒ env.importer(data, ctx.userId) map { game ⇒
|
||||
Redirect(routes.Analyse.replay(game.id, "white"))
|
||||
} recover {
|
||||
case e ⇒ {
|
||||
logwarn(e.getMessage)
|
||||
Redirect(routes.Importer.importGame)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,27 +16,25 @@ private[importer] final class Importer(
|
|||
bookmark: ActorRef,
|
||||
delay: Duration) {
|
||||
|
||||
def apply(data: ImportData, user: Option[String]): Fu[Option[Game]] =
|
||||
gameExists(data.pgn) {
|
||||
data preprocess user match {
|
||||
case scalaz.Success(Preprocessed(game, moves, result)) ⇒ for {
|
||||
def apply(data: ImportData, user: Option[String]): Fu[Game] = gameExists(data.pgn) {
|
||||
(data preprocess user).fold[Fu[Game]](fufail(_), {
|
||||
case Preprocessed(game, moves, result) ⇒ for {
|
||||
_ ← $insert(game) >>
|
||||
(GameRepo denormalize game) >>
|
||||
applyMoves(game.id, moves)
|
||||
dbGame ← $find.byId[Game](game.id)
|
||||
_ ← ~((result |@| dbGame) apply {
|
||||
case (res, dbg) ⇒ finish(dbg, res)
|
||||
_ ← ~((dbGame |@| result) apply {
|
||||
case (dbg, res) ⇒ finish(dbg, res)
|
||||
}) >>- ~((dbGame |@| user) apply {
|
||||
case (dbg, u) ⇒ bookmark ! (dbg.id -> u)
|
||||
})
|
||||
} yield game.some
|
||||
case _ ⇒ fuccess(none)
|
||||
}
|
||||
} yield game
|
||||
})
|
||||
}
|
||||
|
||||
private def gameExists(pgn: String)(processing: ⇒ Fu[Option[Game]]): Fu[Option[Game]] =
|
||||
private def gameExists(pgn: String)(processing: ⇒ Fu[Game]): Fu[Game] =
|
||||
$find.one(lila.game.Query pgnImport pgn) flatMap {
|
||||
_.fold(processing)(game ⇒ fuccess(game.some))
|
||||
_.fold(processing)(game ⇒ fuccess(game))
|
||||
}
|
||||
|
||||
private def finish(game: Game, result: Result): Funit = result match {
|
||||
|
|
Loading…
Reference in a new issue