<commit_before>express = require('express')
app = express()
server = require('http').createServer(app)
io = require('socket.io')(server)
stylus = require('stylus')
config = require('./config')
mongoskin = require('mongoskin')
MongoStore = require('connect-mongo')(express)
crypto = require('crypto')
bcrypt = require('bcrypt')
passport = require('passport')
localStrategy = require('passport-local').Strategy
jwt = require('jsonwebtoken')
gameEngine = require('./game')

# MongoDB connection
mongoUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME
mongoPassword = process.env.OPENSHIFT_MONGODB_DB_PASSWORD
login = if process.env.OPENSHIFT_MONGODB_DB_PASSWORD then "#{mongoUser}:#{mongoPassword}@" else ""
mongoHost = process.env.OPENSHIFT_MONGODB_DB_HOST || '127.0.0.1'
mongoPort = process.env.OPENSHIFT_MONGODB_DB_PORT || '27017'
appName = process.env.OPENSHIFT_APP_NAME || 'netrunner'
mongoUrl = "mongodb://#{login}#{mongoHost}:#{mongoPort}/#{appName}"
db = mongoskin.db(mongoUrl)

# Game lobby
gameid = 0
games = []

swapSide = (side) ->
  if side is "Corp" then "Runner" else "Corp"

removePlayer = (socket, username) ->
  for game, i in games
    for player, j in game.players
      if player.user.username is username
        game.players.splice(j, 1)
        socket.to(game.gameid).emit('netrunner', {type: "say", user: "__system__", text: "#{username} left the game."})
        break
    if game.players.length is 0
      games.splice(i, 1)
      break

# Socket.io
io.use (socket, next) ->
  if socket.handshake.query.token
    jwt.verify socket.handshake.query.token, config.salt, (err, user) ->
      socket.request.user = user unless err
  next()

chat = io.of('/chat').on 'connection', (socket) ->
  socket.on 'netrunner', (msg) ->
    msg.date = new Date()
    chat.emit('netrunner', msg)
    db.collection('messages').insert msg, (err, result) ->

lobby = io.of('/lobby').on 'connection', (socket) ->
  lobby.emit('netrunner', {type: "games", games: games})

  socket.on 'disconnect', () ->
    removePlayer(socket, socket.request.user.username) if socket.request.user
    lobby.emit('netrunner', {type: "games", games: games})

  socket.on 'netrunner', (msg) ->
    switch msg.action
      when "create"
        game = {date: new Date(), gameid: ++gameid, title: msg.title,\
                players: [{user: socket.request.user, side: "Corp"}]}
        games.push(game)
        socket.join(gameid)
        socket.emit("netrunner", {type: "game", gameid: gameid})
        lobby.emit('netrunner', {type: "games", games: games})
<commit_msg>Changed default side for game creator<commit_after>express = require('express')
app = express()
server = require('http').createServer(app)
io = require('socket.io')(server)
stylus = require('stylus')
config = require('./config')
mongoskin = require('mongoskin')
MongoStore = require('connect-mongo')(express)
crypto = require('crypto')
bcrypt = require('bcrypt')
passport = require('passport')
localStrategy = require('passport-local').Strategy
jwt = require('jsonwebtoken')
gameEngine = require('./game')

# MongoDB connection
mongoUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME
mongoPassword = process.env.OPENSHIFT_MONGODB_DB_PASSWORD
login = if process.env.OPENSHIFT_MONGODB_DB_PASSWORD then "#{mongoUser}:#{mongoPassword}@" else ""
mongoHost = process.env.OPENSHIFT_MONGODB_DB_HOST || '127.0.0.1'
mongoPort = process.env.OPENSHIFT_MONGODB_DB_PORT || '27017'
appName = process.env.OPENSHIFT_APP_NAME || 'netrunner'
mongoUrl = "mongodb://#{login}#{mongoHost}:#{mongoPort}/#{appName}"
db = mongoskin.db(mongoUrl)

# Game lobby
gameid = 0
games = []

swapSide = (side) ->
  if side is "Corp" then "Runner" else "Corp"

removePlayer = (socket, username) ->
  for game, i in games
    for player, j in game.players
      if player.user.username is username
        game.players.splice(j, 1)
        socket.to(game.gameid).emit('netrunner', {type: "say", user: "__system__", text: "#{username} left the game."})
        break
    if game.players.length is 0
      games.splice(i, 1)
      break

# Socket.io
io.use (socket, next) ->
  if socket.handshake.query.token
    jwt.verify socket.handshake.query.token, config.salt, (err, user) ->
      socket.request.user = user unless err
  next()

chat = io.of('/chat').on 'connection', (socket) ->
  socket.on 'netrunner', (msg) ->
    msg.date = new Date()
    chat.emit('netrunner', msg)
    db.collection('messages').insert msg, (err, result) ->

lobby = io.of('/lobby').on 'connection', (socket) ->
  lobby.emit('netrunner', {type: "games", games: games})

  socket.on 'disconnect', () ->
    removePlayer(socket, socket.request.user.username) if socket.request.user
    lobby.emit('netrunner', {type: "games", games: games})

  socket.on 'netrunner', (msg) ->
    switch msg.action
      when "create"
        game = {date: new Date(), gameid: ++gameid, title: msg.title,\
                players: [{user: socket.request.user, side: "Runner"}]}
        games.push(game)
        socket.join(gameid)
        socket.emit("netrunner", {type: "game", gameid: gameid})
        lobby.emit('netrunner', {type: "games", games: games})

{'fraction_non_alphanumeric': 0.16413255360623782, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 4.090277777777778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1012459', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1725, 'n_words': 383}