game.js
function handleGame(request, response, game, path) {
    var resp;
    switch(path[0]) {
    case 'board':
        // add pieces to the board
        if (request.method === "POST") {
            requestBody(request, function(form) {

                var player = playerFromReq(request, response, game);
                // console.info('adding pieces, player:'+player.name);
                // console.info('form info:'+JSON.stringify(form));

                if (form && form.shape && form.color &&
                    form.row && form.column && player) {

                    // TODO should do form check?
                    var row = parseInt(form.row, 10);
                    var column = parseInt(form.column, 10);
                    var piece = new Piece(form.shape, form.color);

                    // check player has piece
                    var idx = -1, _idx = 0;
                    for (var p in player.pieces) {
                        var _piece = player.pieces[p];
                        //console.log('check:'+JSON.stringify(p)+', and:'+
                        //          JSON.stringify(piece));
                        if (piece.equals(_piece)) {
                            idx = _idx;
                            break;
                        }
                        _idx += 1;
                    }

                    if (idx > -1) {
                        var gp = new GamePiece(piece, row, column);
                        // console.info('adding piece:'+JSON.stringify(gp));
                        resp = addGamePiece(game, gp);
                        if (typeof resp === "string") {
                            // add gamepiece failed
                            response.writeHead(409, {'Content-Type': 'text/json'});
                            response.write(resp, 'utf-8');
                            response.end();
                            return;
                        } else {
                            // add gamepiece succeeded
                            player.points += resp;
                            player.pieces.splice(idx, 1);
                            respOk(response, '', 'text/json');
                        }
                    }
                }
            });
            return;
        }
        // get pieces on the board
        resp = JSON.stringify(game.board);
        break;
    case 'players':
        handlePlayers(request, response, game, path.slice(1));
        return;
    case 'pieces':
        // get pieces in the bag
        resp = JSON.stringify(game.pieces);
        break;
    case 'chat':
        handleChat(request, response, game.chat);
        break;
    case 'dimensions':
        resp = JSON.stringify(game.dimensions);
    }
    respOk(response, resp, 'text/json');
}
function handleGame(request, response, game, path) {
    var resp;
    switch(path[0]) {
    case 'board':
        // add pieces to the board
        if (request.method === "POST") {
            requestBody(request, function(form) {

                var player = playerFromReq(request, response, game);
                // console.info('adding pieces, player:'+player.name);
                // console.info('form info:'+JSON.stringify(form));

                if (form && form.shape && form.color &&
                    form.row && form.column && player) {

                    // TODO should do form check?
                    var row = parseInt(form.row, 10);
                    var column = parseInt(form.column, 10);
                    var piece = new Piece(form.shape, form.color);

                    // check player has piece
                    var idx = -1, _idx = 0;
                    for (var p in player.pieces) {
                        var _piece = player.pieces[p];
                        //console.log('check:'+JSON.stringify(p)+', and:'+
                        //          JSON.stringify(piece));
                        if (piece.equals(_piece)) {
                            idx = _idx;
                            break;
                        }
                        _idx += 1;
                    }

                    if (idx > -1) {
                        var gp = new GamePiece(piece, row, column);
                        // console.info('adding piece:'+JSON.stringify(gp));
                        resp = addGamePiece(game, gp);
                        if (typeof resp === "string") {
                            // add gamepiece failed
                            response.writeHead(409, {'Content-Type': 'text/json'});
                            response.write(resp, 'utf-8');
                            response.end();
                            return;
                        } else {
                            // add gamepiece succeeded
                            player.points += resp;
                            player.pieces.splice(idx, 1);
                            respOk(response, '', 'text/json');
                        }
                    }
                }
            });
            return;
        }
        // get pieces on the board
        resp = JSON.stringify(game.board);
        break;
    case 'players':
        handlePlayers(request, response, game, path.slice(1));
        return;
    case 'pieces':
        // get pieces in the bag
        resp = JSON.stringify(game.pieces);
        break;
    case 'chat':
        handleChat(request, response, game.chat);
        break;
    case 'dimensions':
        resp = JSON.stringify(game.dimensions);
    }
    respOk(response, resp, 'text/json');
}
function handleGame ( request , response , game , path ) { var resp ; switch ( path [ 0 ] ) { case 'board' : // add pieces to the board if ( request . method === "POST" ) { requestBody ( request , function ( form ) { var player = playerFromReq ( request , response , game ) ; // console.info('adding pieces, player:'+player.name); // console.info('form info:'+JSON.stringify(form)); if ( form && form . shape && form . color && form . row && form . column && player ) { // TODO should do form check? var row = parseInt ( form . row , 10 ) ; var column = parseInt ( form . column , 10 ) ; var piece = new Piece ( form . shape , form . color ) ; // check player has piece var idx = - 1 , _idx = 0 ; for ( var p in player . pieces ) { var _piece = player . pieces [ p ] ; //console.log('check:'+JSON.stringify(p)+', and:'+ //          JSON.stringify(piece)); if ( piece . equals ( _piece ) ) { idx = _idx ; break ; } _idx += 1 ; } if ( idx > - 1 ) { var gp = new GamePiece ( piece , row , column ) ; // console.info('adding piece:'+JSON.stringify(gp)); resp = addGamePiece ( game , gp ) ; if ( typeof resp === "string" ) { // add gamepiece failed response . writeHead ( 409 , { 'Content-Type' : 'text/json' } ) ; response . write ( resp , 'utf-8' ) ; response . end ( ) ; return ; } else { // add gamepiece succeeded player . points += resp ; player . pieces . splice ( idx , 1 ) ; respOk ( response , '' , 'text/json' ) ; } } } } ) ; return ; } // get pieces on the board resp = JSON . stringify ( game . board ) ; break ; case 'players' : handlePlayers ( request , response , game , path . slice ( 1 ) ) ; return ; case 'pieces' : // get pieces in the bag resp = JSON . stringify ( game . pieces ) ; break ; case 'chat' : handleChat ( request , response , game . chat ) ; break ; case 'dimensions' : resp = JSON . stringify ( game . dimensions ) ; } respOk ( response , resp , 'text/json' ) ; }
Handle a game resource transaction.
- POST to add piece to the board.
- Forward player transactions to separate function.
- GET pieces on board & in bag
- GET dimensions
Handle a game resource transaction . - POST to add piece to the board . - Forward player transactions to separate function . - GET pieces on board & in bag - GET dimensions