Last active
August 29, 2015 14:24
-
-
Save PetterKraabol/8b3682bc493ffae567e6 to your computer and use it in GitHub Desktop.
Random Generated Flight Schedules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written by Petter Kraabøl | |
function Airport (city, name, capacity, x, y){ | |
this.city = city; // City name | |
this.name = name; // Airport Name | |
this.capacity = capacity; // Plane capacity | |
this.x = x; // x coordinate | |
this.y = y; // y coordinate | |
} | |
function Plane (name, capacity, airport, flight){ | |
this.name = name; // Plane name | |
this.capacity = capacity; // Seats | |
this.airport = airport; // Current airport, destination if in flight | |
this.flight = flight; // Flight route if any (object/false) | |
} | |
function Flight (plane, origin, destination, departure, arrival){ | |
this.plane = plane; // Plane | |
this.origin = origin; // Origin Airport | |
this.destination= destination; // destination Airport | |
this.departure = departure; // Date time (dd/mm/yyyy hh:mm:ss) | |
this.arrival = arrival; // Date time (dd/mm/yyyy hh:mm:ss) | |
} | |
// Example Data | |
var airport = []; | |
// City Name Capacity X Y | |
airport[0] = new Airport('lineware', 'Modern Flannel Airport', 10, 886, 165); | |
airport[1] = new Airport('Subtrax', 'Pointless Waterbird Airport', 10, 859, 471); | |
airport[2] = new Airport('Cityhigh', 'Grim Leather Airport', 10, 196, 584); | |
airport[3] = new Airport('danbam', 'Pottery Stormy Airport', 10, 505, 826); | |
airport[4] = new Airport('E-ice', 'Wrench Aberrant Airport', 10, 145, 677); | |
airport[5] = new Airport('Andexon', 'Swift Dusty Fox Airport', 10, 524, 86); | |
airport[6] = new Airport('Opendom', 'Elastic Maximum Cloud Airport', 10, 887, 293); | |
airport[7] = new Airport('O-soloware', 'Grim Blue Notorious Airport', 10, 332, 245); | |
var plane = []; | |
// Name Capacity Airport Active | |
plane[0] = new Plane('Fixsolzim Airways', 20, airport[0], false); | |
plane[1] = new Plane('Waretraxbam Airways', 20, airport[1], false); | |
plane[2] = new Plane('Joyholding Airways', 20, airport[2], false); | |
plane[3] = new Plane('Plusron Airways', 20, airport[3], false); | |
plane[4] = new Plane('Bamzim Airways', 20, airport[4], false); | |
plane[5] = new Plane('Dongvivahex Airways', 20, airport[5], false); | |
plane[6] = new Plane('Plustom Airways', 20, airport[6], false); | |
plane[7] = new Plane('Goldenfind Airways', 20, airport[7], false); | |
plane[8] = new Plane('Matdubplus Airways', 20, airport[0], false); | |
plane[9] = new Plane('Vilacom Airways', 20, airport[1], false); | |
plane[10] = new Plane('Indigotechno Airways', 20, airport[2], false); | |
plane[11] = new Plane('Sonelectrics Airways', 20, airport[3], false); | |
plane[12] = new Plane('Duo-fax Airways', 20, airport[4], false); | |
plane[13] = new Plane('Lahouse Airways', 20, airport[5], false); | |
plane[14] = new Plane('Sailcone Airways', 20, airport[6], false); | |
plane[15] = new Plane('Finvivadex Airways', 20, airport[7], false); | |
var flight = []; | |
// Generate Flights | |
for(var i = 0; i < plane.length; i++){ | |
var date = new Date(); | |
var end = dateAdd(date, 'year', 1); | |
while(date <= end){ | |
var departure = generateDelay(date); | |
var destination = findDestination(plane[i].airport); | |
var arrival = calculateArrival(departure, plane[i].airport, destination); | |
var newFlight = new Flight(plane[i], plane[i].airport, destination, departure, arrival); | |
flight.push(newFlight); | |
// Set new values | |
plane[i].airport = destination; | |
date = arrival; | |
} | |
} | |
console.log(JSON.stringify(flight)); | |
function generateDelay(date){ | |
var minutes = 0; | |
var delay = date; | |
var rng = Math.random()*100; | |
// Strike 1-21 days at 00:00:00 | |
if(rng < 0.005) | |
delay = new Date(delay.getFullYear(), delay.getMonth(), delay.getDate() + Math.floor(Math.random() * 21 + 1)); | |
// No more flights today Starts the next day at 00:00:00 | |
if(rng < 0.5) | |
delay = new Date(delay.getFullYear(), delay.getMonth(), delay.getDate()+1); | |
// Delayed for random minutes + constant | |
minutes = Math.floor(Math.random() * 512 + 1) + 60; | |
delay = dateAdd(delay, 'minute', minutes); | |
// Set seconds to 00 | |
delay = new Date(delay.getFullYear(), delay.getMonth(), delay.getDate(), delay.getHours(), delay.getMinutes()); | |
return delay; | |
} | |
function findDestination(origin){ | |
while(true){ | |
var rng = Math.floor(Math.random() * airport.length); | |
if(airport[rng] !== origin.name) | |
return airport[rng]; | |
} | |
} | |
function calculateArrival(date, origin, destination){ | |
var distance = calculateDistance(origin, destination); | |
var travelTime = distance*2 + Math.floor(Math.random() * 30 + 1); | |
return dateAdd(date, 'minute', travelTime); | |
} | |
function calculateDistance(a, b){ | |
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2)); | |
} | |
function dateAdd(date, interval, units) { | |
var ret = new Date(date); | |
switch(interval.toLowerCase()) { | |
case 'year' : ret.setFullYear(ret.getFullYear() + units); break; | |
case 'quarter': ret.setMonth(ret.getMonth() + 3*units); break; | |
case 'month' : ret.setMonth(ret.getMonth() + units); break; | |
case 'week' : ret.setDate(ret.getDate() + 7*units); break; | |
case 'day' : ret.setDate(ret.getDate() + units); break; | |
case 'hour' : ret.setTime(ret.getTime() + units*3600000); break; | |
case 'minute' : ret.setTime(ret.getTime() + units*60000); break; | |
case 'second' : ret.setTime(ret.getTime() + units*1000); break; | |
default : ret = undefined; break; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment