Class: GameMachine::Actor::Base Abstract

Inherits:
JavaLib::GameActor
  • Object
show all
Defined in:
server/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


53
54
55
56
57
58
# File 'server/lib/game_machine/actor/base.rb', line 53

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

.aspectsObject


43
44
45
# File 'server/lib/game_machine/actor/base.rb', line 43

def aspects
  @aspects ||= []
end

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

Find a local actor by name

Returns:


66
67
68
# File 'server/lib/game_machine/actor/base.rb', line 66

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://[email protected]


71
72
73
74
# File 'server/lib/game_machine/actor/base.rb', line 71

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:


93
94
95
# File 'server/lib/game_machine/actor/base.rb', line 93

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:


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

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:


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

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

.hashring(name) ⇒ Object


60
61
62
# File 'server/lib/game_machine/actor/base.rb', line 60

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

.local_path(name) ⇒ Object


97
98
99
# File 'server/lib/game_machine/actor/base.rb', line 97

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

.loggerObject


22
23
24
# File 'server/lib/game_machine/actor/base.rb', line 22

def logger
  @logger ||= LoggerFactory.getLogger(self.name)
end

.model_filter(message) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'server/lib/game_machine/actor/base.rb', line 101

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

.player_controllerObject


38
39
40
# File 'server/lib/game_machine/actor/base.rb', line 38

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


33
34
35
36
# File 'server/lib/game_machine/actor/base.rb', line 33

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

Instance Method Details

#on_receive(message) ⇒ Object


148
149
150
# File 'server/lib/game_machine/actor/base.rb', line 148

def on_receive(message)
  unhandled(message)
end

#onReceive(message) ⇒ Object

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


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

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


138
139
140
141
# File 'server/lib/game_machine/actor/base.rb', line 138

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

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


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'server/lib/game_machine/actor/base.rb', line 156

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

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


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'server/lib/game_machine/actor/base.rb', line 172

def schedule_message_once(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_once(duration, get_self, message, dispatcher, nil)
end

#senderObject


152
153
154
# File 'server/lib/game_machine/actor/base.rb', line 152

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