<commit_before>'use strict'

var ecstatic = require('ecstatic')
var events = require('events')
var http = require('http')
var url = require('url')
var ws = require('ws')

var statics = ecstatic({ root: __dirname })
var server = http.createServer((req, res) => {
  var path = url.parse(req.url).pathname
  if (path === '/' || path.startsWith('/js') || path.startsWith('/img'))
    return statics(req, res)
})

var wss = new ws.Server({ server: server })

var MAX_PER_ROOM = 20

function room() {
  var roomEvents = new events.EventEmitter
  return {
    count: 0,
    add: function addToRoom(ws) {
      this.count++
      const onMove = (moveData) => { ws.send(moveData, () => {}) }
      const onMessage = (message) => { roomEvents.emit('move', message) }
      ws.on('message', onMessage)
      roomEvents.on('move', onMove)
      ws.on('error', (e) => { console.error(e) })
      ws.on('close', () => {
        this.count--
        ws.removeListener('message', onMessage)
        roomEvents.removeListener('move', onMove)
      })
    }
  }
}

var rooms = (function push(arr, c) {
  if (!--c) { return [] }
  return arr.concat([room()])
})([], 100)

wss.on('connection', ws => {
  for (var i = 0; i < rooms.length; i++) {
    if (rooms[i].count < MAX_PER_ROOM) {
      rooms[i].add(ws)
      return
    }
  }
})
<commit_msg>fix player count per room<commit_after>'use strict'

var ecstatic = require('ecstatic')
var events = require('events')
var http = require('http')
var url = require('url')
var ws = require('ws')

var statics = ecstatic({ root: __dirname })
var server = http.createServer((req, res) => {
  var path = url.parse(req.url).pathname
  if (path === '/' || path.startsWith('/js') || path.startsWith('/img'))
    return statics(req, res)
})

var wss = new ws.Server({ server: server })

var MAX_PER_ROOM = 20

function room() {
  var roomEvents = new events.EventEmitter
  return {
    count: 0,
    add: function addToRoom(ws) {
      this.count++
      const onMove = (moveData) => { ws.send(moveData, () => {}) }
      const onMessage = (message) => { roomEvents.emit('move', message) }
      ws.on('message', onMessage)
      roomEvents.on('move', onMove)
      ws.on('error', (e) => { console.error(e) })
      ws.on('close', () => {
        this.count--
        ws.removeListener('message', onMessage)
        roomEvents.removeListener('move', onMove)
      })
    }
  }
}

var rooms = (function push(arr, c) {
  if (!c) { return [] }
  return arr.concat([room()]).concat(push(arr, --c))
})([], 100)

wss.on('connection', ws => {
  for (var i = 0; i < rooms.length; i++) {
    if (rooms[i].count < MAX_PER_ROOM) {
      rooms[i].add(ws)
      return
    }
  }
})

{'fraction_non_alphanumeric': 0.19054707852623745, 'fraction_numerical': 0.005210271678451805, 'mean_word_length': 3.042105263157895, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '1473471', 'n_tokens_mistral': 955, 'n_tokens_neox': 932, 'n_words': 215}