Skip to content

Instantly share code, notes, and snippets.

@rom1504
Last active September 3, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rom1504/473596128828a684471e to your computer and use it in GitHub Desktop.
Save rom1504/473596128828a684471e to your computer and use it in GitHub Desktop.
remove names from protocol.json
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