All files BusdriverService.js

100% Statements 56/56
100% Branches 6/6
100% Functions 25/25
100% Lines 56/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128          17x 17x 17x 17x 17x       17x 17x       17x 51x 51x     17x 51x         51x         19x 57x         28x 28x 28x 84x   28x     28x 28x   28x 84x 84x     28x       27x   27x 231x   27x       26x   26x 222x 13x       13x 39x     13x   13x       26x 39x 39x     26x 39x 27x       26x 222x         25x 12x 36x 15x         25x 12x         37x 37x 111x   37x          
import Busdriver from "../src/Busdriver.js";
import GossipExchangeStation from "./GossipExchangeStation.js";
 
class BusdriverService {
  constructor(properties) {
    this.allRoutes = properties.allRoutes;
    this.allDrivers = [];
    this.stations = [];
    this.driversKnowAllGossips = []; //index is id
    this.initialize();
  }
 
  initialize() {
    this.driversKnowAllGossips = new Array(this.allRoutes.length);
    this.driversKnowAllGossips.fill(false);
  }
 
  busdriverFactory() {
    const createBusdriver = (busdriverProperties) => {
      let busdriver = new Busdriver(busdriverProperties);
      return busdriver;
    };
 
    this.allDrivers = this.allRoutes.map((route, index) => {
      let properties = {
        route: route,
        id: index,
        numberOfGossips: this.allRoutes.length,
      };
      return createBusdriver(properties);
    });
  }
 
  allDriveToNextStation() {
    this.allDrivers.forEach((driver) => {
      driver.driveToNextStation();
    });
  }
 
  calculateNumberOfDriversAtStation() {
    const getHighestStationNumber = () => {
      let shallowArray = [];
      this.allRoutes.forEach((route) => {
        shallowArray = shallowArray.concat(route);
      });
      return Math.max(...shallowArray) + 1;
    };
 
    let overviewArray = new Array(getHighestStationNumber());
    overviewArray.fill(0);
 
    this.allDrivers.forEach((driver) => {
      let currentStation = driver.getCurrentStation();
      overviewArray[currentStation] += 1;
    });
 
    this.numberOfDriversAtStation = overviewArray;
  }
 
  exchangeGossipIsPossibleAtStations() {
    let boolArray = [];
 
    this.numberOfDriversAtStation.forEach((numberOfDrivers, index) => {
      boolArray[index] = numberOfDrivers > 1;
    });
    this.stationHasMultipleDriversArray = boolArray;
  }
 
  createStationsForGossipExhange() {
    this.stations = [];
 
    const createSingleStation = (station, pushDriversToStation, index) => {
      if (station) {
        let stationProperties = {
          numberOfGossips: this.allDrivers.length,
          busdriversAtStation: [],
        };
        this.allDrivers.forEach((driver) => {
          pushDriversToStation(driver, index, stationProperties);
        });
 
        let stationObject = new GossipExchangeStation(stationProperties);
 
        this.stations.push(stationObject);
      }
    };
 
    const isAtStation = (driver, stationIndex) => {
      let currentStation = driver.getCurrentStation();
      return currentStation === stationIndex;
    };
 
    const pushDriversToStation = (driver, index, stationProperties) => {
      if (isAtStation(driver, index)) {
        stationProperties.busdriversAtStation.push(driver);
      }
    };
 
    this.stationHasMultipleDriversArray.forEach((station, index) => {
      createSingleStation(station, pushDriversToStation, index);
    });
  }
 
  getDriversHaveFullGossipKnowledge() {
    const passDrivers = (driverWithFullKnowledgeArray) => {
      driverWithFullKnowledgeArray.forEach((driver, index) => {
        if (driver) {
          this.driversKnowAllGossips[index] = true;
        }
      });
    };
 
    this.stations.forEach((station) => {
      passDrivers(station.getDriversKnowAllGossips());
    });
  }
 
  isGossipKnowledgeComplete() {
    let result = 1;
    this.driversKnowAllGossips.forEach((driver) => {
      result *= driver;
    });
    return result === 1;
  }
}
 
export default BusdriverService;