Class: Manufactured::Commands::Mining

Inherits:
Omega::Server::Command show all
Includes:
Omega::Server::CommandHelpers
Defined in:
lib/manufactured/commands/mining.rb

Overview

Represents action of one Ship mining a Cosmos::Resource

Invokes various Manufactured::Callback handlers upon various events.

The callback events/types invoked include:

  • 'mining_stopped' - invoked when miner stops mining with event, stopped reason, ship, and resource source as params. Reasons include:

** 'mining_distance_exceeded' ** 'ship_cargo_full' ** 'ship_docked' ** 'resource_depleted'

  • 'resource_collected' - invoked when miner collects the resource from the source, with event, miner, resource source, and quantity mined during this operation as params

Instance Attribute Summary collapse

Attributes inherited from Omega::Server::Command

#added_at, #exec_rate, #hooks, #last_ran_at, #node, #ran_first_hooks, #registry

Instance Method Summary collapse

Methods included from Omega::Server::CommandHelpers

#invoke, #retrieve, #run_callbacks, #update_registry

Methods inherited from Omega::Server::Command

#cmd_json, json_create, #run_hooks, #to_s

Constructor Details

#initialize(args = {}) ⇒ Mining

Manufactured::Commands::Mining initializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize mining command with

Options Hash (args):


85
86
87
88
89
90
91
# File 'lib/manufactured/commands/mining.rb', line 85

def initialize(args = {})
  attr_from_args args, :ship  => nil,
                       :resource => nil

  super(args)
  @error = false
end

Instance Attribute Details

#resourceObject

Cosmos::Resource being mined


30
31
32
# File 'lib/manufactured/commands/mining.rb', line 30

def resource
  @resource
end

#shipObject

Mining Ship ship


27
28
29
# File 'lib/manufactured/commands/mining.rb', line 27

def ship
  @ship
end

Instance Method Details

#after_hookObject


123
124
125
126
127
# File 'lib/manufactured/commands/mining.rb', line 123

def after_hook
  # update ship and resources
  update_registry(@ship)
  invoke 'cosmos::set_resource', @resource
end

#before_hookObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/manufactured/commands/mining.rb', line 103

def before_hook
  # update ship location & cosmos resource/entity
  @ship = retrieve(@ship.id)

  # update location from motel
  @ship.location = invoke 'motel::get_location', 'with_id', @ship.location.id

   begin
     @resource = invoke 'cosmos::get_resource', @resource.id
   rescue Exception => e
     # if any problems retrieving resource, invalidate command
     @resource.quantity = 0
     @error = true
   end

   # retrieve entity regardless of resource retrieval errs
   #   (for use in last_hook below)
   @resource.entity = invoke 'cosmos::get_entity', 'with_id', @resource.entity_id
end

#first_hookObject


98
99
100
101
# File 'lib/manufactured/commands/mining.rb', line 98

def first_hook
  @ship.start_mining(@resource)
  update_registry(@ship)
end

#idObject

Return the unique id of this mining command.

Currently a ship may only mine one source at a time, TODO incorporate multiple resources into this


40
41
42
43
# File 'lib/manufactured/commands/mining.rb', line 40

def id
  id = @ship.nil? ? "" : @ship.id.to_s
  "mining-cmd-#{id}"
end

#last_hookObject


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/manufactured/commands/mining.rb', line 129

def last_hook
  @ship.stop_mining

  reason = ''
  r = gen_resource

  # ship & resource are too far apart or in different systems
  if (@ship.location.parent_id != r.entity.location.parent_id ||
     (@ship.location - r.entity.location) > @ship.mining_distance)
    reason = 'mining_distance_exceeded'

  # ship is at max capacity
  elsif (@ship.cargo_quantity + r.quantity) > @ship.cargo_capacity
    reason = 'ship_cargo_full'

  # ship has become docked
  elsif @ship.docked?
    reason = 'ship_docked'

  elsif r.quantity == 0
    reason = 'resource_depleted'

  #else
  #  reason = 'unknown' ?
  end

  ::RJR::Logger.debug "ship #{@ship.id} cannot continue mining due to: #{reason}"
  run_callbacks @ship, 'mining_stopped', @resource, reason
end

#processes?(entity) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/manufactured/commands/mining.rb', line 32

def processes?(entity)
  entity.is_a?(Manufactured::Ship) && entity.id == ship.id
end

#remove?Boolean

Returns:

  • (Boolean)

193
194
195
196
197
198
# File 'lib/manufactured/commands/mining.rb', line 193

def remove?
  # remove if we cannot mine anymore
  return true if @error
  r = gen_resource
  !@ship.can_mine?(r) || !@ship.can_accept?(r)
end

#run!Object


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/manufactured/commands/mining.rb', line 165

def run!
  super
  ::RJR::Logger.debug "invoking mining command #{@ship.id} -> #{@resource}"

  r = gen_resource
  removed_resource = false
  resource_transferred = false
  begin
    # TODO make this variable by including a randomized 'inefficiency' factor
    @resource.quantity -= r.quantity
    removed_resource = true
    @ship.add_resource r
    resource_transferred = true
  rescue Exception => e
  ensure
    @resource.quantity += r.quantity if  removed_resource &&
                                        !resource_transferred
  end

  # run post-mining callbacks and update user attributes
  if resource_transferred
    run_callbacks(@ship, 'resource_collected',
                  @resource, r.quantity)
    invoke 'users::update_attribute', @ship.user_id,
            Users::Attributes::ResourcesCollected.id, r.quantity
  end
end

#should_run?Boolean

Returns:

  • (Boolean)

159
160
161
162
163
# File 'lib/manufactured/commands/mining.rb', line 159

def should_run?
  return false if @error
  r = gen_resource
  super && @ship.can_mine?(r) && @ship.can_accept?(r)
end

#to_json(*a) ⇒ Object

Convert command to json representation and return it


201
202
203
204
205
206
207
208
# File 'lib/manufactured/commands/mining.rb', line 201

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      {:ship => ship,
       :resource => resource}.merge(cmd_json)
  }.to_json(*a)
end

#update(cmd) ⇒ Object


93
94
95
96
# File 'lib/manufactured/commands/mining.rb', line 93

def update(cmd)
  update_from(cmd, :resource)
  super(cmd)
end