Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golf: minor cleanup #269

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions modules/crawler/src/main/scala/Downloader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import org.http4s.implicits.*
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.syntax.*

type PlayerInfo = (NewPlayer, Option[NewFederation])
trait Downloader:
def fetch: fs2.Stream[IO, PlayerInfo]
def fetch: fs2.Stream[IO, (NewPlayer, Option[NewFederation])]

object Downloader:
val downloadUrl = uri"http://ratings.fide.com/download/players_list.zip"
Expand Down Expand Up @@ -41,15 +40,15 @@ object Downloader:
parsePlayer(line).traverse: player =>
player.federationId.traverse(findFederation(_, player.id)).map(player -> _)

def findFederation(id: FederationId, playerId: PlayerId): IO[NewFederation] =
Federation.all.get(id) match
case None => warn"Unkown federation: $id for player: $playerId".as(NewFederation(id, id.value))
case Some(name) => NewFederation(id, name).pure

IO(line.trim.nonEmpty)
.ifM(parse(line), none.pure[IO])
.handleErrorWith(e => Logger[IO].error(e)(s"Error while parsing line: $line").as(none))

def findFederation(id: FederationId, playerId: PlayerId): Logger[IO] ?=> IO[NewFederation] =
Federation.all.get(id) match
case None => warn"Unkown federation: $id for player: $playerId".as(NewFederation(id, id.value))
case Some(name) => NewFederation(id, name).pure

// shamelessly copied (with some minor modificaton) from: https://github.com/lichess-org/lila/blob/8033c4c5a15cf9bb2b36377c3480f3b64074a30f/modules/fide/src/main/FidePlayerSync.scala#L131
def parsePlayer(line: String): Option[NewPlayer] =
def string(start: Int, end: Int): Option[String] = line.substring(start, end).trim.some.filter(_.nonEmpty)
Expand Down
4 changes: 1 addition & 3 deletions modules/types/src/main/scala/FederationId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ object FederationId:
def option(value: String): Option[FederationId] =
either(value).toOption

extension (str: FederationId)
inline def toStr: String = str
inline def value: IronType[String, NonEmpty] = rtc.value(str)
extension (str: FederationId) inline def value: IronType[String, NonEmpty] = rtc.value(str)