Class: Manufactured::Commands::ShieldRefresh

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

Overview

Represents action of shield recharging on a Ship

Associated with an attack command, will cease operation after attack finished and shield is fully refreshed

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, #first_hook, json_create, #last_hook, #run_hooks, #to_s, #update

Constructor Details

#initialize(args = {}) ⇒ ShieldRefresh

Manufactured::Commands::ShieldRefresh initializer

Parameters:

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

    hash of options to initialize mining command with

Options Hash (args):

  • :entity (Manufactured::Entity)

    entity

  • :check_command (Manufactured::Command)

    command which to checked to determine whether or not to continue


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

def initialize(args = {})
  attr_from_args args, :entity => nil,
                       :attack_cmd => nil
  super(args)
end

Instance Attribute Details

#attack_cmdObject

Attack attack command which this shield refresh

command's lifecycle is tried to

24
25
26
# File 'lib/manufactured/commands/shield_refresh.rb', line 24

def attack_cmd
  @attack_cmd
end

#entityObject

entity whose shield is being refreshed


20
21
22
# File 'lib/manufactured/commands/shield_refresh.rb', line 20

def entity
  @entity
end

Instance Method Details

#after_hookObject


54
55
56
57
# File 'lib/manufactured/commands/shield_refresh.rb', line 54

def after_hook
  # persist entity to registry
  update_registry(@entity)
end

#before_hookObject


46
47
48
49
50
51
52
# File 'lib/manufactured/commands/shield_refresh.rb', line 46

def before_hook
  # update entity from registry
  @entity = retrieve(@entity.id)

  # update cmd from registry
  @attack_cmd = retrieve(@attack_cmd.id) if @attack_cmd
end

#idObject

Return the unique id of this command.


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

def id
  "shield-refresh-cmd-#{@entity.nil? ? "" : @entity.id.to_s}"
end

#processes?(tentity) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#remove?Boolean

Returns:

  • (Boolean)

78
79
80
81
82
83
# File 'lib/manufactured/commands/shield_refresh.rb', line 78

def remove?
  # remove if the attack command has been deleted or is otherwise finished
  # and shield is at max level
  (@attack_cmd.nil? || @attack_cmd.remove?) &&
  (@entity.hp == 0 || (@entity.shield_level == @entity.max_shield_level))
end

#run!Object


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/manufactured/commands/shield_refresh.rb', line 64

def run!
  ::RJR::Logger.debug "refreshing shield of #{@entity.id}"
  @last_ran_at ||= Time.now
  if @entity.shield_level < @entity.max_shield_level
    pips =  (Time.now - @last_ran_at) * @entity.shield_refresh_rate
    @entity.shield_level += pips
    @entity.shield_level =
      entity.max_shield_level if entity.shield_level > entity.max_shield_level
  end

  # set last_ran_at after time check
  super
end

#should_run?Boolean

Returns:

  • (Boolean)

59
60
61
62
# File 'lib/manufactured/commands/shield_refresh.rb', line 59

def should_run?
  super && @entity.hp > 0 &&
  (@entity.shield_level < @entity.max_shield_level)
end

#to_json(*a) ⇒ Object

Convert command to json representation and return it


86
87
88
89
90
91
92
93
# File 'lib/manufactured/commands/shield_refresh.rb', line 86

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