Skip to content

Commit

Permalink
Add items.json metadata variations (see #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed May 2, 2015
1 parent 95f19e5 commit 46cee9c
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 24 deletions.
34 changes: 34 additions & 0 deletions bin/wiki_extractor/dvt_template_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var WikiTextParser = require('./wikitext_parser');

var wikiTextParser = new WikiTextParser();

module.exports={parseDvt:parseDvt};

function parseDvt(text)
{
var dv=0;
return text
.split("\n")
.filter(function(element){return element!="" && element.indexOf("{")!=-1 && element.indexOf("On ")==-1;}) // get rid of the unrelated beginning and ending lines
.map(function(element){
var r=wikiTextParser.parseTemplate(element);
if(r==null || ["dvt"].indexOf(r.template)==-1) {
console.log(r);
console.log("problem with parsing template "+element);
return null;
}
return r;
})
.filter(function(r){return r!=null && r.simpleParts.length!=0 && r.simpleParts[0]!="";})
.map(function(r){
dv="dv" in r.namedParts ? parseInt(r.namedParts["dv"]) : dv;
var r={
dv:dv,
description: r.simpleParts[0],
spritetype:"spritetype" in r.namedParts ? r.namedParts["spritetype"] : "block",
sprite:"sprite" in r.namedParts ? r.namedParts["sprite"] : "air"
};
dv++;
return r;
});
}
83 changes: 65 additions & 18 deletions bin/wiki_extractor/item_extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,38 @@ var fs = require('fs');

var wikiTextParser = new WikiTextParser();
var id_table_parser=require('./id_table_template_parser.js');
var dvt_parser=require('./dvt_template_parser.js');
var infobox_field_parser=require('./infobox_field_parser.js');


writeAllItems();
//getDataValue("Coal/DV");
//getDataValue("Stone/DV");



function writeAllItems()
{
async.waterfall([
function(cb){id_table_parser.parseDataValues("Data_values/Item_IDs",cb)},
addVariations,
//function(items,cb){console.log(JSON.stringify(items,null,2));cb(null,items);}
itemsToFullItems
],
indexWrite
);
}

function indexWrite(err,fullItems){
var items={};
for(var i in fullItems)
{
items[fullItems[i]["id"]]=fullItems[i];
}
fs.writeFile("../../enums/items.json", JSON.stringify(items,null,2));
}


// http://minecraft.gamepedia.com/Template:Item
function itemInfobox(page,cb)
{
Expand All @@ -25,6 +55,39 @@ function itemInfobox(page,cb)
});
}

function getDataValue(page,cb)
{
wikiTextParser.getArticle(page,function(err,data) {
if (err) {
cb(err);
return;
}
if (data.indexOf("dvt") == -1) {
cb(new Error("not a dvt page"));
return;
}
var table=dvt_parser.parseDvt(data);
cb(null,table.map(function(fields){
return {
"metadata":fields["dv"],
"displayName":fields["description"]
};
}));
})
}

function addVariations(items,cb)
{
async.map(items,function(item,cb){
getDataValue(item["link"]+"/DV",function(err,table){
if(!err) item["variations"]=table;
cb(null,item);
});
},function(err,results){
cb(null,results);
});
}

function itemsToFullItems(items,cb)
{
async.map(items,function(item,cb){
Expand All @@ -33,31 +96,15 @@ function itemsToFullItems(items,cb)
"id":item["id"],
"displayName":item["displayName"],
"stackSize":data!=null && "stackSize" in data ? data["stackSize"] : null,
"name":item["name"]
"name":item["name"],
"variations":item["variations"]
});
});
},function(err,results){
cb(null,results);
});
}

function writeAllItems()
{
async.waterfall([
function(cb){id_table_parser.parseDataValues("Data_values/Item_IDs",cb)},
itemsToFullItems
],
function(err,fullItems){
var items={};
for(var i in fullItems)
{
items[fullItems[i]["id"]]=fullItems[i];
}
fs.writeFile("../../enums/items.json", JSON.stringify(items,null,2));
});
}

writeAllItems();

// functions that aren't used in the end
function items(cb)
Expand Down
5 changes: 3 additions & 2 deletions bin/wiki_extractor/wikitext_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ WikiTextParser.prototype.getArticle=function(title,cb)
var self=this;
this.client.getArticle(title, function(err, data) {
if (err || !data) {
console.log("error in page "+title);
console.error(err);
//console.log("error in page "+title);
//console.error(err);
cb(err ? err : new Error("can't get the data"));
return;
}
// somehow use ...&redirects=&... to silently follow redirects
Expand Down
108 changes: 104 additions & 4 deletions enums/items.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@
"id": 263,
"displayName": "Coal",
"stackSize": 64,
"name": "coal"
"name": "coal",
"variations": [
{
"metadata": 0,
"displayName": "Coal"
},
{
"metadata": 1,
"displayName": "Charcoal"
}
]
},
"264": {
"id": 264,
Expand Down Expand Up @@ -399,7 +409,17 @@
"id": 322,
"displayName": "Golden Apple",
"stackSize": 64,
"name": "golden_apple"
"name": "golden_apple",
"variations": [
{
"metadata": 0,
"displayName": "Golden Apple"
},
{
"metadata": 1,
"displayName": "Enchanted Golden Apple"
}
]
},
"323": {
"id": 323,
Expand Down Expand Up @@ -807,7 +827,65 @@
"id": 390,
"displayName": "Flower Pot",
"stackSize": 64,
"name": "flower_pot"
"name": "flower_pot",
"variations": [
{
"metadata": 0,
"displayName": "Empty"
},
{
"metadata": 1,
"displayName": "Poppy"
},
{
"metadata": 2,
"displayName": "Dandelion"
},
{
"metadata": 3,
"displayName": "Oak sapling"
},
{
"metadata": 4,
"displayName": "Spruce sapling"
},
{
"metadata": 5,
"displayName": "Birch sapling"
},
{
"metadata": 6,
"displayName": "Jungle sapling"
},
{
"metadata": 7,
"displayName": "Red mushroom"
},
{
"metadata": 8,
"displayName": "Brown mushroom"
},
{
"metadata": 9,
"displayName": "Cactus"
},
{
"metadata": 10,
"displayName": "Dead bush"
},
{
"metadata": 11,
"displayName": "Fern"
},
{
"metadata": 12,
"displayName": "Acacia sapling"
},
{
"metadata": 13,
"displayName": "Dark oak sapling"
}
]
},
"391": {
"id": 391,
Expand Down Expand Up @@ -849,7 +927,29 @@
"id": 397,
"displayName": "Mob head",
"stackSize": 64,
"name": "skull"
"name": "skull",
"variations": [
{
"metadata": 0,
"displayName": "Skeleton Skull"
},
{
"metadata": 1,
"displayName": "Wither Skeleton Skull"
},
{
"metadata": 2,
"displayName": "Zombie Head"
},
{
"metadata": 3,
"displayName": "Head"
},
{
"metadata": 4,
"displayName": "Creeper Head"
}
]
},
"398": {
"id": 398,
Expand Down

0 comments on commit 46cee9c

Please sign in to comment.