1 Star 5 Fork 0

Gitee 极速下载/ElevatorSaga

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/magwo/elevatorsaga
克隆/下载
interfaces.js 3.68 KB
一键复制 编辑 原始数据 按行查看 历史
// Interface that hides actual elevator object behind a more robust facade,
// while also exposing relevant events, and providing some helper queue
// functions that allow programming without async logic.
var asElevatorInterface = function(obj, elevator, floorCount, errorHandler) {
var elevatorInterface = riot.observable(obj);
elevatorInterface.destinationQueue = [];
var tryTrigger = function(event, arg1, arg2, arg3, arg4) {
try {
elevatorInterface.trigger(event, arg1, arg2, arg3, arg4);
} catch(e) { errorHandler(e); }
};
elevatorInterface.checkDestinationQueue = function() {
if(!elevator.isBusy()) {
if(elevatorInterface.destinationQueue.length) {
elevator.goToFloor(_.first(elevatorInterface.destinationQueue));
} else {
tryTrigger("idle");
}
}
};
// TODO: Write tests for this queueing logic
elevatorInterface.goToFloor = function(floorNum, forceNow) {
floorNum = limitNumber(Number(floorNum), 0, floorCount - 1);
// Auto-prevent immediately duplicate destinations
if(elevatorInterface.destinationQueue.length) {
var adjacentElement = forceNow ? _.first(elevatorInterface.destinationQueue) : _.last(elevatorInterface.destinationQueue);
if(epsilonEquals(floorNum, adjacentElement)) {
return;
}
}
elevatorInterface.destinationQueue[(forceNow ? "unshift" : "push")](floorNum);
elevatorInterface.checkDestinationQueue();
};
elevatorInterface.stop = function() {
elevatorInterface.destinationQueue = [];
if(!elevator.isBusy()) {
elevator.goToFloor(elevator.getExactFutureFloorIfStopped());
}
};
elevatorInterface.getFirstPressedFloor = function() { return elevator.getFirstPressedFloor(); }; // Undocumented and deprecated, will be removed
elevatorInterface.getPressedFloors = function() { return elevator.getPressedFloors(); };
elevatorInterface.currentFloor = function() { return elevator.currentFloor; };
elevatorInterface.maxPassengerCount = function() { return elevator.maxUsers; };
elevatorInterface.loadFactor = function() { return elevator.getLoadFactor(); };
elevatorInterface.destinationDirection = function() {
if(elevator.destinationY === elevator.y) { return "stopped"; }
return elevator.destinationY > elevator.y ? "down" : "up";
}
elevatorInterface.goingUpIndicator = createBoolPassthroughFunction(elevatorInterface, elevator, "goingUpIndicator");
elevatorInterface.goingDownIndicator = createBoolPassthroughFunction(elevatorInterface, elevator, "goingDownIndicator");
elevator.on("stopped", function(position) {
if(elevatorInterface.destinationQueue.length && epsilonEquals(_.first(elevatorInterface.destinationQueue), position)) {
// Reached the destination, so remove element at front of queue
elevatorInterface.destinationQueue = _.rest(elevatorInterface.destinationQueue);
if(elevator.isOnAFloor()) {
elevator.wait(1, function() {
elevatorInterface.checkDestinationQueue();
});
} else {
elevatorInterface.checkDestinationQueue();
}
}
});
elevator.on("passing_floor", function(floorNum, direction) {
tryTrigger("passing_floor", floorNum, direction);
});
elevator.on("stopped_at_floor", function(floorNum) {
tryTrigger("stopped_at_floor", floorNum);
});
elevator.on("floor_button_pressed", function(floorNum) {
tryTrigger("floor_button_pressed", floorNum);
});
return elevatorInterface;
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/ElevatorSaga.git
git@gitee.com:mirrors/ElevatorSaga.git
mirrors
ElevatorSaga
ElevatorSaga
master

搜索帮助