Class: GameMachine::Actor::Base Abstract

Inherits:
JavaLib::GameActor
  • Object
show all
Defined in:
lib/game_machine/actor/base.rb

Overview

This class is abstract.

All game actors inherit fromm this class

Constant Summary collapse

ON_RECEIVE_HOOKS =
{}
@@player_controller =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.aspect(new_aspects) ⇒ Object

Sets the message types that this actor knows about. Can be called multiple times. If passed an array of more then one message type, both message types will need to be present on an entity before the system will route the entity to the actor.

messages will be routed to actors based on the aspects it has



45
46
47
48
49
50
# File 'lib/game_machine/actor/base.rb', line 45

def aspect(new_aspects)
  aspects << new_aspects
  unless Application.registered.include?(self)
    Application.register(self)
  end
end

.aspectsObject



35
36
37
# File 'lib/game_machine/actor/base.rb', line 35

def aspects
  @aspects ||= []
end

.find(name = self.name) ⇒ Actor::Ref

Find a local actor by name

Returns:



58
59
60
# File 'lib/game_machine/actor/base.rb', line 58

def find(name=self.name)
  Actor::Ref.new(local_path(name),name)
end

.find_by_address(address, name = self.name) ⇒ Object

find using fully qualified address, ie akka://cluster@ …



63
64
65
66
# File 'lib/game_machine/actor/base.rb', line 63

def find_by_address(address,name=self.name)
  path = "#{address}#{local_path(name)}"
  Actor::Ref.new(path,name)
end

.find_distributed(id, name = self.name) ⇒ Actor::Ref

Returns an actor ref from the distributed ring of actors based on a consistent hashing of the id. The actor returned can be from any server in the cluster

Returns:



85
86
87
# File 'lib/game_machine/actor/base.rb', line 85

def find_distributed(id,name=self.name)
  Actor::Ref.new(distributed_path(id, name),name)
end

.find_distributed_local(id, name = self.name) ⇒ Actor::Ref

Returns a local actor ref from the distributed ring of actors based on a consistent hashing of the id.

Returns:



77
78
79
# File 'lib/game_machine/actor/base.rb', line 77

def find_distributed_local(id,name=self.name)
  Actor::Ref.new(local_distributed_path(id, name),name)
end

.find_remote(server, name = self.name) ⇒ Actor::Ref

Find a remote actor by name

Returns:



70
71
72
# File 'lib/game_machine/actor/base.rb', line 70

def find_remote(server,name=self.name)
  Actor::Ref.new(remote_path(server,name),name)
end

.hashring(name) ⇒ Object



52
53
54
# File 'lib/game_machine/actor/base.rb', line 52

def hashring(name)
  JavaLib::Hashring.get_hashring(name)
end

.local_path(name) ⇒ Object



89
90
91
# File 'lib/game_machine/actor/base.rb', line 89

def local_path(name)
  "/user/#{name}"
end

.model_filter(message) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/game_machine/actor/base.rb', line 93

def model_filter(message)
  if message.is_a?(MessageLib::Entity) && message.has_json_entity
    # Don't convert outgoing messages
    if message.send_to_player
      message
    else
      model = Model.from_entity(message)
      if message.has_player
        model.player_id = message.player.id
      end
      return model
    end
  end
  message
end

.player_controllerObject



30
31
32
# File 'lib/game_machine/actor/base.rb', line 30

def player_controller
  @@player_controller
end

.set_player_controllerObject

Sets the system wide player controller class. When a player logs in, a player controller with this class will be created. The system notifies the player controller when various player lifecycle events happen.

This should only be called on subclasses, never on the Actor base class



25
26
27
28
# File 'lib/game_machine/actor/base.rb', line 25

def set_player_controller
  @@player_controller = self
  GameMachine.logger.info("Player controller set to #{self.name}")
end

Instance Method Details

#on_receive(message) ⇒ Object



140
141
142
# File 'lib/game_machine/actor/base.rb', line 140

def on_receive(message)
  unhandled(message)
end

#onReceive(message) ⇒ Object

So we can hook into message passing for our own filters and the like



136
137
138
# File 'lib/game_machine/actor/base.rb', line 136

def onReceive(message)
  receive_message(message)
end

#receive_message(message) ⇒ Object

This indirection is primarily because Akka’s test actors hide onReceive, so in tests we need to call receive_message



130
131
132
133
# File 'lib/game_machine/actor/base.rb', line 130

def receive_message(message)
  message = self.class.model_filter(message)
  on_receive(message)
end

#schedule_message(message, update_interval, unit = :ms) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/game_machine/actor/base.rb', line 148

def schedule_message(message,update_interval,unit=:ms)
  if unit == :seconds
    unit = java.util.concurrent.TimeUnit::SECONDS
  elsif unit == :ms
    unit = java.util.concurrent.TimeUnit::MILLISECONDS
  else
    GameMachine.logger.error "Invalid unit argument for schedule_message (#{unit})"
    return
  end

  duration = GameMachine::JavaLib::Duration.create(update_interval, unit)
  scheduler = get_context.system.scheduler
  dispatcher = get_context.system.dispatcher
  scheduler.schedule(duration, duration, get_self, message, dispatcher, nil)
end

#senderObject



144
145
146
# File 'lib/game_machine/actor/base.rb', line 144

def sender
  Actor::Ref.new(get_sender)
end