Skip to content

Commit

Permalink
Fix the scripts to extract the recipes from the wiki and update the r…
Browse files Browse the repository at this point in the history
…ecipes with that.

Also put the output file in the arguments of the file instead of printing to stdout.
I used merge_recipes.js so recipes aren't changed, just added. blocks.json and items.json aren't fully updated (see #229) so some recipes are probably still missing.
  • Loading branch information
rom1504 committed Mar 22, 2015
1 parent 0f84a30 commit f4e8051
Show file tree
Hide file tree
Showing 4 changed files with 735 additions and 78 deletions.
23 changes: 16 additions & 7 deletions bin/merge_recipes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
#!/usr/bin/env node
if(process.argv.length!=4)
if(process.argv.length!=5)
{
console.log("Usage : ./merge_recipes.js <oldRecipes.json> <recipes2.json> > <newRecipes.json>");
console.log("Usage : ./merge_recipes.js <oldRecipes.json> <recipes2.json> <newRecipes.json>");
process.exit(1);
}

function print(o)
var fs=require('fs');
var output_file=process.argv[4];

function print(o,file)
{
console.log(JSON.stringify(o,null,2));
fs.writeFile(file, JSON.stringify(o,null,2), function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});
}

var oldRecipes=require("./"+process.argv[2]);
var transformedRecipes=require("./"+process.argv[3]);
var oldRecipes=require(process.argv[2]);
var transformedRecipes=require(process.argv[3]);
var newRecipes=transformedRecipes;

for(var i in oldRecipes) newRecipes[i]=oldRecipes[i];

print(newRecipes);
print(newRecipes,output_file);
140 changes: 80 additions & 60 deletions bin/transform1_recipes.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
#!/usr/bin/env node
if(process.argv.length!=3)
if(process.argv.length!=4)
{
console.log("Usage : ./transform1_recipes.js <recipes.html> > <recipes.json>");
console.log("Usage : node transform1_recipes.js <recipes.html> <recipes.json>");
process.exit(1);
}
// recipes.html should be http://www.minecraftwiki.net/wiki/Crafting/CompleteList
// recipes.html should be http://minecraft.gamepedia.com/Craft with all the recipes showed
// (in the browser : click on all the "show" then ctrl+s the page)

var fs=require('fs');

var input_file=process.argv[2];
var output_file=process.argv[3];

//open the file
var file=fs.readFileSync(process.argv[2],"utf-8");
var file=fs.readFileSync(input_file,"utf-8");
//parser
var v;
var items={};
if((v=file.match(new RegExp("(<tr>\\s+<th><a href=\"http://www.minecraftwiki.net/wiki/[\\s\\S]+?</table>\\s+</td>\\s+<td>[\\s\\S]+?</td>\\s+</tr>)","g")))!=null)
// get all the grids
if((v=file.match(new RegExp("(<tr>\\s+<th>\\s+<p><a href=\"http://minecraft.gamepedia.com/" +
"[\\s\\S]+?</table>\\s+</td>\\s+<td>[\\s\\S]*?</td>\\s+</tr>)","g")))!=null)
{
for(var i in v)
{
var w;
if((w=v[i].match(new RegExp("^<tr>\\s+<th><a [\\s\\S]+?>([\\s\\S]+?)</a>[\\s\\S]*?</th>\\s+[\\s\\S]+?<table class=\"grid-Crafting_Table\" cellpadding=\"0\" cellspacing=\"0\">([\\S\\s]+?)</table>[\\S\\s]+?</tr>$")))!=null)
// parse a grid
if((w=v[i].match(new RegExp("^<tr>\\s+<th>\\s+<p><a [\\s\\S]+?>([\\s\\S]+?)</a>(?:[\\s\\S]+?)?</p>[\\s\\S]*?</th>\\s+[\\s\\S]+?" +
"<table class=\"grid-Crafting_Table\" cellpadding=\"0\" cellspacing=\"0\">([\\S\\s]+?)</table>[\\S\\s]+?</tr>$")))!=null)
{
var displayName=w[1];
var shapeResult=w[2];
var shapeLess;
var x,y,z;
var recipe=[];
if((x=shapeResult.match(new RegExp("<td><span class=\"grid\"><span class=\"border\"><span>[\\s\\S]+?</span></span></span></td>","g")))!=null)
if((x=shapeResult.match(new RegExp("<td><span class=\"grid(?: animated)?\">[\\s\\S]+?</span></td>","g")))!=null)
{
for(var j in x)
{
var c;
if((c=x[j].match(new RegExp("^<td><span class=\"grid\"><span class=\"border\"><span>([\\s\\S]+?)</span></span></span></td>$")))!=null)
if((c=x[j].match(new RegExp("^<td><span class=\"grid(?: animated)?\">([\\s\\S]+?)</span></td>$")))!=null)
{
var d;
if(c[1]==="<span class=\"image\">&nbsp;</span>") recipe.push(null);
else if((d=c[1].match(new RegExp("^<span class=\"image\"><a href=\".+?\" title=\"(.+?)\">")))!=null)
if(c[1]==="<br>") recipe.push(null);
else if((d=c[1].match(new RegExp("^(?:<span class=\"item\"><br></span>)*(?:<span class=\"item active\"><br></span>)?(?:<span class=\"item\"><br></span>)*" +
"<span class=\"item(?: active)?\"(?: title=\".+?\")?><a href=\".+?\"><img alt=\"(.+?)\"")))!=null)
{
recipe.push(d[1]);
}
else if((d=c[1].match(new RegExp("^<span class=\"animated\" data-imgs=\"(.+?)\">")))!=null)
{
recipe.push(d[1]);
}
else { console.log("Error 8"); break;}
else { console.log("Error 8"); console.log(c[1]); break;}
}
else { console.log("Error 7"); break;}
}
if(recipe.length!=9) { console.log("Error 9"); console.log(shapeResult); break;}
}
else { console.log("Error 6"); console.log(shapeResult); break;}
if((y=shapeResult.match(new RegExp("<td class=\"shapeless\">([\\S\\s]*?)</td>")))!=null)
Expand All @@ -54,10 +64,10 @@ if((v=file.match(new RegExp("(<tr>\\s+<th><a href=\"http://www.minecraftwiki.net
}
else { console.log("Error 5"); break;}
var result,output;
if((z=shapeResult.match(new RegExp("<td rowspan=\"3\"><span class=\"grid output\"><span class=\"border\"><span>([\\S\\s]+?)</span></span></span></td>")))!=null)
if((z=shapeResult.match(new RegExp("<td rowspan=\"3\"><span class=\"grid(?: animated)? output(?: animated)?\"><span class=\"item(?: active)?\"(?: title=\".+?\")?>([\\S\\s]+?)</span></span></td>")))!=null)
{
var a;
if((a=z[1].match(new RegExp("^<span class=\"image\"><a href=\".+?\" title=\"(.+?)\">")))!=null)
if((a=z[1].match(new RegExp("^<a href=\".+?\"><img alt=\"(.+?)\"")))!=null)
{
result=a[1];
if((a=z[1].match(new RegExp("^<span class=\"number\"><a .+?>([0-9]+)</a></span>")))!=null)
Expand All @@ -68,13 +78,17 @@ if((v=file.match(new RegExp("(<tr>\\s+<th><a href=\"http://www.minecraftwiki.net
}
else if((a=z[1].match(new RegExp("^<span class=\"animated\" data-imgs=\"(.+?)\">")))!=null)
{
var result=a[1];
result=a[1];
}
else { console.log("Error 4"); break;}
else { console.log("Error 4"); console.log(z[1]); break;}
if(output=="" && result=="") { console.log("Error 10"); console.log(z[1]); break;}
}
else { console.log("Error 3"); break;}
else { console.log("Error 3"); console.log(shapeResult); break;}
var item={"result":result,"output":output,"recipe":recipe,"shapeless":shapeLess};
items[result]=item;
//TODO: handles multiple recipes for one output item
if(!items[result])
items[result]=[];
items[result].push(item);
}
else { console.log("Error 2"); console.log(v[i]); break;}
}
Expand All @@ -98,49 +112,55 @@ function separate(s)
var nitems={};
for(var i in items)
{
var results=separate(i);
var recipes=[];
var n=results.length;
for(var j in items[i]["recipe"])
{
if(items[i]["recipe"][j]===null) recipes.push(null);
else
{
var sepa=separate(items[i]["recipe"][j]);
if(sepa.length>n) n=sepa.length;
recipes.push(sepa);
}
}
for(var j=0;j<n;j++)
{
var nresult;
var noutput;
if(results.length>1)
{
var nresultOutput=results[j].split(",");
nresult=nresultOutput[0];
noutput=nresultOutput.length===1 ? 1 : nresultOutput[1];
}
else
{
nresult=results[0];
noutput=items[i]["output"];
}
var nshapeless=items[i]["shapeless"];
var nrecipe=[];
for(var k in recipes)
{
if(recipes[k]===null) nrecipe.push(null);
else if(recipes[k].length===1) nrecipe.push(recipes[k][0]);
else nrecipe.push(recipes[k][j]);
}
var nitem={"output":parseInt(noutput),"recipe":nrecipe,"shapeless":nshapeless ? 1 : 0};
if(nitems[nresult]!==undefined) nitems[nresult].push(nitem);
else nitems[nresult]=[nitem];
}
for(var l in items[i])
{
var results = separate(i);
var recipes = [];
var n = results.length;
for (var j in items[i][l]["recipe"]) {
if (items[i][l]["recipe"][j] === null) recipes.push(null);
else {
var sepa = separate(items[i][l]["recipe"][j]);
if (sepa.length > n) n = sepa.length;
recipes.push(sepa);
}
}
for (var j = 0; j < n; j++) {
var nresult;
var noutput;
if (results.length > 1) {
var nresultOutput = results[j].split(",");
nresult = nresultOutput[0];
noutput = nresultOutput.length === 1 ? 1 : nresultOutput[1];
}
else {
nresult = results[0];
noutput = items[i][l]["output"];
}
var nshapeless = items[i][l]["shapeless"];
var nrecipe = [];
for (var k in recipes) {
if (recipes[k] === null) nrecipe.push(null);
else if (recipes[k].length === 1) nrecipe.push(recipes[k][0]);
else nrecipe.push(recipes[k][j]);
}
var nitem = {"output": parseInt(noutput), "recipe": nrecipe, "shapeless": nshapeless ? 1 : 0};
if (nitems[nresult] !== undefined) nitems[nresult].push(nitem);
else nitems[nresult] = [nitem];
}
}
}
function print(o)


function print(o,file)
{
console.log(JSON.stringify(o,null,2));
fs.writeFile(file, JSON.stringify(o,null,2), function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});
}
print(nitems);

print(nitems,output_file);
32 changes: 22 additions & 10 deletions bin/transform2_recipes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env node
if(process.argv.length!=5)
if(process.argv.length!=6)
{
console.log("Usage : ./transform2_recipes.js <recipes.json> <blocks.json> <items.json> > <recipes2.json>");
console.log("Usage : ./transform2_recipes.js <recipes.json> <blocks.json> <items.json> <recipes2.json>");
process.exit(1);
}

var recipes=require("./"+process.argv[2]);
var blocks=require("./"+process.argv[3]);
var items=require("./"+process.argv[4]);

function print(o)
{
console.log(JSON.stringify(o,null,2));
}
var fs=require('fs');

var recipes=require(process.argv[2]);
var blocks=require(process.argv[3]);
var items=require(process.argv[4]);
var output_file=process.argv[5];


var ids={};
for(var i in blocks) ids[blocks[i].displayName]=i;
Expand Down Expand Up @@ -179,4 +179,16 @@ for(var i in recipes)
nrecipes[i]=anrecipe;
}

print(nrecipes);

function print(o,file)
{
fs.writeFile(file, JSON.stringify(o,null,2), function(err) {
if(err) {
return console.log(err);
}

console.log("The file was saved!");
});
}

print(nrecipes,output_file);
Loading

0 comments on commit f4e8051

Please sign in to comment.