<commit_before>Protocol =
  CHANGE_HP      : 1
  SWITCH         : 2
  CHANGE_PP      : 3
  REQUEST_ACTION : 4

class @BattleCollection extends Backbone.Collection
  model: Battle

  initialize: (models, options) =>
    {@socket} = options

    @socket.addEvents
      'start battle': @startBattle
      'update battle': @updateBattle
      'battle switch': @battleSwitch
      'request action': @requestAction

  startBattle: (socket, id, numActive, index, teams) =>
    console.log "BATTLE STARTED."
    $battle = $('.battle')
    battle = new Battle({id, numActive, socket, index, teams})
    battle.view = new BattleView(el: $battle, model: battle)
    @add(battle)

  requestAction: (socket, battleId, validActions, team) =>
    console.log "ACTION REQUESTED:"
    console.log validActions

    battle = @get(battleId)
    view = battle.view
    battle.teams[battle.index] = team
    view.enableButtons(validActions)

  updateBattle: (socket, battleId, actions) =>
    view = @get(battleId).view
    for action in actions
      [ type, player, rest... ] = action
      protocol = (key  for key, value of Protocol when value == type)[0]
      console.log "Received protocol: #{protocol}"
      switch type
        when Protocol.CHANGE_HP
          [slot, newHP] = rest
          view.model.getPokemon(player, slot).hp = newHP
          # TODO: Have this be called automatically.
          view.changeHP(player, slot)
        when Protocol.SWITCH
          team = view.model.getTeam(player)
          [toSlot, fromSlot] = rest
          [team[toSlot], team[fromSlot]] = [team[fromSlot], team[toSlot]]
          # TODO: Again, automatic.
          view.renderBattle()
        when Protocol.CHANGE_PP
          [slot, moveIndex, newPP] = rest
          pokemon = view.model.getPokemon(player, slot)
          pokemon.pp[moveIndex] = newPP
        when Protocol.REQUEST_ACTION
          [validActions] = rest
          console.log "ACTION REQUESTED:"
          console.log validActions
          view.enableButtons(validActions)


  battleSwitch: (socket, battleId, playerIndex, fromSlot, toSlot) =><commit_msg>Remove old request action<commit_after>Protocol =
  CHANGE_HP      : 1
  SWITCH         : 2
  CHANGE_PP      : 3
  REQUEST_ACTION : 4

class @BattleCollection extends Backbone.Collection
  model: Battle

  initialize: (models, options) =>
    {@socket} = options

    @socket.addEvents
      'start battle': @startBattle
      'update battle': @updateBattle
      'battle switch': @battleSwitch

  startBattle: (socket, id, numActive, index, teams) =>
    console.log "BATTLE STARTED."
    $battle = $('.battle')
    battle = new Battle({id, numActive, socket, index, teams})
    battle.view = new BattleView(el: $battle, model: battle)
    @add(battle)

  updateBattle: (socket, battleId, actions) =>
    view = @get(battleId).view
    for action in actions
      [ type, player, rest... ] = action
      protocol = (key  for key, value of Protocol when value == type)[0]
      console.log "Received protocol: #{protocol}"
      switch type
        when Protocol.CHANGE_HP
          [slot, newHP] = rest
          view.model.getPokemon(player, slot).hp = newHP
          # TODO: Have this be called automatically.
          view.changeHP(player, slot)
        when Protocol.SWITCH
          team = view.model.getTeam(player)
          [toSlot, fromSlot] = rest
          [team[toSlot], team[fromSlot]] = [team[fromSlot], team[toSlot]]
          # TODO: Again, automatic.
          view.renderBattle()
        when Protocol.CHANGE_PP
          [slot, moveIndex, newPP] = rest
          pokemon = view.model.getPokemon(player, slot)
          pokemon.pp[moveIndex] = newPP
        when Protocol.REQUEST_ACTION
          [validActions] = rest
          console.log "ACTION REQUESTED:"
          console.log validActions
          view.enableButtons(validActions)


  battleSwitch: (socket, battleId, playerIndex, fromSlot, toSlot) =>
{'fraction_non_alphanumeric': 0.10885562040091347, 'fraction_numerical': 0.002537427048972342, 'mean_word_length': 2.5836363636363635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '6711269', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1174, 'n_words': 309}