forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.scala
143 lines (111 loc) · 4.19 KB
/
Config.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package lila.setup
import chess.format.Fen
import chess.variant.{ FromPosition, Variant }
import chess.{ Clock, Game as ChessGame, Situation, Speed }
import scalalib.model.Days
import lila.lobby.TriColor
import lila.rating.PerfType
private[setup] trait Config:
// Whether or not to use a clock
val timeMode: TimeMode
// Clock time in minutes
val time: Double
// Clock increment in seconds
val increment: Clock.IncrementSeconds
// Correspondence days per turn
val days: Days
// Game variant code
val variant: Variant
// Creator player color
val color: TriColor
def hasClock = timeMode == TimeMode.RealTime
lazy val creatorColor = color.resolve()
def makeGame(v: Variant): ChessGame =
ChessGame(situation = Situation(v), clock = makeClock.map(_.toClock))
def makeGame: ChessGame = makeGame(variant)
def validClock = !hasClock || clockHasTime
def validSpeed(isBot: Boolean) =
!isBot || makeClock.forall: c =>
Speed(c) >= Speed.Bullet
def clockHasTime = time + increment.value > 0
def makeClock = hasClock.option(justMakeClock)
protected def justMakeClock =
Clock.Config(
Clock.LimitSeconds((time * 60).toInt),
if clockHasTime then increment else Clock.IncrementSeconds(1)
)
def makeDaysPerTurn: Option[Days] = (timeMode == TimeMode.Correspondence).option(days)
def makeSpeed: Speed = chess.Speed(makeClock)
def perfType: PerfType = lila.rating.PerfType(variant, makeSpeed)
def perfKey = perfType.key
trait Positional:
self: Config =>
def fen: Option[Fen.Full]
def strictFen: Boolean
lazy val validFen = variant != FromPosition ||
fen.exists: f =>
Fen.read(f).exists(_.playable(strictFen))
def fenGame(builder: ChessGame => Fu[Game]): Fu[Game] =
val baseState = fen.ifTrue(variant.fromPosition).flatMap {
Fen.readWithMoveNumber(FromPosition, _)
}
val (chessGame, state) = baseState.fold(makeGame -> none[Situation.AndFullMoveNumber]) {
case sit @ Situation.AndFullMoveNumber(s, _) =>
val game = ChessGame(
situation = s,
ply = sit.ply,
startedAtPly = sit.ply,
clock = makeClock.map(_.toClock)
)
if Fen.write(game).isInitial then makeGame(chess.variant.Standard) -> none
else game -> baseState
}
builder(chessGame).dmap { game =>
state.fold(game) { case sit @ Situation.AndFullMoveNumber(Situation(board, _), _) =>
game.copy(
chess = game.chess.copy(
situation = game.situation.copy(
board = game.board.copy(
history = board.history,
variant = FromPosition
)
),
ply = sit.ply
)
)
}
}
object Config extends BaseConfig
trait BaseConfig:
val variants = List(chess.variant.Standard.id, chess.variant.Chess960.id)
val variantDefault = chess.variant.Standard
val variantsWithFen = variants :+ FromPosition.id
val aiVariants = variants :+
chess.variant.Crazyhouse.id :+
chess.variant.KingOfTheHill.id :+
chess.variant.ThreeCheck.id :+
chess.variant.Antichess.id :+
chess.variant.Atomic.id :+
chess.variant.Horde.id :+
chess.variant.RacingKings.id :+
chess.variant.FromPosition.id
val variantsWithVariants =
variants :+
chess.variant.Crazyhouse.id :+
chess.variant.KingOfTheHill.id :+
chess.variant.ThreeCheck.id :+
chess.variant.Antichess.id :+
chess.variant.Atomic.id :+
chess.variant.Horde.id :+
chess.variant.RacingKings.id
val variantsWithFenAndVariants =
variantsWithVariants :+ FromPosition.id
val speeds = Speed.all.map(_.id)
private val timeMin = 0
private val timeMax = 180
private val acceptableFractions = Set(1 / 4d, 1 / 2d, 3 / 4d, 3 / 2d)
def validateTime(t: Double) =
t >= timeMin && t <= timeMax && (t.isWhole || acceptableFractions(t))
private val incrementMin = Clock.IncrementSeconds(0)
private val incrementMax = Clock.IncrementSeconds(180)
def validateIncrement(i: Clock.IncrementSeconds) = i >= incrementMin && i <= incrementMax