<commit_before>'use strict';

const ChallengeServerClient = require('../runner/challenge_server_client');
const RecordingSystem = require('../runner/recording_system');
const RoundManagement = require('./round_management');

function ChallengeSession(implementationRunner) {
    this._implementationRunner = implementationRunner;
}

ChallengeSession.forRunner = function(implementationRunner) {
    return new ChallengeSession(implementationRunner);
}

ChallengeSession.prototype.withConfig = function(config) {
    this._config = config;
    return this;
}

ChallengeSession.prototype.withActionProvider = function(actionProvider) {
    this._actionProvider = actionProvider;
    return this;
}

/**
 * The entry point.
 */
ChallengeSession.prototype.start = function() {
    var self = this;

    self._recordingSystem = new RecordingSystem(self._config.recordingSystemShouldBeOn);

    var auditStream = self._config.getAuditStream();

    return self._recordingSystem.isRunning().then(function(isRunning) {
        if (!isRunning) {
            auditStream.log('Please run `record_screen_and_upload` before continuing.');
            return;
        }

        auditStream.log(`Connecting to ${self._config.getHostname()}`);
        return self._runApp();
    });
}

ChallengeSession.prototype._runApp = function() {
    var self = this;

    var auditStream = self._config.getAuditStream();
    
    return new Promise(function(resolve) {
        self._challengeServerClient = new ChallengeServerClient(
                self._config.getHostname(),
                self._config.getPort(),
                self._config.getJourneyId(),
                self._config.getUseColours());

        self._checkStatusOfChallenge()
            .then(function(shouldContinue) {
                if (!shouldContinue) {
                    resolve();
                }

                return self._actionProvider.get();
            })
            .then(function(userInput) {
                auditStream.log(`Selected action is: ${userInput}`);
                return self._executeUserAction(userInput);
            })
            .then(function(roundDescription) {
                return RoundManagement.saveDescription(self._recordingSystem, roundDescription, auditStream);
            })
            .then(() => resolve());
    });
};

ChallengeSession.prototype._checkStatusOfChallenge = function() {
    var self = this;

    var auditStream = self._config.getAuditStream();

    return new Promise(function(resolve) {
        self._challengeServerClient.getJourneyProgress()
            .then(function(journeyProgress) {
                auditStream.log(journeyProgress);
                return self._challengeServerClient.getAvailableActions();
            })
            .then(function(availableActions) {
                auditStream.log(availableActions);
                resolve(availableActions.indexOf('No actions available.') === -1);
            });
    });
};

ChallengeSession.prototype._executeUserAction = function(userInput) {
    var self = this;

    if (userInput !== 'deploy') {
        return self._executeAction(userInput);
    }

    return self._implementationRunner.run()
        .then(function() {
            var lastFetchedRound = RoundManagement.getLastFetchedRound();
            return self._recordingSystem.deployNotifyEvent(lastFetchedRound);
        })
        .then(function() {
            return self._executeAction(userInput);
        });
};

ChallengeSession.prototype._executeAction = function(userInput) {
    var self = this;

    var auditStream = self._config.getAuditStream();
    
    return new Promise(function(resolve) {
        self._challengeServerClient.sendAction(userInput)
            .then(function(actionFeedback) {
                auditStream.log(actionFeedback);
                return self._challengeServerClient.getRoundDescription();
            })
            .then((roundDescription) => resolve(roundDescription));
    });
};

module.exports = ChallengeSession;<commit_msg>Simplify ChallengeSession<commit_after>'use strict';

var ChallengeServerClient = require('../runner/challenge_server_client');
var RecordingSystem = require('../runner/recording_system');
var RoundManagement = require('./round_management');
var Promise = require('promise');

function ChallengeSession(implementationRunner) {
    this._implementationRunner = implementationRunner;
}

ChallengeSession.forRunner = function(implementationRunner) {
    return new ChallengeSession(implementationRunner);
}

ChallengeSession.prototype.withConfig = function(config) {
    this._config = config;
    this._auditStream = config.getAuditStream();
    return this;
}

ChallengeSession.prototype.withActionProvider = function(actionProvider) {
    this._actionProvider = actionProvider;
    return this;
}

/**
 * The entry point.
 */
ChallengeSession.prototype.start = function() {
    var self = this;

    self._recordingSystem = new RecordingSystem(self._config.recordingSystemShouldBeOn);

    return self._recordingSystem.isRunning().then(function(isRunning) {
        if (!isRunning) {
            self._auditStream.log('Please run `record_screen_and_upload` before continuing.');
            return;
        }

        self._auditStream.log(`Connecting to ${self._config.getHostname()}`);
        return self._runApp();
    });
}

ChallengeSession.prototype._runApp = function() {
    var self = this;
    
    self._challengeServerClient = new ChallengeServerClient(
        self._config.getHostname(),
        self._config.getPort(),
        self._config.getJourneyId(),
        self._config.getUseColours());

    return self._checkStatusOfChallenge()
        .then(function(shouldContinue) {
            if (!shouldContinue) {
                resolve();
            }

            return self._actionProvider.get();
        })
        .then(function(userInput) {
            self._auditStream.log(`Selected action is: ${userInput}`);
            return self._executeUserAction(userInput);
        })
        .then(function(roundDescription) {
            return RoundManagement.saveDescription(self._recordingSystem, roundDescription, self._auditStream);
        });
};

ChallengeSession.prototype._checkStatusOfChallenge = function() {
    var self = this;

    return self._challengeServerClient.getJourneyProgress()
        .then(function(journeyProgress) {
            self._auditStream.log(journeyProgress);
            return self._challengeServerClient.getAvailableActions();
        })
        .then(function(availableActions) {
            self._auditStream.log(availableActions);
            return Promise.resolve(availableActions.indexOf('No actions available.') === -1);
        });
};

ChallengeSession.prototype._executeUserAction = function(userInput) {
    var self = this;

    if (userInput !== 'deploy') {
        return self._executeAction(userInput);
    }

    return self._implementationRunner.run()
        .then(function() {
            var lastFetchedRound = RoundManagement.getLastFetchedRound();
            return self._recordingSystem.deployNotifyEvent(lastFetchedRound);
        })
        .then(function() {
            return self._executeAction(userInput);
        });
};

ChallengeSession.prototype._executeAction = function(userInput) {
    var self = this;

    return self._challengeServerClient.sendAction(userInput)
        .then(function(actionFeedback) {
            self._auditStream.log(actionFeedback);
            return self._challengeServerClient.getRoundDescription();
        });
};

module.exports = ChallengeSession;
{'fraction_non_alphanumeric': 0.11013157894736843, 'fraction_numerical': 0.0002631578947368421, 'mean_word_length': 2.9019507186858315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4988088', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1916, 'n_words': 310}