forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically set user title from notes containing FIDE URLs
- Loading branch information
Showing
10 changed files
with
74 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lila.user | ||
|
||
import play.api.libs.ws.WS | ||
import play.api.Play.current | ||
|
||
case class Title(value: String) extends AnyVal with StringValue | ||
|
||
object Title { | ||
|
||
// important: names are as stated on FIDE profile pages | ||
val all = Seq( | ||
"GM" -> "Grandmaster", | ||
"WGM" -> "Woman Grandmaster", | ||
"IM" -> "International Master", | ||
"WIM" -> "Woman Intl. Master", | ||
"FM" -> "FIDE Master", | ||
"WFM" -> "Woman FIDE Master", | ||
"NM" -> "National Master", | ||
"CM" -> "Candidate Master", | ||
"WCM" -> "Woman Candidate Master", | ||
"WNM" -> "Woman National Master", | ||
"LM" -> "Lichess Master", | ||
"BOT" -> "Chess Robot" | ||
) | ||
|
||
val bot = lila.common.LightUser.botTitle | ||
|
||
val names = all.toMap | ||
lazy val fromNames = all.map(_.swap).toMap | ||
|
||
def titleName(title: String) = names get title getOrElse title | ||
} | ||
|
||
final class TitleUrl { | ||
|
||
// https://ratings.fide.com/card.phtml?event=740411 | ||
private val FideProfileUrlRegex = """(?:https?:https://)ratings\.fide\.com/card\.phtml\?event=(\d+)""".r | ||
// > FIDE title</td><td colspan=3 bgcolor=#efefef> Grandmaster</td> | ||
private val FideProfileTitleRegex = """> FIDE title</td><td colspan=3 bgcolor=#efefef> ([^<]+)</td>""".r.unanchored | ||
|
||
def apply(url: String): Fu[Option[String]] = url.trim match { | ||
case FideProfileUrlRegex(id) => parseIntOption(id) ?? fromFideProfile | ||
case _ => fuccess(none) | ||
} | ||
|
||
private def fromFideProfile(id: Int): Fu[Option[String]] = { | ||
WS.url(s"""https://ratings.fide.com/card.phtml?event=$id""").get().map(_.body) map { | ||
case FideProfileTitleRegex(name) => Title.fromNames get name | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters