Presentation given to the London Ruby User Group (LRUG) and the Ipswich Ruby User Group (IPRUG) on a simple project I made called IttyBittyBoom.com which is an HTML5 based bomberman clone.
15. //Trigger the bombdrop on the game client MrJaba.Bomberman.GameClient.trigger( 'send_bomb_drop' ); //Send the Bomb drop message socket. send ( { "type" : "send_bomb_drop" , "uuid" : "5a5c0a80-844f-012d-5d1f-001b63957646" , "data" :{ "x" : "0" , "y" : "90" }} ); //The server will update it's map of players to bombs and issue an update every 5msec {type: "update_bombs" , positions:{ "7fd93b20-8465-012d-5d20-001b63957646" :{ x: "0" y: "0" } }} //The socket receives all messages through this: socket . onmessage = function ( evt ){ var data = JSON. parse (evt. data ) handleEvent ( data[ 'type' ], data ); };
16. updateBombPositions : function ( positions ){ $ .each(positions, function (bombId, bomb){ if ( isNewBomb(bombId) ){ MrJaba.Bomberman.bombs[bombId] = new Bomb (bombId, bomb) } }) }, //Event Handling Code calls this
18. //tick tick...boom MrJaba.Bomberman.detonate( this , this .getTileX(), this .getTileY()); detonate : function ( bomb, tileX, tileY ){ delete MrJaba.Bomberman.bombs[bomb.getId()]; MrJaba.Bomberman.GameClient.trigger( 'send_bomb_detonate' , bomb.getId()); bomb.toExplosion(); },
19. Apparently Explosions Are...Unpleasant sendHotBurningDeath : function (){ var alreadyKilled = this .killed; this .tilesAround( this .getTileX(), this .getTileY(), function (tileX, tileY, direction, radius){ MrJaba.Bomberman.killPlayersAt( tileX, tileY, alreadyKilled ); }); },
26. class GameController < Cramp::Controller::Websocket on_data :receive_message def receive_message ( data ) message = JSON .parse(data) type = message[ 'type' ] uuid = message[ 'uuid' ] if Game .player_in_game?(uuid) || type == "register" update_last_message_time(uuid) self .send( "receive_ #{type} " , message, uuid) end end end
27. Example! { "type" : "send_bomb_drop" , "uuid" : "4bba07e0-846c-012d-5d29-001b63957646" , "data" :{ "x" : "0" , "y" : "0" }} Client: def receive_message ( data ) message = JSON .parse(data) type = message[ 'type' ] uuid = message[ 'uuid' ] if Game .player_in_game?(uuid) || type == "register" update_last_message_time(uuid) self .send( "receive_ #{type} " , message, uuid) end end Server: def receive_send_bomb_drop ( message, uuid ) position = { :x => message[ 'data' ][ 'x' ], :y => message[ 'data' ][ 'y' ]} Game .bomb_positions[uuid] = position if ! uuid.nil? end
28. Again and Again and Again Recurring Actions are super easy in Cramp
29. periodic_timer :push_states , :every => 0.05 def push_states data = { :type => 'update_positions' , :positions => game_states_for_connection}.to_json render data end def push_bombs if Game .bomb_positions.size > 0 data = { :type => 'update_bombs' , :positions => Game .bomb_positions}.to_json render data end end periodic_timer :push_bombs , :every => 0.05