Skip to content

Commit

Permalink
tweak scala charts generators
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 29, 2012
1 parent c7d9397 commit eeeb1e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
38 changes: 14 additions & 24 deletions app/analyse/AdvantageChart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,29 @@ final class AdvantageChart(advices: Analysis.InfoAdvices) {

val max = 15

def columns = AdvantageChart.columns
def columns = Json generate List(
"string" :: "Move" :: Nil,
"number" :: "Advantage" :: Nil)

def rows = Json generate {

val scale = floatBox(-max to max) _

def rows = Json generate chartValues
def move(info: Info, advice: Option[Advice]) = info.color.fold(
"%d. %s", "%d... %s"
).format(info.turn, info.move.uci) + advice.fold(" " + _.nag.symbol, "")

private lazy val values: List[(String, Float)] =
(advices sliding 2 collect {
case (info, advice) :: (next, _) :: Nil
(next.score, next.mate) match {
case (Some(score), _) move(info, advice) -> scale(score.pawns)
case (_, Some(mate)) move(info, advice) -> {
case (Some(score), _) List(move(info, advice), scale(score.pawns))
case (_, Some(mate)) List(move(info, advice), {
val mateDelta = math.abs(mate.toFloat / 100)
val whiteWins = info.color.fold(mate < 0, mate > 0)
scale(whiteWins.fold(max - mateDelta, mateDelta - max))
}
case _ move(info, none) -> scale(0)
})
case _ List(move(info, none), scale(0))
}
}).toList

private val scale = floatBox(-max to max) _

private def chartValues: List[List[Any]] = values map {
case (move, score) List(move, score)
}

private def move(info: Info, advice: Option[Advice]) = info.color.fold(
"%d. %s", "%d... %s"
).format(info.turn, info.move.uci) + advice.fold(" " + _.nag.symbol, "")

}

object AdvantageChart {

val columns = Json generate List(
"string" :: "Move" :: Nil,
"number" :: "Advantage" :: Nil)
}
10 changes: 5 additions & 5 deletions app/analyse/TimePie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import com.codahale.jerkson.Json

final class TimePie(val pov: Pov) {

import pov._

val columns = Json generate List(
def columns = Json generate List(
"string" :: "Move time" :: Nil,
"number" :: "Moves" :: Nil)

def rows = Json generate {

import pov._

val steps = (0 to 5) ++ (6 to 12 by 2) ++ (15 to 30 by 3) ++ (35 to 60 by 5)

val ranges = steps zip (steps drop 1) map { case (a, b) Range(a, b) }
Expand All @@ -29,8 +29,8 @@ final class TimePie(val pov: Pov) {
else "%d to %d seconds".format(min, max)
}

ranges zip (ranges map nbMoves) collect {
case (range, nb) if nb > 0 List(nameRange(range), nb)
ranges zip (ranges map nbMoves) collect {
case (range, nb) if nb > 0 List(nameRange(range), nb)
}
}
}

0 comments on commit eeeb1e8

Please sign in to comment.