commands/history.js
/**
 * @brief This function defines the handler for the /ttt 'history' command.
 *
 * @return object 'history' handler object containing the following methods:
 *                - parseArgs
 *                - handle
 */
@brief This function defines the handler for the /ttt 'history' command.
@return object 'history' handler object containing the following methods:
- parseArgs
- handle
@brief This function defines the handler for the / ttt ' history ' command . @return object ' history ' handler object containing the following methods : - parseArgs - handle
function initializeCommand() {
    'use strict';
    const util = require('util');
    const Game = require('../models/game');
    const db = require('../lib/db');
    const drawer = require('../lib/drawer');
    const constants = require('../lib/constants');
    const slackUtil = require('../lib/slackutil');
    const logger = require('../lib/logger.js').create();
    const config = require('../config');
    const locale = require('../locales').get(config.locale);
    const labels = locale[constants.commands.HISTORY];
    function parseArgs() {
        return true;
    }
    function handle(req, res) {
        console.log(req.body);
        let channelId = req.body.channel_id;
        let teamId = req.body.team_id;
        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) {
                return res.json(
                    slackUtil.createResponse({
                        text: labels.GAME_NOT_FOUND_ERROR,
                        response_type: "ephemeral"
                    })
                );
            }
            let currentPlayerIndex = 0;
            if (game.players[1].name === game.currentPlayer) {
                currentPlayerIndex = 1;
            }
            let status = util.format(
                labels.CMD_STATUS_TEXT,
                slackUtil.getUserDisplay(game.players[0]),
                slackUtil.getUserDisplay(game.players[1])
            );
            let history = game.history;
            let historyText = "";
            let i = 0;
            let index = 0;
            let symbol = constants.drawState[constants.moveState.X];
            while (i < history.length) {
                historyText = historyText + (i + 1) + ". "
                        + slackUtil.getUserDisplay(game.players[index])
                        + " placed an `" + symbol + "` at location `"
                        + history[i].move + "` at `"
                        + history[i].time.toUTCString() + "`\n";
                index = (index + 1) % 2;
                if (symbol === constants.drawState[constants.moveState.X]) {
                    symbol = constants.drawState[constants.moveState.O];
                } else {
                    symbol = constants.drawState[constants.moveState.X];
                }
                i = i + 1;
            }
            if (history.length === 0) {
                historyText = labels.HISTORY_NOT_FOUND_ERROR;
            }
            res.json(
                slackUtil.createResponse({
                    text: status,
                    response_type: "ephemeral",
                    attachments: [{
                        text: historyText + "\n"
                                + util.format(
                            labels.CMD_STATUS_TURN_TEXT,
                            slackUtil.getUserDisplay(
                                game.players[currentPlayerIndex]
                            )
                        )
                                + "\n" + drawer.draw(game.board),
                        color: "#36a64f",
                        mrkdwn_in: [
                            "text"
                        ]
                    }]
                })
            );
        }
        db.lookup(
            Game,
            {
                teamId,
                channelId,
                state: constants.gameState.STARTED
            },
            dbLookupCallback
        );
    }
    return Object.freeze({
        parseArgs,
        handle
    });
}
function initializeCommand ( ) { 'use strict' ; const util = require ( 'util' ) ; const Game = require ( '../models/game' ) ; const db = require ( '../lib/db' ) ; const drawer = require ( '../lib/drawer' ) ; const constants = require ( '../lib/constants' ) ; const slackUtil = require ( '../lib/slackutil' ) ; const logger = require ( '../lib/logger.js' ) . create ( ) ; const config = require ( '../config' ) ; const locale = require ( '../locales' ) . get ( config . locale ) ; const labels = locale [ constants . commands . HISTORY ] ; function parseArgs ( ) { return true ; } function handle ( req , res ) { console . log ( req . body ) ; let channelId = req . body . channel_id ; let teamId = req . body . team_id ; 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 ) { return res . json ( slackUtil . createResponse ( { text : labels . GAME_NOT_FOUND_ERROR , response_type : "ephemeral" } ) ) ; } let currentPlayerIndex = 0 ; if ( game . players [ 1 ] . name === game . currentPlayer ) { currentPlayerIndex = 1 ; } let status = util . format ( labels . CMD_STATUS_TEXT , slackUtil . getUserDisplay ( game . players [ 0 ] ) , slackUtil . getUserDisplay ( game . players [ 1 ] ) ) ; let history = game . history ; let historyText = "" ; let i = 0 ; let index = 0 ; let symbol = constants . drawState [ constants . moveState . X ] ; while ( i < history . length ) { historyText = historyText + ( i + 1 ) + ". " + slackUtil . getUserDisplay ( game . players [ index ] ) + " placed an `" + symbol + "` at location `" + history [ i ] . move + "` at `" + history [ i ] . time . toUTCString ( ) + "`\n" ; index = ( index + 1 ) % 2 ; if ( symbol === constants . drawState [ constants . moveState . X ] ) { symbol = constants . drawState [ constants . moveState . O ] ; } else { symbol = constants . drawState [ constants . moveState . X ] ; } i = i + 1 ; } if ( history . length === 0 ) { historyText = labels . HISTORY_NOT_FOUND_ERROR ; } res . json ( slackUtil . createResponse ( { text : status , response_type : "ephemeral" , attachments : [ { text : historyText + "\n" + util . format ( labels . CMD_STATUS_TURN_TEXT , slackUtil . getUserDisplay ( game . players [ currentPlayerIndex ] ) ) + "\n" + drawer . draw ( game . board ) , color : "#36a64f" , mrkdwn_in : [ "text" ] } ] } ) ) ; } db . lookup ( Game , { teamId , channelId , state : constants . gameState . STARTED } , dbLookupCallback ) ; } return Object . freeze ( { parseArgs , handle } ) ; }
@brief This function defines the handler for the /ttt 'history' command.
@brief This function defines the handler for the / ttt ' history ' command .
// Access the 'Game' model. // Acces mongodb utility. // Access the application constants object. // Access Slack utility object to format the message response. // Access bunyan logger. // Get current configuration. // Setup locales config /**
     * @brief This function checks the request body to see if all the valid
     *        parameters are present. Since the 'history' command does not need
     *        data, it does not need to validate the request body.
     *
     * @return boolean indicating the request validity
     */ /**
         * @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, its history & attributes are returned in the
         *        response.
         *
         * @param error db lookup error
         * @param game valid game from the db
         *
         * @return none
         */ // No ongling game found in this channel. // No ongling game found in this channel. // Show status of the current game. // Get historical moves for this game. // Text representing the historical moves. // Find a valid game being played in this channel.