forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simul.scala
183 lines (146 loc) · 4.79 KB
/
Simul.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package lila.simul
import chess.variant.Variant
import lila.user.{ User, Title }
import org.joda.time.DateTime
import ornicar.scalalib.Random
case class Simul(
_id: Simul.ID,
name: String,
status: SimulStatus,
clock: SimulClock,
applicants: List[SimulApplicant],
pairings: List[SimulPairing],
variants: List[Variant],
createdAt: DateTime,
hostId: String,
hostRating: Int,
hostTitle: Option[Title],
hostGameId: Option[String], // game the host is focusing on
startedAt: Option[DateTime],
finishedAt: Option[DateTime],
hostSeenAt: Option[DateTime],
color: Option[String],
text: String,
team: Option[String]
) {
def id = _id
def fullName = s"$name simul"
def isCreated = !isStarted
def isStarted = startedAt.isDefined
def isFinished = status == SimulStatus.Finished
def isRunning = status == SimulStatus.Started
def hasApplicant(userId: String) = applicants.exists(_ is userId)
def hasPairing(userId: String) = pairings.exists(_ is userId)
def hasUser(userId: String) = hasApplicant(userId) || hasPairing(userId)
def addApplicant(applicant: SimulApplicant) = Created {
if (!hasApplicant(applicant.player.user) && variants.has(applicant.player.variant))
copy(applicants = applicants :+ applicant)
else this
}
def removeApplicant(userId: String) = Created {
copy(applicants = applicants filterNot (_ is userId))
}
def accept(userId: String, v: Boolean) = Created {
copy(applicants = applicants map {
case a if a is userId => a.copy(accepted = v)
case a => a
})
}
def removePairing(userId: String) =
copy(pairings = pairings filterNot (_ is userId)).finishIfDone
def nbAccepted = applicants.count(_.accepted)
def startable = isCreated && nbAccepted > 1
def start = startable option copy(
status = SimulStatus.Started,
startedAt = DateTime.now.some,
applicants = Nil,
pairings = applicants collect {
case a if a.accepted => SimulPairing(a.player)
},
hostSeenAt = none
)
def updatePairing(gameId: String, f: SimulPairing => SimulPairing) = copy(
pairings = pairings collect {
case p if p.gameId == gameId => f(p)
case p => p
}
).finishIfDone
def ejectCheater(userId: String): Option[Simul] =
hasUser(userId) option removeApplicant(userId).removePairing(userId)
private def finishIfDone =
if (pairings.forall(_.finished))
copy(
status = SimulStatus.Finished,
finishedAt = DateTime.now.some,
hostGameId = none
)
else this
def gameIds = pairings.map(_.gameId)
def perfTypes: List[lila.rating.PerfType] = variants.flatMap { variant =>
lila.game.PerfPicker.perfType(
speed = chess.Speed(clock.config.some),
variant = variant,
daysPerTurn = none
)
}
def applicantRatio = s"${applicants.count(_.accepted)}/${applicants.size}"
def variantRich = variants.size > 3
def isHost(userOption: Option[User]): Boolean = userOption ?? isHost
def isHost(user: User): Boolean = user.id == hostId
def playingPairings = pairings filterNot (_.finished)
def hostColor = (color flatMap chess.Color.apply) | chess.Color(scala.util.Random.nextBoolean)
def setPairingHostColor(gameId: String, hostColor: chess.Color) =
updatePairing(gameId, _.copy(hostColor = hostColor))
def isNotBrandNew = createdAt isBefore DateTime.now.minusSeconds(10)
private def Created(s: => Simul): Simul = if (isCreated) s else this
def spotlightable =
(hostRating >= 2400 || hostTitle.isDefined) &&
isCreated &&
applicants.size < 80
def wins = pairings.count(p => p.finished && p.wins.has(false))
def draws = pairings.count(p => p.finished && p.wins.isEmpty)
def losses = pairings.count(p => p.finished && p.wins.has(true))
def ongoing = pairings.count(_.ongoing)
}
object Simul {
type ID = String
case class OnStart(simul: Simul)
private def makeName(host: User) =
if (host.title.isDefined) host.titleUsername
else RandomName()
def make(
host: User,
clock: SimulClock,
variants: List[Variant],
color: String,
text: String,
team: Option[String]
): Simul = Simul(
_id = Random nextString 8,
name = makeName(host),
status = SimulStatus.Created,
clock = clock,
hostId = host.id,
hostRating = host.perfs.bestRatingIn {
variants flatMap { variant =>
lila.game.PerfPicker.perfType(
speed = chess.Speed(clock.config.some),
variant = variant,
daysPerTurn = none
)
}
},
hostTitle = host.title,
hostGameId = none,
createdAt = DateTime.now,
variants = variants,
applicants = Nil,
pairings = Nil,
startedAt = none,
finishedAt = none,
hostSeenAt = DateTime.now.some,
color = color.some,
text = text,
team = team
)
}