<commit_before>"use strict";


var Elevator = require('./elevator');
var events = require('events');
var debug = require('debug')('Passenger');

/**
 * Passenger
 */
class Passenger extends events.EventEmitter {

	constructor(id, elevator, origin, direction, destination) {
		super();

		this.id = id;
		this.elevator = elevator;
		this.origin = origin;
		this.direction = direction;
		this.destination = destination;
		this.satisfaction = 100;

		this.state = Passenger.WAITING;

		this.init();
	}

	/**
	 * Starts this passenger's trip.
	 * @return {[type]} [description]
	 */
	init() {
		this.elevator.on('stop', this.checkFloor.bind(this));		

		this.requestElevator();
	}

	/**
	 * @return {[int]} The current state of this passenger.
	 */
	getState() {
		return this.state;
	}

	/**
	 * Checks at every stop of the elevator to see if we should be doing something
	 * @param  {number} floor       The floor where the elevator is
	 * @param  {number} direction 	The direction the elevator is heading
	 */
	checkFloor(floor, direction) {
		if (Passenger.WAITING === this.state) {
			if (this.origin === floor && (direction == undefined || this.direction === direction)) {
				this.getOnElevator();
			}
		} else if (Passenger.TRAVELLING === this.state) {
			if (this.destination === floor) {
				this.getOffElevator();
			}
		}
	}

	/**
	 * Request the elevator to come to this Passenger's origin.
	 */
	requestElevator() {
		this.elevator.requestFloor(this.origin, this.direction);
	}

	/**
	 * Passenger boards the elevator and then issues a new request to go to destination
	 */
	getOnElevator() {
		debug('%d getting on floor %d', this.id, this.origin);
		this.state = Passenger.TRAVELLING;
		this.emit('embark');
		this.elevator.requestFloor(this.destination, null);
	}

	/**
	 * Passenger gets off the elevator and is now complete.
	 */
	getOffElevator() {
		debug('%d getting off floor %d', this.id, this.destination);
		this.elevator.removeListener('stop', this.checkFloor);
		this.state = Passenger.DONE;
		this.emit('disembark');
	}
}

Passenger.WAITING = 0;
Passenger.TRAVELLING = 1;
Passenger.DONE = 2;

module.exports = Passenger;<commit_msg>Added time stamps for travel.<commit_after>"use strict";


var Elevator = require('./elevator');
var events = require('events');
var process = require('process');
var debug = require('debug')('Passenger');

/**
 * Passenger
 */
class Passenger extends events.EventEmitter {

	constructor(id, elevator, origin, direction, destination) {
		super();

		this.id = id;
		this.elevator = elevator;
		this.origin = origin;
		this.direction = direction;
		this.destination = destination;
		this.satisfaction = 100;

		this.state = Passenger.WAITING;

		this.waitTimes = [];
		this.actionTimes = [];

		this.init();
	}

	/**
	 * Starts this passenger's trip.
	 * @return {[type]} [description]
	 */
	init() {
		this.elevator.on('stop', this.checkFloor.bind(this));		

		this.requestElevator();
	}

	/**
	 * @return {[int]} The current state of this passenger.
	 */
	getState() {
		return this.state;
	}

	/**
	 * Checks at every stop of the elevator to see if we should be doing something
	 * @param  {number} floor       The floor where the elevator is
	 * @param  {number} direction 	The direction the elevator is heading
	 */
	checkFloor(floor, direction) {
		if (Passenger.WAITING === this.state) {
			if (this.origin === floor && (direction == undefined || this.direction === direction)) {
				this.getOnElevator();
			}
		} else if (Passenger.TRAVELLING === this.state) {
			if (this.destination === floor) {
				this.getOffElevator();
			}
		}
	}

	/**
	 * Request the elevator to come to this Passenger's origin.
	 */
	requestElevator() {
		this.noteTime();
		this.elevator.requestFloor(this.origin, this.direction);
	}

	/**
	 * Passenger boards the elevator and then issues a new request to go to destination
	 */
	getOnElevator() {
		this.noteTime();
		debug('%d getting on floor %d', this.id, this.origin);
		this.state = Passenger.TRAVELLING;
		this.emit('embark');
		this.elevator.requestFloor(this.destination, null);
	}

	/**
	 * Passenger gets off the elevator and is now complete.
	 */
	getOffElevator() {
		this.noteTime();
		debug('%d getting off floor %d', this.id, this.destination);
		
		this.elevator.removeListener('stop', this.checkFloor);
		this.state = Passenger.DONE;
		
		debug('Wait time: %d . %d', this.waitTimes[0][0], this.waitTimes[0][1]);
		debug('Travel time: %d . %d', this.waitTimes[1][0], this.waitTimes[1][1]);

		this.emit('disembark');
	}

	/**
	 * Keeps track of the times that the elevator is taking to do its thing.
	 */
	noteTime() {
		if (this.actionTimes.length > 0) {
			this.waitTimes.push(process.hrtime(this.actionTimes[this.actionTimes.length - 1]));
		}
		this.actionTimes.push(process.hrtime());		
	}
}

Passenger.WAITING = 0;
Passenger.TRAVELLING = 1;
Passenger.DONE = 2;

module.exports = Passenger;
{'fraction_non_alphanumeric': 0.1506318793314309, 'fraction_numerical': 0.004484304932735426, 'mean_word_length': 3.594569288389513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6457706', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1602, 'n_words': 408}