Module: Missions::DSL::Assignment

Includes:
Helpers
Defined in:
lib/missions/dsl/assignment.rb

Overview

Mission Assignment

Class Method Summary collapse

Methods included from Helpers

#dsl_module_for, included, #is_dsl_category?

Class Method Details

.add_resource(entity_id, rs_params = {}) ⇒ Object

Add a resource to the specified manufactured entity


62
63
64
65
66
67
68
69
# File 'lib/missions/dsl/assignment.rb', line 62

def self.add_resource(entity_id, rs_params={})
  proc { |mission|
    entity = mission.mission_data[entity_id]
    rs  = Cosmos::Resource.new({:id => Motel.gen_uuid,
                                :entity => entity}.merge(rs_params))
    node.notify 'manufactured::add_resource', entity.id, rs
  }
end

.create_asteroid(id, entity_params = {}) ⇒ Object

Create an asteroid w/ the specified params


40
41
42
43
44
45
46
47
48
49
# File 'lib/missions/dsl/assignment.rb', line 40

def self.create_asteroid(id, entity_params={})
  proc { |mission|
    entity_params[:id] = Motel.gen_uuid if entity_params[:id].nil?
    entity_params[:name] = entity_params[:id] if entity_params[:name].nil?
    ast = Cosmos::Entities::Asteroid.new entity_params
    mission.mission_data[id] = ast
    node.invoke 'cosmos::create_entity', ast
    update_mission mission
  }
end

.create_entity(id, entity_params = {}) ⇒ Object

Create a ship with the specified params

TODO rename or expand to also create stations


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/missions/dsl/assignment.rb', line 26

def self.create_entity(id, entity_params={})
  proc { |mission|
    # create new entity using specified params
    entity_params[:id] = Motel.gen_uuid if entity_params[:id].nil?
    entity = Manufactured::Ship.new entity_params
    mission.mission_data[id] = entity

    # TODO only if ship does not exist
    node.invoke('manufactured::create_entity', entity)
    update_mission mission
  }
end

.create_resource(entity_id, rs_params = {}) ⇒ Object

Associate a resource w/ an existing cosmos entity


52
53
54
55
56
57
58
59
# File 'lib/missions/dsl/assignment.rb', line 52

def self.create_resource(entity_id, rs_params={})
  proc { |mission|
    entity = mission.mission_data[entity_id]
    rs  = Cosmos::Resource.new({:id => Motel.gen_uuid,
                                :entity => entity}.merge(rs_params))
    node.notify 'cosmos::set_resource', rs
  }
end

.schedule_expiration_eventsObject

Add an event to the registry for mission timeout/expiration


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/missions/dsl/assignment.rb', line 127

def self.schedule_expiration_events
  proc { |mission|
    timestamp = mission.assigned_time + mission.timeout
    expired   = Missions::Events::Expired.new :mission   => mission,
                                              :timestamp => timestamp
    failed    = Missions::Events::Failed.new  :mission   => mission,
                                              :timestamp => timestamp
    registry << expired
    registry << failed
  }
end

.store(id, lookup) ⇒ Object

Invoke the specified lookup proc and store the result in the mission data


16
17
18
19
20
21
# File 'lib/missions/dsl/assignment.rb', line 16

def self.store(id, lookup)
  proc { |mission|
    mission.mission_data[id] = lookup.call(mission)
    update_mission mission
  }
end

.subscribe_to(*args) ⇒ Object

Subscribe node to entity or subsystem event


119
120
121
122
123
124
# File 'lib/missions/dsl/assignment.rb', line 119

def self.subscribe_to(*args)
  proc { |mission|
    args.length > 2 ? subscribe_to_entity_event(*args).call(mission) :
                      subscribe_to_subsystem_event(*args).call(mission)
  }
end

.subscribe_to_entity_event(entities, evnt, handlers) ⇒ Object

Subcribe node to event on specified entity(ies) invoking handler(s) when event occurs


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/missions/dsl/assignment.rb', line 73

def self.subscribe_to_entity_event(entities, evnt, handlers)
  proc { |mission|
    entities = [entities] unless entities.is_a?(Array)
    handlers = [handlers] unless handlers.is_a?(Array)

    entities.collect! { |entity|
      # this could be an array
      entity.is_a?(String) ? mission.mission_data[entity] : entity
    }.flatten!

    entities.each { |entity|
      # add handler to registry
      eid     = Missions::Events::Manufactured.gen_id(entity.id, evnt)

      # TODO mark event handler as persistant &
      # remove handlers on mission completion/failure
      handler = Omega::Server::EventHandler.new(:event_id => eid) { |e|
                  handlers.each { |h| h.call(mission, e) }
                }
      registry << handler

      # subscribe to server side events
      node.invoke('manufactured::subscribe_to', entity.id, evnt)
    }
  }
end

.subscribe_to_subsystem_event(evnt, handlers) ⇒ Object

Subscribe node to subsystem event invoking handler(s) when it occurs

TODO other subsystem events


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/missions/dsl/assignment.rb', line 104

def self.subscribe_to_subsystem_event(evnt, handlers)
  proc { |mission|
    handlers = [handlers] unless handlers.is_a?(Array)

    handler = Missions::EventHandlers::ManufacturedEventHandler.
                new(:manu_event_type => evnt, :persist => true){ |e|
                handlers.each { |h| h.call(mission, e) }
              }
    registry << handler

    node.invoke('manufactured::subscribe_to', evnt)
  }
end