How to implement a multiplayer pacman clone called mongoman using node.js, websockets, html5 and mongodb. There will be a live gaming session (I'm bringing gear to host it)
handles the hiscore list. •Organized using the cluster api in Node.js 0.6.X •1 parent process and X child processes handling game •If a child dies a new one is born Friday, July 6, 12
workers handle the websockets for (var i = 0; i < numCPUs; i++) { var worker = cluster.fork(); worker.on('message', function(msg) { if(msg != null && msg['cmd'] == 'online') { console.log("================================ worker online"); console.dir(msg); } }); } // If the worker thread dies just print it to the console and for a new one cluster.on('death', function(worker) { console.log('worker ' + worker.pid + ' died'); cluster.fork(); }); Friday, July 6, 12
work with the websocket module •Wrote own session handling using capped collection •A bit HACKY but works •Using named cookie to tie sessions together •I should probably fix it Friday, July 6, 12
= connectUtils.parseCookie(request.httpRequest.headers['cookie']); // Grab the session id var sessionId = cookie['connect.sid']; // Grab the username based on the session id and initialize the board state.sessionsCollection.findOne({id:sessionId}, function(err, session) { if(err) throw err; session = typeof session == 'undefined' || session == null ? {} : session; initializeBoard(state, session, self); }) •Uses connect npm package for nice to have cookie parser functions •Using capped collection as well Friday, July 6, 12
{new:true, upsert:false}, function(err, board) { // Code to deal with the board or the non existance of the board } Initializing a board •Using findAndModify to pick a board •Atomic operation to add player to existing board •Process bound Friday, July 6, 12
ghost AI A state machine can also be represented by a table, but in general, the state diagram is more intuitive, making it easier to consider how the system works as a whole. A table can be useful in Friday, July 6, 12
AI A state machine can also be represented by a table, but in general, the state diagram is more intuitive, making it easier to consider how the system works as a whole. A table can be useful in YOU Friday, July 6, 12
garbage collection •“–nouse-idle-notification” •avoid v8 needing to iterate over a massive list of objects that are not collectable •lowers latency and pauses Friday, July 6, 12
the ping time to the server •20ms = 40 ms delivery = 25 fps •135 = 270 ms delivery = 4 fps •Now you know why it’s a WOW EU server or US server Friday, July 6, 12
high connection count •Avoid process switching is possible •Latency is the mind killer •If you users are in Germany have a local server for Germany Keep in mind Friday, July 6, 12
the ping time to the client •Profile both the browser and the server using available tools •Chrome development tools •Collect stats wire usage etc. Profile the game Friday, July 6, 12
as Android •In the near future we can use for real 2+ years I recon •Supported by Firefox, Chrome and Safari and even Opera but not IE (probably NEVER) WebGL and mobile Friday, July 6, 12