Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added player timeouts

  • Loading branch information...
commit de1fb578bf02c2da0349acb8538dc1f4e7e4ffda 1 parent d5f65c7
@MrJaba MrJaba authored
View
21 controllers/game_controller.rb
@@ -2,6 +2,7 @@
class GameController < Cramp::Controller::Websocket
periodic_timer :push_states, :every => 0.1
periodic_timer :push_bombs, :every => 0.1
+ periodic_timer :cleanup, :every => 1
on_data :receive_message
class << self
attr_accessor :player_states
@@ -14,6 +15,7 @@ def receive_message(data)
message = JSON.parse(data)
type = message['type']
uuid = message['uuid']
+ update_last_message_time(uuid)
self.send("receive_#{type}", message, uuid)
end
@@ -31,7 +33,7 @@ def push_bombs
def receive_register(message, uuid=nil)
player_uuid = UUID.new.generate
- GameController.player_states[player_uuid] = {:x => 0, :y => 0, :score => 0}
+ GameController.player_states[player_uuid] = {:x => 0, :y => 0, :score => 0, :last_message_time => Time.now}
render ({:type => 'uuid', :uuid => player_uuid, :class_id => self.object_id}).to_json
end
@@ -63,4 +65,21 @@ def restart_player(uuid)
GameController.player_states[uuid][:state] = 'restart'
end
+ def cleanup
+ states = GameController.player_states.dup
+ states.each_pair do |uuid, state|
+ GameController.player_states.delete(uuid) if timed_out?(state)
+ end
+ end
+
+ def timed_out?(player_state)
+ (Time.now - player_state[:last_message_time]) > 20
+ end
+
+ def update_last_message_time(uuid)
+ if( uuid && GameController.player_states[uuid] )
+ GameController.player_states[uuid][:last_message_time] = Time.now
+ end
+ end
+
end
View
6 public/javascripts/game.js
@@ -181,6 +181,12 @@ MrJaba.Bomberman = function(){
var li_class = ( uuid === MrJaba.Bomberman.uuid ) ? "me" : "opponent"
$("#scores").prepend("<li class='"+li_class+"'><span class='score'>"+position.score+"</span><span class='uuid'>"+uuid+"</span></li>");
})
+ var tmpOpponents = MrJaba.Bomberman.opponents;
+ $.each(tmpOpponents, function(uuid, position){
+ if(positions[uuid] === undefined){
+ delete MrJaba.Bomberman.opponents[uuid];
+ }
+ })
sortScores();
},
View
2  public/javascripts/game_client.js
@@ -11,7 +11,7 @@ var GameClient = function(){
socket.onmessage = function(evt){
var data = JSON.parse(evt.data)
handleEvent( data['type'], data );
- };
+ };
}
var notifyPlayerMove = function(){
Please sign in to comment.
Something went wrong with that request. Please try again.