// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('../..')(server);
var port = process.env.PORT || 3000;

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(__dirname + '/public'));

// Chatroom

// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;
var lastCard = '';
var userCards = {};

// Update userCards, lastCard
function initCards() {
}

// Update userCards, lastCard
// return if false, the user wins and the game is finished.
function updateCards(username, card) {
}

// Return response string
function toStringCards(username) {
}

io.on('connection', function (socket) {
  var addedUser = false;

  // when the client emits 'new message', this listens and executes
  socket.on('new message', function (data) {
    // we tell the client to execute 'new message'
    socket.broadcast.emit('new message', {
      username: socket.username,
      message: data
    });
  });

  // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

  // when the client emits 'typing', we broadcast it to others
  socket.on('typing', function () {
    socket.broadcast.emit('typing', {
      username: socket.username
    });
  });

  // when the client emits 'stop typing', we broadcast it to others
  socket.on('stop typing', function () {
    socket.broadcast.emit('stop typing', {
      username: socket.username
    });
  });

  // when the user disconnects.. perform this
  socket.on('disconnect', function () {
    // remove the username from global usernames list
    if (addedUser) {
      delete usernames[socket.username];
      --numUsers;

      // echo globally that this client has left
      socket.broadcast.emit('user left', {
        username: socket.username,
        numUsers: numUsers
      });
    }
  });
});

@@ -438,16 +438,34 @@
 s = %7B%7D;%0A
+var sockets = %7B%7D;%0A
 var numU
@@ -474,16 +474,16 @@
 rs = 0;%0A
-
 var last
@@ -512,16 +512,37 @@
 ds = %7B%7D;
+%0Avar started = false;
 %0A%0A// Upd
@@ -784,16 +784,35 @@
 name) %7B%0A
+  return username;%0A
 %7D%0A%0Aio.on
@@ -993,41 +993,525 @@
     
-// we tell the client to execute 
+if (data === 'start') %7B%0A      if (started) %7B%0A%09socket.broadcast.send('new message', %7B%0A%09  username: 'Server',%0A          message: 'Already started'%0A%09%7D);%0A      %7D else %7B%0A%09started = true;%0A%09initCards();%0A%09socket.emit('new message', %7B%0A%09  username: 'Server',%0A          message: 'Start Game!'%0A%09%7D);%0A%09socket.broadcast.emit('new message', %7B%0A%09  username: 'Server',%0A          message: 'Start Game!'%0A%09%7D);%0A%0A%09var list = Object.keys(sockets);%0A%09for (var i = 0; i %3C list.length; i++) %7B%0A          var s = sockets%5Blist%5Bi%5D%5D;%0A          s.emit(
 'new
@@ -1519,36 +1519,184 @@
 message'
+, %7B
 %0A    
-socket.broadcas
+        username: 'Cards',%0A%09    message: toStringCards(s.username)%0A%09  %7D);%0A        %7D%0A      %7D%0A    %7D else %7B%0A      if (!updateCards(socket.username, data)) %7B%0A%09socke
 t.emit('
@@ -1715,68 +1715,458 @@
 , %7B%0A
+%09
   
-    username: socket.username,%0A      message: data%0A    %7D);
+username: 'Server',%0A          message: 'You WIN!'%0A%09%7D);%0A%09socket.broadcast.emit('new message', %7B%0A%09  username: 'Server',%0A          message: socket.username + ' WIN!'%0A%09%7D);%0A%09started = false;%0A      %7D else %7B%0A%09var list = Object.keys(sockets);%0A%09for (var i = 0; i %3C list.length; i++) %7B%0A          var s = sockets%5Blist%5Bi%5D%5D;%0A          s.emit('new message', %7B%0A            username: 'Cards',%0A%09    message: toStringCards(s.username)%0A%09  %7D);%0A        %7D%0A      %7D%0A    %7D
 %0A  %7D
@@ -2427,24 +2427,24 @@
 global list%0A
-
     username
@@ -2463,24 +2463,56 @@
 = username;%0A
+    sockets%5Busername%5D = socket;%0A
     ++numUse

add game logic