Transportation

by AntonLapshin

TypeScript

var distances = {
  "1-2": 10,
  "1-3": 12,
  "3-2": 12,
  "1-4": 15,
  "3-4": 25,
  "2-4": 24,
  "4-5": 50,
  "2-5": 30,
  "1-5": 40,
  "3-5": 48
};

class DistanceAPI {
  public static get(origin: Location, destination: Location) {
    var distance = distances["{0}-{1}".f(origin.id, destination.id)];
    distance = distance || distances["{1}-{0}".f(origin.id, destination.id)] || 0;
    return new Route(distance, (distance / 50) * 60); // 50km/h -> duration in minutes
  }
}

enum LocationTypes {
  House = 'House', Morgue = 'Morgue', Church = 'Church', Crematorium = 'Crem', Garage = 'Garage'
}
enum TaskTypes {
  PickUp = 'PickUp', Drop = 'Drop'
}
enum OutcomeResults {
  InProgress = 0, Success = 1, Fail = -1
}

String.prototype.f = function() {
  var s = this,
    i = arguments.length;
  while (i--) {
    s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
  }
  return s;
};

Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

class Route {
  private distance;
  private duration;
  constructor(distance: int, duration: int) {
    this.distance = distance;
    this.duration = duration;
  }
  public getDistance() {
    return this.distance;
  }
  public getDuration() {
    return this.duration;
  }
}

class Hours {
  private start: int; // hours
  private end: int; // hours
  constructor(start: int, end: int) {
    this.start = start;
    this.end = end;
  }
  public toString = (): string => {
    return this.start === this.end && this.start === 0 ? 'none' : "{0}:00-{1}:00".f(this.start, this.end);
  }
}

class TimeFrame {
  private start: int;
  private end: int;
  constructor(start: int | Date, end: int | Date) {
    this.start = (new Date(start)).getTime();
    this.end = (new Date(end)).getTime();
  }
  public toString = (): string => {
   ...