Skip to content

Instantly share code, notes, and snippets.

@Catsync
Last active April 22, 2016 20:35
Show Gist options
  • Save Catsync/1a3a57a2a4ecc682f320a2e4b1337418 to your computer and use it in GitHub Desktop.
Save Catsync/1a3a57a2a4ecc682f320a2e4b1337418 to your computer and use it in GitHub Desktop.
Chrome Snippet script to automate updating a Code Combat level's code
(function(currentView) {
var thangs = currentView.level.get('thangs');
// **** Change sample code and solutions **** //
// Search level thangs
for(var i=0; i < thangs.length; i++) {
var thang = thangs[i];
// Search thang components
if(thang.components) {
for(var j=0; j < thang.components.length; j++) {
var component = thang.components[j];
// Found programmableMethods component
if(component.config && component.config.programmableMethods && component.config.programmableMethods.plan) {
var plan = component.config.programmableMethods.plan;
// Change JS Source
console.log("Changing JS source");
var newJS = plan.source.replace(/this/g,'hero');
newJS = newJS.replace(/loop \{/g,'while(true) {');
plan.source = newJS;
// Change PY Source
console.log("Changing PY source")
plan.languages.python = plan.languages.python.replace(/self/g,'hero');
plan.languages.python = plan.languages.python.replace(/loop\:/g,'while True:');
// Change coffee Source
// console.log("Changing coffee source")
// plan.languages.coffeescript = plan.languages.coffeescript.replace(/\@/g,'hero.');
// Change lua Source
// console.log("Changing lua source")
// plan.languages.lua = plan.languages.lua.replace(/self/g,'hero');
// Change solutions
if(plan.solutions) {
for(k=0; k < plan.solutions.length; k++) {
var sol = plan.solutions[k];
if(sol.language === "javascript") {
sol.source = sol.source.replace(/this/g,'hero');
sol.source = sol.source.replace(/loop \{/g,'while(true) {');
}
if(sol.language === "python") {
sol.source = sol.source.replace(/self/g,'hero');
sol.source = sol.source.replace(/loop\:/g,'while True:');
}
}
console.log('Updated solutions');
} else {
console.log("No solutions to update");
}
}
}
}
}
console.log("Saving thangs...");
currentView.level.set('thangs', thangs);
// **** Change guides and intros ****/
var documentation = currentView.level.get('documentation');
console.log("Updating articles...");
if(documentation && documentation.specificArticles) {
for(var a=0; a < documentation.specificArticles.length; a++) {
var article = documentation.specificArticles[a];
if(article.name == "Overview" || article.name == "Intro") {
article.body = article.body.replace(/this\./g,'hero.');
article.body = article.body.replace(/self\./g, 'hero.');
// article.body = article.body.replace(/\@/g, 'hero.');
}
}
console.log("Saving articles...");
currentView.level.set('documentation', documentation);
} else {
console.log("No articles found.");
}
})(currentView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment