commands/start.js
/**
     * @brief This function handles the /ttt 'start' command. If the user being
     *        challenged is a user in the current channel, then a new game can
     *        be started between the two players. The player making the first
     *        move is picked at random and the game state is updated in the
     *        database. If a game is already going on the channel, then an error
     *        is returned to the user.
     *
     * @param req express http request object
     * @param res express http response object
     *
     * @return none
     */
@brief This function handles the /ttt 'start' command. If the user being
challenged is a user in the current channel, then a new game can
be started between the two players. The player making the first
move is picked at random and the game state is updated in the
database. If a game is already going on the channel, then an error
is returned to the user.

@param req express http request object
@param res express http response object

@return none
@brief This function handles the / ttt ' start ' command . If the user being challenged is a user in the current channel then a new game can be started between the two players . The player making the first move is picked at random and the game state is updated in the database . If a game is already going on the channel then an error is returned to the user . @param req express http request object @param res express http response object @return none
function handle(req, res) {
        let player1 = {
            id: req.body.user_id,
            name: req.body.user_name
        };
        let player2 = {
            name: req.body.tttUserName
        };
        let channelId = req.body.channel_id;
        let channelName = req.body.channel_name;
        let teamId = req.body.team_id;
        slackUtil.isValidUser(player2.name, channelId, function (error, userId) {
            logger.error(error);
            if (error !== undefined) {
                if (error.ok !== true) {
                    return res.json(
                        slackUtil.createResponse({
                            text: labels.CHANNEL_ERROR,
                            response_type: "ephemeral",
                            attachments: [{
                                text: labels.CHANNEL_DETAIL_ERROR,
                                color: "#ff0000",
                                mrkdwn_in: [
                                    "text"
                                ]
                            }]
                        })
                    );
                }
                return res.json(
                    slackUtil.createResponse({
                        text: labels.DB_ERROR,
                        response_type: "ephemeral"
                    })
                );
            }
            if (userId === undefined) {
                return res.json(
                    slackUtil.createResponse({
                        text: labels.USER_ERROR,
                        response_type: "ephemeral",
                        attachments: [{
                            text: util.format(
                                labels.USER_DETAIL_ERROR,
                                player2.name,
                                channelId,
                                channelName
                            ),
                            color: "#ff0000",
                            mrkdwn_in: [
                                "text"
                            ]
                        }]
                    })
                );
            }
            player2.id = userId;
            function dbLookupCallback(error, game) {
                if (error !== undefined && error !== null) {
                    logger.error(error);
                    return res.json(
                        slackUtil.createResponse({
                            text: labels.DB_ERROR,
                            response_type: "ephemeral"
                        })
                    );
                }
                if (game !== undefined && game !== null) {
                    logger.info(game, "Game already started in this channel!");
                    return res.json(
                        slackUtil.createResponse({
                            text: labels.USER_ERROR,
                            response_type: "ephemeral",
                            attachments: [{
                                text: labels.GAME_START_ERROR,
                                color: "#ff0000",
                                mrkdwn_in: [
                                    "text"
                                ]
                            }]
                        })
                    );
                }
                if (startMap[channelId] === undefined) {
                    startMap[channelId] = player1;
                } else {
                    return res.json(
                        slackUtil.createResponse({
                            text: labels.USER_ERROR,
                            response_type: "ephemeral",
                            attachments: [{
                                text: labels.GAME_SYNC_ERROR,
                                color: "#ff0000",
                                mrkdwn_in: [
                                    "text"
                                ]
                            }]
                        })
                    );
                }
                game = new Game();
                let index = getRandomInt(1, 2);
                if (index === 1) {
                    updateGame(game, {
                        teamId,
                        channelId,
                        startPlayer: player1,
                        player1,
                        player2
                    });
                } else {
                    updateGame(game, {
                        teamId,
                        channelId,
                        startPlayer: player1,
                        player1: player2,
                        player2: player1
                    });
                }
                db.insert(game, function (saveError) {
                    startMap[channelId] = undefined;
                    if (saveError) {
                        return res.send(saveError);
                    }
                    res.json(
                        slackUtil.createResponse({
                            text: labels.CMD_TEXT,
                            attachments: [{
                                text: util.format(
                                    labels.CMD_DETAIL_TEXT,
                                    slackUtil.getUserDisplay(player1),
                                    slackUtil.getUserDisplay(player2)
                                ) + "\n"
                                        + "`X` -> "
                                        + slackUtil.getUserDisplay(
                                    game.players[0]
                                ) + "\n"
                                        + "`O` -> "
                                        + slackUtil.getUserDisplay(
                                    game.players[1]
                                ) + "\n"
                                        + "First move by: "
                                        + slackUtil.getUserDisplay(
                                    game.players[0]
                                ) + "\n"
                                        + drawer.draw(game.board),
                                color: "#36a64f",
                                mrkdwn_in: [
                                    "text"
                                ]
                            }]
                        })
                    );
                });
            }
            db.lookup(
                Game,
                {
                    teamId,
                    channelId,
                    state: constants.gameState.STARTED
                },
                dbLookupCallback
            );
        });
    }
function handle ( req , res ) { let player1 = { id : req . body . user_id , name : req . body . user_name } ; let player2 = { name : req . body . tttUserName } ; let channelId = req . body . channel_id ; let channelName = req . body . channel_name ; let teamId = req . body . team_id ; slackUtil . isValidUser ( player2 . name , channelId , function ( error , userId ) { logger . error ( error ) ; if ( error !== undefined ) { if ( error . ok !== true ) { return res . json ( slackUtil . createResponse ( { text : labels . CHANNEL_ERROR , response_type : "ephemeral" , attachments : [ { text : labels . CHANNEL_DETAIL_ERROR , color : "#ff0000" , mrkdwn_in : [ "text" ] } ] } ) ) ; } return res . json ( slackUtil . createResponse ( { text : labels . DB_ERROR , response_type : "ephemeral" } ) ) ; } if ( userId === undefined ) { return res . json ( slackUtil . createResponse ( { text : labels . USER_ERROR , response_type : "ephemeral" , attachments : [ { text : util . format ( labels . USER_DETAIL_ERROR , player2 . name , channelId , channelName ) , color : "#ff0000" , mrkdwn_in : [ "text" ] } ] } ) ) ; } player2 . id = userId ; function dbLookupCallback ( error , game ) { if ( error !== undefined && error !== null ) { logger . error ( error ) ; return res . json ( slackUtil . createResponse ( { text : labels . DB_ERROR , response_type : "ephemeral" } ) ) ; } if ( game !== undefined && game !== null ) { logger . info ( game , "Game already started in this channel!" ) ; return res . json ( slackUtil . createResponse ( { text : labels . USER_ERROR , response_type : "ephemeral" , attachments : [ { text : labels . GAME_START_ERROR , color : "#ff0000" , mrkdwn_in : [ "text" ] } ] } ) ) ; } if ( startMap [ channelId ] === undefined ) { startMap [ channelId ] = player1 ; } else { return res . json ( slackUtil . createResponse ( { text : labels . USER_ERROR , response_type : "ephemeral" , attachments : [ { text : labels . GAME_SYNC_ERROR , color : "#ff0000" , mrkdwn_in : [ "text" ] } ] } ) ) ; } game = new Game ( ) ; let index = getRandomInt ( 1 , 2 ) ; if ( index === 1 ) { updateGame ( game , { teamId , channelId , startPlayer : player1 , player1 , player2 } ) ; } else { updateGame ( game , { teamId , channelId , startPlayer : player1 , player1 : player2 , player2 : player1 } ) ; } db . insert ( game , function ( saveError ) { startMap [ channelId ] = undefined ; if ( saveError ) { return res . send ( saveError ) ; } res . json ( slackUtil . createResponse ( { text : labels . CMD_TEXT , attachments : [ { text : util . format ( labels . CMD_DETAIL_TEXT , slackUtil . getUserDisplay ( player1 ) , slackUtil . getUserDisplay ( player2 ) ) + "\n" + "`X` -> " + slackUtil . getUserDisplay ( game . players [ 0 ] ) + "\n" + "`O` -> " + slackUtil . getUserDisplay ( game . players [ 1 ] ) + "\n" + "First move by: " + slackUtil . getUserDisplay ( game . players [ 0 ] ) + "\n" + drawer . draw ( game . board ) , color : "#36a64f" , mrkdwn_in : [ "text" ] } ] } ) ) ; } ) ; } db . lookup ( Game , { teamId , channelId , state : constants . gameState . STARTED } , dbLookupCallback ) ; } ) ; }
@brief This function handles the /ttt 'start' command.
@brief This function handles the / ttt ' start ' command .
// Check if the given user is a valid member of this channel. // This error is due to an error calling the channels.info API. // This error is due to an error getting the list of errors. // Unable to get the user id of this user from Slack. /**
             * @brief This function is invoked when the database lookup
             *        operation to find the channel's ongoing game is complete.
             *        If a valid game is found, an error is returned. Otherwise,
             *        a new game is initialized and saved to the db.
             *
             * @param error db lookup error
             * @param game valid game from the db
             *
             * @return none
             */ // No ongling game found in this channel. // Create a new game. // Pick a player at random to make the first move. // Save the new game to the database. // Find the ongoing game in the channel.