<commit_before>package lila.game

import lila.db.Types._
import lila.user.UserRepo

import reactivemongo.core.commands.Count

import reactivemongo.bson.{ BSONDocument, BSONInteger }

final class CrosstableApi(coll: Coll) {

  import Crosstable.Result

  private val maxGames = 20

  def apply(game: Game): Fu[Option[Crosstable]] = game.userIds.distinct match {
    case List(u1, u2) => apply(u1, u2)
    case _            => fuccess(none)
  }

  def apply(u1: String, u2: String): Fu[Option[Crosstable]] =
    coll.find(select(u1, u2)).one[Crosstable] orElse create(u1, u2) recover {
      case e: reactivemongo.core.commands.LastError =>
        play.api.Logger("crosstable").warn(e.getMessage)
        none
    }

  def add(game: Game): Funit = game.userIds.distinct.sorted match {
    case List(u1, u2) =>
      val result = Result(game.id, game.winnerUserId)
      val bsonResult = Crosstable.crosstableBSONHandler.writeResult(result, u1)
      val bson = BSONDocument(
        "$inc" -> BSONDocument(Crosstable.BSONFields.nbGames -> BSONInteger(1))
      ) ++ {
          if (game.rated) BSONDocument("$push" -> BSONDocument(
            Crosstable.BSONFields.results -> BSONDocument(
              "$each" -> List(bsonResult),
              "$slice" -> -maxGames
            )))
          else BSONDocument()<commit_msg>reduce logging<commit_after>package lila.game

import lila.db.Types._
import lila.user.UserRepo

import reactivemongo.core.commands.Count

import reactivemongo.bson.{ BSONDocument, BSONInteger }

final class CrosstableApi(coll: Coll) {

  import Crosstable.Result

  private val maxGames = 20

  def apply(game: Game): Fu[Option[Crosstable]] = game.userIds.distinct match {
    case List(u1, u2) => apply(u1, u2)
    case _            => fuccess(none)
  }

  def apply(u1: String, u2: String): Fu[Option[Crosstable]] =
    coll.find(select(u1, u2)).one[Crosstable] orElse create(u1, u2) recover {
      case e: reactivemongo.core.commands.LastError =>
        play.api.Logger("crosstable").info(e.getMessage)
        none
    }

  def add(game: Game): Funit = game.userIds.distinct.sorted match {
    case List(u1, u2) =>
      val result = Result(game.id, game.winnerUserId)
      val bsonResult = Crosstable.crosstableBSONHandler.writeResult(result, u1)
      val bson = BSONDocument(
        "$inc" -> BSONDocument(Crosstable.BSONFields.nbGames -> BSONInteger(1))
      ) ++ {
          if (game.rated) BSONDocument("$push" -> BSONDocument(
            Crosstable.BSONFields.results -> BSONDocument(
              "$each" -> List(bsonResult),
              "$slice" -> -maxGames
            )))
          else BSONDocument()
{'fraction_non_alphanumeric': 0.1266490765171504, 'fraction_numerical': 0.01206181681115718, 'mean_word_length': 2.949404761904762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6803345', 'n_tokens_mistral': 906, 'n_tokens_neox': 889, 'n_words': 174}