forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbPlayer.scala
134 lines (108 loc) · 3.21 KB
/
DbPlayer.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
package game
import chess._
import user.User
case class DbPlayer(
id: String,
color: Color,
aiLevel: Option[Int],
ps: String = "",
isWinner: Option[Boolean] = None,
isOfferingDraw: Boolean = false,
isOfferingRematch: Boolean = false,
lastDrawOffer: Option[Int] = None,
isProposingTakeback: Boolean = false,
userId: Option[String] = None,
elo: Option[Int] = None,
eloDiff: Option[Int] = None,
moveTimes: String = "",
blurs: Int = 0) {
def encodePieces(allPieces: Iterable[(Pos, Piece, Boolean)]): String =
allPieces withFilter (_._2.color == color) map {
case (pos, piece, dead) ⇒ pos.piotr.toString + {
if (dead) piece.role.forsyth.toUpper
else piece.role.forsyth
}
} mkString ""
def withEncodedPieces(allPieces: Iterable[(Pos, Piece, Boolean)]) = copy(
ps = encodePieces(allPieces)
)
def withUser(user: User): DbPlayer = copy(
userId = user.id.some,
elo = user.elo.some)
def isAi = aiLevel.isDefined
def isHuman = !isAi
def hasUser = userId.isDefined
def isUser(u: User) = userId.fold(_ == u.id, false)
def wins = isWinner getOrElse false
def hasMoveTimes = moveTimes.size > 4
def moveTimeList: List[Int] = MoveTime decode moveTimes
def finish(winner: Boolean) = copy(
isWinner = if (winner) Some(true) else None
)
def offerDraw(turn: Int) = copy(
isOfferingDraw = true,
lastDrawOffer = Some(turn)
)
def removeDrawOffer = copy(isOfferingDraw = false)
def offerRematch = copy(isOfferingRematch = true)
def removeRematchOffer = copy(isOfferingRematch = false)
def proposeTakeback = copy(isProposingTakeback = true)
def removeTakebackProposition = copy(isProposingTakeback = false)
def encode: RawDbPlayer = RawDbPlayer(
id = id,
ps = ps,
ai = aiLevel,
w = isWinner,
elo = elo,
ed = eloDiff,
isOfferingDraw = if (isOfferingDraw) Some(true) else None,
isOfferingRematch = if (isOfferingRematch) Some(true) else None,
lastDrawOffer = lastDrawOffer,
isProposingTakeback = if (isProposingTakeback) Some(true) else None,
uid = userId,
mts = Some(moveTimes) filter ("" !=),
bs = Some(blurs) filter (0 !=)
)
}
object DbPlayer {
def apply(
color: Color,
aiLevel: Option[Int]): DbPlayer = DbPlayer(
id = IdGenerator.player,
color = color,
aiLevel = aiLevel)
def white = apply(Color.White, None)
def black = apply(Color.Black, None)
}
case class RawDbPlayer(
id: String,
ps: String,
ai: Option[Int],
w: Option[Boolean],
elo: Option[Int],
ed: Option[Int],
isOfferingDraw: Option[Boolean],
isOfferingRematch: Option[Boolean],
lastDrawOffer: Option[Int],
isProposingTakeback: Option[Boolean],
uid: Option[String],
mts: Option[String],
bs: Option[Int]) {
def decode(color: Color): DbPlayer = DbPlayer(
id = id,
color = color,
ps = ps,
aiLevel = ai,
isWinner = w,
elo = elo,
eloDiff = ed,
isOfferingDraw = isOfferingDraw | false,
isOfferingRematch = isOfferingRematch | false,
lastDrawOffer = lastDrawOffer,
isProposingTakeback = isProposingTakeback | false,
userId = uid,
moveTimes = mts | "",
blurs = bs | 0
)
}