Skip to content

Commit e28f275

Browse files
committed
Correspondence with limited increment lichess-org#4462
1 parent abac35f commit e28f275

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

core/src/main/scala/CorrespondenceClock.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.time.Instant
66

77
// times are expressed in seconds
88
case class CorrespondenceClock(
9+
base: Int,
910
increment: Int,
1011
whiteTime: Float,
1112
blackTime: Float
@@ -27,19 +28,29 @@ case class CorrespondenceClock(
2728
)
2829

2930
// in seconds
30-
def estimateTotalTime: Int = increment * 40 / 2
31+
def estimateTotalTime: Int = base + increment * 40 / 2
3132

3233
def incrementHours: Int = increment / 60 / 60
3334

3435
object CorrespondenceClock:
3536
val hourSeconds = 60 * 60
3637
val daySeconds = 24 * hourSeconds
3738

38-
def apply(daysPerTurn: Int, turnColor: Color, lastMoveAt: Instant): CorrespondenceClock =
39-
val increment = daysPerTurn * 24 * 60 * 60
39+
// TODO: specify which color to add days to, if allowing addition to either color
40+
def apply(
41+
base: Int,
42+
increment: Int,
43+
remainingTime: Float,
44+
daysToAdd: Int,
45+
turnColor: Color,
46+
lastMoveAt: Instant
47+
): CorrespondenceClock =
48+
val limit = base
49+
val incremented = (remainingTime + (daysToAdd * daySeconds)).max(limit * daySeconds).toInt.max(0)
4050
val secondsLeft = (lastMoveAt.toSeconds + increment - nowSeconds).toInt.max(0)
4151
CorrespondenceClock(
52+
base = base,
4253
increment = increment,
43-
whiteTime = turnColor.fold(secondsLeft, increment).toFloat,
44-
blackTime = turnColor.fold(increment, secondsLeft).toFloat
54+
whiteTime = turnColor.fold(secondsLeft, incremented).toFloat,
55+
blackTime = turnColor.fold(incremented, secondsLeft).toFloat
4556
)

0 commit comments

Comments
 (0)