<commit_before># app/client/game/play.coffee

# methods


answerQuestion = (answer) ->
  # pause asset
  currentAsset().pause()
  $(currentAsset()).unbind('timeupdate')

  # calculate points
  points = parseInt($('#asset-bar').text(), 10)
  # if asset hasn't started, max points
  if isNaN points then points = currentGame().pointsPerQuestion

  # update current game object
  Games.update currentGameId(),
    $addToSet:
      answers:
        questionId: currentQuestion()._id
        answer: answer
        points: points
    $inc:
      currentQuestion: 1

  question_counter = Session.get('currentQuestion')

  # If we answered the last question
  if question_counter + 1 == currentQuiz().questionIds.length
    Meteor.call 'endGame', currentGameId(), (error, result) ->
      Router.go 'game', _id: currentGameId(), action: 'result'
  else
    # otherwise go to the next question
    Session.set('currentQuestion', question_counter + 1)
    Template.question.startQuestion()


randomSegment = (sound) ->
  unless sound.segments?.length then return null
  sound.segments[Math.floor(Math.random() * sound.segments.length)]


# helpers

Template.assets.helpers
  segments: ->
    questions = currentQuiz().questionIds.map (id) -> Questions.findOne id
    sounds = questions.map (question) -> Sounds.findOne question.soundId

    # wrap the sound segments with id
    sounds.map (sound) ->
      id:   sound._id
      path: "/audio/#{randomSegment(sound)}"

  # start playback of audio element
  playAsset: (callback) ->
    asset = currentAsset()

    # bind audio progress
    @bindAssetProgress asset
    # play asset
    asset.play()

    ## check that the audio element is playing, if not deal with it
    # setTimeout( ->
    #   if asset.paused
    #     notify
    #       title:   "Gør dig klar!"
    #       confirm: "Spil!"
    #   else
    #     $('.alternative').prop 'disabled', false
    #     callback asset if callback?
    # , 1000)

  # binds audio element progression with progress bar
  bindAssetProgress: (asset) ->
    $(asset).bind 'timeupdate', ->
      percent = (this.currentTime * 100) / this.duration
      Session.set 'gameProgress', percent
      value = (currentGame().pointsPerQuestion * (100 - percent)) / 100

      # update progress bar width depending on audio progress
      $('#asset-bar')
        .attr('style', "width: #{100 - percent}%")
        .text Math.floor(value)

# Template.quiz.helpers
#   currentGameId: ->
#     currentGameId()

Template.question.helpers
  currentQuestion: -> currentQuiz().name

  currentQuestionNumber: -> currentGame().currentQuestion + 1

  numberOfQuestions: -> numberOfQuestions()

  alternatives: ->
    currentQuestion().alternatives

  progressBarColor: ->
    percent = Session.get 'gameProgress'
    if percent is 100
      ''
    else if percent > 66
      'percent-bar-danger'
    else if percent > 33
      'progress-bar-warning'
    else
      'progress-bar-success'


Template.question.startQuestion = ->
  # Reset progress bar
  $('#asset-bar')
    .attr('style', "width: 100%")
    .text Math.floor(currentQuiz().pointsPerQuestion)

  $('.alternative').prop 'disabled', false
  playAsset

# rendered
Template.question.rendered = ->
  Template.question.startQuestion()


# events
Template.question.events
  # answer question with clicked alternative
  'click .alternative': (event) ->
    $('.alternative').prop 'disabled', true
    answerQuestion event.target.id
<commit_msg>[Fix] Misspelled `playAsset`<commit_after># app/client/game/play.coffee

# methods


answerQuestion = (answer) ->
  # pause asset
  currentAsset().pause()
  $(currentAsset()).unbind('timeupdate')

  # calculate points
  points = parseInt($('#asset-bar').text(), 10)
  # if asset hasn't started, max points
  if isNaN points then points = currentGame().pointsPerQuestion

  # update current game object
  Games.update currentGameId(),
    $addToSet:
      answers:
        questionId: currentQuestion()._id
        answer: answer
        points: points
    $inc:
      currentQuestion: 1

  question_counter = Session.get('currentQuestion')

  # If we answered the last question
  if question_counter + 1 == currentQuiz().questionIds.length
    Meteor.call 'endGame', currentGameId(), (error, result) ->
      Router.go 'game', _id: currentGameId(), action: 'result'
  else
    # otherwise go to the next question
    Session.set('currentQuestion', question_counter + 1)
    Template.question.startQuestion()


randomSegment = (sound) ->
  unless sound.segments?.length then return null
  sound.segments[Math.floor(Math.random() * sound.segments.length)]


# helpers

Template.assets.helpers
  segments: ->
    questions = currentQuiz().questionIds.map (id) -> Questions.findOne id
    sounds = questions.map (question) -> Sounds.findOne question.soundId

    # wrap the sound segments with id
    sounds.map (sound) ->
      id:   sound._id
      path: "/audio/#{randomSegment(sound)}"

  # start playback of audio element
  playAsset: (callback) ->
    asset = currentAsset()

    # bind audio progress
    @bindAssetProgress asset
    # play asset
    asset.play()

    ## check that the audio element is playing, if not deal with it
    # setTimeout( ->
    #   if asset.paused
    #     notify
    #       title:   "Gør dig klar!"
    #       confirm: "Spil!"
    #   else
    #     $('.alternative').prop 'disabled', false
    #     callback asset if callback?
    # , 1000)

  # binds audio element progression with progress bar
  bindAssetProgress: (asset) ->
    $(asset).bind 'timeupdate', ->
      percent = (this.currentTime * 100) / this.duration
      Session.set 'gameProgress', percent
      value = (currentGame().pointsPerQuestion * (100 - percent)) / 100

      # update progress bar width depending on audio progress
      $('#asset-bar')
        .attr('style', "width: #{100 - percent}%")
        .text Math.floor(value)

# Template.quiz.helpers
#   currentGameId: ->
#     currentGameId()

Template.question.helpers
  currentQuestion: -> currentQuiz().name

  currentQuestionNumber: -> currentGame().currentQuestion + 1

  numberOfQuestions: -> numberOfQuestions()

  alternatives: ->
    currentQuestion().alternatives

  progressBarColor: ->
    percent = Session.get 'gameProgress'
    if percent is 100
      ''
    else if percent > 66
      'percent-bar-danger'
    else if percent > 33
      'progress-bar-warning'
    else
      'progress-bar-success'


Template.question.startQuestion = ->
  # Reset progress bar
  $('#asset-bar')
    .attr('style', "width: 100%")
    .text Math.floor(currentQuiz().pointsPerQuestion)

  $('.alternative').prop 'disabled', false
  Template.assets.playAsset()

# rendered
Template.question.rendered = ->
  Template.question.startQuestion()


# events
Template.question.events
  # answer question with clicked alternative
  'click .alternative': (event) ->
    $('.alternative').prop 'disabled', true
    answerQuestion event.target.id

{'fraction_non_alphanumeric': 0.12408444635932787, 'fraction_numerical': 0.009191440471061324, 'mean_word_length': 3.084457478005865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '212105', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1977, 'n_words': 526}