safer List init for captcher

pull/83/head
Thibault Duplessis 2013-09-20 16:47:03 +02:00
parent bbf07e7e44
commit a07a0ed2d6
1 changed files with 9 additions and 3 deletions

View File

@ -5,13 +5,13 @@ import scala.concurrent.Future
import akka.actor._
import akka.pattern.{ ask, pipe }
import chess.format.{ Forsyth, pgn }
import chess.{ Game ChessGame, Color }
import play.api.libs.concurrent.Akka.system
import play.api.Play.current
import scalaz.{ NonEmptyList, OptionT }
import spray.caching.{ LruCache, Cache }
import chess.format.{ Forsyth, pgn }
import chess.{ Game ChessGame, Color }
import lila.common.Captcha, Captcha._
import lila.db.api.$find
import lila.hub.actorApi.captcha._
@ -89,7 +89,13 @@ private final class Captcher extends Actor {
} map (_.notation) toNel
private def rewind(game: Game, pgnString: String): Option[ChessGame] =
pgn.Reader.withSans(pgnString, _.init) map (_.game) toOption
pgn.Reader.withSans(pgnString, safeInit) map (_.game) toOption
private def safeInit[A](list: List[A]): List[A] = list match {
case x :: Nil Nil
case x :: xs x :: safeInit(xs)
case _ Nil
}
private def fen(game: ChessGame): String = Forsyth >> game takeWhile (_ != ' ')
}