Class: Route

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/galaxy/models/route.rb

Instance Method Summary collapse

Constructor Details

#initialize(match, state) ⇒ Route

match is not really used, remains for consistency with other models



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/galaxy/models/route.rb', line 12

def initialize match, state # match is not really used, remains for consistency with other models
  
  return if match.join == 'N$MCE' # Skip header
  match[1..4].each_with_index do |m,i|
    if m != '-'
      super :cargo => ['cap', 'mat', 'col', 'empty'][i]
      self.target = Planet.new_or_update [m], state.merge({:race=>nil,:product=>nil,:created_by=>self}) unless self.target = Planet.lookup(m)
    end
  end
  planet = Planet.new_or_update [match[0]], state.merge({:race=>nil,:product=>nil,:created_by=>self}) unless planet = Planet.lookup(match[0])
  race = Race.lookup(state[:owner])
  
  race.routes << self
  planet.routes << self
  target.incoming_routes << self
  add if self.class.dataset # Add instantiated model to dataset if it is defined
end

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/galaxy/models/route.rb', line 43

def <=>(other)
  case other
    when nil then 1
    when Route then key <=> other.key
    when Race then race <=> other
    when Planet then planet == other ? 0 : target <=> other
    when Integer, Float then planet.distance(target) <=> other
    when String then self <=> other.downcase.to_sym
    when Symbol then 
    return 0 if race == other
    return 0 if planet == other
    return 0 if target == other
    return 0 if cargo.downcase.include? other.to_s
    key.downcase <=> other.to_s
  else raise ArgumentError, 'Comparison with a wrong type'
  end
end

#keyObject



41
# File 'lib/galaxy/models/route.rb', line 41

def key ; [planet.num, target.num, cargo].join('.') end

#killObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/galaxy/models/route.rb', line 30

def kill
  if result = super
    race.routes.delete self if race
    planet.routes.delete self if planet and planet == self
    target.incoming_routes.delete self if target and target == self
    self.planet = nil
    self.target = nil
  end
  result
end