<commit_before>shuffle = require 'shuffle-array'
ucfirst = require 'ucfirst'
sets = require 'simplesets'

class Game
  constructor: (data, @$page) ->
    @questionDataOrder = shuffle(data)
    @currentQuestionIndex = -1
    @score = 0
    @guess = ''
    @correctGuessIndices = []

    # Generate valid character
    @validCharacters = new sets.Set()
    for questionData in @questionDataOrder
      for char in questionData.name
        @validCharacters.add(char.toLowerCase())
    @validCharacters.remove(' ')

    # Add first and last name to data structure
    for i, questionData of @questionDataOrder
      splitName = questionData.name.split(' ')
      @questionDataOrder[i].firstName = splitName[0]
      @questionDataOrder[i].lastName = splitName[1]

    @$name = @$page.find('.person .name')
    @$page.find('.progress .total').text(@questionDataOrder.length)

  # Sets the game difficulty
  setDifficulty: (@difficulty) -> @

  # Gets the current question data
  getQuestion: ->
    @questionDataOrder[@currentQuestionIndex]

  # Loads the next person (if available)
  nextQuestion: ->
    ++@currentQuestionIndex
    if @gameIsOver()
      @gameOverCb()
    else
      questionData = @getQuestion()
      @guess = ''
      
      # Update DOM
      @$name.removeClass('correct incorrect')
      @$page.find('.person .picture').attr('src', questionData.image)
      @$page.find('.person .title').text(questionData.title)
      @updateGuess()

  # ## Guessing

  # Returns true if the character is a valid guess
  isValidChar: (char) ->
    return @validCharacters.has(char)

  # Adds a character to the guess
  addChar: (char) ->
    @guess += char
    @guess = @guess.substring(0, @getQuestion().firstName.length)
    @checkGuess()

  # Removes a character from the guess
  removeChar: ->
    @guess = @guess.substring(0, @guess.length - 1)
    @checkGuess()

  # Checks if a guess is correct
  checkGuess: ->
    @updateGuess()
    if @guessIsCorrect()
      @correctGuessIndices.push @currentQuestionIndex
      # Make name flash green
      @$name.addClass('correct')
      # Delay next question
      setTimeout =>
        @$page.find('.progress .current').text(++@score)
        @nextQuestion()
      , 500
    else # incorrect
      if @guess.length == @getQuestion().firstName.length
        @$name.addClass('incorrect')
      else
        @$name.removeClass('incorrect')

  # Updates the guess state
  updateGuess: ->
    firstName = @getQuestion().firstName
    remainingChars = firstName.length - @guess.length
    starChars = ''
    for i in [0...remainingChars]
      starChars += '*'
    displayName = ucfirst(@guess) + starChars
    @$name.text(displayName)

  # Returns true if the current guess is correct
  guessIsCorrect: ->
    questionData = @getQuestion()<commit_msg>Made incorrect guess go to next question<commit_after>shuffle = require 'shuffle-array'
ucfirst = require 'ucfirst'
sets = require 'simplesets'

delayBetweenQuestions = 500

class Game
  constructor: (data, @$page) ->
    @questionDataOrder = shuffle(data)
    @currentQuestionIndex = -1
    @score = 0
    @guess = ''
    @correctGuessIndices = []

    # Generate valid character
    @validCharacters = new sets.Set()
    for questionData in @questionDataOrder
      for char in questionData.name
        @validCharacters.add(char.toLowerCase())
    @validCharacters.remove(' ')

    # Add first and last name to data structure
    for i, questionData of @questionDataOrder
      splitName = questionData.name.split(' ')
      @questionDataOrder[i].firstName = splitName[0]
      @questionDataOrder[i].lastName = splitName[1]

    @$name = @$page.find('.person .name')
    @$page.find('.progress .total').text(@questionDataOrder.length)

  # Sets the game difficulty
  setDifficulty: (@difficulty) -> @

  # Gets the current question data
  getQuestion: ->
    @questionDataOrder[@currentQuestionIndex]

  # Loads the next person (if available)
  nextQuestion: ->
    ++@currentQuestionIndex
    if @gameIsOver()
      @gameOverCb()
    else
      questionData = @getQuestion()
      @guess = ''
      
      # Update DOM
      @$name.removeClass('correct incorrect')
      @$page.find('.person .picture').attr('src', questionData.image)
      @$page.find('.person .title').text(questionData.title)
      @updateGuess()

  # ## Guessing

  # Returns true if the character is a valid guess
  isValidChar: (char) ->
    return @validCharacters.has(char)

  # Adds a character to the guess
  addChar: (char) ->
    @guess += char
    @guess = @guess.substring(0, @getQuestion().firstName.length)
    @checkGuess()

  # Removes a character from the guess
  removeChar: ->
    @guess = @guess.substring(0, @guess.length - 1)
    @checkGuess()

  # Checks if a guess is correct
  checkGuess: ->
    if !@transitioning
      @updateGuess()
      if @guessIsCorrect()
        @correctGuessIndices.push @currentQuestionIndex
        # Make name flash green
        @$name.addClass('correct')
        @transitioning = true
        # Delay next question
        setTimeout =>
          @transitioning = false
          @$page.find('.progress .current').text(++@score)
          @nextQuestion()
        , delayBetweenQuestions
      else # incorrect
        firstName = @getQuestion().firstName
        if @guess.length == firstName.length
          @$name.addClass('incorrect')
          @transitioning = true
          @$name.text(firstName)
          setTimeout =>
            @transitioning = false
            @nextQuestion()
          , delayBetweenQuestions * 2

  # Updates the guess state
  updateGuess: ->
    firstName = @getQuestion().firstName
    remainingChars = firstName.length - @guess.length
    starChars = ''
    for i in [0...remainingChars]
      starChars += '*'
    displayName = ucfirst(@guess) + starChars
    @$name.text(displayName)

  # Returns true if the current guess is correct
  guessIsCorrect: ->
    questionData = @getQuestion()
{'fraction_non_alphanumeric': 0.12174643157010916, 'fraction_numerical': 0.0038623005877413937, 'mean_word_length': 2.9030144167758847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '4302376', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1769, 'n_words': 430}