<commit_before>const wiki = require('./wiki')
const { EventEmitter } = require('events')
const debug = require('debug')('WikiBattle:game')

let $id = 0
const HINT_TIMEOUT = 40 // seconds
const BACKLINKS_TIMEOUT = 90 // seconds

module.exports = WikiBattle

function WikiBattle (io, origin, goal) {
  if (!(this instanceof WikiBattle)) return new WikiBattle(io, origin, goal)
  EventEmitter.call(this)
  $id %= 2e15 // should be enough for anyone!
  this.id = `game:${++$id}`
  this.players = []
  this.origin = origin
  this.goal = goal
  this.io = io
}

WikiBattle.prototype.emitSocket = function (...args) {
  return this.players.forEach(player => {
    const sock = player.sock
    if (sock) {
      sock.emit(...args)
    }
  })
}

WikiBattle.prototype.connect = function (connectingPlayer) {
  debug('connect player', connectingPlayer.id)
  this.players.forEach(player => {
    player.notifyConnect(connectingPlayer)
    connectingPlayer.notifyConnect(player)
  })
  this.players.push(connectingPlayer)
  return this
}

WikiBattle.prototype.disconnect = function (disconnectingPlayer) {
  debug('disconnect player', disconnectingPlayer.id)
  disconnectingPlayer.disconnect()
  this.navigate(disconnectingPlayer, null)

  var connected = this.players.filter(p => p.connected)
  connected.forEach(p => {
    p.notifyDisconnect(disconnectingPlayer)
  })
  if (connected.length === 1) {
    connected[0].win()
    this.end()
  }
}

WikiBattle.prototype.checkWin = function () {
  const hasWon = p => p.current() === this.goal
  if (this.players.some(hasWon)) {
    this.players.forEach(p => {
      hasWon(p) ? p.win() : p.lose()
    })
    this.end()
  }
}

WikiBattle.prototype.navigateInner = function (player, to) {
  player.navigateTo(to)
  this.emitSocket('navigated', player.id, to)
  this.checkWin()
}

WikiBattle.prototype.navigate = function (player, to) {
  debug('navigate (maybe)', player.id, `${player.current()} -> ${to}`)
  if (!player.current()) {
    return this.navigateInner(player, to)
  }
  wiki.get(player.current(), (e, page) => {<commit_msg>do not check linksTo() when navigating to null after disconnect<commit_after>const wiki = require('./wiki')
const { EventEmitter } = require('events')
const debug = require('debug')('WikiBattle:game')

let $id = 0
const HINT_TIMEOUT = 40 // seconds
const BACKLINKS_TIMEOUT = 90 // seconds

module.exports = WikiBattle

function WikiBattle (io, origin, goal) {
  if (!(this instanceof WikiBattle)) return new WikiBattle(io, origin, goal)
  EventEmitter.call(this)
  $id %= 2e15 // should be enough for anyone!
  this.id = `game:${++$id}`
  this.players = []
  this.origin = origin
  this.goal = goal
  this.io = io
}

WikiBattle.prototype.emitSocket = function (...args) {
  return this.players.forEach(player => {
    const sock = player.sock
    if (sock) {
      sock.emit(...args)
    }
  })
}

WikiBattle.prototype.connect = function (connectingPlayer) {
  debug('connect player', connectingPlayer.id)
  this.players.forEach(player => {
    player.notifyConnect(connectingPlayer)
    connectingPlayer.notifyConnect(player)
  })
  this.players.push(connectingPlayer)
  return this
}

WikiBattle.prototype.disconnect = function (disconnectingPlayer) {
  debug('disconnect player', disconnectingPlayer.id)
  disconnectingPlayer.disconnect()
  this.navigate(disconnectingPlayer, null)

  var connected = this.players.filter(p => p.connected)
  connected.forEach(p => {
    p.notifyDisconnect(disconnectingPlayer)
  })
  if (connected.length === 1) {
    connected[0].win()
    this.end()
  }
}

WikiBattle.prototype.checkWin = function () {
  const hasWon = p => p.current() === this.goal
  if (this.players.some(hasWon)) {
    this.players.forEach(p => {
      hasWon(p) ? p.win() : p.lose()
    })
    this.end()
  }
}

WikiBattle.prototype.navigateInner = function (player, to) {
  player.navigateTo(to)
  this.emitSocket('navigated', player.id, to)
  this.checkWin()
}

WikiBattle.prototype.navigate = function (player, to) {
  debug('navigate (maybe)', player.id, `${player.current()} -> ${to}`)
  if (to === null || !player.current()) {
    return this.navigateInner(player, to)
  }
  wiki.get(player.current(), (e, page) => {
{'fraction_non_alphanumeric': 0.1531316980233389, 'fraction_numerical': 0.004763038818766373, 'mean_word_length': 3.952830188679245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '5958397', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1424, 'n_words': 283}