<commit_before>{Conditions} = require '../conditions'

# Abstracts out sending messages from player to battle.
# Makes the Battle smoothly go into the next turn
# Necessary to separate out making commands and executing commands.
class @BattleController
  constructor: (@battle) ->
    @arranged = []
    @arranging = false

  # Tells the player to execute a certain move by name. The move is added
  # to the list of player actions, which are executed once the turn continues.
  makeMove: (player, moveName, forSlot = 0, forTurn = @battle.turn, args...) ->
    return  if @battle.isOver()
    return  if forTurn != @battle.turn
    pokemon = @battle.getTeam(player.id).at(forSlot)
    return  if !pokemon
    request = @battle.requestFor(pokemon)
    return  if !request
    return  if moveName not in (request.moves || [])
    move = @battle.getMove(moveName)
    @battle.recordMove(player.id, move, forSlot, args...)
    @transitionToNextState()

  # Tells the player to switch with a certain pokemon specified by position.
  # The switch is added to the list of player actions, which are executed
  # once the turn continues.
  makeSwitch: (player, toPosition, forSlot = 0, forTurn = @battle.turn) ->
    return  if @battle.isOver()
    return  if forTurn != @battle.turn
    pokemon = @battle.getTeam(player.id).at(forSlot)
    return  if !pokemon
    request = @battle.requestFor(pokemon)
    return  if !request
    return  if toPosition not in (request.switches || [])
    @battle.recordSwitch(player.id, toPosition, forSlot)
    @transitionToNextState()

  # Makes a player forfeit.
  forfeit: (player) ->
    return  if @battle.isOver()
    @battle.forfeit(player)
    @sendUpdates()

  addSpectator: (spectator) ->
    @battle.addSpectator(spectator)
    if @arranging && spectator not in @battle.players
      teams = @battle.players.map((p) -> p.team.toJSON(hidden: true))
      spectator.send? 'team preview', @battle.id, teams

  removeSpectator: (spectator) ->
    @battle.removeSpectator(spectator)

  messageSpectators: (user, message) ->
    for spectator in @battle.spectators
      spectator.send('update battle chat', @battle.id, user.id, message)

  # Continue or begin a new turn if each player has made an action.
  transitionToNextState: ->
    return  if not @battle.areAllRequestsCompleted()
    if @battle.replacing
      @battle.performReplacements()
    else
      @continueTurn()<commit_msg>Sim: Whoops. Send updates after replacing.<commit_after>{Conditions} = require '../conditions'

# Abstracts out sending messages from player to battle.
# Makes the Battle smoothly go into the next turn
# Necessary to separate out making commands and executing commands.
class @BattleController
  constructor: (@battle) ->
    @arranged = []
    @arranging = false

  # Tells the player to execute a certain move by name. The move is added
  # to the list of player actions, which are executed once the turn continues.
  makeMove: (player, moveName, forSlot = 0, forTurn = @battle.turn, args...) ->
    return  if @battle.isOver()
    return  if forTurn != @battle.turn
    pokemon = @battle.getTeam(player.id).at(forSlot)
    return  if !pokemon
    request = @battle.requestFor(pokemon)
    return  if !request
    return  if moveName not in (request.moves || [])
    move = @battle.getMove(moveName)
    @battle.recordMove(player.id, move, forSlot, args...)
    @transitionToNextState()

  # Tells the player to switch with a certain pokemon specified by position.
  # The switch is added to the list of player actions, which are executed
  # once the turn continues.
  makeSwitch: (player, toPosition, forSlot = 0, forTurn = @battle.turn) ->
    return  if @battle.isOver()
    return  if forTurn != @battle.turn
    pokemon = @battle.getTeam(player.id).at(forSlot)
    return  if !pokemon
    request = @battle.requestFor(pokemon)
    return  if !request
    return  if toPosition not in (request.switches || [])
    @battle.recordSwitch(player.id, toPosition, forSlot)
    @transitionToNextState()

  # Makes a player forfeit.
  forfeit: (player) ->
    return  if @battle.isOver()
    @battle.forfeit(player)
    @sendUpdates()

  addSpectator: (spectator) ->
    @battle.addSpectator(spectator)
    if @arranging && spectator not in @battle.players
      teams = @battle.players.map((p) -> p.team.toJSON(hidden: true))
      spectator.send? 'team preview', @battle.id, teams

  removeSpectator: (spectator) ->
    @battle.removeSpectator(spectator)

  messageSpectators: (user, message) ->
    for spectator in @battle.spectators
      spectator.send('update battle chat', @battle.id, user.id, message)

  # Continue or begin a new turn if each player has made an action.
  transitionToNextState: ->
    return  if not @battle.areAllRequestsCompleted()
    if @battle.replacing
      @battle.performReplacements()
      @sendUpdates()
    else
      @continueTurn()
{'fraction_non_alphanumeric': 0.10747854515733551, 'fraction_numerical': 0.0008173273395995096, 'mean_word_length': 3.470319634703196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '4650043', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1519, 'n_words': 469}