Last active
September 3, 2015 21:25
-
-
Save rom1504/473596128828a684471e to your computer and use it in GitHub Desktop.
remove names from protocol.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var proto=require("./../../enums/protocol"); | |
var fs=require("fs"); | |
function tranformProtocol(protocol) | |
{ | |
return {"types":transformTypesObject(protocol.types),"states":transformStates(protocol.states)}; | |
} | |
function transformStates(states) | |
{ | |
var transformedStates = Object | |
.keys(states) | |
.reduce(function(transformedStates, state) { | |
transformedStates[state] = Object | |
.keys(states[state]) | |
.reduce(function(stateO, direction) { | |
stateO[direction] = Object | |
.keys(states[state][direction]) | |
.reduce(function(packetsO, packetName) { | |
packetsO[states[state][direction][packetName].id] = | |
transformPacket(states[state][direction][packetName]); | |
return packetsO; | |
}, {}); | |
return stateO; | |
}, {}); | |
return transformedStates; | |
}, {}); | |
transformedStates=reorder(["handshaking","status","login","play"],transformedStates); | |
return transformedStates; | |
} | |
function reorder (order, obj) { | |
return order.reduce (function (rslt, prop) { | |
rslt[prop] = obj[prop]; | |
return rslt; | |
}, {}); | |
} | |
function write(protocol){ | |
fs.writeFile("../../enums/protocol.json", JSON.stringify(protocol,null,2)); | |
} | |
function transformPacket(packet) | |
{ | |
return transformFields(packet["fields"]); | |
} | |
function transformFields(fields) | |
{ | |
return fields.map(function(field){return transformField(field);}); | |
} | |
function transformField(field) | |
{ | |
return transformType(field.type); | |
} | |
function transformType(type) { | |
if(type[0] == "container") | |
return tranformContainer(type); | |
if(type[0] == "buffer") | |
return transformBuffer(type); | |
if(type[0] == "switch") | |
return transformSwitch(type); | |
if(type[0]=="array") | |
return transformArray(type); | |
if(type[0]=="option") | |
return transformOption(type); | |
return type; | |
} | |
function tranformContainer(type) | |
{ | |
return ["container", transformFields(type[1])]; | |
} | |
function transformBuffer(type) | |
{ | |
return ["buffer", {"countType": transformType(type[1]["countType"])}]; | |
} | |
function transformSwitch(type) | |
{ | |
return ["switch", { | |
"compareTo":"too lazy for this", | |
"fields":transformTypesObject(type[1]["fields"]), | |
"default":type[1]["default"] ? transformType(type[1]["default"]) : type[1]["default"] | |
}]; | |
} | |
function transformTypesObject(typesObject) | |
{ | |
return Object.keys(typesObject).reduce(function(acc,key){ | |
acc[key]=transformType(typesObject[key]); | |
return acc; | |
},{}); | |
} | |
function transformArray(type) | |
{ | |
return ["array", type[1]["count"] ? "too lazy for this" : { | |
"type":transformType(type[1]["type"]), | |
"countType":transformType(type[1]["countType"]) | |
}]; | |
} | |
function transformOption(type) | |
{ | |
return ["option",transformType(type[1])]; | |
} | |
write(tranformProtocol(proto)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment