   opposingTeams = _.pull teams, sessionTeam
   return opposingTeams[0]
 
-incrementUserSimulationCount = (userID, type) ->
-  inc = {}
-  inc[type] = 1
-  User.update {_id: userID}, {$inc: inc}, (err, affected) ->
-    log.error "Error incrementing #{type} for #{userID}: #{err}" if err
-
-
-addMatchToSessions = (clientResponseObject, newScoreObject, callback) ->
-  matchObject = {}
-  matchObject.date = new Date()
-  matchObject.opponents = {}
-  for session in clientResponseObject.sessions
-    sessionID = session.sessionID
-    matchObject.opponents[sessionID] = {}
-    matchObject.opponents[sessionID].sessionID = sessionID
-    matchObject.opponents[sessionID].userID = session.creator
-    matchObject.opponents[sessionID].metrics = {}
-    matchObject.opponents[sessionID].metrics.rank = Number(newScoreObject[sessionID].gameRanking)
-
-  log.info "Match object computed, result: #{matchObject}"
-  log.info "Writing match object to database..."
-  #use bind with async to do the writes
-  sessionIDs = _.pluck clientResponseObject.sessions, 'sessionID'
-  async.each sessionIDs, updateMatchesInSession.bind(@,matchObject), (err) -> callback err, null
-
-updateMatchesInSession = (matchObject, sessionID, callback) ->
-  currentMatchObject = {}
-  currentMatchObject.date = matchObject.date
-  currentMatchObject.metrics = matchObject.opponents[sessionID].metrics
-  opponentsClone = _.cloneDeep matchObject.opponents
-  opponentsClone = _.omit opponentsClone, sessionID
-  opponentsArray = _.toArray opponentsClone
-  currentMatchObject.opponents = opponentsArray
-
-  sessionUpdateObject =
-    $push: {matches: {$each: [currentMatchObject], $slice: -200}}
-  log.info "Updating session #{sessionID}"
-  LevelSession.update {"_id":sessionID}, sessionUpdateObject, callback
+
+addNewSessionsToQueue = (sessionID, callback) ->
+  sessions = [@clientResponseObject.originalSessionID, sessionID]
+  addPairwiseTaskToQueue sessions, callback
+
 
 
 


handleTimedOutTask = (req, res, taskBody) -> errors.clientTimeout res, "The results weren't provided within the timeout"

verifyClientResponse = (responseObject, res) ->
  unless typeof responseObject is "object"
    errors.badInput res, "The response to that query is required to be a JSON object."
    null
  else
    responseObject

logTaskComputation = (taskObject,taskLogObject, callback) ->
  taskLogObject.calculationTimeMS = taskObject.calculationTimeMS
  taskLogObject.sessions = taskObject.sessions
  taskLogObject.save callback


updateSessions = (taskObject,callback) ->
  sessionIDs = _.pluck taskObject.sessions, 'sessionID'

  async.map sessionIDs, retrieveOldSessionData, (err, oldScores) ->
    if err? then callback err, {"error": "There was an error retrieving the old scores"}

    oldScoreArray = _.toArray putRankingFromMetricsIntoScoreObject taskObject, oldScores
    newScoreArray = bayes.updatePlayerSkills oldScoreArray
    saveNewScoresToDatabase newScoreArray, callback


saveNewScoresToDatabase = (newScoreArray, callback) ->
  async.eachSeries newScoreArray, updateScoreInSession, (err) -> callback err,newScoreArray


updateScoreInSession = (scoreObject,callback) ->
  LevelSession.findOne {"_id": scoreObject.id}, (err, session) ->
    if err? then return callback err, null

    session = session.toObject()
    newTotalScore = scoreObject.meanStrength - 1.8 * scoreObject.standardDeviation
    scoreHistoryAddition = [Date.now(), newTotalScore]
    updateObject =
      meanStrength: scoreObject.meanStrength
      standardDeviation: scoreObject.standardDeviation
      totalScore: newTotalScore
      $push: {scoreHistory: {$each: [scoreHistoryAddition], $slice: -1000}}

    LevelSession.update {"_id": scoreObject.id}, updateObject, callback
    log.info "New total score for session #{scoreObject.id} is #{updateObject.totalScore}"


putRankingFromMetricsIntoScoreObject = (taskObject,scoreObject) ->

  query.exec (err, retrievedSession) ->
    if err? then return callback err
    userHasPermissionToSubmitCode = retrievedSession.creator is req.user?.id and
      not _.isEqual(retrievedSession.code, retrievedSession.submittedCode)
    unless userHasPermissionToSubmitCode then return callback "You are unauthorized to submit that game to the simulator"
    callback null

