Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: squaremo/ponk
base: 763db2d368
...
head fork: squaremo/ponk
compare: 2a1eef3aa2
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 50 additions and 3 deletions.
  1. +49 −2 app.js
  2. +1 −1  public/index.html
View
51 app.js
@@ -1,11 +1,58 @@
var express = require('express'),
-server = express.createServer();
+sockjs = require('sockjs');
+
+function debug(msg) {
+ console.log(msg);
+}
+
+var server = express.createServer();
server.configure(
function() {
server.use(express.static(__dirname + '/public'))
});
-// Fight!
+var socks = sockjs.createServer();
+socks.installHandlers(server, {prefix:'/socks'});
+
+socks.on('connection', function(conn) {
+ initPlayer(conn);
+});
+
+function initPlayer(connection) {
+ connection.on('data', function(msg) {
+ connection.removeAllListeners('data');
+ var player = new Player(connection, msg);
+ debug("Welcome player " + player.info.name);
+ makeMatches(player);
+ });
+}
+function Player(conn, name) {
+ this.info = {name: name, wins: 0};
+ this.connection = conn;
+}
+
+var queue = [];
+function makeMatches(player) {
+ queue.push(player);
+ attemptMatches();
+}
+
+function attemptMatches() {
+ if (queue.length > 1) {
+ var player1 = queue.shift();
+ var player2 = queue.shift();
+ gameOn(player1, player2);
+ attemptMatches();
+ }
+}
+
+function gameOn(player1, player2) {
+ debug("Matching " + player1.info.name + " with " + player2.info.name);
+ player1.connection.write(JSON.stringify({start: player2.info}));
+ player2.connection.write(JSON.stringify({start: player1.info}));
+}
+
+// Fight!
server.listen(process.env['VCAP_APP_PORT'] || 3000);
View
2  public/index.html
@@ -1,4 +1,3 @@
-
<!DOCTYPE html>
<html lang="en">
<head>
@@ -74,6 +73,7 @@
<script src="js/bootstrap-collapse.js"></script>
<script src="js/bootstrap-carousel.js"></script>
<script src="js/bootstrap-typeahead.js"></script>
+ <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
</body>
</html>

No commit comments for this range

Something went wrong with that request. Please try again.