<commit_before>package lila.tournament

import chess.Clock.{ Config => ClockConfig }
import chess.variant.Variant
import chess.{ Mode, StartingPosition }
import lila.db.BSON
import lila.db.dsl._
import lila.rating.PerfType
import lila.user.User.lichessId
import reactivemongo.api.bson._

object BSONHandlers {

  implicit private[tournament] val statusBSONHandler = tryHandler[Status](
    { case BSONInteger(v) => Status(v) toTry s"No such status: $v" },
    x => BSONInteger(x.id)
  )

  implicit private[tournament] val scheduleFreqHandler = tryHandler[Schedule.Freq](
    { case BSONString(v) => Schedule.Freq(v) toTry s"No such freq: $v" },
    x => BSONString(x.name)
  )

  implicit private[tournament] val scheduleSpeedHandler = tryHandler[Schedule.Speed](
    { case BSONString(v) => Schedule.Speed(v) toTry s"No such speed: $v" },
    x => BSONString(x.name)
  )

  implicit val tournamentClockBSONHandler = tryHandler[ClockConfig](
    {
      case doc: BSONDocument =>
        for {
          limit <- doc.getAsTry[Int]("limit")
          inc   <- doc.getAsTry[Int]("increment")
        } yield ClockConfig(limit, inc)
    },
    c =>
      BSONDocument(
        "limit"     -> c.limitSeconds,
        "increment" -> c.incrementSeconds
      )
  )

  implicit private val spotlightBSONHandler = Macros.handler[Spotlight]

  implicit val battleBSONHandler = Macros.handler[TeamBattle]

  implicit private val leaderboardRatio =
    BSONIntegerHandler.as[LeaderboardApi.Ratio](
      i => LeaderboardApi.Ratio(i.toDouble / 100_000),
      r => (r.value * 100_1000).toInt<commit_msg>fix tournament leaderboard ratio bson reader<commit_after>package lila.tournament

import chess.Clock.{ Config => ClockConfig }
import chess.variant.Variant
import chess.{ Mode, StartingPosition }
import lila.db.BSON
import lila.db.dsl._
import lila.rating.PerfType
import lila.user.User.lichessId
import reactivemongo.api.bson._

object BSONHandlers {

  implicit private[tournament] val statusBSONHandler = tryHandler[Status](
    { case BSONInteger(v) => Status(v) toTry s"No such status: $v" },
    x => BSONInteger(x.id)
  )

  implicit private[tournament] val scheduleFreqHandler = tryHandler[Schedule.Freq](
    { case BSONString(v) => Schedule.Freq(v) toTry s"No such freq: $v" },
    x => BSONString(x.name)
  )

  implicit private[tournament] val scheduleSpeedHandler = tryHandler[Schedule.Speed](
    { case BSONString(v) => Schedule.Speed(v) toTry s"No such speed: $v" },
    x => BSONString(x.name)
  )

  implicit val tournamentClockBSONHandler = tryHandler[ClockConfig](
    {
      case doc: BSONDocument =>
        for {
          limit <- doc.getAsTry[Int]("limit")
          inc   <- doc.getAsTry[Int]("increment")
        } yield ClockConfig(limit, inc)
    },
    c =>
      BSONDocument(
        "limit"     -> c.limitSeconds,
        "increment" -> c.incrementSeconds
      )
  )

  implicit private val spotlightBSONHandler = Macros.handler[Spotlight]

  implicit val battleBSONHandler = Macros.handler[TeamBattle]

  implicit private val leaderboardRatio =
    BSONIntegerHandler.as[LeaderboardApi.Ratio](
      i => LeaderboardApi.Ratio(i.toDouble / 100_000),
      r => (r.value * 100_000).toInt
{'fraction_non_alphanumeric': 0.11636589919103921, 'fraction_numerical': 0.007778469197261979, 'mean_word_length': 3.515449438202247, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3098657', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1041, 'n_words': 224}