<commit_before>TurkServer.Assigners = {}

# Default top level class
class TurkServer.Assigner
  initialize: (batch) ->
    @batch = batch
    @lobby = batch.lobby

    # Pre-bind callbacks below to avoid ugly fat arrows
    @lobby.events.on "user-join", @userJoined.bind(@)
    @lobby.events.on "user-status", @userStatusChanged.bind(@)
    @lobby.events.on "user-leave", @userLeft.bind(@)
    return

  assignToNewInstance: (assts, treatments) ->
    @lobby.pluckUsers( _.pluck(assts, "userId") )

    instance = @batch.createInstance(treatments)
    instance.setup()
    instance.addAssignment(asst) for asst in assts
    return instance

  userJoined: ->
  userStatusChanged: ->
  userLeft: ->

###
  Very dumb assigner: puts everyone who joins into a single group.
  Once the instance ends, puts users in exit survey.
###
class TurkServer.Assigners.TestAssigner extends TurkServer.Assigner
  initialize: ->
    super
    # Take any experiment from this batch, creating it if it doesn't exist
    if (exp = Experiments.findOne(batchId: @batch.batchId))?
      @instance = TurkServer.Instance.getInstance(exp._id)
    else
      @instance = @batch.createInstance(@batch.getTreatments())
      @instance.setup()

  userJoined: (asst) ->
    if asst.getInstances().length > 0
      @lobby.pluckUsers( [asst.userId] )
      asst.showExitSurvey()
    else
      try
        @instance.addAssignment(asst)
      @lobby.pluckUsers( [asst.userId] )

###
  Assigns everyone who joins in a separate group.
  Anyone who is done with their instance goes into the exit survey
###
class TurkServer.Assigners.SimpleAssigner extends TurkServer.Assigner
  constructor: ->

  userJoined: (asst) ->
    if asst.getInstances().length > 0
      # Send user to exit survey
      @lobby.pluckUsers( [asst.userId] )
      asst.showExitSurvey()
    else
      # Assign user to instance
      treatment = _.sample @batch.getTreatments()
      @assignToNewInstance( [asst], [treatment] )

###
  Assigns users first to a tutorial treatment,
  then to a single group.
  An event on the lobby is used to trigger the group.
###<commit_msg>fixed SimpleAssigner<commit_after>TurkServer.Assigners = {}

# Default top level class
class TurkServer.Assigner
  initialize: (batch) ->
    @batch = batch
    @lobby = batch.lobby

    # Pre-bind callbacks below to avoid ugly fat arrows
    @lobby.events.on "user-join", @userJoined.bind(@)
    @lobby.events.on "user-status", @userStatusChanged.bind(@)
    @lobby.events.on "user-leave", @userLeft.bind(@)
    return

  assignToNewInstance: (assts, treatments) ->
    @lobby.pluckUsers( _.pluck(assts, "userId") )

    instance = @batch.createInstance(treatments)
    instance.setup()
    instance.addAssignment(asst) for asst in assts
    return instance

  userJoined: ->
  userStatusChanged: ->
  userLeft: ->

###
  Very dumb assigner: puts everyone who joins into a single group.
  Once the instance ends, puts users in exit survey.
###
class TurkServer.Assigners.TestAssigner extends TurkServer.Assigner
  initialize: ->
    super
    # Take any experiment from this batch, creating it if it doesn't exist
    if (exp = Experiments.findOne(batchId: @batch.batchId))?
      @instance = TurkServer.Instance.getInstance(exp._id)
    else
      @instance = @batch.createInstance(@batch.getTreatments())
      @instance.setup()

  userJoined: (asst) ->
    if asst.getInstances().length > 0
      @lobby.pluckUsers( [asst.userId] )
      asst.showExitSurvey()
    else
      try
        @instance.addAssignment(asst)
      @lobby.pluckUsers( [asst.userId] )

###
  Assigns everyone who joins in a separate group.
  Anyone who is done with their instance goes into the exit survey
###
class TurkServer.Assigners.SimpleAssigner extends TurkServer.Assigner
  constructor: ->

  userJoined: (asst) ->
    if asst.getInstances().length > 0
      # Send user to exit survey
      @lobby.pluckUsers( [asst.userId] )
      asst.showExitSurvey()
    else
      # Assign user to instance
      treatment = _.sample @batch.getTreatments() || []
      @assignToNewInstance( [asst], [treatment] )

###
  Assigns users first to a tutorial treatment,
  then to a single group.
  An event on the lobby is used to trigger the group.
###
{'fraction_non_alphanumeric': 0.1105598866052445, 'fraction_numerical': 0.0009449562957713206, 'mean_word_length': 3.3028455284552845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1736726', 'n_tokens_mistral': 1336, 'n_tokens_neox': 1303, 'n_words': 367}