Hokay. So, organization, now that I've had time to think about it a bit more:
I would, first, have an instantiatable class called something like MCBufferReader. This class has methods to read Minecraft's data type, and after each call, saves the byte offset where it stopped reading. So it has readLong, readString, readInt, etc etc etc, abstracting out alll of the byte-level work.
Then you have a map of every single event type to the data associated with it. Something like (and this packet is TOTALLY made up):
{
0x02: {
event: "login",
handler: Player,
fields: [
{ username: "string" },
{ loginTime: "long" }
]
}
}
Now, when a 0x02 packet comes in, you pull it out of your map, and you feed it to MCBufferReader. MCBufferReader iterates through the array of names/types, and reads each one from the buffer in order. At the end of it, it returns a javascript object like this:
{
username: "Firecat",
loginTime: 56782194823
}
The input processor tacks on the event name (and any other metadata you might need) so it becomes this:
{
event: "login",
fields: {
username: "Firecat",
loginTime: 56782194823
}
}
And now it calls Player.handle(input). Player is the function stored in 'handler' above, input is the object in that last code block.
Bam. Instant sexiness. Now each of your main objects has a handle() method that looks at the event name and dispatches accordingly. OR, you could put a function in each main object , with the same name as any event it can handle. Then you just call
this[handler][event](obj);
and everything is even more automated.
Of course, that assumes that each event only needs to be consumed by one object. If an event occurs that should be observed by the Player, and everything in an NPCs array, and maybe the World object, then you'll want to use the Event Listener pattern and call something like
this.fire(event, data);
. Then you can use Node.js's
EventEmitter object to make your life smooth and easy. Actually, now that I'm thinking about it more, that might even be the better way to do it regardless. Your call