alts
This commit is contained in:
@@ -1,2 +1,43 @@
|
||||
class Player < ApplicationRecord
|
||||
# A Player can be an alt of another player (the main)
|
||||
belongs_to :main_player, class_name: "Player", optional: true, foreign_key: "main_player_id"
|
||||
|
||||
# A Player can have multiple alts
|
||||
has_many :alternate_players, class_name: "Player", foreign_key: "main_player_id"
|
||||
|
||||
validate :no_circular_references
|
||||
|
||||
def total_wins
|
||||
w = self.wins
|
||||
alternate_players.each { |alt|
|
||||
w += alt.wins
|
||||
}
|
||||
w
|
||||
end
|
||||
|
||||
def total_losses
|
||||
l = self.losses
|
||||
alternate_players.each { |alt|
|
||||
l += alt.losses
|
||||
}
|
||||
l
|
||||
end
|
||||
|
||||
def total_purse
|
||||
p = self.purse
|
||||
alternate_players.each { |alt|
|
||||
p += alt.purse
|
||||
}
|
||||
p
|
||||
end
|
||||
|
||||
def alt?
|
||||
self.main_player
|
||||
end
|
||||
|
||||
def no_circular_references
|
||||
if main_player_id.present? && (main_player.main_player== self)
|
||||
errors.add(:main_account, "circular reference")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user