2015-11-17 21:25:16 -08:00
|
|
|
// what are the executeChallenge functions?
|
|
|
|
// Should be responsible for starting after a submit action
|
|
|
|
// Should not be responsible for displaying results
|
|
|
|
// Should return results
|
|
|
|
// should grab editor value
|
|
|
|
// depends on main editor
|
|
|
|
window.common = (function(global) {
|
|
|
|
const {
|
|
|
|
ga,
|
|
|
|
Rx: { Observable },
|
|
|
|
common = { init: [] }
|
|
|
|
} = global;
|
|
|
|
|
|
|
|
let attempts = 0;
|
|
|
|
|
|
|
|
common.executeChallenge$ = function executeChallenge$() {
|
|
|
|
const code = common.editor.getValue();
|
|
|
|
const head = common.arrayToNewLineString(common.head);
|
|
|
|
const tail = common.arrayToNewLineString(common.tail);
|
|
|
|
|
|
|
|
attempts++;
|
|
|
|
|
|
|
|
ga('send', 'event', 'Challenge', 'ran-code', common.challengeName);
|
|
|
|
|
2015-11-24 13:48:14 -08:00
|
|
|
// run checks for unsafe code
|
|
|
|
return common.detectUnsafeCode$(code)
|
|
|
|
// add head and tail and detect loops
|
|
|
|
.map(code => head + code + tail)
|
|
|
|
.flatMap(code => {
|
2015-11-19 21:51:38 -08:00
|
|
|
if (common.challengeType === common.challengeTypes.HTML) {
|
2015-11-22 19:42:53 -08:00
|
|
|
|
|
|
|
if (common.hasJs(code)) {
|
2015-11-24 13:48:14 -08:00
|
|
|
// html has a script code
|
|
|
|
// add faux code and test in webworker
|
|
|
|
return common.addFaux$(code)
|
2015-11-22 19:42:53 -08:00
|
|
|
.flatMap(code => common.detectLoops$(code))
|
|
|
|
.flatMap(({ err }) => {
|
|
|
|
if (err) {
|
2015-11-24 13:48:14 -08:00
|
|
|
return Observable.throw(err);
|
2015-11-22 19:42:53 -08:00
|
|
|
}
|
2015-11-24 13:48:14 -08:00
|
|
|
return common.updatePreview$(code)
|
|
|
|
.flatMap(() => common.runPreviewTests$({
|
|
|
|
code,
|
|
|
|
tests: common.tests.slice()
|
|
|
|
}));
|
2015-11-22 19:42:53 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:48:14 -08:00
|
|
|
// no script code detected in html code
|
|
|
|
// Update preview and run tests in iframe
|
|
|
|
return common.updatePreview$(code)
|
2015-11-22 19:42:53 -08:00
|
|
|
.flatMap(code => common.runPreviewTests$({
|
|
|
|
code,
|
|
|
|
tests: common.tests.slice()
|
|
|
|
}));
|
2015-11-19 21:51:38 -08:00
|
|
|
}
|
2015-11-17 21:25:16 -08:00
|
|
|
|
2015-11-24 13:48:14 -08:00
|
|
|
// js challenge
|
|
|
|
// remove comments and add tests to string
|
2015-11-24 15:12:54 -08:00
|
|
|
return Observable.just(common.addTestsToString(Object.assign(
|
2015-11-19 21:51:38 -08:00
|
|
|
{
|
|
|
|
code: common.removeComments(code),
|
|
|
|
tests: common.tests.slice()
|
|
|
|
}
|
2015-11-24 15:12:54 -08:00
|
|
|
)))
|
2015-11-22 19:42:53 -08:00
|
|
|
.flatMap(common.detectLoops$)
|
2015-11-24 13:48:14 -08:00
|
|
|
.flatMap(({ err, code, data, userTests }) => {
|
2015-11-22 19:42:53 -08:00
|
|
|
if (err) {
|
2015-11-24 13:48:14 -08:00
|
|
|
return Observable.throw(err);
|
2015-11-22 19:42:53 -08:00
|
|
|
}
|
2015-11-17 21:25:16 -08:00
|
|
|
|
2015-11-24 13:48:14 -08:00
|
|
|
// run tests
|
|
|
|
// for now these are running in the browser
|
2015-11-22 19:42:53 -08:00
|
|
|
return common.runTests$({
|
|
|
|
data,
|
|
|
|
code,
|
|
|
|
userTests,
|
|
|
|
output: data.output.replace(/\\\"/gi, '')
|
|
|
|
});
|
|
|
|
});
|
2015-11-17 21:25:16 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return common;
|
|
|
|
}(window));
|