Class Player
In: app/models/player.rb
Parent: ActiveRecord::Base

Methods

Public Instance methods

Adds a messaage to the players log

[Source]

    # File app/models/player.rb, line 77
77:   def add_to_log(msg)
78:     self.log_events << LogEvent.create(msg)
79:   end

Let the player collect all the taxes from his/her moon

[Source]

    # File app/models/player.rb, line 59
59:   def collect_taxes
60:     collected_money = 0
61:     self.moons.each do |m|
62:       if m.product == 'money'
63:         if m.last_update_on.nil?
64:          m.last_update_on = m.player.last_update_on
65:         end
66:         collected_money += m.taxes*m.rounds_passed
67:         m.last_update_on += Game.round_length*m.rounds_passed
68:         m.save
69:       end
70:     end
71:     self.money += collected_money
72:     add_to_log("You got #{collected_money} money from #{self.moons.size} moons for #{self.rounds_passed} #{LogEvent.pluralize('round',self.rounds_passed)} passed!") unless collected_money.zero?
73:     collected_money
74:   end

Find the home moon of the user

[Source]

    # File app/models/player.rb, line 8
 8:   def home_moon
 9:     Moon.find(:first, :conditions => ["home_moon = 'true' AND player_id = ?",id])
10:   end

Calculate the level of the player

[Source]

    # File app/models/player.rb, line 18
18:   def level
19:     return 0 if moons.empty?
20:     strength = 0
21:     moons.each do |m|
22:       strength += m.fleet.total_strength unless m.fleet.nil?
23:       strength += m.satellite_count * 2
24:     end
25:     (strength/moons.size).round
26:   end

Returns true if a user owns a moon

[Source]

    # File app/models/player.rb, line 13
13:   def owns(moon)
14:         moons.include?(moon)
15:   end

Returns the rounds that passed since the last login of the player

[Source]

    # File app/models/player.rb, line 30
30:   def rounds_passed
31:     self.last_update_on = Time.now if self.last_update_on.nil?
32:     ((Time.now-self.last_update_on)/Game.round_length).to_i
33:     #
34:   end

Let all the fight take place for the player

[Source]

    # File app/models/player.rb, line 37
37:   def run_fights
38:     self.fleets.each do |f|
39:       if f.arrived?
40:         f.fight
41:       end
42:     end
43:   end

Let all production take place on the player’s moons

[Source]

    # File app/models/player.rb, line 46
46:   def run_production
47:     self.moons.each do |m|
48:       unless m.product == 'money'
49:         amount = [m.amount, m.capacity_for_passed_rounds ].min
50:         product = LogEvent.pluralize( Spaceship.get_name(m.product) , amount)
51:         m.produce_ships( amount )
52:         self.add_to_log("Produced #{amount} #{LogEvent.pluralize(product,amount)} on #{m.name}") unless amount.zero?
53:         m.save
54:       end
55:     end
56:   end

[Validate]