forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserHelper.scala
307 lines (274 loc) · 10.3 KB
/
UserHelper.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package lila.app
package templating
import controllers.routes
import mashup._
import play.api.i18n.Lang
import lila.app.ui.ScalatagsTemplate._
import lila.common.LightUser
import lila.i18n.{ I18nKey, I18nKeys => trans }
import lila.rating.{ Perf, PerfType }
import lila.user.{ Title, User }
trait UserHelper { self: I18nHelper with StringHelper with NumberHelper =>
def ratingProgress(progress: Int): Option[Frag] =
if (progress > 0) goodTag(cls := "rp")(progress).some
else if (progress < 0) badTag(cls := "rp")(math.abs(progress)).some
else none
val topBarSortedPerfTypes: List[PerfType] = List(
PerfType.Bullet,
PerfType.Chess960,
PerfType.Blitz,
PerfType.KingOfTheHill,
PerfType.Rapid,
PerfType.ThreeCheck,
PerfType.Classical,
PerfType.Antichess,
PerfType.Correspondence,
PerfType.Atomic,
PerfType.Horde,
PerfType.Crazyhouse
)
def showPerfRating(rating: Int, name: String, nb: Int, provisional: Boolean, clueless: Boolean, icon: Char)(
implicit lang: Lang
): Frag =
span(
title := s"$name rating over ${nb.localize} games",
dataIcon := icon,
cls := "text"
)(
if (clueless) frag(nbsp, nbsp, nbsp, if (nb < 1) "-" else "?")
else frag(rating, provisional option "?")
)
def showPerfRating(perfType: PerfType, perf: Perf)(implicit lang: Lang): Frag =
showPerfRating(
perf.intRating,
perfType.trans,
perf.nb,
perf.provisional,
perf.clueless,
perfType.iconChar
)
def showPerfRating(u: User, perfType: PerfType)(implicit lang: Lang): Frag =
showPerfRating(perfType, u perfs perfType)
def showPerfRating(u: User, perfKey: String)(implicit lang: Lang): Option[Frag] =
PerfType(perfKey) map { showPerfRating(u, _) }
def showBestPerf(u: User)(implicit lang: Lang): Option[Frag] =
u.perfs.bestPerf map { case (pt, perf) =>
showPerfRating(pt, perf)
}
def showBestPerfs(u: User, nb: Int)(implicit lang: Lang): List[Frag] =
u.perfs.bestPerfs(nb) map { case (pt, perf) =>
showPerfRating(pt, perf)
}
def showRatingDiff(diff: Int): Frag =
diff match {
case 0 => span("±0")
case d if d > 0 => goodTag(s"+$d")
case d => badTag(s"−${-d}")
}
def lightUser = env.user.lightUserSync
def usernameOrId(userId: String) = lightUser(userId).fold(userId)(_.name)
def titleNameOrId(userId: String) = lightUser(userId).fold(userId)(_.titleName)
def titleNameOrAnon(userId: Option[String]) = userId.flatMap(lightUser).fold(User.anonymous)(_.titleName)
def isOnline(userId: String) = env.socket isOnline userId
def isStreaming(userId: String) = env.streamer.liveStreamApi isStreaming userId
def anonUserSpan(cssClass: Option[String] = None, modIcon: Boolean = false) =
span(
cls := List("offline" -> true, "user-link" -> true, ~cssClass -> cssClass.isDefined)
)(
if (modIcon)
frag(
moderatorIcon,
User.anonMod
)
else User.anonymous
)
def userIdLink(
userIdOption: Option[User.ID],
cssClass: Option[String] = None,
withOnline: Boolean = true,
withTitle: Boolean = true,
truncate: Option[Int] = None,
params: String = "",
modIcon: Boolean = false
)(implicit ctx: Lang): Tag =
userIdOption.flatMap(lightUser).fold[Tag](anonUserSpan(cssClass, modIcon)) { user =>
userIdNameLink(
userId = user.id,
username = user.name,
isPatron = user.isPatron,
title = withTitle ?? user.title map Title.apply,
cssClass = cssClass,
withOnline = withOnline,
truncate = truncate,
params = params,
modIcon = modIcon
)
}
def lightUserLink(
user: LightUser,
cssClass: Option[String] = None,
withOnline: Boolean = true,
withTitle: Boolean = true,
truncate: Option[Int] = None,
params: String = ""
)(implicit lang: Lang): Tag =
userIdNameLink(
userId = user.id,
username = user.name,
isPatron = user.isPatron,
title = withTitle ?? user.title map Title.apply,
cssClass = cssClass,
withOnline = withOnline,
truncate = truncate,
params = params,
modIcon = false
)
def titleTag(title: Option[Title]): Option[Frag] =
title map { t =>
frag(userTitleTag(t), nbsp)
}
def titleTag(lu: LightUser): Frag = titleTag(lu.title map Title.apply)
private def userIdNameLink(
userId: String,
username: String,
isPatron: Boolean,
cssClass: Option[String],
withOnline: Boolean,
truncate: Option[Int],
title: Option[Title],
params: String,
modIcon: Boolean
)(implicit lang: Lang): Tag =
a(
cls := userClass(userId, cssClass, withOnline),
href := userUrl(username, params = params)
)(
withOnline ?? (if (modIcon) moderatorIcon else lineIcon(isPatron)),
titleTag(title),
truncate.fold(username)(username.take)
)
def userLink(
user: User,
cssClass: Option[String] = None,
withOnline: Boolean = true,
withPowerTip: Boolean = true,
withTitle: Boolean = true,
withBestRating: Boolean = false,
withPerfRating: Option[PerfType] = None,
name: Option[Frag] = None,
params: String = ""
)(implicit lang: Lang): Tag =
a(
cls := userClass(user.id, cssClass, withOnline, withPowerTip),
href := userUrl(user.username, params)
)(
withOnline ?? lineIcon(user),
withTitle option titleTag(user.title),
name | user.username,
userRating(user, withPerfRating, withBestRating)
)
def userSpan(
user: User,
cssClass: Option[String] = None,
withOnline: Boolean = true,
withPowerTip: Boolean = true,
withTitle: Boolean = true,
withBestRating: Boolean = false,
withPerfRating: Option[PerfType] = None,
name: Option[Frag] = None
)(implicit lang: Lang): Frag =
span(
cls := userClass(user.id, cssClass, withOnline, withPowerTip),
dataHref := userUrl(user.username)
)(
withOnline ?? lineIcon(user),
withTitle option titleTag(user.title),
name | user.username,
userRating(user, withPerfRating, withBestRating)
)
def userIdSpanMini(userId: String, withOnline: Boolean = false)(implicit lang: Lang): Tag = {
val user = lightUser(userId)
val name = user.fold(userId)(_.name)
span(
cls := userClass(userId, none, withOnline),
dataHref := userUrl(name)
)(
withOnline ?? lineIcon(user),
user.??(u => titleTag(u.title map Title.apply)),
name
)
}
private def renderRating(perf: Perf): Frag =
frag(
" (",
perf.intRating,
perf.provisional option "?",
")"
)
private def userRating(user: User, withPerfRating: Option[PerfType], withBestRating: Boolean): Frag =
withPerfRating match {
case Some(perfType) => renderRating(user.perfs(perfType))
case _ if withBestRating =>
user.perfs.bestPerf ?? { case (_, perf) =>
renderRating(perf)
}
case _ => ""
}
private def userUrl(username: String, params: String = ""): Option[String] =
(username != "Ghost" && username != "ghost") option s"""${routes.User.show(username)}$params"""
def userClass(
userId: String,
cssClass: Option[String],
withOnline: Boolean,
withPowerTip: Boolean = true
): List[(String, Boolean)] =
if (userId == "ghost") List("user-link" -> true, ~cssClass -> cssClass.isDefined)
else
(withOnline ?? List((if (isOnline(userId)) "online" else "offline") -> true)) ::: List(
"user-link" -> true,
~cssClass -> cssClass.isDefined,
"ulpt" -> withPowerTip
)
def userGameFilterTitle(u: User, nbs: UserInfo.NbGames, filter: GameFilter)(implicit
lang: Lang
): Frag =
if (filter == GameFilter.Search) frag(iconTag(""), br, trans.search.advancedSearch())
else splitNumber(userGameFilterTitleNoTag(u, nbs, filter))
private def transLocalize(key: I18nKey, number: Int)(implicit lang: Lang) =
key.pluralSameTxt(number)
def userGameFilterTitleNoTag(u: User, nbs: UserInfo.NbGames, filter: GameFilter)(implicit
lang: Lang
): String =
filter match {
case GameFilter.All => transLocalize(trans.nbGames, u.count.game)
case GameFilter.Me => nbs.withMe ?? { transLocalize(trans.nbGamesWithYou, _) }
case GameFilter.Rated => transLocalize(trans.nbRated, u.count.rated)
case GameFilter.Win => transLocalize(trans.nbWins, u.count.win)
case GameFilter.Loss => transLocalize(trans.nbLosses, u.count.loss)
case GameFilter.Draw => transLocalize(trans.nbDraws, u.count.draw)
case GameFilter.Playing => transLocalize(trans.nbPlaying, nbs.playing)
case GameFilter.Bookmark => transLocalize(trans.nbBookmarks, nbs.bookmark)
case GameFilter.Imported => transLocalize(trans.nbImportedGames, nbs.imported)
case GameFilter.Search => trans.search.advancedSearch.txt()
}
def describeUser(user: User)(implicit lang: Lang) = {
val name = user.titleUsername
val nbGames = user.count.game
val createdAt = org.joda.time.format.DateTimeFormat forStyle "M-" print user.createdAt
val currentRating = user.perfs.bestPerf ?? { case (pt, perf) =>
s" Current ${pt.trans} rating: ${perf.intRating}."
}
s"$name played $nbGames games since $createdAt.$currentRating"
}
val patronIconChar = ""
val lineIconChar = ""
val lineIcon: Frag = i(cls := "line")
def patronIcon(implicit lang: Lang): Frag =
i(cls := "line patron", title := trans.patron.lichessPatron.txt())
val moderatorIcon: Frag = i(cls := "line moderator", title := "Lichess Mod")
private def lineIcon(patron: Boolean)(implicit lang: Lang): Frag = if (patron) patronIcon else lineIcon
private def lineIcon(user: Option[LightUser])(implicit lang: Lang): Frag = lineIcon(user.??(_.isPatron))
def lineIcon(user: LightUser)(implicit lang: Lang): Frag = lineIcon(user.isPatron)
def lineIcon(user: User)(implicit lang: Lang): Frag = lineIcon(user.isPatron)
def lineIconChar(user: User): Frag = if (user.isPatron) patronIconChar else lineIconChar
}