game.js
function handlePlayers(request, response, game, path) {
    var func, player, resp;
    if (!path.length) {
        // return info on the players collection

        if (request.method === "POST") {
            player = playerFromReq(request, response, game);
            if (player) {
                // end turn
                // TODO should this be under /players/<name>/?
                func = function (form) {
                    if (form && form.end_turn) {
                        switchPlayers(game, player);
                        respOk(response);
                    }
                };
            } else {
                // add player to a game
                func = function(form) {
                    if (form && form.name) {
                        addPlayerToGame(game, form.name);
                        var jar = new cookies(request, response);
                        jar.set("player", encodeURIComponent(form.name),
                                {httpOnly: false});
                        respOk(response, '', 'text/json');
                    }
                };
            }
            requestBody(request, func);
            return;
        } else if (request.method === 'DELETE') {
            // delete player from a game
            func = function(form) {
                if (form && form.name) {
                    player = game.players[form.name];
                    if (player === undefined) {
                        // huh? player is not in this game
                        response.writeHead(404, {'Content-Type': 'text/json'});
                        response.end();
                        return;
                    }
                    nextTurn(game, player);
                    game.returnPieces(player.pieces);
                    delete game.players[form.name];
                    if (Object.keys(game.players).length === 0) {
                        delete games[game.name];
                    }
                    respOk(response);
                }
            };
            requestBody(request, func);
            return;
        }  else {
            resp = JSON.stringify(game.players);
        }

    } else {
        // return info on a specific player
        player = game.players[path[0]];
        if (typeof player === 'undefined') {
            // player not found
            response.writeHead(404, {'Content-Type': 'text/json'});
            response.end();
            return;
        }

        if (path[1] === 'pieces') {
            resp = JSON.stringify(player.pieces);
        }
    }
    respOk(response, resp, 'text/json');
}
function handlePlayers(request, response, game, path) {
    var func, player, resp;
    if (!path.length) {
        // return info on the players collection

        if (request.method === "POST") {
            player = playerFromReq(request, response, game);
            if (player) {
                // end turn
                // TODO should this be under /players/<name>/?
                func = function (form) {
                    if (form && form.end_turn) {
                        switchPlayers(game, player);
                        respOk(response);
                    }
                };
            } else {
                // add player to a game
                func = function(form) {
                    if (form && form.name) {
                        addPlayerToGame(game, form.name);
                        var jar = new cookies(request, response);
                        jar.set("player", encodeURIComponent(form.name),
                                {httpOnly: false});
                        respOk(response, '', 'text/json');
                    }
                };
            }
            requestBody(request, func);
            return;
        } else if (request.method === 'DELETE') {
            // delete player from a game
            func = function(form) {
                if (form && form.name) {
                    player = game.players[form.name];
                    if (player === undefined) {
                        // huh? player is not in this game
                        response.writeHead(404, {'Content-Type': 'text/json'});
                        response.end();
                        return;
                    }
                    nextTurn(game, player);
                    game.returnPieces(player.pieces);
                    delete game.players[form.name];
                    if (Object.keys(game.players).length === 0) {
                        delete games[game.name];
                    }
                    respOk(response);
                }
            };
            requestBody(request, func);
            return;
        }  else {
            resp = JSON.stringify(game.players);
        }

    } else {
        // return info on a specific player
        player = game.players[path[0]];
        if (typeof player === 'undefined') {
            // player not found
            response.writeHead(404, {'Content-Type': 'text/json'});
            response.end();
            return;
        }

        if (path[1] === 'pieces') {
            resp = JSON.stringify(player.pieces);
        }
    }
    respOk(response, resp, 'text/json');
}
function handlePlayers ( request , response , game , path ) { var func , player , resp ; if ( ! path . length ) { // return info on the players collection if ( request . method === "POST" ) { player = playerFromReq ( request , response , game ) ; if ( player ) { // end turn // TODO should this be under /players/<name>/? func = function ( form ) { if ( form && form . end_turn ) { switchPlayers ( game , player ) ; respOk ( response ) ; } } ; } else { // add player to a game func = function ( form ) { if ( form && form . name ) { addPlayerToGame ( game , form . name ) ; var jar = new cookies ( request , response ) ; jar . set ( "player" , encodeURIComponent ( form . name ) , { httpOnly : false } ) ; respOk ( response , '' , 'text/json' ) ; } } ; } requestBody ( request , func ) ; return ; } else if ( request . method === 'DELETE' ) { // delete player from a game func = function ( form ) { if ( form && form . name ) { player = game . players [ form . name ] ; if ( player === undefined ) { // huh? player is not in this game response . writeHead ( 404 , { 'Content-Type' : 'text/json' } ) ; response . end ( ) ; return ; } nextTurn ( game , player ) ; game . returnPieces ( player . pieces ) ; delete game . players [ form . name ] ; if ( Object . keys ( game . players ) . length === 0 ) { delete games [ game . name ] ; } respOk ( response ) ; } } ; requestBody ( request , func ) ; return ; } else { resp = JSON . stringify ( game . players ) ; } } else { // return info on a specific player player = game . players [ path [ 0 ] ] ; if ( typeof player === 'undefined' ) { // player not found response . writeHead ( 404 , { 'Content-Type' : 'text/json' } ) ; response . end ( ) ; return ; } if ( path [ 1 ] === 'pieces' ) { resp = JSON . stringify ( player . pieces ) ; } } respOk ( response , resp , 'text/json' ) ; }
Handle a player resource transaction.
- POST to add player to the game.
- GET player pieces
Handle a player resource transaction . - POST to add player to the game . - GET player pieces