<commit_before>var _ = require('lodash')
var sprintf = require('sprintf-js').sprintf
var EventEmitter = require('eventemitter2').EventEmitter2
var State = require('./state.js')
var formatEvent = require('./util/events.js').formatEvent

module.exports = function (id, level, opts) {
  opts = opts || {size: 700}

  function load (level) {
    var tmp
    var maps = []
    level.maps.forEach(function (map) {
      map.start.forEach(function (start) {
        tmp = _.cloneDeep(map)
        tmp.start = [start]
        maps.push(tmp)
      })
    })
    var config = _.cloneDeep(level.config)
    config.stages = maps.length
    config.energy = 2400
    return {maps: maps, config: config}
  }

  level = load(level)

  var container = document.getElementById(id)

  var main = require('./ui/main.js')(container, opts)
  var message = require('./ui/message.js')(container)
  var energy = require('./ui/energy.js')(container)
  var score = require('./ui/score.js')(container)
  var stages = require('./ui/stages.js')(container)

  var state = new State(level.config)

  var events = new EventEmitter()

  var game = require('./game.js')(main.canvas, level.maps[0])

  game.events.on(['player', 'collect'], function (event) {
    state.energy.current += 50
    state.score.current += 10
    energy.update(state.energy)
    score.update(state.score)
  })

  game.events.on(['player', 'exit'], function (event) {
    state.energy.current -= 600
    energy.update(state.energy)
  })

  game.events.on(['player', 'enter'], function (event) {
    if (_.isEqual(event.tile, level.maps[state.stages.current].target)) {
      completed()
    } else {
      failed()
    }
  })

  game.events.onAny(function (event) {
    events.emit(this.event, event)
  })

  function endgame (text) {
    setTimeout(function () {
      message.show(text + ' \n SCORE ' + sprintf('%05d', state.score.current))
    }, 500)
  }

  function failed () {
    if (state.energy.current <= 0) {
      energy.ghost()
      events.emit(['level', 'failed'], formatEvent({ level: level.config.name }))
      main.hide()
      endgame('GAME OVER')
    }
  }

  function completed () {
    events.emit(['map', 'completed'], formatEvent({ map: state.stages.current }))
    state.score.current += 2000
    score.update(state.score, {magnitude: 0.5, duration: 200})
    energy.update(state.energy)
    game.flash()

    setTimeout(function () {
      if (state.energy.current > 0) {
        var remaining = state.energy.current
        var counter = setInterval(function () {
          if (remaining <= 0) {
            clearInterval(counter)
          } else {
            state.score.current += Math.min(300, remaining) * 3
            remaining -= Math.min(300, remaining)
            energy.blink()
            console.log('blink')
            score.update(state.score, {magnitude: 0.25, duration: 250})
          }
        }, 150)
      }

      if (state.stages.current === state.stages.total - 1) {
        events.emit(['level', 'completed'], formatEvent({ level: level.config.name }))
        setTimeout(function () {
          main.hide()
          endgame('COMPLETE!')
        }, 700)
      } else {
        state.stages.current += 1
        setTimeout(function () {
          main.hide()
          setTimeout(function () {
            game.reload(level.maps[state.stages.current])
            stages.update(state.stages)
            state.energy.current += 900
            energy.update(state.energy)
            main.show()
          }, 400)
        }, 700)
      }<commit_msg>Remove log<commit_after>var _ = require('lodash')
var sprintf = require('sprintf-js').sprintf
var EventEmitter = require('eventemitter2').EventEmitter2
var State = require('./state.js')
var formatEvent = require('./util/events.js').formatEvent

module.exports = function (id, level, opts) {
  opts = opts || {size: 700}

  function load (level) {
    var tmp
    var maps = []
    level.maps.forEach(function (map) {
      map.start.forEach(function (start) {
        tmp = _.cloneDeep(map)
        tmp.start = [start]
        maps.push(tmp)
      })
    })
    var config = _.cloneDeep(level.config)
    config.stages = maps.length
    config.energy = 2400
    return {maps: maps, config: config}
  }

  level = load(level)

  var container = document.getElementById(id)

  var main = require('./ui/main.js')(container, opts)
  var message = require('./ui/message.js')(container)
  var energy = require('./ui/energy.js')(container)
  var score = require('./ui/score.js')(container)
  var stages = require('./ui/stages.js')(container)

  var state = new State(level.config)

  var events = new EventEmitter()

  var game = require('./game.js')(main.canvas, level.maps[0])

  game.events.on(['player', 'collect'], function (event) {
    state.energy.current += 50
    state.score.current += 10
    energy.update(state.energy)
    score.update(state.score)
  })

  game.events.on(['player', 'exit'], function (event) {
    state.energy.current -= 600
    energy.update(state.energy)
  })

  game.events.on(['player', 'enter'], function (event) {
    if (_.isEqual(event.tile, level.maps[state.stages.current].target)) {
      completed()
    } else {
      failed()
    }
  })

  game.events.onAny(function (event) {
    events.emit(this.event, event)
  })

  function endgame (text) {
    setTimeout(function () {
      message.show(text + ' \n SCORE ' + sprintf('%05d', state.score.current))
    }, 500)
  }

  function failed () {
    if (state.energy.current <= 0) {
      energy.ghost()
      events.emit(['level', 'failed'], formatEvent({ level: level.config.name }))
      main.hide()
      endgame('GAME OVER')
    }
  }

  function completed () {
    events.emit(['map', 'completed'], formatEvent({ map: state.stages.current }))
    state.score.current += 2000
    score.update(state.score, {magnitude: 0.5, duration: 200})
    energy.update(state.energy)
    game.flash()

    setTimeout(function () {
      if (state.energy.current > 0) {
        var remaining = state.energy.current
        var counter = setInterval(function () {
          if (remaining <= 0) {
            clearInterval(counter)
          } else {
            state.score.current += Math.min(300, remaining) * 3
            remaining -= Math.min(300, remaining)
            energy.blink()
            score.update(state.score, {magnitude: 0.25, duration: 250})
          }
        }, 150)
      }

      if (state.stages.current === state.stages.total - 1) {
        events.emit(['level', 'completed'], formatEvent({ level: level.config.name }))
        setTimeout(function () {
          main.hide()
          endgame('COMPLETE!')
        }, 700)
      } else {
        state.stages.current += 1
        setTimeout(function () {
          main.hide()
          setTimeout(function () {
            game.reload(level.maps[state.stages.current])
            stages.update(state.stages)
            state.energy.current += 900
            energy.update(state.energy)
            main.show()
          }, 400)
        }, 700)
      }
{'fraction_non_alphanumeric': 0.15778281825914375, 'fraction_numerical': 0.01814573291749362, 'mean_word_length': 2.4549461312438785, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1573502', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2147, 'n_words': 424}