clock configs everywhere

puzzle-ui
Thibault Duplessis 2016-12-05 18:09:38 +01:00
parent 68f220e754
commit bb27550595
17 changed files with 36 additions and 35 deletions

View File

@ -77,7 +77,7 @@ object Setup extends LilaController with TheftPrevention {
variant = config.variant,
initialFen = config.fen,
timeControl = config.makeClock map { c =>
TimeControl.Clock(c.limit, c.increment)
TimeControl.Clock(chess.Clock.Config(c.limit, c.increment))
} orElse config.makeDaysPerTurn.map {
TimeControl.Correspondence.apply
} getOrElse TimeControl.Unlimited,

View File

@ -22,7 +22,7 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
description = describePov(pov))
def titleGame(g: Game) = {
val speed = chess.Speed(g.clock).name
val speed = chess.Speed(g.clock.map(_.config)).name
val variant = g.variant.exotic ?? s" ${g.variant.name}"
s"$speed$variant Chess • ${playerText(g.whitePlayer)} vs ${playerText(g.blackPlayer)}"
}
@ -34,7 +34,7 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
val speedAndClock =
if (game.imported) "imported"
else game.clock.fold(chess.Speed.Correspondence.name) { c =>
s"${chess.Speed(c.some).name} (${c.show})"
s"${chess.Speed(c.config).name} (${c.show})"
}
val mode = game.mode.name
val variant = if (game.variant == chess.variant.FromPosition) "position setup chess"
@ -75,10 +75,10 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
case v => v.name
}
def shortClockName(clock: Option[Clock])(implicit ctx: UserContext): Html =
def shortClockName(clock: Option[Clock.Config])(implicit ctx: UserContext): Html =
clock.fold(trans.unlimited())(shortClockName)
def shortClockName(clock: Clock): Html = Html(clock.show)
def shortClockName(clock: Clock.Config): Html = Html(clock.show)
def modeName(mode: Mode)(implicit ctx: UserContext): String = mode match {
case Mode.Casual => trans.casual.str()
@ -248,7 +248,7 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}
def challengeTitle(c: lila.challenge.Challenge)(implicit ctx: UserContext) = {
val speed = c.clock.map(_.chessClock).fold(trans.unlimited.str()) { clock =>
val speed = c.clock.map(_.config).fold(trans.unlimited.str()) { clock =>
s"${chess.Speed(clock).name} (${clock.show})"
}
val variant = c.variant.exotic ?? s" ${c.variant.name}"

View File

@ -5,6 +5,6 @@
@c.daysPerTurn.map { days =>
<span class="text" data-icon=";">@{(days == 1).fold(trans.oneDay(), trans.nbDays(days))}</span>
}.getOrElse {
<span class="text" data-icon="p">@shortClockName(c.clock.map(_.chessClock))</span>
<span class="text" data-icon="p">@shortClockName(c.clock.map(_.config))</span>
}
</p>

View File

@ -13,7 +13,7 @@
@lightUser(pov.opponent.userId).flatMap(_.title).map(" " + _)
</div>
@pov.game.clock.map { c =>
<div class="center"><span data-icon="p"> @shortClockName(c)</span></div>
<div class="center"><span data-icon="p"> @shortClockName(c.config)</span></div>
}.getOrElse {
@ctxOption.map { ctx =>
@pov.game.daysPerTurn.map { days =>

View File

@ -15,7 +15,7 @@
@userIdLink(simul.hostId.some)
</td>
<td class="small">
<span class="text" data-icon="p">@simul.clock.show</span>
<span class="text" data-icon="p">@simul.clock.config.show</span>
</td>
<td class="text" data-icon="r">@simul.applicants.size</td>
<td>

View File

@ -1,5 +1,5 @@
@(sim: lila.simul.Simul)(implicit ctx: Context)
<span class="setup @if(sim.variantRich){rich}">
@sim.clock.show •
@sim.clock.config.show •
@sim.variants.map(_.name).mkString(", ")
</span>

View File

@ -7,7 +7,7 @@
<span data-icon="@pt.iconChar"></span>
}
</div>
<span class="clock">@sim.clock.show</span><br />
<span class="clock">@sim.clock.config.show</span><br />
<div class="setup">
@sim.variants.map(_.name).mkString(", ") • @trans.casual()
</div>

View File

@ -10,7 +10,7 @@
@playerLink(pov.game.blackPlayer, withRating = false, withOnline = false, withDiff = false, variant = pov.game.variant)
</div>
<br />
<span data-icon="p"> @shortClockName(pov.game.clock)</span>, @game.variantLink(pov.game.variant, variantName(pov.game.variant))
<span data-icon="p"> @shortClockName(pov.game.clock.map(_.config))</span>, @game.variantLink(pov.game.variant, variantName(pov.game.variant))
@if(pov.game.rated) {
, @trans.rated()
}

View File

@ -208,7 +208,7 @@
@p.game.perfType.map { pt =>
<span data-icon="@pt.iconChar"></span>
}
@shortClockName(p.game.clock)
@shortClockName(p.game.clock.map(_.config))
</a>
}
</td>

View File

@ -31,14 +31,14 @@ private object BSONHandlers {
}
implicit val TimeControlBSONHandler = new BSON[TimeControl] {
def reads(r: Reader) = (r.intO("l") |@| r.intO("i")) {
case (limit, inc) => TimeControl.Clock(limit, inc)
case (limit, inc) => TimeControl.Clock(chess.Clock.Config(limit, inc))
} orElse {
r intO "d" map TimeControl.Correspondence.apply
} getOrElse TimeControl.Unlimited
def writes(w: Writer, t: TimeControl) = t match {
case TimeControl.Clock(l, i) => $doc("l" -> l, "i" -> i)
case TimeControl.Correspondence(d) => $doc("d" -> d)
case TimeControl.Unlimited => $empty
case TimeControl.Clock(chess.Clock.Config(l, i)) => $doc("l" -> l, "i" -> i)
case TimeControl.Correspondence(d) => $doc("d" -> d)
case TimeControl.Unlimited => $empty
}
}
implicit val VariantBSONHandler = new BSONHandler[BSONInteger, Variant] {

View File

@ -1,6 +1,6 @@
package lila.challenge
import chess.variant.{Variant, FromPosition}
import chess.variant.{ Variant, FromPosition }
import chess.{ Mode, Clock, Speed }
import org.joda.time.DateTime
@ -87,10 +87,11 @@ object Challenge {
object TimeControl {
case object Unlimited extends TimeControl
case class Correspondence(days: Int) extends TimeControl
case class Clock(limit: Int, increment: Int) extends TimeControl {
case class Clock(config: chess.Clock.Config) extends TimeControl {
// All durations are expressed in seconds
def show = chessClock.show
lazy val chessClock = chess.Clock(limit, increment)
def limit = config.limit
def increment = config.increment
def show = config.show
}
}
@ -102,8 +103,8 @@ object Challenge {
}
private def speedOf(timeControl: TimeControl) = timeControl match {
case c: TimeControl.Clock => Speed(c.chessClock)
case _ => Speed.Correspondence
case TimeControl.Clock(config) => Speed(config)
case _ => Speed.Correspondence
}
private def perfTypeOf(variant: Variant, timeControl: TimeControl): PerfType =

View File

@ -77,7 +77,7 @@ final class ChallengeApi(
variant = pov.game.variant,
initialFen = initialFen,
timeControl = (pov.game.clock, pov.game.daysPerTurn) match {
case (Some(clock), _) => TimeControl.Clock(clock.limit, clock.increment)
case (Some(clock), _) => TimeControl.Clock(clock.config)
case (_, Some(days)) => TimeControl.Correspondence(days)
case _ => TimeControl.Unlimited
},

View File

@ -18,7 +18,7 @@ private[challenge] final class Joiner(onStart: String => Unit) {
c.challengerUserId.??(UserRepo.byId) flatMap { challengerUser =>
def makeChess(variant: chess.variant.Variant): chess.Game =
chess.Game(board = chess.Board init variant, clock = c.clock.map(_.chessClock))
chess.Game(board = chess.Board init variant, clock = c.clock.map(_.config.toClock))
val baseState = c.initialFen.ifTrue(c.variant == chess.variant.FromPosition) flatMap Forsyth.<<<
val (chessGame, state) = baseState.fold(makeChess(c.variant) -> none[SituationPlus]) {
@ -28,7 +28,7 @@ private[challenge] final class Joiner(onStart: String => Unit) {
player = color,
turns = sit.turns,
startedAtTurn = sit.turns,
clock = c.clock.map(_.chessClock))
clock = c.clock.map(_.config.toClock))
if (Forsyth.>>(game) == Forsyth.initial) makeChess(chess.variant.Standard) -> none
else game -> baseState
}

View File

@ -31,11 +31,11 @@ final class JsonView(
"initialFen" -> c.initialFen,
"rated" -> c.mode.rated,
"timeControl" -> (c.timeControl match {
case c@TimeControl.Clock(l, i) => Json.obj(
case c@TimeControl.Clock(clock) => Json.obj(
"type" -> "clock",
"limit" -> l,
"increment" -> i,
"show" -> c.show)
"limit" -> clock.limit,
"increment" -> clock.increment,
"show" -> clock.show)
case TimeControl.Correspondence(d) => Json.obj(
"type" -> "correspondence",
"daysPerTurn" -> d)

View File

@ -78,7 +78,7 @@ case class Hook(
def likePoolFiveO = poolCompatible && clock.show == "5+0"
private lazy val speed = Speed(clock.some)
private lazy val speed = Speed(clock)
}
object Hook {

View File

@ -31,7 +31,7 @@ private[setup] trait Config {
lazy val creatorColor = color.resolve
def makeGame(v: chess.variant.Variant): ChessGame =
ChessGame(board = Board init v, clock = makeClock)
ChessGame(board = Board init v, clock = makeClock.map(_.toClock))
def makeGame: ChessGame = makeGame(variant)
@ -42,7 +42,7 @@ private[setup] trait Config {
def makeClock = hasClock option justMakeClock
protected def justMakeClock =
Clock((time * 60).toInt, clockHasTime.fold(increment, 1))
Clock.Config((time * 60).toInt, clockHasTime.fold(increment, 1))
def makeDaysPerTurn: Option[Int] = (timeMode == TimeMode.Correspondence) option days
}
@ -68,7 +68,7 @@ trait Positional { self: Config =>
player = color,
turns = sit.turns,
startedAtTurn = sit.turns,
clock = makeClock)
clock = makeClock.map(_.toClock))
if (Forsyth.>>(game) == Forsyth.initial) makeGame(chess.variant.Standard) -> none
else game -> baseState
}

View File

@ -83,7 +83,7 @@ case class Tournament(
case _ => false
}
def speed = Speed(clock.some)
def speed = Speed(clock)
def perfType = PerfPicker.perfType(speed, variant, none)
def perfLens = PerfPicker.mainOrDefault(speed, variant, none)