<commit_before>package lila.lobby

import org.joda.time.DateTime
import ornicar.scalalib.Random
import play.api.libs.json._

import chess.{ Variant, Mode, Clock }
import lila.common.EloRange
import lila.user.User

case class Hook(
    id: String,
    uid: String, // owner socket uid
    sid: Option[String], // owner cookie (used to prevent multiple hooks)
    variant: Int,
    hasClock: Boolean,
    time: Option[Int],
    increment: Option[Int],
    mode: Int,
    allowAnon: Boolean,
    color: String,
    userId: Option[String],
    username: String,
    elo: Option[Int],
    eloRange: String,
    engine: Boolean,
    gameId: Option[String] = None,
    createdAt: DateTime) {

  def open = gameId.isEmpty
  def closed = !open

  def realColor = Color orDefault color

  def realVariant = Variant orDefault variant

  def realMode = Mode orDefault mode

  lazy val realEloRange: Option[EloRange] = EloRange noneIfDefault eloRange

  def render: JsObject = Json.obj(
    "id" -> id,
    "uid" -> uid,
    "username" -> username,
    "elo" -> elo,
    "variant" -> realVariant.toString,
    "mode" -> realMode.toString,
    "allowAnon" -> allowAnon,
    "clock" -> clockOption.map(c ⇒ renderClock(c.limit, c.increment)),
    "time" -> clockOption.map(_.estimateTotalTime),
    "speed" -> chess.Speed(clockOption).id,
    "emin" -> realEloRange.map(_.min),
    "emax" -> realEloRange.map(_.max),
    "color" -> chess.Color(color).??(_.name),
    "engine" -> engine)

  private def clockOption = (time filter (_ ⇒ hasClock)) |@| increment apply Clock.apply

  private def renderClock(time: Int, inc: Int) = "%d + %d".format(time / 60, inc)
}

object Hook {

  val idSize = 8

  def make(
    uid: String,
    variant: Variant,
    clock: Option[Clock],
    mode: Mode,
    allowAnon: Boolean,
    color: String,
    user: Option[User],
    sid: Option[String],
    eloRange: EloRange): Hook = new Hook(
    id = Random nextString idSize,
    uid = uid,
    variant = variant.id,
    hasClock = clock.isDefined,
    time = clock map (_.limit),
    increment = clock map (_.increment),
    mode = mode.id,
    allowAnon = allowAnon,
    color = color,
    userId = user map (_.id),
    username = user.fold(User.anonymous)(_.username),
    sid = sid,
    elo = user map (_.elo),
    eloRange = eloRange.toString,
    engine = user.??(_.engine),
    createdAt = DateTime.now)
}<commit_msg>fix anon hooks<commit_after>package lila.lobby

import org.joda.time.DateTime
import ornicar.scalalib.Random
import play.api.libs.json._

import chess.{ Variant, Mode, Clock }
import lila.common.EloRange
import lila.user.User

case class Hook(
    id: String,
    uid: String, // owner socket uid
    sid: Option[String], // owner cookie (used to prevent multiple hooks)
    variant: Int,
    hasClock: Boolean,
    time: Option[Int],
    increment: Option[Int],
    mode: Int,
    allowAnon: Boolean,
    color: String,
    userId: Option[String],
    username: String,
    elo: Option[Int],
    eloRange: String,
    engine: Boolean,
    gameId: Option[String] = None,
    createdAt: DateTime) {

  def open = gameId.isEmpty
  def closed = !open

  def realColor = Color orDefault color

  def realVariant = Variant orDefault variant

  def realMode = Mode orDefault mode

  lazy val realEloRange: Option[EloRange] = EloRange noneIfDefault eloRange

  def render: JsObject = Json.obj(
    "id" -> id,
    "uid" -> uid,
    "username" -> username,
    "elo" -> elo,
    "variant" -> realVariant.toString,
    "mode" -> realMode.toString,
    "allowAnon" -> allowAnon,
    "clock" -> clockOption.map(c ⇒ renderClock(c.limit, c.increment)),
    "time" -> clockOption.map(_.estimateTotalTime),
    "speed" -> chess.Speed(clockOption).id,
    "emin" -> realEloRange.map(_.min),
    "emax" -> realEloRange.map(_.max),
    "color" -> chess.Color(color).??(_.name),
    "engine" -> engine)

  private def clockOption = (time filter (_ ⇒ hasClock)) |@| increment apply Clock.apply

  private def renderClock(time: Int, inc: Int) = "%d + %d".format(time / 60, inc)
}

object Hook {

  val idSize = 8

  def make(
    uid: String,
    variant: Variant,
    clock: Option[Clock],
    mode: Mode,
    allowAnon: Boolean,
    color: String,
    user: Option[User],
    sid: Option[String],
    eloRange: EloRange): Hook = new Hook(
    id = Random nextString idSize,
    uid = uid,
    variant = variant.id,
    hasClock = clock.isDefined,
    time = clock map (_.limit),
    increment = clock map (_.increment),
    mode = mode.id,
    allowAnon = allowAnon || user.isEmpty,
    color = color,
    userId = user map (_.id),
    username = user.fold(User.anonymous)(_.username),
    sid = sid,
    elo = user map (_.elo),
    eloRange = eloRange.toString,
    engine = user.??(_.engine),
    createdAt = DateTime.now)
}
{'fraction_non_alphanumeric': 0.13320825515947468, 'fraction_numerical': 0.0012507817385866166, 'mean_word_length': 2.9916805324459235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '5543513', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1596, 'n_words': 439}