<commit_before>package lila.tournament

import akka.actor.{ ActorSystem, Props }
import akka.pattern.ask
import akka.stream.scaladsl._
import com.github.ghik.silencer.silent
import org.joda.time.DateTime
import play.api.libs.json._
import scala.concurrent.duration._
import scala.concurrent.Promise

import lila.common.config.{ MaxPerPage, MaxPerSecond }
import lila.common.paginator.Paginator
import lila.common.{ Bus, Debouncer, LightUser, WorkQueues }
import lila.game.{ Game, GameRepo, LightPov, Pov }
import lila.hub.actorApi.lobby.ReloadTournaments
import lila.hub.LightTeam
import lila.hub.LightTeam._
import lila.round.actorApi.round.{ AbortForce, GoBerserk }
import lila.socket.Socket.SendToFlag
import lila.user.{ User, UserRepo }
import makeTimeout.short

final class TournamentApi(
    cached: Cached,
    userRepo: UserRepo,
    gameRepo: GameRepo,
    playerRepo: PlayerRepo,
    pairingRepo: PairingRepo,
    tournamentRepo: TournamentRepo,
    apiJsonView: ApiJsonView,
    autoPairing: AutoPairing,
    pairingSystem: arena.PairingSystem,
    callbacks: TournamentApi.Callbacks,
    renderer: lila.hub.actors.Renderer,
    socket: TournamentSocket,
    tellRound: lila.round.TellRound,
    trophyApi: lila.user.TrophyApi,
    verify: Condition.Verify,
    duelStore: DuelStore,
    pause: Pause,
    cacheApi: lila.memo.CacheApi,
    lightUserApi: lila.user.LightUserApi,
    proxyRepo: lila.round.GameProxyRepo
)(
    implicit ec: scala.concurrent.ExecutionContext,
    system: ActorSystem,
    mat: akka.stream.Materializer,
    mode: play.api.Mode
) {

  private val workQueue =
    new WorkQueues(buffer = 256, expiration = 1 minute, timeout = 10 seconds, name = "tournament")

  def get(id: Tournament.ID) = tournamentRepo byId id

  def createTournament(
      setup: TournamentSetup,
      me: User,
      myTeams: List[LightTeam],
      getUserTeamIds: User => Fu[List[TeamID]]
  ): Fu[Tournament] = {
    val tour = Tournament.make(
      by = Right(me),
      name = setup.name,
      clock = setup.clockConfig,
      minutes = setup.minutes,
      waitMinutes = setup.waitMinutes | DataForm.waitMinuteDefault,
      startDate = setup.startDate,
      mode = setup.realMode,
      password = <PASSWORD>,
      variant = setup.realVariant,
      position =
        DataForm.startingPosition(setup.position | chess.StartingPosition.initial.fen, setup.realVariant),
      berserkable = setup.berserkable | true,
      teamBattle = setup.teamBattleByTeam map TeamBattle.init,
      description = setup.description
    ) |> { tour =>
      tour.perfType.fold(tour) { perfType =>
        tour.copy(conditions = setup.conditions.convert(perfType, myTeams.view.map(_.pair).toMap))
      }
    }
    tournamentRepo.insert(tour) >>
      join(tour.id, me, tour.password, setup.teamBattleByTeam, getUserTeamIds, none) inject tour
  }

  def update(old: Tournament, data: TournamentSetup, myTeams: List[LightTeam]): Funit = {
    import data._
    val tour = old.copy(
      name = name | old.name,
      clock = clockConfig,
      minutes = minutes,
      variant = realVariant,
      startsAt = startDate | old.startsAt,
      position = DataForm.startingPosition(position | chess.StartingPosition.initial.fen, realVariant),
      noBerserk = !(~berserkable),
      teamBattle = old.teamBattle,
      description = description
    ) |> { tour =>
      tour.perfType.fold(tour) { perfType =>
        tour.copy(conditions = conditions.convert(perfType, myTeams.view.map(_.pair).toMap))
      }
    }
    tournamentRepo update tour void
  }

  private[tournament] def create(tournament: Tournament): Funit = {
    tournamentRepo.insert(tournament).void
  }

  def teamBattleUpdate(
      tour: Tournament,
      data: TeamBattle.DataForm.Setup,
      filterExistingTeamIds: Set[TeamID] => Fu[Set[TeamID]]
  ): Funit =
    filterExistingTeamIds(data.potentialTeamIds) flatMap { teamIds =>
      tournamentRepo.setTeamBattle(tour.id, TeamBattle(teamIds, data.nbLeaders))<commit_msg>allow changing tournament password - closes #6402<commit_after>package lila.tournament

import akka.actor.{ ActorSystem, Props }
import akka.pattern.ask
import akka.stream.scaladsl._
import com.github.ghik.silencer.silent
import org.joda.time.DateTime
import play.api.libs.json._
import scala.concurrent.duration._
import scala.concurrent.Promise

import lila.common.config.{ MaxPerPage, MaxPerSecond }
import lila.common.paginator.Paginator
import lila.common.{ Bus, Debouncer, LightUser, WorkQueues }
import lila.game.{ Game, GameRepo, LightPov, Pov }
import lila.hub.actorApi.lobby.ReloadTournaments
import lila.hub.LightTeam
import lila.hub.LightTeam._
import lila.round.actorApi.round.{ AbortForce, GoBerserk }
import lila.socket.Socket.SendToFlag
import lila.user.{ User, UserRepo }
import makeTimeout.short

final class TournamentApi(
    cached: Cached,
    userRepo: UserRepo,
    gameRepo: GameRepo,
    playerRepo: PlayerRepo,
    pairingRepo: PairingRepo,
    tournamentRepo: TournamentRepo,
    apiJsonView: ApiJsonView,
    autoPairing: AutoPairing,
    pairingSystem: arena.PairingSystem,
    callbacks: TournamentApi.Callbacks,
    renderer: lila.hub.actors.Renderer,
    socket: TournamentSocket,
    tellRound: lila.round.TellRound,
    trophyApi: lila.user.TrophyApi,
    verify: Condition.Verify,
    duelStore: DuelStore,
    pause: Pause,
    cacheApi: lila.memo.CacheApi,
    lightUserApi: lila.user.LightUserApi,
    proxyRepo: lila.round.GameProxyRepo
)(
    implicit ec: scala.concurrent.ExecutionContext,
    system: ActorSystem,
    mat: akka.stream.Materializer,
    mode: play.api.Mode
) {

  private val workQueue =
    new WorkQueues(buffer = 256, expiration = 1 minute, timeout = 10 seconds, name = "tournament")

  def get(id: Tournament.ID) = tournamentRepo byId id

  def createTournament(
      setup: TournamentSetup,
      me: User,
      myTeams: List[LightTeam],
      getUserTeamIds: User => Fu[List[TeamID]]
  ): Fu[Tournament] = {
    val tour = Tournament.make(
      by = Right(me),
      name = setup.name,
      clock = setup.clockConfig,
      minutes = setup.minutes,
      waitMinutes = setup.waitMinutes | DataForm.waitMinuteDefault,
      startDate = setup.startDate,
      mode = setup.realMode,
      password = <PASSWORD>,
      variant = setup.realVariant,
      position =
        DataForm.startingPosition(setup.position | chess.StartingPosition.initial.fen, setup.realVariant),
      berserkable = setup.berserkable | true,
      teamBattle = setup.teamBattleByTeam map TeamBattle.init,
      description = setup.description
    ) |> { tour =>
      tour.perfType.fold(tour) { perfType =>
        tour.copy(conditions = setup.conditions.convert(perfType, myTeams.view.map(_.pair).toMap))
      }
    }
    tournamentRepo.insert(tour) >>
      join(tour.id, me, tour.password, setup.teamBattleByTeam, getUserTeamIds, none) inject tour
  }

  def update(old: Tournament, data: TournamentSetup, myTeams: List[LightTeam]): Funit = {
    import data._
    val tour = old.copy(
      name = name | old.name,
      clock = clockConfig,
      minutes = minutes,
      variant = realVariant,
      startsAt = startDate | old.startsAt,
      password = <PASSWORD>,
      position = DataForm.startingPosition(position | chess.StartingPosition.initial.fen, realVariant),
      noBerserk = !(~berserkable),
      teamBattle = old.teamBattle,
      description = description
    ) |> { tour =>
      tour.perfType.fold(tour) { perfType =>
        tour.copy(conditions = conditions.convert(perfType, myTeams.view.map(_.pair).toMap))
      }
    }
    tournamentRepo update tour void
  }

  private[tournament] def create(tournament: Tournament): Funit = {
    tournamentRepo.insert(tournament).void
  }

  def teamBattleUpdate(
      tour: Tournament,
      data: TeamBattle.DataForm.Setup,
      filterExistingTeamIds: Set[TeamID] => Fu[Set[TeamID]]
  ): Funit =
    filterExistingTeamIds(data.potentialTeamIds) flatMap { teamIds =>
      tournamentRepo.setTeamBattle(tour.id, TeamBattle(teamIds, data.nbLeaders))
{'fraction_non_alphanumeric': 0.10480566248603006, 'fraction_numerical': 0.001986837203526636, 'mean_word_length': 3.5425831923293853, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 28, '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': '7550379', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2562, 'n_words': 520}