Class Fleet
In: app/models/fleet.rb
Parent: ActiveRecord::Base

Methods

Public Instance methods

Add ships from another fleet

[Source]

    # File app/models/fleet.rb, line 69
69:   def +(other)
70:     self.weak_ships += other.weak_ships
71:     self.medium_ships += other.medium_ships
72:     self.strong_ships += other.strong_ships
73:   end

Did the fleet arrive at its destination?

[Source]

    # File app/models/fleet.rb, line 64
64:   def arrived?
65:     self.moving? && self.arrival_on < Time.now
66:   end

Fight the fleet at the destination

[Source]

    # File app/models/fleet.rb, line 46
46:   def fight
47:     destination = Moon.find(self.destination_moon)
48:     destination.create_fleet # Create a new fleet if theres none on the destination
49:     enemy = destination.player
50:     if self.player.owns(destination) # The player owns the destination, then merge the fleets
51:       destination.fleet.merge(self)
52:       self.player.add_to_log("Your fleet reached #{destination.name} and is now deployed there.")
53:     else # The player doesn't own the destination: FIGHT! ^^
54:       Fight.new(destination,self)
55:     end
56:   end

Merges two fleets

[Source]

    # File app/models/fleet.rb, line 33
33:   def merge(fleet)
34:     self.+(fleet)
35:     fleet.destroy
36:     self.save
37:   end

Is the fleet moving?

[Source]

    # File app/models/fleet.rb, line 59
59:   def moving?
60:     self.moving == 'true'
61:   end

Shirnks the fleet to a certain strength (not very efficient yet)

[Source]

    # File app/models/fleet.rb, line 19
19:   def shrink_to_strength(strength)
20:     until self.total_strength <= strength
21:       case rand(3)
22:       when 0:
23:         self.weak_ships -= 1 if self.weak_ships > 0
24:       when 1:
25:         self.medium_ships -= 1 if self.medium_ships > 0
26:       when 2:
27:         self.strong_ships -= 1 if self.strong_ships > 0
28:       end
29:     end
30:   end

Stops a moving fleet

[Source]

    # File app/models/fleet.rb, line 40
40:   def stop
41:     self.moving = 'false'
42:     self.arrival_on,self.departure_on,self.starting_moon,self.destination_moon = nil
43:   end

Take ships away from this fleet

[Source]

    # File app/models/fleet.rb, line 76
76:   def substract(other)
77:     self.weak_ships -= other.weak_ships
78:     self.medium_ships -= other.medium_ships
79:     self.strong_ships -= other.strong_ships
80:   end

The strength of the fleet

[Source]

    # File app/models/fleet.rb, line 9
 9:   def total_strength
10:     self.weak_ships + self.medium_ships*2 + self.strong_ships*3
11:   end

This fleet wins against the other fleet?

[Source]

    # File app/models/fleet.rb, line 14
14:   def wins_against(fleet)
15:     self.total_strength > fleet.total_strength
16:   end

Protected Instance methods

Validates the fleet (for being saved into the database)

[Source]

    # File app/models/fleet.rb, line 85
85:   def validate
86:     errors.add(:weak_ships,'can\'t be negativ') if self.weak_ships < 0
87:     errors.add(:medium_ships,'can\'t be negativ') if self.medium_ships < 0
88:     errors.add(:strong_ships,'can\'t be negativ') if self.strong_ships < 0
89:     
90:     if self.moving == 'true'
91:       errors.add(:fleet,'has to contain at least one ship') if total_strength.zero?
92:     end
93:   end

[Validate]