Class: Manufactured::Commands::Construction

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

Overview

Represents action of one Station constructing another manufactured entity.

Checking of resources & the actual construction (eg call to station.construct) should be done prior to invoking this, this will generate a construction delay for a parameterized duration, after which the location will be added to the registry via call to manufactured::create_entity

Invokes various Manufactured::Callback handlers upon various events. The callback events/types invoked include:

  • 'partial_construction' - invoked upon every iteration of the construction cycle w/ the given fraction of construction completed

  • 'construction_complete' - invoked when construction is fully completed

  • 'construction_failed' - invoked if entity could not be created for some reason

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

#after_hook, #before_hook, #cmd_json, #first_hook, json_create, #last_hook, #run_hooks, #to_s

Constructor Details

#initialize(args = {}) ⇒ Construction

Manufactured::Commands::Construction initializer

Options Hash (args):


58
59
60
61
62
63
64
65
# File 'lib/manufactured/commands/construction.rb', line 58

def initialize(args = {})
  attr_from_args args, :station   => nil,
                       :entity    => nil,
                       :completed => false,
                       :start_time => nil
  @start_time = Time.parse(@start_time) if @start_time.is_a?(String)
  super(args)
end

Instance Attribute Details

#completedObject

Bool indicating if construction is completed


37
38
39
# File 'lib/manufactured/commands/construction.rb', line 37

def completed
  @completed
end

#entityObject

entity constructed


34
35
36
# File 'lib/manufactured/commands/construction.rb', line 34

def entity
  @entity
end

#start_timeObject

Time construction was started


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

def start_time
  @start_time
end

#stationObject

Station Station station


31
32
33
# File 'lib/manufactured/commands/construction.rb', line 31

def station
  @station
end

Instance Method Details

#idObject

Return the unique id of this construction command.


43
44
45
46
47
# File 'lib/manufactured/commands/construction.rb', line 43

def id
  sid = @station.nil? ? "" : @station.id
  eid = @entity.nil? ? "" : @entity.id
  "#{sid}-#{eid}"
end

#processes?(tentity) ⇒ Boolean


49
50
51
# File 'lib/manufactured/commands/construction.rb', line 49

def processes?(tentity)
  tentity.is_a?(Manufactured::Station) && tentity.id == station.id
end

#remove?Boolean


110
111
112
113
# File 'lib/manufactured/commands/construction.rb', line 110

def remove?
  # remove if completed
  self.completed
end

#run!Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/manufactured/commands/construction.rb', line 76

def run!
  ::RJR::Logger.debug "invoking construction cycle #{@station.id} -> #{@entity.id}"
  super

  t = Time.now
  @start_time ||= Time.now
  const_time = @entity.class.construction_time(@entity.type)
  total_time = t - @start_time

  self.completed = (total_time >= const_time)

  if self.completed
    err = false

    # create the entity in registry
    begin
      invoke('manufactured::create_entity', @entity)

    # catch construction errors
    # TODO how to deal w/ resources already removed? perhaps allow user to reattempt cmd?
    rescue Exception => e
      err = true
      run_callbacks @station, 'construction_failed', @entity

    end

    run_callbacks @station, 'construction_complete', @entity unless err

  else
    percentage = total_time / const_time
    run_callbacks @station, 'partial_construction', @entity, percentage
  end
end

#should_run?Boolean


72
73
74
# File 'lib/manufactured/commands/construction.rb', line 72

def should_run?
  super && !self.completed
end

#to_json(*a) ⇒ Object

Convert command to json representation and return it


116
117
118
119
120
121
122
123
124
125
# File 'lib/manufactured/commands/construction.rb', line 116

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      {:station => station,
       :entity  => entity,
       :completed => completed,
       :start_time => start_time}.merge(cmd_json)
  }.to_json(*a)
end

#update(cmd) ⇒ Object

Update command from another


68
69
70
# File 'lib/manufactured/commands/construction.rb', line 68

def update(cmd)
  update_from(cmd, :start_time, :completed)
end