-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
SwissPlayer.scala
105 lines (90 loc) · 2.78 KB
/
SwissPlayer.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
package lila.swiss
import lila.common.LightUser
import lila.rating.PerfType
import lila.user.User
case class SwissPlayer(
id: SwissPlayer.Id, // swissId:userId
swissId: Swiss.Id,
userId: User.ID,
rating: Int,
provisional: Boolean,
points: Swiss.Points,
tieBreak: Swiss.TieBreak,
performance: Option[Swiss.Performance],
score: Swiss.Score,
absent: Boolean,
byes: Set[SwissRound.Number] // byes granted by the pairing system - the player was here
) {
def is(uid: User.ID): Boolean = uid == userId
def is(user: User): Boolean = is(user.id)
def is(other: SwissPlayer): Boolean = is(other.userId)
def present = !absent
def recomputeScore =
copy(
score = Swiss.makeScore(points, tieBreak, performance | Swiss.Performance(rating.toFloat))
)
}
object SwissPlayer {
case class Id(value: String) extends AnyVal with StringValue
def makeId(swissId: Swiss.Id, userId: User.ID) = Id(s"$swissId:$userId")
private[swiss] def make(
swissId: Swiss.Id,
user: User,
perf: PerfType
): SwissPlayer =
new SwissPlayer(
id = makeId(swissId, user.id),
swissId = swissId,
userId = user.id,
rating = user.perfs(perf).intRating,
provisional = user.perfs(perf).provisional,
points = Swiss.Points(0),
tieBreak = Swiss.TieBreak(0),
performance = none,
score = Swiss.Score(0),
absent = false,
byes = Set.empty
).recomputeScore
case class Ranked(rank: Int, player: SwissPlayer) {
def is(other: Ranked) = player is other.player
override def toString = s"$rank. ${player.userId}[${player.rating}]"
}
case class WithUser(player: SwissPlayer, user: LightUser)
sealed private[swiss] trait Viewish {
val player: SwissPlayer
val rank: Int
val user: lila.common.LightUser
val sheet: SwissSheet
}
private[swiss] case class View(
player: SwissPlayer,
rank: Int,
user: lila.common.LightUser,
pairings: Map[SwissRound.Number, SwissPairing],
sheet: SwissSheet
) extends Viewish
private[swiss] case class ViewExt(
player: SwissPlayer,
rank: Int,
user: lila.common.LightUser,
pairings: Map[SwissRound.Number, SwissPairing.View],
sheet: SwissSheet
) extends Viewish
type PlayerMap = Map[User.ID, SwissPlayer]
def toMap(players: List[SwissPlayer]): PlayerMap =
players.view.map(p => p.userId -> p).toMap
object Fields {
val id = "_id"
val swissId = "s"
val userId = "u"
val rating = "r"
val provisional = "pr"
val points = "p"
val tieBreak = "t"
val performance = "e"
val score = "c"
val absent = "a"
val byes = "b"
}
def fields[A](f: Fields.type => A): A = f(Fields)
}