Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed issue with not cleaning up disconnects

  • Loading branch information...
commit aabe397059e3d1ee1b08fbc71f76f2092bc0d9f0 1 parent 8b49ccb
@MrJaba MrJaba authored
View
20 controllers/game_controller.rb
@@ -37,11 +37,7 @@ def push_bombs
def receive_register(message, null_uuid=nil)
player_uuid = UUID.new.generate
- game = GameController.games.last
- if game.nil? || game.full?
- game = Game.new
- GameController.games << game
- end
+ game = find_or_create_game
GameController.connection_to_games[self.object_id] = game
player = game.create_player(player_uuid)
GameController.player_states[player_uuid] = player
@@ -77,7 +73,10 @@ def receive_send_reset_state(message, uuid)
def cleanup
states = GameController.player_states.dup
states.each_pair do |uuid, state|
- GameController.player_states.delete(uuid) if timed_out?(state)
+ if timed_out?(state)
+ GameController.player_states[uuid].game.delete_player(uuid)
+ GameController.player_states.delete(uuid)
+ end
end
end
@@ -95,4 +94,13 @@ def game_states
GameController.connection_to_games[self.object_id].player_states rescue {}
end
+ def find_or_create_game
+ game = GameController.games.select{|game| !game.full? }.first
+ if game.nil?
+ game = Game.new
+ GameController.games << game
+ end
+ game
+ end
+
end
View
6 models/game.rb
@@ -7,7 +7,11 @@ def initialize
end
def create_player(uuid)
- (players << Player.new(uuid, player_colour, spawn_point)).last
+ (players << Player.new(uuid, player_colour, spawn_point, self)).last
+ end
+
+ def delete_player(uuid)
+ players.delete_if{|player| player.uuid == uuid}
end
def player_colour
View
5 models/player.rb
@@ -1,12 +1,13 @@
class Player
- attr_accessor :uuid, :x, :y ,:score, :last_message_time, :colour, :spawn, :state
+ attr_accessor :uuid, :x, :y ,:score, :last_message_time, :colour, :spawn, :state, :game
- def initialize(uuid, colour, spawn)
+ def initialize(uuid, colour, spawn, game)
self.uuid = uuid
self.spawn = spawn
self.score = 0
self.last_message_time = Time.now
self.colour = colour
+ self.game = game
respawn
end
Please sign in to comment.
Something went wrong with that request. Please try again.