Module: Omega::Client::DSL

Included in:
Base
Defined in:
lib/omega/client/dsl.rb

Overview

Omega Client DSL, works best if you including this module in the namespace you would like to use it, eg:

Examples:

using the dsl

include Omega::Client::DSL

# create a new user
user 'newuser', 'withpass'

# create a new galaxy/system/planet
galaxy 'Zeus' do |g|
  system 'Athena', 'HR1925', :location =>
    Location.new(:x => 240, :y => -360, :z => 110) do |sys|
      planet 'Aphrodite', :movement_strategy =>
        orbit(:speed => 0.1, :e => 0.16, :p => 140,
              :direction => random_axis(:orthogonal_to => [0,1,0]))
  end
end

Defined Under Namespace

Classes: Base

Instance Method Summary collapse

Instance Method Details

#asteroid(id, args = {}, &bl) ⇒ Cosmos::Entities::Asteroid

Create new asteroid and return it.

@system must be set to the Cosmos::Entities::SolarSystem

to create the asteroid under

Raises:

  • (ArgumentError)

240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/omega/client/dsl.rb', line 240

def asteroid(id, args={}, &bl)
  system = @solar_system || args[:solar_system]
  raise ArgumentError, "solar_system nil" if system.nil?

  aargs = args.merge({:id => id, :name => id,
                      :solar_system => system})
  ast = Cosmos::Entities::Asteroid.new(aargs)

  RJR::Logger.info "Creating asteroid #{ast} under #{system.name}"
  invoke 'cosmos::create_entity', ast

  dsl.run ast, :asteroid => ast, &bl
  ast
end

#asteroid_belt(args = {}, &bl) ⇒ Array<Cosmos::Entities::Asteroid>

Create an asteroid belt by creating an asteroid field along an elliptical path

Options Hash (args):

  • :p (Integer, Float)

    semi_latus_rectum to use when generating the elliptical path

  • :e (Integer, Float)

    eccentricity to use when generating the elliptical path

  • Array,Array] (Array<Array<Float>,Array<Float>] :direction major/minor direction vectors of the elliptical path axis)

    :direction major/minor direction vectors of the elliptical path axis


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/omega/client/dsl.rb', line 286

def asteroid_belt(args={}, &bl)
  scale = args[:scale] || 30

  p,e = args[:p],args[:e]
  direction = args[:direction]
  path = Motel.elliptical_path(p,e,direction)

  num  = path.size / scale
  locs = []
  0.upto(scale) { |i|
    p = path[num*i]
    locs << Motel::Location.new(:x => p[0], :y => p[1], :z => p[2])
  }

  asteroid_field(:locations => locs, &bl)
end

#asteroid_field(args = {}, &bl) ⇒ Array<Cosmos::Entities::Asteroid>

Create a field of asteroids at the specified locations

TODO option to set num of locations to randomly generate w/ parameterized bounds/location-args

Options Hash (args):


264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/omega/client/dsl.rb', line 264

def asteroid_field(args={}, &bl)
  locs = args[:locations] || []

  asts =
    locs.collect { |loc|
      id = gen_uuid
      loc.id = id
      asteroid(id, {:location => loc}.merge(args))
    }

  dsl.run asts, :asteroids => asts, &bl
  asts
end

#dock(ship_id, station_id) ⇒ Object

Dock ship at the specified station


504
505
506
507
# File 'lib/omega/client/dsl.rb', line 504

def dock(ship_id, station_id)
  RJR::Logger.info "Docking #{ship_id} at #{station_id}"
  invoke 'manufactured::dock', ship_id, station_id
end

#dslObject

Return handle to base dsl instance, use to get/set options such as node/parallel and run operations such as 'join', etc


34
35
36
# File 'lib/omega/client/dsl.rb', line 34

def dsl
  @dsl_base ||= Base.new
end

#galaxy(name, &bl) ⇒ Cosmos::Entities::Galaxy

Create and return a new galaxy


150
151
152
153
154
155
156
157
# File 'lib/omega/client/dsl.rb', line 150

def galaxy(name, &bl)
  g = Cosmos::Entities::Galaxy.new :id   => gen_uuid,
                                   :name => name
  RJR::Logger.info "Creating galaxy #{g}"
  invoke 'cosmos::create_entity', g
  dsl.run g, :galaxy => g, &bl
  g
end

#gen_uuidObject

Generate an return a random uuid

See Also:


41
42
43
# File 'lib/omega/client/dsl.rb', line 41

def gen_uuid
  Motel.gen_uuid
end

#interconnect(*systems) ⇒ Cosmos::Entities::JumpGate

Helper to create interconnections between a series of systems

create gates inbetween


425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/omega/client/dsl.rb', line 425

def interconnect(*systems)
  systems = systems.first if systems.size == 1 &&
                             systems.first.is_a?(Array)
  systems.shuffle!
  0.upto(systems.length - 2) do |i|
    # TODO how to specify location
    # TODO alternate interconn 'types' or algorithms to join
    # various systesms
    jump_gate systems[i], systems[i+1]
    jump_gate systems[i+1], systems[i]
  end
end

#invoke(*args) ⇒ Object

Invoke request using the DSL node / endpoint


73
74
75
# File 'lib/omega/client/dsl.rb', line 73

def invoke(*args)
  dsl.invoke *args
end

#jump_gate(system, endpoint, args = {}) ⇒ Cosmos::Entities::JumpGate

Create new jump gate between two systems and return it


406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/omega/client/dsl.rb', line 406

def jump_gate(system, endpoint, args = {})
  jid   = gen_uuid
  jargs = args.merge({:id   => jid,
                      :name => jid,
                      :solar_system => system,
                      :endpoint => endpoint})
  gate  = Cosmos::Entities::JumpGate.new(jargs)

  RJR::Logger.info "Creating gate #{gate} under #{system}"
  gate = invoke 'cosmos::create_entity', gate

  gate
end

#loc(x, y, z) ⇒ Object

Utility wrapper to simply return a new location


68
69
70
# File 'lib/omega/client/dsl.rb', line 68

def loc(x,y,z)
  Motel::Location.new :x => x, :y => y, :z => z
end

#login(user_id, password) ⇒ Object

Log specified user into the server

See Also:


89
90
91
92
93
94
# File 'lib/omega/client/dsl.rb', line 89

def (user_id, password)
  user = Users::User.new(:id => user_id,
                         :password => password)
  @session = invoke('users::login', user)
  dsl.node.rjr_node.message_headers['session_id'] = @session.id
end

#logoutObject

Log the user out of the server


97
98
99
100
101
# File 'lib/omega/client/dsl.rb', line 97

def logout
  invoke('users::logout', @session.id)
  @session = nil
  dsl.node.rjr_node.message_headers['session_id'] = nil
end

#mission(id, args = {}) ⇒ Object

Create a new Missions::Mission

@param[Hash[ args hash of options to pass directly to mission initializer


529
530
531
532
533
534
# File 'lib/omega/client/dsl.rb', line 529

def mission(id, args={})
  mission = Missions::Mission.new(args.merge({:id => id}))
  RJR::Logger.info "Creating mission #{mission}"
  notify 'missions::create_mission', mission
  mission
end

#missions_event_handler(event, handler_method, args = {}) ⇒ Object


536
537
538
539
540
541
542
543
# File 'lib/omega/client/dsl.rb', line 536

def missions_event_handler(event, handler_method, args={})
  dsl_handler = Missions::DSL::Client::EventHandler.send(handler_method,
                                           args.merge({:event => event}))
  handler = Missions::EventHandlers::DSL.new :event_id => event,
                                             :persist  => true
  handler.exec dsl_handler
  handler
end

#moon(name, args = {}) ⇒ Cosmos::Entities::Moon

Create new moon and return it.

@planet must be set to the Cosmos::Entities::Planet to create the moon under

Raises:

  • (ArgumentError)

373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/omega/client/dsl.rb', line 373

def moon(name, args={})
  raise ArgumentError, "planet is nil" if @planet.nil?

  margs = args.merge({:id => gen_uuid,
                      :name => name,
                      :planet => @planet })
  moon = Cosmos::Entities::Moon.new(margs)

  RJR::Logger.info "Creating moon #{moon} under #{@planet}"
  notify 'cosmos::create_entity', moon
  moon
end

#moons(names, args = {}) ⇒ Object

Create a few child moons under planet

@planet must be set to the Cosmos::Entities::Planet to create the moon under

Options Hash (args):

  • :locations (Hash)

    args to pass onto moon location initializers


394
395
396
397
398
# File 'lib/omega/client/dsl.rb', line 394

def moons(names, args={})
  names.collect { |name|
    moon name, :location => rand_location(args[:locations])
  }
end

#notify(*args) ⇒ Object

Invoke notification using the DSL node / endpoint


78
79
80
# File 'lib/omega/client/dsl.rb', line 78

def notify(*args)
  dsl.notify *args
end

#orbit(args = {}) ⇒ Object

Helper to create a new movement strategy specifying an entity's orbit

Simply wraps Elliptical movement strategy constructor with some defaults for now


351
352
353
354
355
356
# File 'lib/omega/client/dsl.rb', line 351

def orbit(args={})
  # TODO if direction is not specified,
  #      set orthogonal to orientation of star in parent system?
  args[:relative_to] ||= Motel::MovementStrategies::Elliptical::FOCI
  Motel::MovementStrategies::Elliptical.new args
end

#planet(name, args = {}, &bl) ⇒ Cosmos::Entities::Planet

Create new planet and return it.

@solar_system must be set to the Cosmos::Entities::SolarSystem to create the planet under

Raises:

  • (ArgumentError)

331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/omega/client/dsl.rb', line 331

def planet(name, args={}, &bl)
  raise ArgumentError, "solar_system is nil" if @solar_system.nil?

  pargs = args.merge({:id => gen_uuid,
                      :name => name,
                      :solar_system => @solar_system})
  planet = Cosmos::Entities::Planet.new(pargs)

  RJR::Logger.info "Creating planet #{planet} under #{@solar_system}"
  invoke 'cosmos::create_entity', planet

  dsl.run planet, :planet => planet, &bl
  planet
end

#proxied_system(system_id, proxy_id, system_args = {}) ⇒ Cosmos::Entities::SolarSystem

Create a reference to a system running on a remote node


218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/omega/client/dsl.rb', line 218

def proxied_system(system_id, proxy_id, system_args={})
  # TODO how to send request to proxied server so that
  # system properties (name, location) can just be copied ?
  sargs = system_args.merge({:id => system_id,
                             :proxy_to => proxy_id})
  sys  = Cosmos::Entities::SolarSystem.new(sargs)

  # create system
  RJR::Logger.info "Creating proxy solar system #{sys} to #{proxy_id}"
  invoke 'cosmos::create_entity', sys
  sys
end

#rand_location(args = {}) ⇒ Object Also known as: rand_loc

Generate an return a new random Motel::Location, using the specified arguments


56
57
58
# File 'lib/omega/client/dsl.rb', line 56

def rand_location(args={})
  Motel::Location.random args
end

#rand_resourceObject

Generate an return a new random Cosmos::Resource

See Also:

  • Resources.rand_resource

48
49
50
# File 'lib/omega/client/dsl.rb', line 48

def rand_resource
  Omega::Resources.random
end

#random_axis(*args) ⇒ Object Also known as: rand_axis

Wrapper around Motel.random_axis


62
63
64
# File 'lib/omega/client/dsl.rb', line 62

def random_axis(*args)
  Motel.random_axis *args
end

#random_orbit(args = {}) ⇒ Object Also known as: rand_orbit

Helper to create a randomized orbit movement strategy


359
360
361
362
# File 'lib/omega/client/dsl.rb', line 359

def random_orbit(args={})
  args[:relative_to] ||= Motel::MovementStrategies::Elliptical::FOCI
  Motel::MovementStrategies::Elliptical.random args
end

#resource(args = {}) ⇒ Cosmos::Resource

Set new resource on an asteroid and return it.

@asteroid must be set to the Cosmos::Entities::Asteroid to assoicate the resource with

Raises:

  • (ArgumentError)

310
311
312
313
314
315
316
317
318
319
320
# File 'lib/omega/client/dsl.rb', line 310

def resource(args = {})
  asteroid = @asteroid || args[:asteroid]
  raise ArgumentError, "asteroid is nil" if asteroid.nil?
  rs = args[:resource] || Cosmos::Resource.new(args)
  rs.id       = gen_uuid
  rs.entity   = asteroid
  rs.quantity = args[:quantity] if args.has_key?(:quantity)
  RJR::Logger.info "Creating resource #{rs} at #{asteroid}"
  notify 'cosmos::set_resource', rs
  rs
end

#role(nrole) ⇒ Object

Create a new role, or if @user is set, simply add the specified role id to the user

Operates in one of two modes depending on if @user is set. If it is, specify a string role name to this function to be added to the user indicated by @user. Else specify a Users::Role to create on the server side


131
132
133
134
135
136
137
138
139
140
# File 'lib/omega/client/dsl.rb', line 131

def role(nrole)
  if @user
    RJR::Logger.info "Adding role #{nrole} to #{@user}"
    invoke('users::add_role', @user.id, nrole)

  else
    RJR::Logger.info "Creating role #{nrole}"
    invoke('users::create_role', nrole)
  end
end

#schedule_event(interval, event) ⇒ Object

Schedule new periodic event w/ missions subsystem


515
516
517
518
519
520
521
522
523
# File 'lib/omega/client/dsl.rb', line 515

def schedule_event(interval, event)
  evnt =
    Omega::Server::PeriodicEvent.new :id => event.id + '-scheduler',
                                     :interval => interval,
                                     :template_event => event
  RJR::Logger.info "Scheduling event #{evnt}(#{event})"
  notify 'missions::create_event', evnt
  evnt
end

#ship(id, args = {}, &bl) ⇒ Manufactured::Ship

Retrieve ship with the specified id if it exists, else create new ship and return it.

Note callback will be invoked before ship is created, you may use it to set ship parameters for creation


492
493
494
495
496
497
498
499
500
501
# File 'lib/omega/client/dsl.rb', line 492

def ship(id, args={}, &bl)
  begin return invoke 'manufactured::get_entity', 'with_id', id
  rescue Exception => e ; end

  sh = Manufactured::Ship.new(args.merge({:id => id}))
  dsl.run sh, :ship => sh, &bl

  RJR::Logger.info "Creating ship #{sh}"
  invoke 'manufactured::create_entity', sh
end

#station(id, args = {}, &bl) ⇒ Manufactured::Station

Return station with the specified id if it exists, else create new station and return it.

Note callback will be invoked before station is created, you may use it to set station parameters for creation


450
451
452
453
454
455
456
457
458
459
# File 'lib/omega/client/dsl.rb', line 450

def station(id, args={}, &bl)
  begin return invoke 'manufactured::get_entity', 'with_id', id
  rescue Exception => e ; end

  st = Manufactured::Station.new(args.merge({:id => id}))
  dsl.run st, :station => st, &bl

  RJR::Logger.info "Creating station #{st}"
  invoke 'manufactured::create_entity', st
end

#station_orbit(args = {}) ⇒ Object

Return movement strategy that will orbit station around its system's star

Currently this only supports circular orbits based on the station's starting position

Raises:

  • (ArgumentError)

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/omega/client/dsl.rb', line 465

def station_orbit(args={})
  raise ArgumentError, "station is nil" if @station.nil?

  speed = args[:speed] || args[:s]

  # dmaj from current station loc, dmin rand or from args
  dmaj = Motel.normalize(*@station.location.coordinates)
  dmin = Motel.normalize(*Motel.cross_product(*dmaj,
                         *Motel.normalize(rand, rand, rand)))

  Motel::MovementStrategies::Elliptical.new :e     => 0,
                                            :p     => @station.loc.scalar,
                                            :speed => speed,
                                            :dmaj  => dmaj,
                                            :dmin  => dmin
end

#system(name, star_name = nil, args = {}, &bl) ⇒ Cosmos::Entities::SolarSystem

Return the system specified corresponding to the given id, else if not found created it and return it.

If system does not exist, and we are creating a new one, @galaxy must be set. Will optionally create star if star_name is set


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/omega/client/dsl.rb', line 170

def system(name, star_name = nil, args = {}, &bl)
  # lookup / return system
  sys =
    begin dsl.node.invoke('cosmos::get_entity', 'with_name', name)
    rescue Exception => e ; end

  if sys.nil?
    # require galaxy
    galaxy = @galaxy || args[:galaxy]
    raise ArgumentError, "galaxy nil" if galaxy.nil?

    # initialize system
    sargs = args.merge({:id     => args[:id] || gen_uuid,
                        :name   => name,
                        :galaxy => galaxy})
    sys  = Cosmos::Entities::SolarSystem.new(sargs)

    # create system
    RJR::Logger.info "Creating solar system #{sys} under #{galaxy}"
    invoke 'cosmos::create_entity', sys

    # optionally create star
    unless star_name.nil?
      # initialize star
      stargs = {:id   => gen_uuid,
                :name => star_name,
                :solar_system => sys}
      star = Cosmos::Entities::Star.new stargs

      RJR::Logger.info "Creating star #{star} under #{sys}"
      st = invoke 'cosmos::create_entity', star
      sys.add_child st
    end
  end

  # run callback
  dsl.run sys, :solar_system => sys, &bl

  # return system
  sys
end

#user(user_id, password = nil, args = {}, &bl) ⇒ Users::User

Return user w/ the given user_id, else if it is not found create it w/ the specified password and attributes


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/omega/client/dsl.rb', line 110

def user(user_id, password = nil, args = {}, &bl)
  # lookup / return user
  begin return invoke('users::get_entity', 'with_id', user_id)
  rescue Exception => e ; end

  # create / return user
  u = Users::User.new(args.merge({:id => user_id, :password => password,
                                  :registration_code => nil}))
  invoke('users::create_user', u)
  dsl.run u, :user => u, &bl
  u
end