game.js
function handleGames(request, response, path) {
    var resp;
    if (!path.length) {
        if (request.method === "POST") {
            // add a new game object
            requestBody(request, function(form) {
                var gamenm = form.name;
                while (games[gamenm]) {
                    // game already exists, randomize a new one
                    gamenm = gamenm+Math.floor(Math.random()*10);
                }
                var game = new Game(gamenm);
                var jar = new cookies(request, response);
                var p = decodeURIComponent(jar.get('player'));
                games[gamenm] = game;
                addPlayerToGame(game, p);
                // respond with the game name, in case we randomized a new one
                respOk(response, JSON.stringify({name: gamenm}), 'text/json');
            });
        } else {
            // return info on the games collection
            resp = JSON.stringify(games);
            respOk(response, resp, 'text/json');
        }
    } else {
        // return info on a specifc game
        var game = games[path.shift()];
        if (game === undefined) {
            response.writeHead(404, {'Content-Type': 'text/json'});
            response.write("No such game exists", 'utf-8');
            response.end();
            return;
        }
        handleGame(request, response, game, path);
    }
}
function handleGames(request, response, path) {
    var resp;
    if (!path.length) {
        if (request.method === "POST") {
            // add a new game object
            requestBody(request, function(form) {
                var gamenm = form.name;
                while (games[gamenm]) {
                    // game already exists, randomize a new one
                    gamenm = gamenm+Math.floor(Math.random()*10);
                }
                var game = new Game(gamenm);
                var jar = new cookies(request, response);
                var p = decodeURIComponent(jar.get('player'));
                games[gamenm] = game;
                addPlayerToGame(game, p);
                // respond with the game name, in case we randomized a new one
                respOk(response, JSON.stringify({name: gamenm}), 'text/json');
            });
        } else {
            // return info on the games collection
            resp = JSON.stringify(games);
            respOk(response, resp, 'text/json');
        }
    } else {
        // return info on a specifc game
        var game = games[path.shift()];
        if (game === undefined) {
            response.writeHead(404, {'Content-Type': 'text/json'});
            response.write("No such game exists", 'utf-8');
            response.end();
            return;
        }
        handleGame(request, response, game, path);
    }
}
function handleGames ( request , response , path ) { var resp ; if ( ! path . length ) { if ( request . method === "POST" ) { // add a new game object requestBody ( request , function ( form ) { var gamenm = form . name ; while ( games [ gamenm ] ) { // game already exists, randomize a new one gamenm = gamenm + Math . floor ( Math . random ( ) * 10 ) ; } var game = new Game ( gamenm ) ; var jar = new cookies ( request , response ) ; var p = decodeURIComponent ( jar . get ( 'player' ) ) ; games [ gamenm ] = game ; addPlayerToGame ( game , p ) ; // respond with the game name, in case we randomized a new one respOk ( response , JSON . stringify ( { name : gamenm } ) , 'text/json' ) ; } ) ; } else { // return info on the games collection resp = JSON . stringify ( games ) ; respOk ( response , resp , 'text/json' ) ; } } else { // return info on a specifc game var game = games [ path . shift ( ) ] ; if ( game === undefined ) { response . writeHead ( 404 , { 'Content-Type' : 'text/json' } ) ; response . write ( "No such game exists" , 'utf-8' ) ; response . end ( ) ; return ; } handleGame ( request , response , game , path ) ; } }
Handle transaction on game collection resource.
Handle transaction on game collection resource .