Created
December 4, 2018 15:31
-
-
Save vigonotion/0307b3df7b881f5d6aa789540ead39b5 to your computer and use it in GitHub Desktop.
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
{ | |
init: function(elevators, floors) { | |
var elevator = elevators[0]; // Let's use the first elevator | |
var requested_floors = new Set(); | |
var stop_for_leavers = new Set(); | |
var collectPassengersOnPass = function(elevator) { | |
elevator.on("passing_floor", function(floorNum, direction) { | |
// TODO: nur wenn direction auch stimmt | |
// wenn auf der nächsten Etage Passagiere stehen und noch Platz ist, | |
// sofort auf der Etage anhalten | |
if(requested_floors.has(floorNum) && elevator.loadFactor() < 0.9) { | |
elevator.goToFloor(floorNum, true); | |
console.log("🚏 picking a passenger up...") | |
} | |
// wenn jemand auf jeden fall aussteigen möchte, | |
// sofort auf der Etage anhalten | |
else if(stop_for_leavers.has(floorNum)) { | |
elevator.goToFloor(floorNum, true); | |
console.log("➡ letting a passenger out...") | |
} | |
}); | |
}; | |
collectPassengersOnPass(elevator); | |
// gedrückte floors abspeichern | |
floors.forEach(function(floor) { | |
floor.on("up_button_pressed", function() { | |
requested_floors.add(floor.floorNum()); | |
goToFloors(); | |
} ); | |
floor.on("down_button_pressed", function() { | |
requested_floors.add(floor.floorNum()); | |
goToFloors(); | |
} ); | |
}); | |
// Anfragen löschen wenn auf der Etage gehalten wurde | |
elevator.on("stopped_at_floor", function(floorNum) { | |
requested_floors.delete(floorNum); | |
stop_for_leavers.delete(floorNum); | |
}) | |
// //noch unsortiert | |
var goToFloors = function(){ | |
var reqs = Array.from(requested_floors).sort(); | |
var i = 0; | |
if (reqs.length > 0){ | |
console.log(reqs); | |
// for (var i = 0; i < reqs.length; i++){ | |
reqs.forEach(function(){ | |
elevator.goToFloor(reqs[i]); | |
reqs = new Set(reqs); | |
reqs.delete(i); | |
i++; | |
}) | |
// } | |
} | |
else{ | |
elevator.goToFloor( Math.round(floors.length / 2) ); | |
} | |
} | |
//wenn elevator-button gedrückt, fahre zu gedrücktem floor | |
elevator.on("floor_button_pressed", function(floorNum) { | |
//elevator.goToFloor(floorNum); | |
// hinzufügen zu fahrstuhlwunsch | |
// behalte min(fahrstuhlwunsch) und max(fahrstuhlwunsch) und fahre, | |
// je nach richtung, diese an | |
// alle anderen landen in stop_for_leavers[] | |
stop_for_leavers.clear(); | |
stop_for_leavers.add(elevator.getPressedFloors()); | |
elevator.goToFloor( stop_for_leavers.values().next().value[0] ); | |
} ); | |
// Whenever the elevator is idle (has no more queued destinations) ... | |
elevator.on("idle", function() { | |
// goToFloors(); | |
console.log("👀 idling... moving elevator to middle"); | |
}); | |
}, | |
update: function(dt, elevators, floors) { | |
// We normally don't need to do anything here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment