text: // Generated by CoffeeScript 1.9.2
  var ChatServer, app, chatServer, express, http, httpServer, moment, pkg, request, sockjs,
    indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  
  pkg = require('./package.json');
  
  express = require('express');
  
  http = require('http');
  
  moment = require('moment');
  
  sockjs = require('sockjs');
  
  request = require('request');
  
  ChatServer = (function() {
    function ChatServer(httpServer) {
      this.server = sockjs.createServer({
        prefix: '/chat',
        log: function(severity, message) {
          if (severity === 'error') {
            return console.log(message);
          }
        }
      });
      this.lobby = [];
      this.server.installHandlers(httpServer);
      this.server.on('connection', (function(_this) {
        return function(conn) {
          conn.writeJSON = function(data) {
            return conn.write(JSON.stringify(data));
          };
          conn.on('data', function(data) {
            return _this.constructor.emitData(conn, data);
          });
          return conn.on('auth', function(data) {
            return _this.auth(conn, data);
          });
        };
      })(this));
    }
  
    ChatServer.emitData = function(conn, data) {
      data = JSON.parse(data);
      if (data.type != null) {
        return conn.emit(data.type, data);
      }
    };
  
    ChatServer.prototype.broadcast = function(data) {
      var conn, j, len, ref, ref1, results;
      if (data.room != null) {
        ref = this.lobby;
        results = [];
        for (j = 0, len = ref.length; j < len; j++) {
          conn = ref[j];
          if (ref1 = data.room, indexOf.call(conn.userObject.rooms, ref1) >= 0) {
            results.push(conn.writeJSON(data));
          } else {
            results.push(void 0);
          }
        }
        return results;
      }
    };
  
    ChatServer.prototype.auth = function(conn, authData) {
      return request.get({
        uri: "http://127.0.0.1:8001/game/" + authData.gameID + "/chat/auth/",
        headers: {
          'User-Agent': pkg.name + "/" + pkg.version,
          Host: 'www.uchicagohvz.org',
          Cookie: "sessionid=" + authData.sessionid
        },
        json: true
      }, (function(_this) {
        return function(err, res, body) {
          if (res.statusCode === 200) {
            conn.userObject = body;
            conn.on('chat', function(data) {
              return _this.chat(conn, data);
            });
            conn.on('close', function() {
              return _this.removeConn(conn);
            });
            _this.lobby.push(conn);
            return conn.writeJSON({
              type: 'authenticated'
            });
          }
        };
      })(this));
    };
  
    ChatServer.prototype.chat = function(conn, data) {
      if (conn.userObject.rooms.length === 1) {
        data.room = conn.userObject.rooms[0];
      }
      this.broadcast(data);
      return this.log(conn, data);
    };
  
    ChatServer.prototype.updateUserRooms = function(uid, roomList) {
      var conn, i, j, len, ref, results;
      ref = this.lobby;
      results = [];
      for (i = j = 0, len = ref.length; j < len; i = ++j) {
        conn = ref[i];
        if (conn.userObject.uid === uid) {
          conn.userObject.rooms = roomList;
          results.push(conn.writeJSON({
            type: 'refresh'
          }));
        } else {
          results.push(void 0);
        }
      }
      return results;
    };
  
    ChatServer.prototype.removeConn = function(conn) {
      return this.lobby = this.lobby.filter(function(v) {
        return v.id !== conn.id;
      });
    };
  
    ChatServer.prototype.log = function(conn, data) {
      return console.log("[" + (moment().format('MM/DD/YYYY hh:mm:ss A')) + "] " + conn.userObject.name + ": " + (JSON.stringify(data)));
    };
  
    return ChatServer;
  
  })();
  
  app = express();
  
  app.use(express.json());
  
  httpServer = http.createServer(app);
  
  httpServer.listen(36452, '127.0.0.1');
  
  chatServer = new ChatServer(httpServer);
  
  app.post('/admin/updateUserRooms', function(req, res) {
    chatServer.updateUserRooms(req.body.uid, req.body.rooms);
    return res.send(200);
  });
  