Class: Planet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match, state) ⇒ Planet

Returns a new instance of Planet.



63
64
65
66
# File 'lib/galaxy/models/planet.rb', line 63

def initialize match, state
  super()
  update_planet match, state
end

Instance Attribute Details

#battlesObject

Returns the value of attribute battles.



29
30
31
# File 'lib/galaxy/models/planet.rb', line 29

def battles
  @battles
end

#created_byObject

Returns the value of attribute created_by.



29
30
31
# File 'lib/galaxy/models/planet.rb', line 29

def created_by
  @created_by
end

#progressObject

Returns the value of attribute progress.



29
30
31
# File 'lib/galaxy/models/planet.rb', line 29

def progress
  @progress
end

Class Method Details

.new_or_update(match, state) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/galaxy/models/planet.rb', line 50

def Planet.new_or_update match, state
  # Lookup by both num and name is necessary since Routes/Fleets/Bombings create planets by name only
  num = match[0][0,1] == '#' ? match[0] : '#' + match[0].to_s
  name = match[3]
  planet = Planet.lookup(num)
  planet ||= Planet.lookup(name) if name 
  if planet
    planet.update_planet match, state
  else
    new match, state
  end
end

Instance Method Details

#<=>(other) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/galaxy/models/planet.rb', line 158

def <=>(other)
  case other
    when nil then 1
    when Planet then num <=> other.num
    when Race then race ? race <=> other : -1
    when Product then product ? product <=> other : -1
    when Bombing then bombings.any? {|r| r == other} ? 0 : 1 #Planet is bigger than Bombing
    when Fleet then fleets.any? {|r| r == other} ? 0 : sent_fleets.any? {|r| r == other} ? 0 : 1 #Planet is bigger than Fleet
    when Route then routes.any? {|r| r == other} ? 0 : incoming_routes.any? {|r| r == other}? 0 : 1 #Planet is bigger than Route
    when Integer then num <=> other
    when String then self <=> other.downcase.to_sym
    when Symbol then 
    return aster? ? 0 : 1 if other == :aster or other == :asteroid or other == :min
    return max_size? ? 0 : 1 if other == :max_size or other == :max 
    return uninhabited? ? 0 : 1 if other == :uninhabited or other == :free or other == :empty
    return unidentified? ? 0 : 1 if other == :unidentified or other == :unid or other == :unknown
    return 0 if name and name.downcase.to_sym == other
    return 0 if race and race == other 
    return 0 if product and product == other
    key <=> other.to_s
  else raise ArgumentError, 'Comparison with a wrong type'
  end
end

#add_route(route) ⇒ Object



31
32
33
34
35
36
# File 'lib/galaxy/models/planet.rb', line 31

def add_route(route)
  # Find if target Route of this cargo type already exist, remove previous Route if found
  routes.each do |r|
    remove_route r if r == route.cargo 
  end
end

#aster?Boolean

Boolean tests on Planets

Returns:

  • (Boolean)


148
# File 'lib/galaxy/models/planet.rb', line 148

def aster? ; size and size < 1 end

#distance(planet) ⇒ Object



125
126
127
128
# File 'lib/galaxy/models/planet.rb', line 125

def distance planet
  #game.geometry.distance (planet1, planet2)
  0 
end

#keyObject



122
# File 'lib/galaxy/models/planet.rb', line 122

def key ; name ? name : numkey end

#max_size?Boolean

Returns:

  • (Boolean)


150
# File 'lib/galaxy/models/planet.rb', line 150

def max_size? ; size and size == 2500 end

#numkeyObject



123
# File 'lib/galaxy/models/planet.rb', line 123

def numkey ; num ? '#' + num.to_s : nil end

#produced_by_matObject Also known as: produced



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/galaxy/models/planet.rb', line 133

def produced_by_mat
  return nil unless self.product and self.product.ship?
  produced_mass = @progress*self.product.mass 
  available_mass = self.mat/10
  missing_mass = self.product.mass - produced_mass - available_mass
  if missing_mass < 0
    @progress
  else
    missing_l = (1/self.res+10)*missing_mass #
    return 1-(available_mass + missing_l/10)/(produced_mass+available_mass+missing_l/10)
  end
end

#produced_full_matObject



130
131
# File 'lib/galaxy/models/planet.rb', line 130

def produced_full_mat
@progress end

#remove_route(route) ⇒ Object



38
39
40
# File 'lib/galaxy/models/planet.rb', line 38

def remove_route(route)
  route.kill
end

#suppliersObject



46
47
48
# File 'lib/galaxy/models/planet.rb', line 46

def suppliers
  incoming_routes.map {|r| r.planet}
end

#targetsObject



42
43
44
# File 'lib/galaxy/models/planet.rb', line 42

def targets
  routes.map {|r| r.target}
end

#unidentified?Boolean Also known as: unknown?

Returns:

  • (Boolean)


155
# File 'lib/galaxy/models/planet.rb', line 155

def unidentified? ; !race and !size end

#uninhabited?Boolean Also known as: free?

Returns:

  • (Boolean)


152
# File 'lib/galaxy/models/planet.rb', line 152

def uninhabited? ; !race and size end

#update_planet(match, state) ⇒ Object

Update information on Planet (based on match size and state hash given)



69
70
71
72
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/galaxy/models/planet.rb', line 69

def update_planet match, state
  # Make attributes hash based on match size (4 possible constructors
  hash = case match.size
    when 13 # Your Planet/ * Planet
    prod_name = match[8]
    prod_key = state[:race] ? state[:race].name + '.' + prod_name : nil 
    product = Product.lookup(prod_name)
    product ||= Product.lookup(prod_key) if prod_key 
    product.planets << self if product
    {:num=>match[0].to_i, :x=>match[1].to_f, :y=>match[2].to_f, :name=>match[3], :size=>match[4].to_f, 
      :pop=>match[5].to_f, :ind=>match[6].to_f, :res=>match[7].to_f, :cap=>match[9].to_f, 
      :mat=>match[10].to_f, :col=>match[11].to_f, :l=>match[12].to_f}
    
    when 8 # Uninhabited Planet
    {:num=>match[0].to_i, :x=>match[1].to_f, :y=>match[2].to_f, :name=>match[3], :size=>match[4].to_f, 
      :res=>match[5].to_f, :cap=>match[6].to_f, :mat=>match[7].to_f}
    
    when 6 # Ships In Production
    total_mass = match[3].to_f / 10 # Mass values given in "Material units", divide by 10
    produced_mass = match[4].to_f / 10
    percentage = produced_mass/total_mass # Calculate ship production progress
    {:progress=>percentage}
    
    when 3 # Unidentified Planet
    {:num=>match[0].to_i, :x=>match[1].to_f, :y=>match[2].to_f}
    
    when 2 # Battle/Bombing generated Planet (known num and name)
    if state[:section] = 'Battle_planets'
      self.battles = state[:battles] = self.battles ? self.battles + 1 : 1 
      state[:planet] = self
    end
    {:num=>match[0].to_i, :name=>match[1]}
    
    when 1 # Route/Fleet/Bombing generated Planet (given EITHER #num or name)
    match[0][0,1] == '#' ?  
    {:num=>match[0][1..-1].to_s} : 
    {:name=>match[0]}
    
    when 0 # All init data given in a state hash
    state 
  else {}
  end 
  # Set all the attributes given in hash    
  hash.each do |key, value| 
    setter = key.to_s.concat('=').to_sym
    if self.respond_to? setter then self.send setter, value else raise ArgumentError end
  end
  state[:race].planets << self if state[:race]
  num ? add(numkey) : add if self.class.dataset # Add instantiated/updated model to dataset if dataset established
  @created_by = state[:created_by]
  self
end