forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simul.scala
134 lines (105 loc) · 3.54 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
package lila.simul
import chess.variant.Variant
import lila.user.User
import org.joda.time.{ DateTime, Duration }
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,
hostGameId: Option[String], // game the host is focusing on
startedAt: Option[DateTime],
finishedAt: Option[DateTime]) {
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.contains(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 startable = isCreated && applicants.count(_.accepted) > 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)
})
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.Classical,
variant = variant,
daysPerTurn = none)
}
def applicantRatio = s"${applicants.count(_.accepted)}/${applicants.size}"
def variantRich = variants.size > 3
def isHost(userOption: Option[User]) = userOption ?? (_.id == hostId)
private def Created(s: => Simul): Simul = if (isCreated) s else this
}
object Simul {
type ID = String
def make(
host: User,
clock: SimulClock,
variants: List[Variant]): Simul = Simul(
_id = Random nextStringUppercase 8,
name = RandomName(),
status = SimulStatus.Created,
clock = clock,
hostId = host.id,
hostRating = host.perfs.bestRatingIn {
variants flatMap { variant =>
lila.game.PerfPicker.perfType(
speed = chess.Speed(clock.chessClock.some),
variant = variant,
daysPerTurn = none)
}
},
hostGameId = none,
createdAt = DateTime.now,
variants = variants,
applicants = Nil,
pairings = Nil,
startedAt = none,
finishedAt = none)
}