Class Moon
In: app/models/moon.rb
Parent: ActiveRecord::Base

Methods

Public Class methods

Creates a new moon :) In the MoWaon galaxy, this is possible

[Source]

     # File app/models/moon.rb, line 138
138:   def self.make
139:     m = Moon.new
140:     srand
141:     begin
142:       m.x_pos,m.y_pos = rand(Galaxy.boundry)+1,rand(Galaxy.boundry)+1
143:     end until Moon.find(:all,:conditions => "x_pos >= #{m.x_pos-1} AND x_pos <= #{m.x_pos+3} AND y_pos >= #{m.y_pos-1} AND y_pos <= #{m.y_pos+1}").empty?
144:     m.size = ((rand*150).to_i%3+1).to_s
145:     m.name = self.new_name
146:     m.reset_production
147:     m.create_fleet
148:     m.save
149:     m
150:   end

Creates a random name for the moon

[Source]

     # File app/models/moon.rb, line 153
153:   def self.new_name(fix_count=3)
154:     begin
155:       fixes = %w( ba be bi bo bu ta te ti to tu na ne ni no nu ka ke ki ko ku fa fe fi fo fu bla ug ju ja ji jo je on da po pe pal ox ux puz zu za ze )
156:       name = ""
157:       fix_count.times { name += fixes[rand(fixes.size)] }
158:       name = name.capitalize
159:     end until Moon.find(:all,:conditions => "name = '#{name}'").empty?
160:     name
161:   end

Public Instance methods

Cancels the current production and resets it back to money

[Source]

    # File app/models/moon.rb, line 28
28:   def cancel_production
29:     case self.product
30:     when 'money':
31:       strength = 0
32:     when 'stalitte'
33:       strength = 1
34:     else
35:       strength = self.product
36:     end
37:     
38:     unless self.player.nil?
39:       self.player.money += ( 0.75 * ( self.amount.to_i * Spaceship.get_price(strength) ) ).to_i
40:       self.player.save
41:     end
42:     self.reset_production
43:   end

How many ships can be produced for the amount of rounds passed

[Source]

    # File app/models/moon.rb, line 63
63:   def capacity_for_passed_rounds
64:     (((self.rounds_needed_to_produce_ship - self.rounds_left) + self.rounds_passed) / self.rounds_needed_to_produce_ship ).to_i
65:     # <-- A comment to fix RadRails syntax highlighting error ^^
66:   end

Creates a new fleet if the moon has none yet

[Source]

     # File app/models/moon.rb, line 83
 83:   def create_fleet
 84:     if self.fleet.nil?
 85:       self.fleet = Fleet.new
 86:       
 87:       case self.size.to_i
 88:       when 1:
 89:         self.fleet.weak_ships = 3
 90:         self.fleet.medium_ships = 2
 91:         self.fleet.strong_ships = 1
 92:       when 2:
 93:         self.fleet.weak_ships = 3
 94:         self.fleet.medium_ships = 3
 95:         self.fleet.strong_ships = 2
 96:       when 3:
 97:         self.fleet.weak_ships = 3
 98:         self.fleet.medium_ships = 3
 99:         self.fleet.strong_ships = 3
100:       end
101:       
102:       self.fleet.save
103:     end
104:   end

Calculates the distance to another moon

[Source]

    # File app/models/moon.rb, line 53
53:   def distance_to(moon)
54:         (Math::sqrt((x_pos-moon.x_pos)*(x_pos-moon.x_pos)+(y_pos-moon.y_pos)*(y_pos-moon.y_pos))).round
55:   end

Is this a home moon?

[Source]

   # File app/models/moon.rb, line 7
7:   def is_home_moon?
8:     home_moon == 'true' ? true : false
9:   end

Produces a certain amount of ships

[Source]

     # File app/models/moon.rb, line 107
107:   def produce_ships(amount_to_produce)
108:     create_fleet
109:     
110:     case self.product.to_s
111:     when '1'
112:       self.fleet.weak_ships += amount_to_produce
113:     when '2'
114:       self.fleet.medium_ships += amount_to_produce
115:     when '3'
116:       self.fleet.strong_ships += amount_to_produce
117:     when 'satellite'
118:       self.satellite_count += amount_to_produce
119:     end
120:     
121:     if amount_to_produce.zero?
122:       self.rounds_left -= self.rounds_passed
123:     else
124:       self.rounds_left = rounds_needed_to_produce_ship-(self.rounds_passed-self.rounds_left)%rounds_needed_to_produce_ship
125:     end
126:         
127:     self.fleet.save
128:     self.amount -= amount_to_produce
129:     if (self.amount.zero?)
130:       self.player.money += self.taxes * (capacity_for_passed_rounds - amount_to_produce) * rounds_needed_to_produce_ship
131:       self.player.save
132:       self.reset_production
133:     end
134:     self.last_update_on += Game.round_length*self.rounds_passed
135:   end

Resets the production to money

[Source]

    # File app/models/moon.rb, line 22
22:   def reset_production
23:     self.product, self.amount, self.rounds_left = 'money', 1, 1
24:     self.save
25:   end

How long does the moon need to produce a certain or the current ship in production

[Source]

    # File app/models/moon.rb, line 69
69:   def rounds_needed_to_produce_ship(strength=nil)
70:     if strength.nil?
71:       case self.product.to_s
72:       when 'satellite'
73:         return 6-self.size.to_i
74:       else
75:         return (self.product.to_i+1)+(4-self.size.to_i)
76:       end
77:     else
78:       return (strength.to_i+1)+(4-self.size.to_i)
79:     end
80:   end

How many rounds passed since the last update of the player

[Source]

    # File app/models/moon.rb, line 46
46:   def rounds_passed
47:     self.last_update_on = self.player.last_update_on if self.last_update_on.nil? # Set last_update_on of the moon to the players value if the moon has never been updated
48:     ((Time.now-self.last_update_on)/Game.round_length).to_i
49:     # <-- Fix RadRails syntax highlighting bug
50:   end

How much money do you get from this moon per round

[Source]

    # File app/models/moon.rb, line 12
12:   def taxes
13:     size.to_i * 5
14:   end

How long will it take to reach another moon

[Source]

    # File app/models/moon.rb, line 58
58:   def time_to_reach_from(moon)
59:     (1.5*distance_to(moon)+0.5).round
60:   end

Retruns the strength of the fleet + strength of all satellites on the moon

[Source]

    # File app/models/moon.rb, line 17
17:   def total_strength
18:     self.fleet.total_strength + self.satellite_count*2
19:   end

Protected Instance methods

[Source]

     # File app/models/moon.rb, line 165
165:   def validate
166:     errors.add(:amount, "should be an integer") unless amount.is_a?Fixnum
167:     errors.add(:amount, "should be positive") unless amount.nil? || amount > 0
168:   end

[Validate]