commands/start.js
/**
             * @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
             */
@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
@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
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"
                                ]
                            }]
                        })
                    );
                });
            }
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" ] } ] } ) ) ; } ) ; }
@brief This function is invoked when the database lookup
        operation to find the channel's ongoing game is complete.
@brief This function is invoked when the database lookup operation to find the channel ' s ongoing game is complete .
// 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.