Skip to content

Instantly share code, notes, and snippets.

@yuanoook
Created August 21, 2019 07:40
Show Gist options
  • Save yuanoook/a842c0fc111ca7aaf36f283c633d2562 to your computer and use it in GitHub Desktop.
Save yuanoook/a842c0fc111ca7aaf36f283c633d2562 to your computer and use it in GitHub Desktop.
Autopilot.js
/*
Autopilot System
Flow
Condition > Behavior
Auto
Detect > Suggest > [Wait]
Switch >
Manual Control
Automatic Execution
Autopilot {
start {
loop {
auto.detect
auto.suggest
auto.wait
}
},
auto: {
detect: func,
predictor.test
suggest: func,
predictor.predict
wait: func
switcher.watchInterfering
},
predictor: {
experience: [Behavior {
Condition,
Suggest
},...]
test: func
predict: func
log: func
},
switcher: {
watchInterfering
? switcher.manualControl
: switcher.automaticExecute
manualControl
predictor.log
automaticExecute
}
}
*/
var autoPilot = {
start: function() {
this.auto.init(this);
setTimeout(() => {
this.auto.detect();
this.auto.suggest();
this.auto.wait();
}, 1000);
},
auto: {
candidates: [],
init: function(autoPilot) {
this.predictor = autoPilot.predictor.init();
this.switcher = autoPilot.switcher.init();
},
detect: function() {
this.candidates = this.predictor.test();
},
suggest: function() {
this.predictor.predict(this.candidates);
},
wait: function() {
this.switcher.watchInterfering();
}
},
predictor: {
wisdom: [],
init: function() {
return this;
},
test: function() {
return this.wisdom.map(behavior => behavior.condition() && behavior).filter(x => x);
},
predict: function(candidates) {
candidates.forEach(candidate => candidate.suggest());
},
log: function(experience) {
this.wisdom.push(experience);
}
},
switcher: {
init: function() {
var $switcher = $('<div></div>');
$switcher.css({
position: 'fixed',
top: 0,
left: 0,
padding: 10,
background: 'rgba(255,255,255,.7)'
}).html(`
<h3>autoPilot</h3>
<b>Off</b>
<span>Speed: 0ms</span>
`);
$switcher.appendTo('body');
return this;
},
watchInterfering: function() {
}
}
};
autoPilot.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment