 
   getAction: (clientId) =>
     @playerActions[clientId]
+
+  hasWeather: (weatherName) =>
+    weather = (if @hasWeatherCancelAbilityOnField() then "None" else @weather)
+    weatherName == weather
+
+  hasWeatherCancelAbilityOnField: =>
+    _.any @getActivePokemon(), (pokemon) ->
+      pokemon.hasAbility('Air Lock') || pokemon.hasAbility('Cloud Nine')
+
+  # Add `string` to a buffer that will be sent to each client.
+  message: (string) =>
+    @buffer.push(string)
+
+  clearMessages: =>
+    while @buffer.length > 0
+      @buffer.pop()
+
+  # Sends to each player the battle messages that have been queued up
+  # TODO: It should be sent to spectators as well
+  sendMessages: =>
+    @message 'The turn continued.'
+    for id, player of @players
+      player.updateChat('SERVER', @buffer.join("<br>"))
+    @clearMessages()
 
   # 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.


    @makeSwitch(player, index)

  hasWeather: (weatherName) =>
    weather = (if @hasWeatherCancelAbilityOnField() then "None" else @weather)
    weatherName == weather

  hasWeatherCancelAbilityOnField: =>
    _.any @getActivePokemon(), (pokemon) ->
      pokemon.hasAbility('Air Lock') || pokemon.hasAbility('Cloud Nine')

  # Returns true if all requests have been completed. False otherwise.
  areAllRequestsCompleted: =>
    requests = 0
    requests += 1  for id of @requests
    requests == 0

  # Add `string` to a buffer that will be sent to each client.
  message: (string) =>
    @buffer.push(string)

  clearBuffer: =>
    while @buffer.length > 0
      @buffer.pop()

  startNewTurn: =>
    @turn++


  getTeam: (id) =>
    @getPlayer(id).team

  # Returns all opponents of a given player. In a 1v1 it returns
  # an array with only one opponent.
  getOpponents: (id) =>
    opponents = []
    for playerId, player of @players
      opponents.push(player)  if id != playerId
    opponents

  # Returns all opponent pokemon of a given player.
  #
  # id - The player who's opponent's pokemon we want to retrieve
  # max (optional) - The maximum amount of pokemon per opponent.
  #
  getOpponentPokemon: (id, max) =>
    opponents = @getOpponents(id)
    teams = (opponent.team.all()  for opponent in opponents)
    teams = (team.slice(0, max)   for team in teams)  if max?
    _.flatten(teams)

  # Returns all active pokemon on the field belonging to both players.
  # Active pokemon include fainted pokemon that have not been switched out.
  getActivePokemon: =>
    pokemon = []
    for id, player of @players
