Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added games in - limit 4 per game

  • Loading branch information...
commit a8864079a8ed96f602ddea234ef5784d586db3ac 1 parent 7435e3c
Tom Crinson MrJaba authored
23 controllers/game_controller.rb
View
@@ -1,16 +1,17 @@
-require 'ruby-debug'
class GameController < Cramp::Controller::Websocket
periodic_timer :push_states, :every => 0.05
periodic_timer :push_bombs, :every => 0.05
- periodic_timer :cleanup, :every => 1
+ #periodic_timer :cleanup, :every => 1
on_data :receive_message
class << self
attr_accessor :player_states
attr_accessor :games
attr_accessor :bomb_positions
+ attr_accessor :connection_to_games
end
TIMEOUT = 20
- @games = [Game.new]
+ @games = []
+ @connection_to_games = {}
@player_states = {}
@bomb_positions = {}
@@ -23,7 +24,7 @@ def receive_message(data)
end
def push_states
- data = {:type => 'update_positions', :positions => player_states}.to_json
+ data = {:type => 'update_positions', :positions => game_states}.to_json
render data
end
@@ -37,7 +38,12 @@ def push_bombs
def receive_register(message, null_uuid=nil)
player_uuid = UUID.new.generate
game = GameController.games.last
- player = game.create_player
+ if game.nil? || game.full?
+ game = Game.new
+ GameController.games << game
+ end
+ GameController.connection_to_games[self.object_id] = game
+ player = game.create_player(player_uuid)
GameController.player_states[player_uuid] = player
render ({:type => 'register', :uuid => player_uuid, :colour => player.colour}).to_json
end
@@ -85,11 +91,8 @@ def update_last_message_time(uuid)
end
end
- def player_states
- GameController.player_states.inject({}) do |sum, uuid_player|
- uuid, player = uuid_player
- sum.merge( {uuid => player.to_param } )
- end
+ def game_states
+ GameController.connection_to_games[self.object_id].player_states rescue {}
end
end
1  cramp/controller/body.rb
View
@@ -39,7 +39,6 @@ def schedule_dequeue
EventMachine.next_tick do
next unless body = @queue.shift
-
Array(body).each {|chunk| @body_callback.call(chunk) }
schedule_dequeue unless @queue.empty?
end
14 models/game.rb
View
@@ -6,8 +6,8 @@ def initialize
self.players = []
end
- def create_player
- (players << Player.new(player_colour, spawn_point)).last
+ def create_player(uuid)
+ (players << Player.new(uuid, player_colour, spawn_point)).last
end
def player_colour
@@ -22,5 +22,15 @@ def spawn_point
when 3 then [6,6]
end
end
+
+ def player_states
+ players.inject({}) do |sum, player|
+ sum.merge( {player.uuid => player.to_param } )
+ end
+ end
+
+ def full?
+ players.size == 4
+ end
end
5 models/player.rb
View
@@ -1,7 +1,8 @@
class Player
- attr_accessor :x, :y ,:score, :last_message_time, :colour, :spawn, :state
+ attr_accessor :uuid, :x, :y ,:score, :last_message_time, :colour, :spawn, :state
- def initialize(colour, spawn)
+ def initialize(uuid, colour, spawn)
+ self.uuid = uuid
self.spawn = spawn
self.score = 0
self.last_message_time = Time.now
Please sign in to comment.
Something went wrong with that request. Please try again.