lib/game_machine/handlers/game.rb in game_machine-0.0.11 vs lib/game_machine/handlers/game.rb in game_machine-1.0.2

- old
+ new

@@ -1,12 +1,13 @@ module GameMachine module Handlers class Game < Actor::Base include Commands - attr_reader :destinations + attr_reader :destinations, :game_message_routes def post_init(*args) + @game_message_routes = Routes.instance.game_message_routes @destinations = {} end def on_receive(message) @@ -25,10 +26,34 @@ entities.each do |entity| if entity.save entity = GameSystems::ObjectDbProxy.save_entity(entity) end + if entity.has_game_messages + entity.game_messages.get_game_message_list.each do |game_message| + if game_message.has_destination_id + destination = game_message.destination_id + elsif game_message.has_destination + destination = game_message.destination + else + GameMachine.logger.warn "Unable to find destination for game message, skipping" + next + end + + if route = game_message_routes.fetch(destination) + game_message.set_player_id(entity.player.id) + if route[:distributed] + Actor::Base.find_distributed(entity.player.id,route[:to]).tell(game_message) + else + Actor::Base.find(route[:to]).tell(game_message) + end + else + GameMachine.logger.warn "No route for destination #{destination}" + end + end + end + if entity.has_destination unless actor_ref = destinations.fetch(entity.destination,nil) destination = entity.destination.gsub('/','::') actor_ref = Actor::Base.find(destination) destinations[entity.destination] = actor_ref @@ -37,10 +62,10 @@ actor_ref.tell(entity) GameMachine.logger.debug("RouteToDestination: #{entity.id} #{destination}") next end - # TODO: Optimize this + # DEPRECATED - convert to named routes as we do for game messages component_names = entity.component_names GameMachine.logger.debug("Dispatch: #{entity} #{component_names.to_a.inspect}") next if component_names.empty? Application.registered.each do |klass| dispatched = false